This repository was archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhtml_components.py
More file actions
52 lines (37 loc) · 1.54 KB
/
html_components.py
File metadata and controls
52 lines (37 loc) · 1.54 KB
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
import sys
from pathlib import Path
import mako.exceptions
import rich
from mako.template import Template
from mako.lookup import TemplateLookup
from pandas.core.frame import Series
import helpers
from word import DpsRuWord
class TemplateBase:
def __init__(self, template_path: Path):
module_dir = Path(__file__).parent
lookup = TemplateLookup(directories=[module_dir / 'assets/templates/']) # TODO To rsc
self._template = Template(filename=str(template_path), lookup=lookup)
def _render_helper(self, **kwargs) -> str:
try:
return self._template.render(**kwargs)
except Exception: # pylint: disable=broad-except
rich.print(f'{helpers.timeis()} [red]Template exception:')
rich.print(mako.exceptions.text_error_template().render())
sys.exit(1)
class HeaderTemplate(TemplateBase):
def __init__(self):
super().__init__('./assets/templates/header.html')
def render(self, css: str, js='') -> str:
return self._render_helper(css=css, js=js)
class WordTemplate(TemplateBase):
def render(self, word: DpsRuWord, table_data_read: str) -> str:
return self._render_helper(
conjugations=helpers.CONJUGATIONS,
declensions=helpers.DECLENSIONS,
indeclinables=helpers.INDECLINABLES,
table_data_read=table_data_read,
word=word)
class AbbreviationTemplate(TemplateBase):
def render(self, abbreviation: Series) -> str:
return self._render_helper(abbreviation=abbreviation)