improves KaTex matrix support and fixes #1

This commit is contained in:
2024-05-03 04:30:50 +02:00
parent 5db91dc6f3
commit b1bdf3129a
2 changed files with 51 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
# Joplin Metadata to Zola Metadata Converter
This is a very rudimentary script that converts an Exported Joplin Markdown + Font Matter file into a Format that the SSG Zola can unnderstand and parse.
This is a very rudimentary script that converts an Exported Joplin Markdown + Font Matter file into a Format that the SSG Zola can unnderstand and parse. This sript suporrts converting KaTeX equations and Memaid Diagrams (requires edits to the Zola Theme for KaTeX and Mermaid support).
## Requirements
@@ -45,10 +45,11 @@ python jmzm.py -r -b path/to/batch/folder/ -m /path/to/new/Folder
---
## WIP
## WIP / TODO:
- [x] ~~Automatically sort converted files into thier own subfolders~~
- [x] ~~place any associated assats into the subfolders with the Markdown files~~
- [ ] Pharse KaTeX expressions to guantee intercompatibillity between Joplin and Zola
- [ ] Integrade Mermaid support into my Zola HTML Template
- [x] ~~Pharse KaTeX expressions to guantee intercompatibillity between Joplin and Zola~~
- [x] ~~Integrade Mermaid support into my Zola HTML Template~~
- [x] ~~Pharse Mermaid Diagram to a Zola compatible format (HTML)~~
- [ ] Auto Detect changes to a Folder, convert any incoming files and output them to the Zola Posts directory

52
jmzm.py
View File

@@ -2,6 +2,11 @@ import argparse
import os
import re
# Splits a String into a list with delimiter and keeps the delimiter in the list
def splitkeep(s, delimiter):
split = s.split(delimiter)
return [substr + delimiter for substr in split[:-1]] + [split[-1]]
# this function is used to convert links from relative to absolute
def find_and_fix_links(path_to_file, remove, input_type):
filepath = path_to_file.replace(".md", "") + "/index.md"
@@ -10,28 +15,28 @@ def find_and_fix_links(path_to_file, remove, input_type):
res_dir = ""
print("Moving Resource Files...")
for i in range(len(data)):
if(re.search("\!\[.*\]\(.*\)", data[i]) != None):
if(re.search(r"\!\[.*\]\(.*\)", data[i]) != None):
id = data[i].replace(" ","").replace("![", "").split("]")[0]
img_path = "_resources/" + id
path_to_folder = path_to_file.replace(".md", "")
back = len(path_to_folder.split("/")) - len(re.findall("\.\.\/", data[i])) - input_type
back = len(path_to_folder.split("/")) - len(re.findall(r"\.\.\/", data[i])) - input_type
parrent_folder = ""
for j in range(0, back):
parrent_folder += path_to_folder.split("/")[j] + "/"
os.rename(parrent_folder + img_path, path_to_folder + "/" + id)
res_dir = parrent_folder + "_resources/"
data[i] = re.sub(re.search("\(.*\)", data[i]).group() , id , data[i])
elif(re.search("\[.*\]\(.*\)", data[i]) != None):
data[i] = re.sub(re.search(r"\(.*\)", data[i]).group() , id , data[i])
elif(re.search(r"\[.*\]\(.*\)", data[i]) != None):
id = data[i].replace("[", "").split("]")[0]
img_path = "_resources/" + id
path_to_folder = path_to_file.replace(".md", "")
back = len(path_to_folder.split("/")) - len(re.findall("\.\.\/", data[i])) - input_type
back = len(path_to_folder.split("/")) - len(re.findall(r"\.\.\/", data[i])) - input_type
parrent_folder = ""
for j in range(0, back):
parrent_folder += path_to_folder.split("/")[j] + "/"
os.rename(parrent_folder + img_path, path_to_folder + "/" + id)
res_dir = parrent_folder + "_resources/"
data[i] = re.sub(re.search("\(.*\)", data[i]).group() , id , data[i])
data[i] = re.sub(re.search(r"\(.*\)", data[i]).group() , id , data[i])
with open(filepath,'w',encoding='utf-8') as output_file:
output_file.writelines(data)
@@ -113,6 +118,41 @@ def pasre_file(lines, path_to_file, tags, remove, input_type):
j += 1
data[j] = "</div>\n"
print("Mermaid Diagrams converted Successfully!")
print("Converting KaTex matrices...")
for i in range(lines, len(data)):
if("\\{" in data[i]):
data[i] = data[i].replace("\\{","\\\\{")
if("\\}" in data[i]):
data[i] = data[i].replace("\\}","\\\\}")
to = data[i].find("\\end")
frm = data[i].find("\\begin")
# FIXME: Quadrupleing Backslashes for Math functions is broken af
if ("\\begin" in data[i]):
d = "\\begin"
s = splitkeep(data[i], d)
x = []
for k in range(len(s)):
if (s[k].find("\\end") != -1):
f = "\\end"
res = splitkeep(s[k], f)
for l in range(len(res)):
x.append(res[l])
else:
x.append(s[k])
for k in range(len(x)):
if (x[k].find("\\end") != -1):
x[k] = x[k].replace("\\\\","\\\\\\\\")
res = ""
for k in range(len(x)):
res += x[k]
data[i] = res
print("KaTex matrices converted Successfully!")
with open(path_to_file,'w',encoding='utf-8') as output_file:
output_file.writelines(data)