aboutsummaryrefslogtreecommitdiffstats
path: root/quips_utils.py
blob: aaf1b7aff3304b21ed23cd7abadce5ee7c73ced2 (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
# Copyright (C) 2022 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# You may use this file under the terms of the CC0 license.
# See the file LICENSE.CC0 from this package for details.

import re
from pathlib import Path
from dash import dcc, html
from web_utils import get_header, get_column, get_file_content
import dash_dangerously_set_inner_html


def get_quip_number(s):
    return int(re.findall("\d{4}", s)[0])


def replace_quip_url(s):
    # quip-0001.html -> /quips/1
    quip_links = re.findall("quip-\d{4}-?[\w-]*.html", s)
    replacements = {}
    for old in quip_links:
        if old not in replacements:
            number = get_quip_number(old)
            replacements[old] = f"/quips/{number}"

    for k, v in replacements.items():
        s = s.replace(k, v)
    return s


def get_quips_index():
    return """index"""


def get_html_content(fname):
    content = ""
    with open(fname) as f:
        content = f.read()

    content = replace_quip_url(content)
    return content


def get_quip_html(html_file):
    return html.Div(
        [
            get_header(),
            html.Div(
                children=[],
                style={"height": "10px"},
                className="row",
            ),
            html.Div(
                children=[
                    html.Div(
                        dash_dangerously_set_inner_html.DangerouslySetInnerHTML(
                            get_html_content(html_file)
                        ),
                    ),
                ],
                className="row justify-center",
            ),
        ]
    )


def get_quip_layout_from_file(quip_file):
    if not Path(quip_file).exists():
        raise Exception(f"File '{quip_file}' do not exist")

    return html.Div(
        [
            get_header(),
            html.Div(
                children=[],
                style={"height": "10px"},
                className="row",
            ),
            html.Div(
                children=[
                    get_column(
                        divs=[get_quips_index()],
                        columns_number="three",
                    ),
                    get_column(
                        divs=[get_file_content(quip_file)],
                        columns_number="seven",
                    ),
                ],
                className="row justify-center",
            ),
        ]
    )


def resolve_quips_url(numbers, pathname):
    if not pathname.startswith("/quips/"):
        return False

    number = pathname.replace("/quips/", "")
    try:
        n = int(number)
    except ValueError:
        return False

    if n not in numbers:
        return False

    return True