Files
JmZm/jmzm.py

262 lines
9.8 KiB
Python

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"
with open(filepath,'r',encoding='utf-8') as input_file:
data = input_file.readlines()
res_dir = ""
print("Moving Resource Files...")
for i in range(len(data)):
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(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(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(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(r"\(.*\)", data[i]).group() , id , data[i])
with open(filepath,'w',encoding='utf-8') as output_file:
output_file.writelines(data)
print("Resources Moved Successfully!")
if(remove):
if(res_dir != ""):
print("Removing Resource Folder...")
try:
if(len(os.listdir(res_dir)) == 0):
os.removedirs(res_dir)
print("Resource Folder removed Successfully!")
else:
print("Resource Folder not empty. Skipping...")
except:
print("Failed to remove Resource Folder! Continueing...")
else:
print("Resource Folder not found. Skipping...")
# this function is used to move the file to a subfolder
def move_to_subfolder(path_to_file):
path_to_folder = path_to_file.replace(".md", "")
try:
os.mkdir(path_to_folder)
os.rename(path_to_file, path_to_folder + "/index.md")
except:
print("Folder already exists. Aborting...")
return
# this function is used to parse the file
def pasre_file(lines, path_to_file, tags, remove, input_type):
try:
with open(path_to_file,'r',encoding='utf-8') as input_file:
data = input_file.readlines()
except:
print("File not found or Filetype not supported! Aborting...")
return
if(lines == 0):
for i in range(1, len(data)):
if (data[i] == "---\n" and lines == 0):
lines = i + 1
print("Metadata found! Number of lines: " + str(lines + 1))
break
if (lines == 0):
print("No Metadata found! Aborting...")
return
print("Parsing file...")
data[0] = "+++\n"
data[1] = data[1].replace("title: ","title = \"").replace("\'","").replace("\n", "\"\n")
data[2] = data[3].replace("created: ","date = \"").replace("\n", "\"\n")
data[3] = "\n"
data[4] = "[taxonomies]\n"
if(tags != None):
data[5] = "tags = " + tags + "\n"
elif(lines >= 7 and data[7] == "tags:\n"):
print("Tags detected, adding Automatically...")
tags = "tags = ["
for i in range(8, lines -1):
if(i != lines -2):
tags += data[i].title().replace(" - ", "\"").replace("\n", "\", ")
else:
tags += data[i].title().replace(" - ", "\"").replace("\n", "\"]\n")
data[5] = tags
print("Tags added Successfully!")
else:
data[5] = "tags = []\n"
data[6] = "+++\n"
for i in range(7, lines):
data[i] = ""
for i in range(lines, len(data)):
if("```mermaid\n" in data[i]):
print("Mermaid detected. Converting...")
data[i] = "<div class=\"mermaid\">\n"
j = i + 1
while("```" not in data[j]):
data[j] = data[j].replace("\n", ";\n")
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")
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)
print("Moving file to subfolder...")
move_to_subfolder(path_to_file)
find_and_fix_links(path_to_file, remove, input_type)
print("File parsed Successfully!")
return True
# this function is used to move the folders to thier a diffrent given path
def move_folders(path_to_dest, path_to_src, remove):
try:
dir = os.listdir(path_to_src)
except:
return
print("Moving Folders...")
for file in dir:
os.rename(path_to_src + "/" + file, path_to_dest + "/" + file)
print("Folders moved Successfully!")
if(remove):
print("Removing Source Folders...")
try:
if(len(os.listdir(path_to_src)) == 0):
os.removedirs(path_to_src)
print("Folders removed Successfully!")
else:
print("Source Folder not empty. Skipping...")
except:
print("Failed to remove Source Folders! Skipping...")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="input file to format to md", required=False)
parser.add_argument("-l", "--lines", help="number of lines to replace. The defalt Automatically set. (Requires -i or -b)", required=False)
parser.add_argument("-t", "--tags", help="list of tags in form: \"[\\\"Test\\\", \\\"123\\\"]\" (Requires -i or -b)", required=False)
parser.add_argument("-b", "--batch", help="convert all files in the given folder (Requires a Trailing Slash in the given Filepath \".../example/\")", required=False)
parser.add_argument("-v", "--version", help="show the installed version", required=False, action="store_true")
parser.add_argument("-m", "--move", help="move the converted files to a diffrent folder (Requires -i or -b)", required=False)
parser.add_argument("-r", "--remove", help="remove ALL the old Directories in the Filepath given with -i or -b (Requires -m)", required=False, action="store_true")
args = parser.parse_args()
path_to_file = args.input
batch = args.batch
version = args.version
move = args.move
remove = args.remove
if(version == True):
print("Joplin Metadata to Zola Metadata Converter:")
print("- Author: Focron")
print("- Version: 1.0.4")
return
if(path_to_file == None and batch == None):
print("No input file or folder given. Use -h for help. Aborting...")
return
lines = 0
if(args.lines != None):
lines = int(args.lines)
tags = args.tags
if(batch != None):
print("Batch conversion started...")
try:
dir = os.listdir(batch + "/")
except:
print("Folder not found or Path is not a Folder. Aborting...")
return
success = False
for file in dir:
if file.endswith(".md"):
filepath = batch + "/" + file
success = pasre_file(lines, filepath, tags, remove, 2)
if(move != None):
move_folders(move, batch, remove)
if(success == True):
print("Batch conversion finished Successfully!")
else:
print("Batch conversion Failed! Aborting!")
return
if(path_to_file != None):
print("File conversion started...")
success = False
success = pasre_file(lines, path_to_file, tags, remove, 1)
if(move != None):
path_file_arr = path_to_file.split("/")
parrent_folder = ""
for j in range(0, len(path_file_arr) - 1):
parrent_folder += path_file_arr[j] + "/"
move_folders(move, parrent_folder, remove)
if(success == True):
print("File conversion finished Successfully!")
else:
print("File conversion Failed! Aborting!")
return
if __name__ == "__main__":
main()