From 5224c2eababf5d8303d17ba00d21c43c3a2b96aa Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 1 Dec 2018 00:51:59 +0100 Subject: displaying points a bit --- scraper/builder.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) (limited to 'scraper') diff --git a/scraper/builder.py b/scraper/builder.py index f962cade..73d6bfb3 100644 --- a/scraper/builder.py +++ b/scraper/builder.py @@ -1,16 +1,66 @@ #!/usr/bin/python +import os import glob -from jinja2 import Environment, PackageLoader, select_autoescape +import mistune +from jinja2 import Environment, FileSystemLoader, select_autoescape + +public_path = "../site/public" +content_path = "../site/content" +template_path = "../site/templates" env = Environment( - loader=FileSystemLoader('../site/templates'), - autoescape=select_autoescape(['html', 'xml']) + loader=FileSystemLoader(template_path), + autoescape=select_autoescape([]) ) +renderer = mistune.Renderer(escape=False) +markdown = mistune.Markdown(renderer=renderer) + +def wide_section(line): + return "
" + markdown(line) + "
" + +def normal_section(lines): + if len(lines): + return "
" + markdown("\n\n".join(lines)) + "
" + return "" + def build_file(fn): print(fn) + output_path = os.path.dirname(fn).replace(content_path, public_path) + output_fn = os.path.join(output_path, "index.html") + with open(fn, "r") as file: + sections = file.read().split("\n\n") + metadata = {} + for line in sections[0].split("\n"): + print(line) + key, value = line.split(': ', 1) + metadata[key.lower()] = value + + groups = [] + current_group = [] + for section in sections[1:]: + if '![wide]' in section: + groups.append(normal_section(current_group)) + groups.append(wide_section(section)) + current_group = [] + else: + current_group.append(section) + groups.append(normal_section(current_group)) + content = "".join(groups) + + template = env.get_template("page.html") + html = template.render(metadata=metadata, content=content) + + os.makedirs(output_path, exist_ok=True) + with open(output_fn, "w") as file: + file.write(html) def build_site(): - for fn in glob.glob("../content/**/index.txt", recursive=True): - build_file(fn) \ No newline at end of file + print("Building...") + for fn in glob.iglob(os.path.join(content_path, "**/index.txt"), recursive=True): + print(fn) + build_file(fn) + +if __name__ == '__main__': + build_site() -- cgit v1.2.3-70-g09d2