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