aboutsummaryrefslogtreecommitdiff
path: root/blog/generate_html.py
blob: 1d8d0639c97622c0422e9b018c14e9909584154a (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
#! /usr/bin/python3

"""
A script to generate html from the rss feed
"""

import xml.etree.ElementTree as et

# Generates a html table for the article
# I use table layout because fuck you
def make_article_table(article_info, article):
    return """
        <table border="1" width="60%">
            <tr><td><h2 id=\"{name}\">{title}</h2>--- {date}</td></tr>
            <tr><td>{article}</td></tr>
        </table>
    """.format(
        name=article_info["name"],
        title=article_info["title"],
        date=article_info["pubDate"],
        article=article
    )

def main():
    tree = et.parse("articles.xml")
    root = tree.getroot()

    template = ""

    # Open html template
    with open("template.html", "r") as fp:
        template = fp.read()

    article_list = "<ul>\n"
    article_html = ""

    # Get articles from rss
    for item in root:
        article_info = {
            "title": item.find("title").text,
            "name": item.find("name").text,
            "pubDate": item.find("pubDate").text
        }

        article = ""
        
        with open(item.find("file").text, "r") as fp:
            article = fp.read()

        # Remove article tags.
        article = article[article.find("<article>")+9::]
        article = article[:article.find("</article>"):]

        # Add article table to html
        article_html += make_article_table(article_info, article)

        # Add article to list.
        article_list += \
            f"<li><a href=\"#{article_info["name"]}\">{article_info["title"]}</a></li>\n"

    article_list += "</ul>"

    # Format the articles into the html
    template = template.format(article_list=article_list, articles=article_html)
    print(template)

if __name__ == "__main__":
    main()