[SSG] Fix generate_site. parse_file for new file format.
Change sort order. Move argument parsing outside main. Better error reporting. Better exit codes.
This commit is contained in:
parent
2729f74683
commit
3d8a816b66
62
nova.py
62
nova.py
|
@ -30,7 +30,7 @@ class EpisodeList(UserList):
|
||||||
|
|
||||||
def sort(self, *_args, **_kwargs):
|
def sort(self, *_args, **_kwargs):
|
||||||
"Sorts the EpisodeList"
|
"Sorts the EpisodeList"
|
||||||
super().sort(key=lambda x: x.date, reverse=True)
|
super().sort(key=lambda x: x.date, reverse=False)
|
||||||
|
|
||||||
def generate_thumbnails(self):
|
def generate_thumbnails(self):
|
||||||
"Generates thumbnails for all the videos"
|
"Generates thumbnails for all the videos"
|
||||||
|
@ -88,20 +88,21 @@ class EpisodeList(UserList):
|
||||||
with open(html, "w") as file:
|
with open(html, "w") as file:
|
||||||
file.write(episode.render(self.template, thumbnail))
|
file.write(episode.render(self.template, thumbnail))
|
||||||
|
|
||||||
last = self.data[-1]
|
last = self.data[-1]
|
||||||
last_name = self.output + gen_name(last.date, last.slug) + ".html"
|
last_name = f"{self.output}{gen_name(last.date, last.slug)}.html"
|
||||||
shutil.copy2(last_name, self.output + "index.html")
|
shutil.copy2(last_name, self.output + "index.html")
|
||||||
|
|
||||||
|
|
||||||
class Episode:
|
class Episode:
|
||||||
"Represents one episode of podcast"
|
"Represents one episode of podcast"
|
||||||
def __init__(self, date, slug, title, show_notes, video_src, audio_src):
|
def __init__(self, date, slug, title, show_notes, video_src, audio_src, config):
|
||||||
self.date = date
|
self.date = date
|
||||||
self.slug = slug
|
self.slug = slug
|
||||||
self.title = title.strip()
|
self.title = title.strip()
|
||||||
self.show_notes = markdown.markdown(show_notes)
|
self.show_notes = markdown.markdown(show_notes)
|
||||||
self.video = video_src
|
self.video = video_src
|
||||||
self.audio = audio_src
|
self.audio = audio_src
|
||||||
|
self.config = config
|
||||||
|
|
||||||
def render(self, template, thumbnail_src):
|
def render(self, template, thumbnail_src):
|
||||||
"Renders the Episode with the given template"
|
"Renders the Episode with the given template"
|
||||||
|
@ -130,10 +131,35 @@ def parse_args():
|
||||||
return input_dir, output_dir
|
return input_dir, output_dir
|
||||||
|
|
||||||
|
|
||||||
def main():
|
class ParseError(ValueError):
|
||||||
|
"Error raised while parsing a file"
|
||||||
|
|
||||||
|
|
||||||
|
def parse_file(file, array_keys=("categories")):
|
||||||
|
"Parses a file"
|
||||||
|
config = {}
|
||||||
|
kv_re = re.compile(r"(?P<key>\w+):\s*(?P<value>.*)")
|
||||||
|
while line := file.readline():
|
||||||
|
if line.rstrip("\n") == "---":
|
||||||
|
break
|
||||||
|
if line.strip() == "":
|
||||||
|
continue
|
||||||
|
if match := kv_re.match(line):
|
||||||
|
if match.group("key").strip().lower() in array_keys:
|
||||||
|
config[match.group("key")] = [i.strip() for i in
|
||||||
|
match.group("value").split(",")]
|
||||||
|
else:
|
||||||
|
config[match.group("key")] = match.group("value")
|
||||||
|
else:
|
||||||
|
raise ParseError(f"Invalid line {line}")
|
||||||
|
|
||||||
|
return (config, file.read())
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
"Main method"
|
"Main method"
|
||||||
root = path.dirname(sys.argv[0]).rstrip("/") + "/"
|
root = path.dirname(sys.argv[0]).rstrip("/") + "/"
|
||||||
input_dir, output_dir = parse_args()
|
input_dir, output_dir = args
|
||||||
|
|
||||||
# Input validation
|
# Input validation
|
||||||
paths = [
|
paths = [
|
||||||
|
@ -142,9 +168,9 @@ def main():
|
||||||
input_dir + "videos",
|
input_dir + "videos",
|
||||||
input_dir + "audio",
|
input_dir + "audio",
|
||||||
]
|
]
|
||||||
if not all(path.isdir(i) for i in paths):
|
if not all(path.isdir((fail := i)) for i in paths):
|
||||||
print("Invalid Input", file=sys.stderr)
|
print(f"Invalid Input. {fail} is not a directory.", file=sys.stderr)
|
||||||
return
|
return 1
|
||||||
|
|
||||||
if not path.isdir(output_dir):
|
if not path.isdir(output_dir):
|
||||||
os.mkdir(output_dir)
|
os.mkdir(output_dir)
|
||||||
|
@ -170,16 +196,21 @@ def main():
|
||||||
date = datetime.strptime(match.group("date"), "%Y-%M-%d")
|
date = datetime.strptime(match.group("date"), "%Y-%M-%d")
|
||||||
slug = match.group("slug")
|
slug = match.group("slug")
|
||||||
with open(input_dir + "md/" + file) as episode:
|
with open(input_dir + "md/" + file) as episode:
|
||||||
title = episode.readline()
|
try:
|
||||||
show_notes = episode.read()
|
config, show_notes = parse_file(episode)
|
||||||
|
except ParseError as err:
|
||||||
|
print(f"Error while parsing file: {file}")
|
||||||
|
print(err)
|
||||||
|
return 2
|
||||||
podcast.append(
|
podcast.append(
|
||||||
Episode(
|
Episode(
|
||||||
date,
|
date,
|
||||||
slug,
|
slug,
|
||||||
title,
|
config["title"],
|
||||||
show_notes,
|
show_notes,
|
||||||
input_dir + "videos/" + gen_name(date, slug) + ".mp4",
|
input_dir + "videos/" + gen_name(date, slug) + ".mp4",
|
||||||
input_dir + "audio/" + gen_name(date, slug) + ".mp3"
|
input_dir + "audio/" + gen_name(date, slug) + ".mp3",
|
||||||
|
config
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -189,7 +220,8 @@ def main():
|
||||||
podcast.generate_atom()
|
podcast.generate_atom()
|
||||||
podcast.generate_site(root)
|
podcast.generate_site(root)
|
||||||
shutil.copytree(input_dir + "overrides", output_dir, dirs_exist_ok=True)
|
shutil.copytree(input_dir + "overrides", output_dir, dirs_exist_ok=True)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
sys.exit(main(parse_args()))
|
||||||
|
|
Loading…
Reference in New Issue