summaryrefslogtreecommitdiff
path: root/main.py
blob: 89c15440057aa9fd8aa4b1d5aa1e3c42455b6e5f (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from urllib.parse import urlparse

import urllib.parse
import lxml.html
import mistune
import houdini
import jinja2
import shutil
import os

ENV = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))

class MyRenderer(mistune.HTMLRenderer):
    def block_code(self, code, info=None):
        if not info:
            return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(code.strip()))
        lexer = get_lexer_by_name(info, stripall=True)
        formatter = HtmlFormatter()
        return highlight(code, lexer, formatter)

    def block_quote(self, content):
        content = content[3:-5]
        out = '\n<blockquote>'
        for line in houdini.escape_html(content.strip()).split("\n"):
            out += '\n<span class="quote">{}</span><br>'.format(line)
        return out + '\n</blockquote>'

    def image(self, link, text, title):
        return "<a href='%s' target='_blank'><img alt='%s' src='%s'></a>" % (
            urlparse(link)._replace(query='').geturl(), text, link
        )

    def heading(self, text, level):
        hash_ = urllib.parse.quote_plus(text)
        return "<h%d id='%s'>%s <a class='header_linker' href='#%s'>[#]</a></h%d>\n" % (
            level, hash_, text, hash_, level
        )

def parse_file(path):
    with open(path, "r") as f:
        unformatted = f.read()

    return parse_text(unformatted)[0]

def parse_text(unformatted):
    md = mistune.create_markdown(
        renderer = MyRenderer(),
        plugins = ["strikethrough", "table", "url", "task_lists", "def_list"]
    )
    html = md(unformatted)
    if html == "":
        return "", ""

    return html, get_headers(html)

def get_headers(html):
    root = lxml.html.fromstring(html)

    headers = []
    thesmallestlevel = 7
    for node in root.xpath('//h1|//h2|//h3|//h4|//h5//h6'):
        level = int(node.tag[-1])
        if level < thesmallestlevel:
            thesmallestlevel = level
        headers.append((
            urllib.parse.unquote_plus(node.attrib["id"]),
            level,
            "#%s" % node.attrib["id"])
        )

    headers = [(i[0], i[1] - thesmallestlevel, i[2]) for i in headers]
    md_template = jinja2.Template("""
        {% for text, depth, link in contents %}
            {{ "    " * depth }} - [{{ text }}]({{ link }})
        {% endfor %}
            """)

    return mistune.html(md_template.render(contents = headers))

def index():
    src_file = "content/index.md"
    template_file = "index.html.j2"
    dst_file = "index.html"

    template = ENV.get_template(template_file)

    with open(os.path.join('static/%s' % dst_file), 'w') as html_file:
        with open(src_file, "r") as f:
            html = template.render(
                prefix = "./",
                title = "eva's site",
                content = parse_text(f.read())[0]
            )
        html_file.write(html)

def thoughts_overview():
    src_file = "content/thoughts.md"
    template_file = "thoughts.html.j2"
    dst_file = "thoughts.html"

    template = ENV.get_template(template_file)

    with open(os.path.join('static/%s' % dst_file), 'w') as html_file:
        with open(src_file, "r") as f:
            html = template.render(
                prefix = "./",
                title = "my thoughts",
                content = parse_text(f.read())[0]
            )
        html_file.write(html)


def thoughts():
    src_path = "content/thoughts"
    template_file = "thought.html.j2"
    dst_path = "static/thoughts"

    template = ENV.get_template(template_file)

    if not os.path.isdir(dst_path):
        os.mkdir(dst_path)

    for file in os.listdir(src_path):
        dst_file = file.replace(".md", ".html")
        with open(os.path.join(dst_path, dst_file), 'w') as html_file:
            with open(os.path.join(src_path, file), 'r') as f:
                html = template.render(
                    prefix = "../",
                    title = file.replace("-", " ").replace(".md", ""),
                    content = parse_text(f.read())[0]
                )
            html_file.write(html)

def about():
    src_file = "content/about.md"
    template_file = "about.html.j2"
    dst_file = "about.html"

    template = ENV.get_template(template_file)

    with open(os.path.join('static/%s' % dst_file), 'w') as html_file:
        with open(src_file, "r") as f:
            html = template.render(
                prefix = "./",
                title = "about me",
                content = parse_text(f.read())[0]
            )
        html_file.write(html)
    
            
def main():
    if os.path.isdir("static"):
        shutil.rmtree("static")
    os.mkdir("static")

    shutil.copytree("content/styles", "static/styles")
    shutil.copytree("content/images", "static/images")

    index()
    about()
    thoughts_overview()
    thoughts()

if __name__ == '__main__':
    main()