add publish tool
This commit is contained in:
parent
48c7a7a043
commit
fc04a79a36
|
@ -0,0 +1,4 @@
|
|||
.venv
|
||||
settings.yml
|
||||
out
|
||||
uv.lock
|
|
@ -0,0 +1 @@
|
|||
3.12
|
|
@ -0,0 +1,3 @@
|
|||
A tool to generate html and gmi static websites from markdown and mp3 files
|
||||
|
||||
"simple" folder is an even simpler tool
|
|
@ -0,0 +1,80 @@
|
|||
import os
|
||||
import re
|
||||
import markdown
|
||||
import yaml
|
||||
import shutil
|
||||
|
||||
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>"""
|
||||
|
||||
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
|
||||
|
||||
with open("settings.yml", "r", encoding="utf-8") as file:
|
||||
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']:
|
||||
htmlhome += "\n<h2>{}</h2>\n<ul>".format(folderinfo['title'])
|
||||
gmihome += "\n\n## {}".format(folderinfo['title'])
|
||||
|
||||
if 'texts' in folderinfo:
|
||||
for textinfo in folderinfo['texts']:
|
||||
with open(os.path.join(folderinfo['folder'], textinfo['file']), "r", encoding="utf-8") as mdfile:
|
||||
mdcontent = mdfile.read()
|
||||
|
||||
# clean header
|
||||
mdcontent = re.sub(r'---\n[\s\S]*?\n---\n*', '', mdcontent)
|
||||
|
||||
htmlcontent = md2html(mdcontent)
|
||||
gmicontent = md2gmi(mdcontent)
|
||||
|
||||
with open("out/html/{}".format(textinfo['file']).replace('.md', '.html'), "w", encoding="utf-8") as htmlfile:
|
||||
htmlfile.write(apply_html_template(htmlcontent, "{} | {}".format(textinfo['title'], index['title'])))
|
||||
|
||||
with open("out/gmi/{}".format(textinfo['file']).replace('.md', '.gmi'), "w", encoding="utf-8") as gmifile:
|
||||
gmifile.write(gmicontent)
|
||||
|
||||
if 'restricted' not in textinfo:
|
||||
htmlhome += "<li><a href=\"{}\">{}</a></li>".format(textinfo['file'].replace('.md', '.html'), textinfo['title'])
|
||||
gmihome += "\n=> {} {}".format(textinfo['file'].replace('.md', '.gmi').replace(' ', '%20'), textinfo['title'])
|
||||
|
||||
if 'sounds' in folderinfo:
|
||||
for soundinfo in folderinfo['sounds']:
|
||||
shutil.copy(os.path.join(folderinfo['folder'], soundinfo['file']), 'out/html')
|
||||
htmlhome += "<li>{}</li><audio controls><source src=\"{}\"></audio>".format(soundinfo['title'], soundinfo['file'])
|
||||
|
||||
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)
|
|
@ -0,0 +1,10 @@
|
|||
[project]
|
||||
name = "publish"
|
||||
version = "0.1.0"
|
||||
description = "A tool to generate html and gmi static websites from markdown files"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"markdown>=3.7",
|
||||
"pyyml>=0.0.2",
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
title: Title of my web site
|
||||
description: Description of my web site
|
||||
folders:
|
||||
- folder: ../path/containing/texts
|
||||
title: Section title
|
||||
texts:
|
||||
- file: file1.md
|
||||
title: Title 1
|
||||
- file: file2.md
|
||||
title: Title 2
|
||||
- folder: ../path/containing/texts
|
||||
title: Other section title
|
||||
texts:
|
||||
- file: file3.md
|
||||
title: Title 3
|
||||
- folder: ../path/containing/sounds
|
||||
title: Sound section title
|
||||
sounds:
|
||||
- file: song1.mp3
|
||||
title: Song 1
|
||||
- file: song 2.mp3
|
||||
title: Song 2
|
Loading…
Reference in New Issue