diff options
| -rw-r--r-- | converters/html_to_md.py | 19 | ||||
| -rw-r--r-- | converters/links_to_gemini.py | 28 | ||||
| -rw-r--r-- | converters/md_to_gemini.py | 28 | ||||
| -rw-r--r-- | main.py | 45 | ||||
| -rw-r--r-- | readme.md | 4 |
5 files changed, 124 insertions, 0 deletions
diff --git a/converters/html_to_md.py b/converters/html_to_md.py new file mode 100644 index 0000000..b6aeae0 --- /dev/null +++ b/converters/html_to_md.py @@ -0,0 +1,19 @@ +import os +from os import walk +import os.path +from markdownify import markdownify + +def convert_html_to_md(HtmlList): + for path in HtmlList: + + pathsplit = path.split("/") + + file = open(str(path), "r").read() + html = markdownify(file, heading_style="ATX") + f = open("output/markdown/" + str(pathsplit[-1]).replace(".html", ".md"), "w") + f.write(html) + f.close() + + + + return "html was converted to markdown (1/3)" diff --git a/converters/links_to_gemini.py b/converters/links_to_gemini.py new file mode 100644 index 0000000..9b3749d --- /dev/null +++ b/converters/links_to_gemini.py @@ -0,0 +1,28 @@ +import os +from os import walk +import os.path + +def convert_links_to_gemini(domain): + + for file in os.listdir("output/gemini"): + processed = "" + f = open("output/gemini/" + file, "r") + for line in f.readlines(): + if ".html" in line and "http" not in line or domain in line: + line = line.replace("html", "gmi") + url = line.split(" ")[1] + + link = url.split("/")[-1] + + line = line.replace(str(url), str(link)) + + processed += line + + else: + processed += line + f.close() + f = open("output/gemini/" + file, "w") + f.write(processed) + f.close() + + return("links are converted to gemini (3/3)") diff --git a/converters/md_to_gemini.py b/converters/md_to_gemini.py new file mode 100644 index 0000000..524f17e --- /dev/null +++ b/converters/md_to_gemini.py @@ -0,0 +1,28 @@ +import os +from os import walk +import os.path +from md2gemini import md2gemini + +def convert_md_to_gemini(): + for file in os.listdir("output/markdown"): + with open("output/markdown/" + str(file), "r") as f: + gemini = md2gemini(f.read()) + f.close() + + f = open("output/gemini/" + str(file).replace(".md", ".gmi"), "w") + f.write(gemini) + f.close() + for file in os.listdir("output/gemini"): + processed = "" + f = open("output/gemini/" + file, "r") + for line in f.readlines(): + if line == "html\n": + pass + + else: + processed += line + f.close() + f = open("output/gemini/" + file, "w") + f.write(processed) + f.close() + return "markdown was converted to gemini (2/3)" @@ -0,0 +1,45 @@ +import os +from os import walk +import os.path +from converters.html_to_md import convert_html_to_md +from converters.md_to_gemini import convert_md_to_gemini +from converters.links_to_gemini import convert_links_to_gemini + +dir = "input" + +def wipe_old(): + for root, dirs, files in os.walk("output"): + for file in files: + os.remove(os.path.join(root, file)) + + return "old stuff is gone" + + +def get_paths(dir): + PathList = [] + for thing in os.listdir(dir): + PathList.append(dir + "/" + thing) + try: + PathList += get_paths(dir + "/" + thing) + except: + pass + return PathList + +def get_html(PathList): + HtmlList = [] + for path in PathList: + if ".html" in path: + HtmlList.append(path) + return HtmlList + + +PathList = get_paths(dir) +HtmlList = get_html(PathList) + + +domain = "aaron.place" #Fill in your domain or leave untouched if you dont have one!!! + +print(wipe_old()) +print(convert_html_to_md(HtmlList)) +print(convert_md_to_gemini()) +print(convert_links_to_gemini(domain)) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f9522eb --- /dev/null +++ b/readme.md @@ -0,0 +1,4 @@ +# Html2Gemini +This simple python programm allows you to convert html to fully usable gmi files. +Using it is fairly simple. install md2gemini and markdownify with pip before you start. +Now place your entire site in the input folder (the script will filter it for html files). Run main.py with python 3 and wait untill it is done. You will now find your .gmi files in the output/gemini folder. You can simply run a server with those files and everything should work. This is script is intended to be used on html and text heavy sites. PHP files will not be converted. Codeblocks are somewhat buggy from time to time. |
