[SSG] Add generate_archives. Visual fixes to code.

This commit is contained in:
Ceda EI 2020-01-26 04:58:09 +05:30
parent 1b4e1f7640
commit 2aaea11e8d
1 changed files with 37 additions and 9 deletions

46
nova.py
View File

@ -22,10 +22,11 @@ def gen_name(date, slug):
class EpisodeList(UserList): class EpisodeList(UserList):
"Represents list of episodes" "Represents list of episodes"
def __init__(self, data, output, template): def __init__(self, data, output, template, archives):
super().__init__(data) super().__init__(data)
self.output = output self.output = output
self.template = template self.template = template
self.archives = archives
def sort(self, *_args, **_kwargs): def sort(self, *_args, **_kwargs):
"Sorts the EpisodeList" "Sorts the EpisodeList"
@ -45,6 +46,15 @@ class EpisodeList(UserList):
def generate_atom(self): def generate_atom(self):
"Generates the Atom feed" "Generates the Atom feed"
def generate_archives(self):
"Generates archives page"
with open(self.output + "archives.html", "w") as file:
episodes = [{
"slug": gen_name(i.date, i.slug) + ".html",
"title": i.title
} for i in self.data]
file.write(self.archives.render(episodes=episodes))
def generate_site(self, root): def generate_site(self, root):
"Generates the entire site" "Generates the entire site"
# Generate CSS from SCSS # Generate CSS from SCSS
@ -119,20 +129,30 @@ def main():
output_dir = args.output_dir.rstrip("/") + "/" output_dir = args.output_dir.rstrip("/") + "/"
# Input validation # Input validation
if not all(path.isdir(i) for i in (input_dir, input_dir + "md", paths = [
input_dir + "videos")): input_dir,
input_dir + "md",
input_dir + "videos",
input_dir + "audios",
]
if not all(path.isdir(i) for i in paths):
print("Invalid Input", file=sys.stderr) print("Invalid Input", file=sys.stderr)
return return
if not path.isdir(args.output_dir): if not path.isdir(args.output_dir):
os.mkdir(args.output_dir) os.mkdir(args.output_dir)
template = jinja2.Environment( env = jinja2.Environment(
loader=jinja2.FileSystemLoader(root), loader=jinja2.FileSystemLoader(root),
autoescape=jinja2.select_autoescape("html") autoescape=jinja2.select_autoescape("html")
).get_template("index.html") )
podcast = EpisodeList([], output_dir, template) podcast = EpisodeList(
[],
output_dir,
env.get_template("index.html"),
env.get_template("archives.html")
)
split = re.compile(r"((?P<date>\d{4}-[01]?\d-[0123]?\d)-(?P<slug>.*).md)") split = re.compile(r"((?P<date>\d{4}-[01]?\d-[0123]?\d)-(?P<slug>.*).md)")
for file in os.listdir(input_dir + "md"): for file in os.listdir(input_dir + "md"):
@ -145,12 +165,20 @@ def main():
with open(input_dir + "md/" + file) as episode: with open(input_dir + "md/" + file) as episode:
title = episode.readline() title = episode.readline()
show_notes = episode.read() show_notes = episode.read()
video = input_dir + "videos/" + gen_name(date, slug) + ".mp4" podcast.append(
audio = input_dir + "audios/" + gen_name(date, slug) + ".mp3" Episode(
podcast.append(Episode(date, slug, title, show_notes, video, audio)) date,
slug,
title,
show_notes,
input_dir + "videos/" + gen_name(date, slug) + ".mp4",
input_dir + "audios/" + gen_name(date, slug) + ".mp3"
)
)
podcast.sort() podcast.sort()
podcast.generate_thumbnails() podcast.generate_thumbnails()
podcast.generate_archives()
podcast.generate_atom() podcast.generate_atom()
podcast.generate_site(root) podcast.generate_site(root)