Passed
Pull Request — master (#79)
by torrua
02:44
created

EnglishItem.select_definitions_by_key()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 14
rs 9.75
c 0
b 0
f 0
cc 1
nop 4
1
# -*- coding: utf-8 -*-
2
3
from loglan_core.definition import BaseDefinition
4
from loglan_core.addons.definition_selector import DefinitionSelector
5
from loglan_core.event import BaseEvent
6
from loglan_core.word import BaseWord
7
from sqlalchemy.sql.selectable import Select
8
9
from app.site.compose import DEFAULT_HTML_STYLE, Item
10
from app.site.compose.definition_formatter import DefinitionFormatter
11
12
13
class EnglishItem(Item):
14
    def __init__(
15
        self,
16
        definitions: list[BaseDefinition],
17
        key: str,
18
        style: str = DEFAULT_HTML_STYLE,
19
    ):
20
        self.definitions = definitions
21
        self.key = key
22
        self.style = style
23
24
    def export_as_html(self):
25
        return "\n".join(
26
            [self.export_for_english(d, self.key, self.style) for d in self.definitions]
27
        )
28
29
    @staticmethod
30
    def export_for_english(  # pylint: disable=too-many-locals
31
        definition: BaseDefinition,
32
        key: str,
33
        style: str = DEFAULT_HTML_STYLE,
34
    ) -> str:
35
        # de = definition english
36
        tags = {
37
            "normal": [
38
                '<span class="dg">(%s)</span>',
39
                '<span class="dt">[%s]</span> ',
40
                ' <span class="db">%s</span>',
41
                f'<span class="definition eng" id={definition.id}>%s</span>',
42
                '<div class="d_line">%s</div>',
43
                '<span class="w_name">%s</span>, ',
44
                '<span class="w_origin">&lt;%s&gt;</span> ',
45
            ],
46
            "ultra": [
47
                "(%s)",
48
                "[%s] ",
49
                " %s",
50
                "<de>%s</de>",
51
                "<ld>%s</ld>",
52
                "<wn>%s</wn>, ",
53
                "<o>&lt;%s&gt;</o> ",
54
            ],
55
        }
56
57
        (
58
            t_d_gram,
59
            t_d_tags,
60
            t_d_body,
61
            t_def,
62
            t_def_line,
63
            t_word_name,
64
            t_word_origin,
65
        ) = tags[style]
66
67
        gram_form = str(definition.slots or "") + definition.grammar_code
68
        def_gram = t_d_gram % gram_form if gram_form else ""
69
        def_tags = (
70
            t_d_tags % definition.case_tags.replace("-", "&zwj;-&zwj;")
71
            if definition.case_tags
72
            else ""
73
        )
74
75
        def_body = DefinitionFormatter(definition).tagged_definition_body(key, t_d_body)
76
        word_name = DefinitionFormatter(definition).tagged_word_name(t_word_name)
77
        word_origin_x = DefinitionFormatter(definition).tagged_word_origin_x(
78
            t_word_origin
79
        )
80
81
        definition = t_def % f"{def_tags}{def_gram}{def_body}"
82
        return t_def_line % f"{word_name}{word_origin_x}{definition}"
83