summaryrefslogtreecommitdiff
path: root/utab/rendering.py
blob: d940d68e62bcee57e43af7338ff29fdf143e2acc (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
import urllib
from .const import *

# dead simple template engine
def render_page(template: str, **kwargs):
    page = template
    for k, v in kwargs.items():
        page = page.replace(f"%{str(k)}%", str(v))
    return page


def render_sites(sites: list, columns=8, rows=4, action="go"):
    top_sites = sorted(sites, key=lambda s: int(s[VISITS]), reverse=True)[
        : (columns * rows)  # top col*row sites, default=32
    ]
    # site_rows: group sites into rows
    if len(top_sites) < 32:
        top_sites.extend([None] * (32 - len(top_sites)))

    site_rows = list(zip(*[top_sites[n::columns] for n in range(columns)]))
    html = '<div class="sites-grid">'
    for row in site_rows:
        html += '<div class="sites-row">'
        for col in row:
            if col is not None:
                html += (
                    '<div class="sites-item">'
                    f'<a class="site" href="/{action}/{urllib.parse.quote(col[URL], safe="")}">'
                    f'<img class="site-favicon" src="{col[FAVICON]}" title="{col[URL]}"/></a>'
                    + f'<p class="site-title">{col[TITLE]}</p>'
                    + "</div>"
                )
        html += "</div>"
    html += "</div>"
    return html