spgit

A static page generator for git repositories

git clone git://git.janzachar.dev/spgit.git


0from .repo import page
1from .repo_file import tree as Tree
2import markdown
3import settings
4
5
6def info(r: Repo) -> str:
7    def unpack(s):
8        out = "<ul>"
9        for k,v in s.items():
10            if type(v) is dict:
11                out += f"<li>{k}:</li>"
12                out += unpack(v)
13            elif type(v) is list:
14                out += f"<li>{k}: {' '.join(v)}</li>"
15            else:
16                out += f"<li>{k}: {v}</li>"
17        out += "</ul>"
18        return out 
19
20    if not r.meta.info:
21        return ""
22
23    return f"""
24        <section id="info">
25            <h2>info</h2>
26            {unpack(r.meta.info)}
27        </section>
28    """
29
30def commits(r) -> str:
31    out = '<h2>commits</h2><table>'
32
33    commits = list(r.repo.iter_commits(all=True, max_count=settings.COMMIT_COUNT))
34    for c in commits:
35        time = c.committed_datetime.date()
36        message = c.message.split("\n")[0]
37        if len(message) > 80:
38            message = message[:80-3] + "..."
39
40        out += f"""
41            <tr>
42                <td>[{time}]</td>
43                <td>{message}</td>
44            </tr>
45        """
46    out += "</table>"
47
48    count = r.repo.head.commit.count()
49    if settings.COMMIT_COUNT < count:
50        out += f"<p> and {count - settings.COMMIT_COUNT} more..."
51
52    return '<section id="commits">' + out + '</section>'
53
54
55def readme(r: Repo) -> str:
56    tree = r.repo.head.commit.tree
57    if "README.md" not in tree:
58        return ""
59
60    file = tree["README.md"].data_stream
61    text = file.read().decode()
62    return '<section id="readme">' + \
63            markdown.markdown(text) + \
64            '</section>'
65
66def files(r: Repo) -> str:
67    tree = r.repo.head.commit.tree
68    return f"""
69        <section id="files">
70            <h2>files</h2>
71            {Tree(r, tree)}
72        </section>
73    """
74
75
76def gen(r: Repo) -> str:
77    return page(r, 
78        info(r) +
79        commits(r) +
80        files(r) +
81        "<hr>" +
82        readme(r)
83    )
84
85