main.py
Python script, ASCII text executable
1import collections 2import string 3from datetime import datetime 4import string 5 6import ampoule_ssg as ampoule 7from ampoule_ssg import markdown 8from ampoule_ssg import extra_jinja 9 10site = ampoule.Site("../blog-build") 11extra_jinja.init_filters(site) 12extra_jinja.init_tests(site) 13 14 15@site.filter("markdown") 16def markdown_filter(text): 17return markdown.markdown2html(text) 18 19 20def article_url(url): 21url = url.lower().rpartition(".")[0] if "." in url else url.lower() 22 23new_url = "" 24for i in url: 25if i in string.ascii_lowercase: 26new_url += i 27elif i in string.whitespace: 28new_url += "-" 29 30return new_url + ".html" 31 32 33# Set context 34site.context["timestamp"] = datetime.now() 35site.context["ampoule"] = ampoule 36 37# Add articles 38articles = ampoule.Index("articles", url_transform=article_url, sort_by=lambda x: x.date, reverse=True, exclude=r"^DRAFT_") 39site.add_from_index(articles, "/articles", "article.html") 40 41# Add projects 42projects = ampoule.Index("projects", url_transform=article_url, sort_by=lambda x: x.file_name) 43site.add_from_index(projects, "/projects", "project.html") 44 45# Create the index pages 46main_page = ampoule.Page(site, "home.html", articles=articles) 47projects_page = ampoule.Page(site, "projects.html", projects=projects) 48topics = collections.defaultdict(list) 49for article in articles: 50for topic in article["topics"]: 51topics[topic].append(("posts", article)) 52for project in projects: 53for topic in project["topics"]: 54topics[topic].append(("projects", project)) 55topics = sorted(topics.items(), key=lambda x: x[0]) 56index_page = ampoule.Page(site, "index.html", topics=topics) 57 58# Add the pages to the site 59site.add_page("/", main_page) 60site.add_page("/projects/", projects_page) 61site.add_page("/index/", index_page) 62 63# Add static files 64site.add_from_index( 65ampoule.Index("static", recursive=True, exclude=r"\.md$", static=True), 66"/static" 67) 68 69site.build(dont_delete=[".git"]) 70