2025-02-15 12:09:45 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import markdown
|
|
|
|
import yaml
|
|
|
|
import shutil
|
2025-02-19 17:48:54 +01:00
|
|
|
import subprocess
|
2025-02-20 10:36:00 +01:00
|
|
|
import unidecode
|
2025-02-15 12:09:45 +01:00
|
|
|
|
|
|
|
htmltemplate = """<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
|
|
|
|
<title>{title}</title>
|
|
|
|
<style>
|
|
|
|
html {{ margin: 20px; }}
|
|
|
|
body {{ margin: auto; max-width: 800px; }}
|
|
|
|
audio {{ height: 14px}}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>{body}</body></html>"""
|
|
|
|
|
2025-02-17 18:03:03 +01:00
|
|
|
settingsfile = "../textes/settings.yml"
|
|
|
|
|
2025-02-15 12:09:45 +01:00
|
|
|
def apply_html_template(body, title):
|
|
|
|
return htmltemplate.format(body=body, title=title)
|
|
|
|
|
|
|
|
def md2html(md):
|
|
|
|
return markdown.markdown(md, extensions=['extra', 'nl2br'])
|
|
|
|
|
|
|
|
def md2gmi(md):
|
|
|
|
gmi = md
|
|
|
|
gmi = re.sub(r'\[(.*)\]\((.*)\)', r'\n=> \2 \1', gmi)
|
|
|
|
gmi = gmi.replace('* * *', '---')
|
|
|
|
gmi = re.sub(r'^\*(.*)\*$', r'> \1', gmi, flags=re.MULTILINE)
|
|
|
|
gmi = re.sub(r'\*(.*)\*', r'\1', gmi)
|
|
|
|
return gmi
|
|
|
|
|
2025-02-17 18:03:03 +01:00
|
|
|
with open(settingsfile, "r", encoding="utf-8") as file:
|
2025-02-15 12:09:45 +01:00
|
|
|
index = yaml.safe_load(file)
|
|
|
|
|
|
|
|
htmlhome = "<h1>{}</h1><p>{}</p>".format(index['title'], index['description'])
|
|
|
|
gmihome = "# {}\n{}".format(index['title'], index['description'])
|
|
|
|
|
|
|
|
os.makedirs("out/html", exist_ok=True)
|
|
|
|
os.makedirs("out/gmi", exist_ok=True)
|
|
|
|
|
|
|
|
for folderinfo in index['folders']:
|
|
|
|
|
2025-02-17 18:03:03 +01:00
|
|
|
sectiontitle = folderinfo['title']
|
|
|
|
|
|
|
|
htmlhome += "\n<h2>{}</h2>\n<ul>".format(sectiontitle)
|
|
|
|
gmihome += "\n\n## {}".format(sectiontitle)
|
|
|
|
|
|
|
|
for fileinfo in folderinfo['files']:
|
|
|
|
|
|
|
|
filetitle = fileinfo['title']
|
2025-02-20 10:36:00 +01:00
|
|
|
filename = fileinfo['file']
|
2025-02-17 18:03:03 +01:00
|
|
|
fileroot, fileextension = os.path.splitext(filename)
|
|
|
|
|
2025-02-20 10:36:00 +01:00
|
|
|
outfilename = unidecode.unidecode(filename)
|
|
|
|
|
2025-02-17 18:03:03 +01:00
|
|
|
if fileextension == '.md':
|
|
|
|
|
|
|
|
with open(os.path.join(folderinfo['folder'], filename), "r", encoding="utf-8") as mdfile:
|
2025-02-15 12:09:45 +01:00
|
|
|
mdcontent = mdfile.read()
|
|
|
|
|
|
|
|
# clean header
|
|
|
|
mdcontent = re.sub(r'---\n[\s\S]*?\n---\n*', '', mdcontent)
|
|
|
|
|
|
|
|
htmlcontent = md2html(mdcontent)
|
|
|
|
gmicontent = md2gmi(mdcontent)
|
|
|
|
|
2025-02-20 10:36:00 +01:00
|
|
|
with open("out/html/{}".format(outfilename).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
|
2025-02-17 18:03:03 +01:00
|
|
|
htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(filetitle, index['title'])))
|
2025-02-15 12:09:45 +01:00
|
|
|
|
2025-02-20 10:36:00 +01:00
|
|
|
with open("out/gmi/{}".format(outfilename).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
|
2025-02-15 12:09:45 +01:00
|
|
|
gmifile.write(gmicontent)
|
|
|
|
|
2025-02-20 10:36:00 +01:00
|
|
|
htmlhome += "<li><a href=\"{}\">{}</a></li>".format(outfilename.replace('.md', '.html'), filetitle)
|
|
|
|
gmihome += "\n=> {} {}".format(outfilename.replace('.md', '.gmi').replace(' ', '%20'), filetitle)
|
2025-02-17 18:03:03 +01:00
|
|
|
|
2025-02-19 17:48:54 +01:00
|
|
|
elif fileextension == ".wav" or fileextension == ".3gp":
|
2025-02-15 12:09:45 +01:00
|
|
|
|
2025-02-20 10:36:00 +01:00
|
|
|
# todo use builtin python library
|
|
|
|
outfilename = unidecode.unidecode(fileroot) + ".mp3"
|
2025-02-19 17:48:54 +01:00
|
|
|
subprocess.run(["ffmpeg",
|
|
|
|
"-i",
|
|
|
|
os.path.join(folderinfo['folder'], filename),
|
2025-02-20 10:36:00 +01:00
|
|
|
os.path.join('out/html', outfilename)])
|
|
|
|
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, outfilename)
|
2025-02-19 17:48:54 +01:00
|
|
|
|
|
|
|
elif fileextension == ".mp3":
|
2025-02-20 10:36:00 +01:00
|
|
|
shutil.copy(os.path.join(folderinfo['folder'], filename), os.path.join('out/html', outfilename))
|
|
|
|
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, outfilename)
|
2025-02-15 12:09:45 +01:00
|
|
|
|
|
|
|
htmlhome += "\n</ul>"
|
|
|
|
|
|
|
|
with open("out/html/index.html", "w", encoding="utf-8") as htmlindex:
|
|
|
|
htmlindex.write(apply_html_template(htmlhome, index['title']))
|
|
|
|
|
|
|
|
with open("out/gmi/index.gmi", "w", encoding="utf-8") as gmiindex:
|
|
|
|
gmiindex.write(gmihome)
|