46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
import re
|
|
|
|
|
|
@dataclass
|
|
class Article:
|
|
path: str
|
|
|
|
def read_contents(self) -> str:
|
|
return Path(self.path).read_text()
|
|
|
|
|
|
articles = [
|
|
Article(
|
|
"./input/art-001.html",
|
|
),
|
|
]
|
|
|
|
# generate slugs and read articles
|
|
slugs = []
|
|
article_contents = []
|
|
for article in articles:
|
|
contents = article.read_contents()
|
|
id_match = re.search(r'<h2 id="(.*?)"', contents)
|
|
title_match = re.search(r'<h2 id=".*?>(.*?)</h2>', contents)
|
|
if id_match is None:
|
|
print("couldn't match id")
|
|
elif title_match is None:
|
|
print("Couldn't match title")
|
|
else:
|
|
id = id_match.group(1)
|
|
title = title_match.group(1)
|
|
article_contents.append(contents)
|
|
slug = f"""<p><a href="#{id}">{title}</a></p>"""
|
|
slugs.append(slug)
|
|
|
|
# plug into template, write to index.html
|
|
template = Path("./input/template.html").read_text()
|
|
template = template.replace(
|
|
"<div><!--- Articles ---></div>", "\n".join(article_contents)
|
|
)
|
|
template = template.replace("<div><!--- Slugs ---></div>", "\n".join(slugs))
|
|
with open("./index.html", "w", encoding="utf-8") as index_html:
|
|
index_html.write(template)
|