By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 main.py

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