|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
1 |
|
""" |
|
3
|
|
|
This module contains a HTMLExportDefinition Model |
|
4
|
|
|
""" |
|
5
|
1 |
|
from loglan_db.model_export import ExportDefinition |
|
6
|
1 |
|
from loglan_db.model_html import DEFAULT_HTML_STYLE |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
1 |
|
class DefinitionFormatter: |
|
10
|
1 |
|
@staticmethod |
|
11
|
1 |
|
def format_body(body: str) -> str: |
|
12
|
|
|
""" |
|
13
|
|
|
Substitutes tags in the definition's body |
|
14
|
|
|
Formats punctuation signs |
|
15
|
|
|
:param body: |
|
16
|
|
|
:return: |
|
17
|
|
|
""" |
|
18
|
1 |
|
to_key = '<k>' # key |
|
19
|
1 |
|
tc_key = '</k>' |
|
20
|
1 |
|
to_log = '<l>' # log |
|
21
|
1 |
|
tc_log = '</l>' |
|
22
|
|
|
|
|
23
|
1 |
|
return body \ |
|
24
|
|
|
.replace("<", "<").replace(">", ">") \ |
|
25
|
|
|
.replace("«", to_key).replace("»", tc_key) \ |
|
26
|
|
|
.replace("{", to_log).replace("}", tc_log) \ |
|
27
|
|
|
.replace("...", "…").replace("--", "—") |
|
28
|
|
|
|
|
29
|
1 |
|
@staticmethod |
|
30
|
1 |
|
def highlight_key(def_body, word) -> str: |
|
31
|
|
|
""" |
|
32
|
|
|
Highlights the current key from the list, deselecting the rest |
|
33
|
|
|
:param def_body: |
|
34
|
|
|
:param word: |
|
35
|
|
|
:return: |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
1 |
|
to_key = '<k>' # key |
|
39
|
1 |
|
tc_key = '</k>' |
|
40
|
|
|
|
|
41
|
1 |
|
word_template_original = f'{to_key}{word}{tc_key}' |
|
42
|
1 |
|
word_template_temp = f'<do_not_delete>{word}</do_not_delete>' |
|
43
|
1 |
|
def_body = def_body.replace(word_template_original, word_template_temp) |
|
44
|
1 |
|
def_body = def_body.replace(to_key, "").replace(tc_key, "") |
|
45
|
1 |
|
def_body = def_body.replace(word_template_temp, word_template_original) |
|
46
|
1 |
|
return def_body |
|
47
|
|
|
|
|
48
|
1 |
|
@staticmethod |
|
49
|
|
|
def tagged_word_origin_x(d_source_word, tag): |
|
50
|
1 |
|
w_origin_x = d_source_word.origin_x \ |
|
51
|
|
|
if d_source_word.origin_x and d_source_word.type.group == "Cpx" else str() |
|
52
|
1 |
|
return tag % w_origin_x if w_origin_x else str() |
|
53
|
|
|
|
|
54
|
1 |
|
@staticmethod |
|
55
|
|
|
def tagged_word_name(usage, d_source_word, tag): |
|
56
|
1 |
|
w_name = d_source_word.name if not usage \ |
|
57
|
|
|
else usage.replace("%", d_source_word.name) |
|
58
|
1 |
|
return tag % w_name |
|
59
|
|
|
|
|
60
|
1 |
|
@staticmethod |
|
61
|
|
|
def tagged_definition_body(body, key_word, tag): |
|
62
|
1 |
|
definition_body = HTMLExportDefinition.format_body(body) |
|
63
|
1 |
|
definition_body = HTMLExportDefinition.highlight_key(definition_body, key_word) |
|
64
|
1 |
|
definition_body = tag % definition_body |
|
65
|
1 |
|
return definition_body |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
class HTMLExportDefinition(ExportDefinition, DefinitionFormatter): |
|
69
|
|
|
""" |
|
70
|
|
|
HTMLExportDefinition Class |
|
71
|
|
|
""" |
|
72
|
|
|
|
|
73
|
1 |
|
def export_for_english(self, word: str, style: str = DEFAULT_HTML_STYLE) -> str: |
|
74
|
|
|
""" |
|
75
|
|
|
|
|
76
|
|
|
:param word: |
|
77
|
|
|
:param style: |
|
78
|
|
|
:return: |
|
79
|
|
|
""" |
|
80
|
|
|
|
|
81
|
|
|
# de = definition english |
|
82
|
1 |
|
tags = { |
|
83
|
|
|
"normal": [ |
|
84
|
|
|
'<span class="dg">(%s)</span>', |
|
85
|
|
|
'<span class="dt">[%s]</span> ', |
|
86
|
|
|
' <span class="db">%s</span>', |
|
87
|
|
|
f'<span class="definition eng" id={self.id}>%s</span>', |
|
88
|
|
|
'<div class="d_line">%s</div>', |
|
89
|
|
|
'<span class="w_name">%s</span>, ', |
|
90
|
|
|
'<span class="w_origin"><%s></span> ', |
|
91
|
|
|
], |
|
92
|
|
|
"ultra": [ |
|
93
|
|
|
'(%s)', '[%s] ', ' %s', '<de>%s</de>', |
|
94
|
|
|
'<ld>%s</ld>', '<wn>%s</wn>, ', '<o><%s></o> ', ], |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
t_d_gram, t_d_tags, t_d_body, t_def, t_def_line, t_word_name, t_word_origin = tags[style] |
|
98
|
|
|
|
|
99
|
1 |
|
gram_form = f'{str(self.slots) if self.slots else ""}' + self.grammar_code |
|
100
|
1 |
|
def_gram = t_d_gram % gram_form if gram_form else '' |
|
101
|
1 |
|
def_tags = t_d_tags % self.case_tags.replace("-", "‍-‍") if self.case_tags else '' |
|
102
|
|
|
|
|
103
|
1 |
|
def_body = HTMLExportDefinition.tagged_definition_body(self.body, word, t_d_body) |
|
104
|
1 |
|
word_name = HTMLExportDefinition.tagged_word_name(self.usage, self.source_word, t_word_name) |
|
105
|
1 |
|
word_origin_x = HTMLExportDefinition.tagged_word_origin_x(self.source_word, t_word_origin) |
|
106
|
|
|
|
|
107
|
1 |
|
definition = t_def % f'{def_tags}{def_gram}{def_body}' |
|
108
|
1 |
|
return t_def_line % f'{word_name}{word_origin_x}{definition}' |
|
109
|
|
|
|
|
110
|
1 |
|
def export_for_loglan(self, style: str = DEFAULT_HTML_STYLE) -> str: |
|
111
|
|
|
""" |
|
112
|
|
|
|
|
113
|
|
|
:param style: |
|
114
|
|
|
:return: |
|
115
|
|
|
""" |
|
116
|
1 |
|
tags = { |
|
117
|
|
|
# usage, gram, body, tags, definition |
|
118
|
|
|
"normal": [ |
|
119
|
|
|
'<span class="du">%s</span> ', '<span class="dg">(%s)</span> ', |
|
120
|
|
|
'<span class="db">%s</span>', ' <span class="dt">[%s]</span>', |
|
121
|
|
|
f'<div class="definition log" id={self.id}>%s</div>', ], |
|
122
|
|
|
"ultra": ['<du>%s</du> ', '(%s) ', '%s', ' [%s]', '<dl>%s</dl>', ], |
|
123
|
|
|
} |
|
124
|
1 |
|
t_d_usage, t_d_gram, t_d_body, t_d_tags, t_definition = tags[style] |
|
125
|
|
|
|
|
126
|
1 |
|
def_usage = t_d_usage % self.usage.replace("%", "—") if self.usage else '' |
|
127
|
1 |
|
gram_form = f"{str(self.slots) if self.slots else ''}" + self.grammar_code |
|
128
|
1 |
|
def_gram = t_d_gram % gram_form if gram_form else '' |
|
129
|
1 |
|
def_body = t_d_body % HTMLExportDefinition.format_body(self.body) |
|
130
|
1 |
|
def_tags = t_d_tags % self.case_tags.replace("-", "‍-‍") if self.case_tags else '' |
|
131
|
|
|
return t_definition % f'{def_usage}{def_gram}{def_body}{def_tags}' |
|
132
|
|
|
|