diff options
Diffstat (limited to 'scraper')
| -rw-r--r-- | scraper/builder.py | 60 |
1 files changed, 55 insertions, 5 deletions
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 "<section class='wide'>" + markdown(line) + "</section>" + +def normal_section(lines): + if len(lines): + return "<section>" + markdown("\n\n".join(lines)) + "</section>" + 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() |
