This commit is contained in:
quenousimporte 2025-02-17 18:03:03 +01:00
parent 375a3b8463
commit acae10d185
2 changed files with 30 additions and 19 deletions

View File

@ -18,6 +18,8 @@ htmltemplate = """<!DOCTYPE html>
</head> </head>
<body>{body}</body></html>""" <body>{body}</body></html>"""
settingsfile = "../textes/settings.yml"
def apply_html_template(body, title): def apply_html_template(body, title):
return htmltemplate.format(body=body, title=title) return htmltemplate.format(body=body, title=title)
@ -32,7 +34,7 @@ def md2gmi(md):
gmi = re.sub(r'\*(.*)\*', r'\1', gmi) gmi = re.sub(r'\*(.*)\*', r'\1', gmi)
return gmi return gmi
with open("settings.yml", "r", encoding="utf-8") as file: with open(settingsfile, "r", encoding="utf-8") as file:
index = yaml.safe_load(file) index = yaml.safe_load(file)
htmlhome = "<h1>{}</h1><p>{}</p>".format(index['title'], index['description']) htmlhome = "<h1>{}</h1><p>{}</p>".format(index['title'], index['description'])
@ -42,12 +44,21 @@ os.makedirs("out/html", exist_ok=True)
os.makedirs("out/gmi", exist_ok=True) os.makedirs("out/gmi", exist_ok=True)
for folderinfo in index['folders']: for folderinfo in index['folders']:
htmlhome += "\n<h2>{}</h2>\n<ul>".format(folderinfo['title'])
gmihome += "\n\n## {}".format(folderinfo['title'])
if 'texts' in folderinfo: sectiontitle = folderinfo['title']
for textinfo in folderinfo['texts']:
with open(os.path.join(folderinfo['folder'], textinfo['file']), "r", encoding="utf-8") as mdfile: 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:
mdcontent = mdfile.read() mdcontent = mdfile.read()
# clean header # clean header
@ -56,20 +67,20 @@ for folderinfo in index['folders']:
htmlcontent = md2html(mdcontent) htmlcontent = md2html(mdcontent)
gmicontent = md2gmi(mdcontent) gmicontent = md2gmi(mdcontent)
with open("out/html/{}".format(textinfo['file']).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile: with open("out/html/{}".format(filename).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(textinfo['title'], index['title']))) htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(filetitle, index['title'])))
with open("out/gmi/{}".format(textinfo['file']).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile: with open("out/gmi/{}".format(filename).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
gmifile.write(gmicontent) gmifile.write(gmicontent)
if 'restricted' not in textinfo: htmlhome += "<li><a href=\"{}\">{}</a></li>".format(filename.replace('.md', '.html'), filetitle)
htmlhome += "<li><a href=\"{}\">{}</a></li>".format(textinfo['file'].replace('.md', '.html'), textinfo['title']) gmihome += "\n=> {} {}".format(filename.replace('.md', '.gmi').replace(' ', '%20'), filetitle)
gmihome += "\n=> {} {}".format(textinfo['file'].replace('.md', '.gmi').replace(' ', '%20'), textinfo['title'])
if 'sounds' in folderinfo: else:
for soundinfo in folderinfo['sounds']:
shutil.copy(os.path.join(folderinfo['folder'], soundinfo['file']), 'out/html') # sounds
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(soundinfo['title'], soundinfo['file']) shutil.copy(os.path.join(folderinfo['folder'], filename), 'out/html')
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(filetitle, filename)
htmlhome += "\n</ul>" htmlhome += "\n</ul>"

View File

@ -3,19 +3,19 @@ description: Description of my web site
folders: folders:
- folder: ../path/containing/texts - folder: ../path/containing/texts
title: Section title title: Section title
texts: files:
- file: file1.md - file: file1.md
title: Title 1 title: Title 1
- file: file2.md - file: file2.md
title: Title 2 title: Title 2
- folder: ../path/containing/texts - folder: ../path/containing/texts
title: Other section title title: Other section title
texts: files:
- file: file3.md - file: file3.md
title: Title 3 title: Title 3
- folder: ../path/containing/sounds - folder: ../path/containing/sounds
title: Sound section title title: Sound section title
sounds: files:
- file: song1.mp3 - file: song1.mp3
title: Song 1 title: Song 1
- file: song 2.mp3 - file: song 2.mp3