aboutsummaryrefslogtreecommitdiff
path: root/blog_experiment/blog/__init__.py
blob: 81332ddd5f46b43d2c6a5644d1af3ee11f4c835a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pathlib
import re

import bicephalus

import htmlgenerator as h

from blog import blog_pages, page, html, pretty, gemtext


class SimplePage(page.BasePage):
    def __init__(self, request, url, title):
        super().__init__(request)
        self.url = url
        self.title = title

    def get_gemini_content(self):
        return (
            bicephalus.Status.OK,
            "text/gemini",
            pathlib.Path(f"static{self.url}index.gmi").read_text(),
        )

    def get_http_content(self):
        return (
            bicephalus.Status.OK,
            "text/html",
            pretty.pretty_html(h.render(
            h.HTML(
                h.HEAD(
                    h.TITLE(self.title),
                ),
                h.BODY(*html.gemini_to_html(gemtext.parse(self.get_gemini_content()[2])))
            ), {})),
        )


def handler(request: bicephalus.Request) -> bicephalus.Response:
    if request.path == "/":
        return blog_pages.Root(request).response()
    if re.match(r"/\d{4}/\d{2}/.*/", request.path):
        blog_file = pathlib.Path("content") / (request.path[1:-1] + ".gmi")
        if blog_file.exists():
            return blog_pages.EntryPage(request, blog_file).response()
    if request.path == "/feed/" and request.proto == bicephalus.Proto.HTTP:
        return blog_pages.Root(request).feed()
    if request.path == "/about/":
        return SimplePage(request, request.path, "About Álex Córcoles").response()
    if request.path == "/laspelis/":
        return SimplePage(request, request.path, "laspelis").response()
    if re.match(r"/laspelis/\d+/?", request.path):
        return SimplePage(request, request.path.removesuffix("/") + "/", request.path).response()

    return page.NotFound(request).response()