publish/publish.py

103 lines
3.5 KiB
Python
Raw Normal View History

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-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']:
filename = fileinfo['file']
filetitle = fileinfo['title']
fileroot, fileextension = os.path.splitext(filename)
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-17 18:03:03 +01:00
with open("out/html/{}".format(filename).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(filetitle, index['title'])))
2025-02-15 12:09:45 +01:00
2025-02-17 18:03:03 +01:00
with open("out/gmi/{}".format(filename).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
2025-02-15 12:09:45 +01:00
gmifile.write(gmicontent)
2025-02-17 18:03:03 +01:00
htmlhome += "<li><a href=\"{}\">{}</a></li>".format(filename.replace('.md', '.html'), filetitle)
gmihome += "\n=> {} {}".format(filename.replace('.md', '.gmi').replace(' ', '%20'), filetitle)
2025-02-19 17:48:54 +01:00
elif fileextension == ".wav" or fileextension == ".3gp":
2025-02-15 12:09:45 +01:00
2025-02-17 18:03:03 +01:00
# sounds
2025-02-19 17:48:54 +01:00
# todo: add a property for dest file (to remove accents)
# todo use builtin library
subprocess.run(["ffmpeg",
"-i",
os.path.join(folderinfo['folder'], filename),
os.path.join('out/html', fileroot + ".mp3")])
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, filename)
elif fileextension == ".mp3":
2025-02-17 18:03:03 +01:00
shutil.copy(os.path.join(folderinfo['folder'], filename), 'out/html')
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, filename)
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)