1
|
1 |
|
import re |
2
|
1 |
|
from typing import Dict |
3
|
|
|
|
4
|
1 |
|
from cleo.io.io import IO |
5
|
|
|
|
6
|
1 |
|
from sdoc.sdoc2 import in_scope, node_store, out_scope |
7
|
1 |
|
from sdoc.sdoc2.node.ItemNode import ItemNode |
8
|
1 |
|
from sdoc.sdoc2.node.Node import Node |
9
|
1 |
|
from sdoc.sdoc2.node.TextNode import TextNode |
10
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
View Code Duplication |
class ItemizeNode(Node): |
|
|
|
|
14
|
|
|
""" |
15
|
|
|
SDoc2 node for itemize. |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
19
|
1 |
|
def __init__(self, io: IO, options: Dict[str, str]): |
20
|
|
|
""" |
21
|
|
|
Object constructor. |
22
|
|
|
|
23
|
|
|
:param OutputStyle io: The IO object. |
24
|
|
|
:param dict[str,str] options: The options of this itemize. |
25
|
|
|
""" |
26
|
1 |
|
super().__init__(io, 'itemize', options) |
27
|
|
|
|
28
|
1 |
|
self._hierarchy_level: int = 0 |
29
|
|
|
""" |
30
|
|
|
The hierarchy level of the itemize. |
31
|
|
|
""" |
32
|
|
|
|
33
|
1 |
|
node_store.first = True |
34
|
|
|
|
35
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
36
|
1 |
|
def get_command(self) -> str: |
37
|
|
|
""" |
38
|
|
|
Returns the command of this node, i.e. itemize. |
39
|
|
|
""" |
40
|
1 |
|
return 'itemize' |
41
|
|
|
|
42
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
43
|
1 |
|
def get_hierarchy_level(self, parent_hierarchy_level: int = -1) -> int: |
44
|
|
|
""" |
45
|
|
|
Returns parent_hierarchy_level + 1. |
46
|
|
|
|
47
|
|
|
:param int parent_hierarchy_level: The level of the parent in the hierarchy. |
48
|
|
|
""" |
49
|
1 |
|
self._hierarchy_level = parent_hierarchy_level + 1 |
50
|
|
|
|
51
|
1 |
|
return self._hierarchy_level |
52
|
|
|
|
53
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
54
|
1 |
|
def get_hierarchy_name(self) -> str: |
55
|
|
|
""" |
56
|
|
|
Returns 'item' |
57
|
|
|
""" |
58
|
1 |
|
return 'item' |
59
|
|
|
|
60
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
61
|
1 |
|
def is_block_command(self) -> bool: |
62
|
|
|
""" |
63
|
|
|
Returns True. |
64
|
|
|
""" |
65
|
1 |
|
return True |
66
|
|
|
|
67
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
68
|
1 |
|
def is_hierarchy_root(self): |
69
|
|
|
""" |
70
|
|
|
Returns True. |
71
|
|
|
""" |
72
|
1 |
|
return self._hierarchy_level == 0 |
73
|
|
|
|
74
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
75
|
1 |
|
def is_inline_command(self) -> bool: |
76
|
|
|
""" |
77
|
|
|
Returns False. |
78
|
|
|
""" |
79
|
|
|
return False |
80
|
|
|
|
81
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
82
|
1 |
|
def is_phrasing(self) -> bool: |
83
|
|
|
""" |
84
|
|
|
Returns True. |
85
|
|
|
""" |
86
|
1 |
|
return False |
87
|
|
|
|
88
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
89
|
1 |
|
def prepare_content_tree(self) -> None: |
90
|
|
|
""" |
91
|
|
|
Method which checks if all child nodes is instance of sdoc.sdoc2.node.ItemNode.ItemNode. If not, removes |
92
|
|
|
from child node list and from node store. |
93
|
|
|
""" |
94
|
1 |
|
obsolete_node_ids = [] |
95
|
|
|
|
96
|
1 |
|
for node_id in self.child_nodes: |
97
|
1 |
|
node = in_scope(node_id) |
98
|
|
|
|
99
|
1 |
|
if isinstance(node, TextNode): |
100
|
|
|
# Ignore text nodes with only whitespace silently. |
101
|
|
|
if re.sub(r'\s+', '', node.argument) != '': |
102
|
|
|
# This text node contains more than only whitespace. |
103
|
|
|
node_store.error("Unexpected text '{0}'".format(node.argument), node) |
104
|
|
|
obsolete_node_ids.append(node_id) |
105
|
|
|
|
106
|
1 |
|
elif not isinstance(node, ItemNode): |
107
|
|
|
# An itemize node can have only item nodes as direct child nodes. |
108
|
|
|
node_store.error("Node: id:{0!s}, {1!s} is not instance of 'ItemNode'".format(str(node.id), node.name), |
109
|
|
|
node) |
110
|
|
|
obsolete_node_ids.append(node_id) |
111
|
|
|
|
112
|
1 |
|
out_scope(node) |
113
|
|
|
|
114
|
1 |
|
self._remove_child_nodes(obsolete_node_ids) |
115
|
|
|
|
116
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
117
|
1 |
|
@staticmethod |
118
|
1 |
|
def level_down(number: str) -> str: |
119
|
|
|
""" |
120
|
|
|
Decrements the level of hierarchy. |
121
|
|
|
|
122
|
|
|
:param str number: The number of last node. |
123
|
|
|
""" |
124
|
1 |
|
number_list = number.split('.') |
125
|
1 |
|
number = '.'.join(number_list[:-1]) |
126
|
|
|
|
127
|
1 |
|
return number |
128
|
|
|
|
129
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
130
|
1 |
|
@staticmethod |
131
|
1 |
|
def level_up(numbers: Dict[str, str]) -> None: |
132
|
|
|
""" |
133
|
|
|
Increments the level of hierarchy. |
134
|
|
|
|
135
|
|
|
:param dict[str,str] numbers: The number of last node. |
136
|
|
|
""" |
137
|
1 |
|
if 'item' in numbers: |
138
|
1 |
|
numbers['item'] += '.0' |
139
|
|
|
else: |
140
|
1 |
|
numbers['item'] = '0' |
141
|
|
|
|
142
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
143
|
1 |
|
def number(self, numbers: Dict[str, str]) -> None: |
144
|
|
|
""" |
145
|
|
|
Passing over all child nodes, for numeration. |
146
|
|
|
|
147
|
|
|
:param dict[str,str] numbers: The number of last node. |
148
|
|
|
""" |
149
|
1 |
|
self.level_up(numbers) |
150
|
|
|
|
151
|
1 |
|
super().number(numbers) |
152
|
|
|
|
153
|
1 |
|
numbers['item'] = self.level_down(numbers['item']) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
157
|
|
|
NodeStore.register_block_command('itemize', ItemizeNode) |
158
|
|
|
|