|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
1 |
|
""" |
|
3
|
|
|
This module contains a HTMLExportDefinition Model |
|
4
|
|
|
""" |
|
5
|
1 |
|
import re |
|
6
|
|
|
|
|
7
|
1 |
|
from loglan_db import db |
|
8
|
1 |
|
from loglan_db.model_db.base_definition import BaseDefinition |
|
9
|
1 |
|
from loglan_db.model_html import DEFAULT_HTML_STYLE |
|
10
|
1 |
|
from loglan_db.model_html.html_word import HTMLExportWord |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
class DefinitionFormatter: |
|
14
|
|
|
""" |
|
15
|
|
|
Additional methods for definition's formatting |
|
16
|
|
|
""" |
|
17
|
1 |
|
@staticmethod |
|
18
|
1 |
|
def format_body(body: str) -> str: |
|
19
|
|
|
""" |
|
20
|
|
|
Substitutes tags in the definition's body |
|
21
|
|
|
Formats punctuation signs |
|
22
|
|
|
:param body: |
|
23
|
|
|
:return: |
|
24
|
|
|
""" |
|
25
|
1 |
|
to_key = '<k>' # key |
|
26
|
1 |
|
tc_key = '</k>' |
|
27
|
1 |
|
to_log = '<l>' # log |
|
28
|
1 |
|
tc_log = '</l>' |
|
29
|
|
|
|
|
30
|
1 |
|
return body \ |
|
31
|
|
|
.replace("<", "<").replace(">", ">") \ |
|
32
|
|
|
.replace("«", to_key).replace("»", tc_key) \ |
|
33
|
|
|
.replace("{", to_log).replace("}", tc_log) \ |
|
34
|
|
|
.replace("...", "…").replace("--", "—") |
|
35
|
|
|
|
|
36
|
1 |
|
@staticmethod |
|
37
|
1 |
|
def highlight_key(def_body, word, case_sensitive: bool = False) -> str: |
|
38
|
|
|
""" |
|
39
|
|
|
Highlights the current key from the list, deselecting the rest |
|
40
|
|
|
:param def_body: |
|
41
|
|
|
:param word: |
|
42
|
|
|
:param case_sensitive: |
|
43
|
|
|
:return: |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
1 |
|
to_key = '<k>' # key |
|
47
|
1 |
|
tc_key = '</k>' |
|
48
|
1 |
|
to_del = '<do_not_delete>' |
|
49
|
1 |
|
tc_del = '</do_not_delete>' |
|
50
|
|
|
|
|
51
|
1 |
|
key_pattern = f"{to_key}{word.replace('*', '.*')}{tc_key}" |
|
52
|
1 |
|
list_of_keys = def_body.replace("</k>", "</k>@").split("@") |
|
53
|
|
|
|
|
54
|
1 |
|
for key in list_of_keys: |
|
55
|
1 |
|
res = re.search(key_pattern, key, flags=0 if case_sensitive else re.IGNORECASE) |
|
56
|
1 |
|
if not res: |
|
57
|
1 |
|
continue |
|
58
|
1 |
|
original_key = res[0] |
|
59
|
1 |
|
replace_key = original_key.replace(to_key, to_del).replace(tc_key, tc_del) |
|
60
|
1 |
|
def_body = def_body.replace(original_key, replace_key) |
|
61
|
|
|
|
|
62
|
1 |
|
def_body = def_body.replace(tc_key, str()).replace(to_key, str()) |
|
63
|
1 |
|
def_body = def_body.replace(to_del, to_key).replace(tc_del, tc_key) |
|
64
|
|
|
|
|
65
|
1 |
|
return def_body |
|
66
|
|
|
|
|
67
|
1 |
|
@staticmethod |
|
68
|
1 |
|
def tagged_word_origin_x(d_source_word, tag: str) -> str: |
|
69
|
|
|
""" |
|
70
|
|
|
Generate Word.origin_x as HTML tag |
|
71
|
|
|
Args: |
|
72
|
|
|
d_source_word: |
|
73
|
|
|
tag: |
|
74
|
|
|
|
|
75
|
|
|
Returns: |
|
76
|
|
|
|
|
77
|
|
|
""" |
|
78
|
1 |
|
w_origin_x = d_source_word.origin_x \ |
|
79
|
|
|
if d_source_word.origin_x and d_source_word.type.group == "Cpx" else str() |
|
80
|
1 |
|
return tag % w_origin_x if w_origin_x else str() |
|
81
|
|
|
|
|
82
|
1 |
|
@staticmethod |
|
83
|
1 |
|
def tagged_word_name(usage: str, d_source_word, tag: str) -> str: |
|
84
|
|
|
""" |
|
85
|
|
|
Generate Word.name as HTML tag |
|
86
|
|
|
Args: |
|
87
|
|
|
usage: |
|
88
|
|
|
d_source_word: |
|
89
|
|
|
tag: |
|
90
|
|
|
|
|
91
|
|
|
Returns: |
|
92
|
|
|
|
|
93
|
|
|
""" |
|
94
|
1 |
|
w_name = d_source_word.name if not usage \ |
|
95
|
|
|
else usage.replace("%", d_source_word.name) |
|
96
|
1 |
|
return tag % w_name |
|
97
|
|
|
|
|
98
|
1 |
|
@classmethod |
|
99
|
1 |
|
def tagged_definition_body(cls, body: str, key_word: str, tag: str) -> str: |
|
100
|
|
|
""" |
|
101
|
|
|
Generate Definition.body as HTML tag with highlighted key word |
|
102
|
|
|
Args: |
|
103
|
|
|
body: |
|
104
|
|
|
key_word: |
|
105
|
|
|
tag: |
|
106
|
|
|
|
|
107
|
|
|
Returns: |
|
108
|
|
|
|
|
109
|
|
|
""" |
|
110
|
1 |
|
definition_body = cls.format_body(body) |
|
111
|
1 |
|
definition_body = cls.highlight_key(definition_body, key_word) |
|
112
|
1 |
|
definition_body = tag % definition_body |
|
113
|
1 |
|
return definition_body |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
1 |
|
class HTMLExportDefinition(BaseDefinition, DefinitionFormatter): |
|
117
|
|
|
""" |
|
118
|
|
|
HTMLExportDefinition Class |
|
119
|
|
|
""" |
|
120
|
|
|
|
|
121
|
1 |
|
_source_word = db.relationship( |
|
122
|
|
|
HTMLExportWord.__name__, back_populates="_definitions", viewonly=True) |
|
123
|
|
|
|
|
124
|
1 |
|
def export_for_english(self, word: str, style: str = DEFAULT_HTML_STYLE) -> str: |
|
125
|
|
|
""" |
|
126
|
|
|
|
|
127
|
|
|
:param word: |
|
128
|
|
|
:param style: |
|
129
|
|
|
:return: |
|
130
|
|
|
""" |
|
131
|
|
|
|
|
132
|
|
|
# de = definition english |
|
133
|
1 |
|
tags = { |
|
134
|
|
|
"normal": [ |
|
135
|
|
|
'<span class="dg">(%s)</span>', |
|
136
|
|
|
'<span class="dt">[%s]</span> ', |
|
137
|
|
|
' <span class="db">%s</span>', |
|
138
|
|
|
f'<span class="definition eng" id={self.id}>%s</span>', |
|
139
|
|
|
'<div class="d_line">%s</div>', |
|
140
|
|
|
'<span class="w_name">%s</span>, ', |
|
141
|
|
|
'<span class="w_origin"><%s></span> ', |
|
142
|
|
|
], |
|
143
|
|
|
"ultra": [ |
|
144
|
|
|
'(%s)', '[%s] ', ' %s', '<de>%s</de>', |
|
145
|
|
|
'<ld>%s</ld>', '<wn>%s</wn>, ', '<o><%s></o> ', ], |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
t_d_gram, t_d_tags, t_d_body, t_def, t_def_line, t_word_name, t_word_origin = tags[style] |
|
149
|
|
|
|
|
150
|
1 |
|
gram_form = self.stringer(self.slots) + self.grammar_code |
|
151
|
1 |
|
def_gram = t_d_gram % gram_form if gram_form else '' |
|
152
|
1 |
|
def_tags = t_d_tags % self.case_tags.replace("-", "‍-‍") if self.case_tags else '' |
|
153
|
|
|
|
|
154
|
1 |
|
def_body = self.tagged_definition_body(self.body, word, t_d_body) |
|
155
|
1 |
|
word_name = self.tagged_word_name(self.usage, self.source_word, t_word_name) |
|
156
|
1 |
|
word_origin_x = self.tagged_word_origin_x(self.source_word, t_word_origin) |
|
157
|
|
|
|
|
158
|
1 |
|
definition = t_def % f'{def_tags}{def_gram}{def_body}' |
|
159
|
1 |
|
return t_def_line % f'{word_name}{word_origin_x}{definition}' |
|
160
|
|
|
|
|
161
|
1 |
|
def export_for_loglan(self, style: str = DEFAULT_HTML_STYLE) -> str: |
|
162
|
|
|
""" |
|
163
|
|
|
|
|
164
|
|
|
:param style: |
|
165
|
|
|
:return: |
|
166
|
|
|
""" |
|
167
|
1 |
|
tags = { |
|
168
|
|
|
# usage, gram, body, tags, definition |
|
169
|
|
|
"normal": [ |
|
170
|
|
|
'<span class="du">%s</span> ', '<span class="dg">(%s)</span> ', |
|
171
|
|
|
'<span class="db">%s</span>', ' <span class="dt">[%s]</span>', |
|
172
|
|
|
f'<div class="definition log" id={self.id}>%s</div>', ], |
|
173
|
|
|
"ultra": ['<du>%s</du> ', '(%s) ', '%s', ' [%s]', '<dl>%s</dl>', ], |
|
174
|
|
|
} |
|
175
|
1 |
|
t_d_usage, t_d_gram, t_d_body, t_d_tags, t_definition = tags[style] |
|
176
|
|
|
|
|
177
|
1 |
|
def_usage = t_d_usage % self.usage.replace("%", "—") if self.usage else '' |
|
178
|
1 |
|
gram_form = f"{str(self.slots) if self.slots else ''}" + self.grammar_code |
|
179
|
1 |
|
def_gram = t_d_gram % gram_form if gram_form else '' |
|
180
|
1 |
|
def_body = t_d_body % self.format_body(self.body) |
|
181
|
1 |
|
def_tags = t_d_tags % self.case_tags.replace("-", "‍-‍") if self.case_tags else '' |
|
182
|
|
|
return t_definition % f'{def_usage}{def_gram}{def_body}{def_tags}' |
|
183
|
|
|
|