Created
April 11, 2024 20:17
-
-
Save pbessman/8be3610fde722d2b518df69f8d37ee0a to your computer and use it in GitHub Desktop.
Simple Static Site Generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import markdown | |
import os | |
import shutil | |
def build_site(): | |
md = markdown.Markdown(extensions=["fenced_code", "codehilite"]) | |
headerfile = open("header.html") | |
header_html = headerfile.read() | |
headerfile.close() | |
footerfile = open("footer.html") | |
footer_html = footerfile.read() | |
footerfile.close() | |
finalfile = open("final.html") | |
final_html = finalfile.read() | |
finalfile.close() | |
# Clear site output folder. | |
shutil.rmtree("outputsite") | |
os.mkdir("outputsite") | |
# Rebuild page bodies. | |
pages = [] | |
for filename in os.listdir("pages"): | |
file = open(f"./pages/{filename}") | |
body_txt = file.read() | |
file.close() | |
title = filename[:-3].replace('_', ' ') | |
link = filename[:-3] + ".html" | |
body_html = md.convert(body_txt) | |
md.reset() | |
pages.append({ | |
"title": title, | |
"link": link, | |
"body": body_html | |
}) | |
# Update footer with pages and finalize. | |
for page in pages: | |
footer_html += f" | <a href='{page['link']}'>{page['title']}</a>" | |
footer_html += final_html | |
# Add header and footer to pages and write to disk. | |
for page in pages: | |
body_html = header_html + '\n' + page["body"] + '\n' + footer_html | |
outpath = f"./outputsite/{page['link']}" | |
output = open(outpath, mode="wt") | |
output.write(body_html) | |
output.close() | |
# Rebuild articles and write to disk. | |
articles = [] | |
for filename in os.listdir("articles"): | |
file = open(f"./articles/{filename}") | |
body_txt = file.read() | |
file.close() | |
date = filename[0:8] | |
title = filename[9:-3].replace('_', ' ') | |
link = filename[:-3] + ".html" | |
articles.append({ | |
"title": title, | |
"date": date, | |
"link": link, | |
}) | |
body_html = md.convert(body_txt) | |
md.reset() | |
body_html = header_html + '\n' + body_html + '\n' + footer_html | |
outpath = f"./outputsite/{link}" | |
output = open(outpath, mode="wt") | |
output.write(body_html) | |
output.close() | |
articles.sort(key=lambda article: article["date"], reverse=True) | |
# Rebuild index and write to disk. | |
index_html = "<ul>" | |
for article in articles: | |
date = article["date"] | |
date = f"{date[4:6]}/{date[6:]}/{date[2:4]}" | |
index_html += f"\n\t<li>{date} — <a href='{article['link']}'>{article['title']}</a></li>" | |
index_html += "\n</ul>\n" | |
index_html = header_html + index_html + footer_html | |
output = open("./outputsite/index.html", mode="wt") | |
output.write(index_html) | |
output.close() | |
# Copy static content. | |
for file in os.listdir("static"): | |
shutil.copyfile(f"./static/{file}", f"./outputsite/{file}") | |
build_site() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import logging | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
from build import build_site | |
class EventHandler(FileSystemEventHandler): | |
LastEventTime = time.time() | |
def on_any_event(self, event): | |
newTime = time.time() | |
deltaTime = newTime - self.LastEventTime | |
self.LastEventTime = newTime | |
if (deltaTime < 1): | |
return | |
if "outputsite" not in event.src_path: | |
print("Change detected in ", event.src_path) | |
build_site() | |
print("Site rebuilt.") | |
# based on the watchdog quickstart: https://pythonhosted.org/watchdog/quickstart.html#a-simple-example | |
if __name__ == "__main__": | |
event_handler = EventHandler() | |
observer = Observer() | |
observer.schedule(event_handler, '.', recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment