|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import re |
|
4
|
|
|
|
|
5
|
|
|
from loglan_core import Definition |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class DefinitionFormatter: |
|
9
|
|
|
""" |
|
10
|
|
|
Additional methods for definition's formatting |
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
def __init__(self, definition: Definition): |
|
14
|
|
|
self.d = definition |
|
15
|
|
|
|
|
16
|
|
|
@property |
|
17
|
|
|
def body_formatted(self) -> str: |
|
18
|
|
|
""" |
|
19
|
|
|
Substitutes tags in the definition's body |
|
20
|
|
|
Formats punctuation signs |
|
21
|
|
|
:return: |
|
22
|
|
|
""" |
|
23
|
|
|
to_key = "<k>" # key |
|
24
|
|
|
tc_key = "</k>" |
|
25
|
|
|
to_log = "<l>" # log |
|
26
|
|
|
tc_log = "</l>" |
|
27
|
|
|
|
|
28
|
|
|
return ( |
|
29
|
|
|
self.d.body.replace("<", "<") |
|
30
|
|
|
.replace(">", ">") |
|
31
|
|
|
.replace("«", to_key) |
|
32
|
|
|
.replace("»", tc_key) |
|
33
|
|
|
.replace("{", to_log) |
|
34
|
|
|
.replace("}", tc_log) |
|
35
|
|
|
.replace("...", "…") |
|
36
|
|
|
.replace("--", "—") |
|
37
|
|
|
) |
|
38
|
|
|
|
|
39
|
|
|
def highlight_key(self, key: str, case_sensitive: bool = False) -> str: |
|
40
|
|
|
""" |
|
41
|
|
|
Highlights the current key from the list, deselecting the rest |
|
42
|
|
|
:param key: |
|
43
|
|
|
:param case_sensitive: |
|
44
|
|
|
:return: |
|
45
|
|
|
""" |
|
46
|
|
|
to_key = "<k>" |
|
47
|
|
|
tc_key = "</k>" |
|
48
|
|
|
to_del = "<do_not_delete>" |
|
49
|
|
|
tc_del = "</do_not_delete>" |
|
50
|
|
|
flag = re.IGNORECASE if not case_sensitive else 0 |
|
51
|
|
|
key_pattern = re.compile( |
|
52
|
|
|
f"{to_key}{re.escape(key.replace('*', '.*'))}{tc_key}", flags=flag |
|
53
|
|
|
) |
|
54
|
|
|
def_body = key_pattern.sub(f"{to_del}\\g<0>{tc_del}", self.body_formatted) |
|
55
|
|
|
def_body = def_body.replace(tc_key, "").replace(to_key, "") |
|
56
|
|
|
def_body = def_body.replace(to_del, to_key).replace(tc_del, tc_key) |
|
57
|
|
|
|
|
58
|
|
|
return def_body |
|
59
|
|
|
|
|
60
|
|
|
def tagged_word_origin_x(self, tag: str) -> str: |
|
61
|
|
|
""" |
|
62
|
|
|
Generate Word.origin_x as HTML tag |
|
63
|
|
|
Args: |
|
64
|
|
|
tag: |
|
65
|
|
|
|
|
66
|
|
|
Returns: |
|
67
|
|
|
|
|
68
|
|
|
""" |
|
69
|
|
|
return ( |
|
70
|
|
|
tag % self.d.source_word.origin_x |
|
71
|
|
|
if (self.d.source_word.origin_x and self.d.source_word.type.group == "Cpx") |
|
72
|
|
|
else str() |
|
73
|
|
|
) |
|
74
|
|
|
|
|
75
|
|
|
def tagged_word_name(self, tag: str) -> str: |
|
76
|
|
|
""" |
|
77
|
|
|
Generate Word.name as HTML tag |
|
78
|
|
|
Args: |
|
79
|
|
|
tag: |
|
80
|
|
|
|
|
81
|
|
|
Returns: |
|
82
|
|
|
|
|
83
|
|
|
""" |
|
84
|
|
|
return ( |
|
85
|
|
|
tag % self.d.source_word.name |
|
86
|
|
|
if not self.d.usage |
|
87
|
|
|
else tag % self.d.usage.replace("%", self.d.source_word.name) |
|
88
|
|
|
) |
|
89
|
|
|
|
|
90
|
|
|
def tagged_definition_body(self, key: str, tag: str) -> str: |
|
91
|
|
|
""" |
|
92
|
|
|
Generate Definition's body as HTML tag with highlighted key word |
|
93
|
|
|
Args: |
|
94
|
|
|
key: |
|
95
|
|
|
tag: |
|
96
|
|
|
|
|
97
|
|
|
Returns: |
|
98
|
|
|
|
|
99
|
|
|
""" |
|
100
|
|
|
return tag % self.highlight_key(key) |
|
101
|
|
|
|