[SSG] Add basic static site generator code template.

This commit is contained in:
Ceda EI 2020-01-25 02:45:30 +05:30
parent cddefb1763
commit 68d853297a
1 changed files with 100 additions and 0 deletions

100
nova.py Executable file
View File

@ -0,0 +1,100 @@
#!/usr/bin/env python3
"Creates a static site for redacted.life"
import argparse
from collections import UserList
import os
import os.path as path
import re
import sys
import jinja2
class EpisodeList(UserList):
"Represents list of episodes"
def __init__(self, data, output, template):
super().__init__(data)
self.output = output
self.template = template
def sort(self):
"Sorts the EpisodeList"
def generate_thumnails(self):
"Generates thumbnails for all the videos"
def generate_atom(self):
"Generates the Atom feed"
def generate_site(self):
"Generates the entire "
class Episode:
"Represents one episode of podcast"
def __init__(self, date, slug, title, show_notes):
self.date = date
self.slug = slug
self.title = title
self.show_notes = show_notes
def render(self, template, thumbnail_src, video_src):
"Renders the Episode with the given template"
return template.render(
title=self.title,
show_notes=jinja2.Markup(self.show_notes),
thumbnail_src=thumbnail_src,
video_src=video_src
)
def store_thumbnail(self, location):
"Stores the thumbnail for given image at path"
def main():
"Main method"
parser = argparse.ArgumentParser()
parser.add_argument("input_dir", help="Input directory")
parser.add_argument("output_dir", help="Output directory")
args = parser.parse_args()
input_dir = args.input_dir.rstrip("/") + "/"
output_dir = args.output_dir.rstrip("/") + "/"
# Input validation
if not all(path.isdir(i) for i in (input_dir, input_dir + "md",
input_dir + "videos")):
print("Invalid Input", file=sys.stderr)
return
if not path.isdir(args.output_dir):
os.mkdir(args.output_dir)
template = jinja2.Environment(
loader=jinja2.FileSystemLoader(input_dir),
autoescape=jinja2.select_autoescape("html")
).get_template("index.html")
podcast = EpisodeList([], output_dir, template)
split = re.compile(r"((?P<date>\d{4}-[01]?\d-[0123]?\d)-(?P<slug>.*))")
for file in os.listdir(input_dir + "md"):
match = split.match(file)
if not match:
print(f"Invalid filename: {file}", file=sys.stderr)
continue
date = match.group("date")
slug = match.group("slug")
with open(file) as f: # pylint: disable=invalid-name
title = f.readline()
show_notes = f.read()
podcast.append(Episode(date, slug, title, show_notes))
podcast.sort()
podcast.generate_thumnails()
podcast.generate_atom()
podcast.generate_site()
if __name__ == "__main__":
main()