1
|
|
|
""" |
2
|
|
|
SDoc |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
9
|
1 |
|
from sdoc.sdoc2 import in_scope, out_scope |
10
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
11
|
1 |
|
from sdoc.sdoc2.node.Node import Node |
12
|
1 |
|
from sdoc.sdoc2.node.TextNode import TextNode |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
class ItemNode(Node): |
16
|
|
|
""" |
17
|
|
|
SDoc2 node for items. |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
21
|
1 |
|
def __init__(self, io, options, argument): |
22
|
|
|
""" |
23
|
|
|
Object constructor. |
24
|
|
|
|
25
|
|
|
:param None|cleo.styles.output_style.OutputStyle io: The IO object. |
26
|
|
|
:param dict[str,str] options: The options of this item. |
27
|
|
|
:param str argument: Not used. |
28
|
|
|
""" |
29
|
1 |
|
super().__init__(io, 'item', options, argument) |
30
|
|
|
|
31
|
1 |
|
self._hierarchy_level = 0 |
32
|
|
|
|
33
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
34
|
1 |
|
def get_command(self): |
35
|
|
|
""" |
36
|
|
|
Returns the command of this node, i.e. item. |
37
|
|
|
|
38
|
|
|
:rtype: str |
39
|
|
|
""" |
40
|
1 |
|
return 'item' |
41
|
|
|
|
42
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
43
|
1 |
|
def get_hierarchy_level(self, parent_hierarchy_level=-1): |
44
|
|
|
""" |
45
|
|
|
Returns parent_hierarchy_level. |
46
|
|
|
|
47
|
|
|
:param int parent_hierarchy_level: The level of the parent in the hierarchy. |
48
|
|
|
|
49
|
|
|
:rtype: int |
50
|
|
|
""" |
51
|
1 |
|
self._hierarchy_level = parent_hierarchy_level + 1 |
52
|
|
|
|
53
|
1 |
|
return self._hierarchy_level |
54
|
|
|
|
55
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
56
|
1 |
|
def get_hierarchy_name(self): |
57
|
|
|
""" |
58
|
|
|
Returns 'item' |
59
|
|
|
|
60
|
|
|
:rtype: str |
61
|
|
|
""" |
62
|
1 |
|
return 'item' |
63
|
|
|
|
64
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
65
|
1 |
|
def is_block_command(self): |
66
|
|
|
""" |
67
|
|
|
Returns False. |
68
|
|
|
|
69
|
|
|
:rtype: bool |
70
|
|
|
""" |
71
|
1 |
|
return False |
72
|
|
|
|
73
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
74
|
1 |
|
def is_inline_command(self): |
75
|
|
|
""" |
76
|
|
|
Returns True. |
77
|
|
|
|
78
|
|
|
:rtype: bool |
79
|
|
|
""" |
80
|
|
|
return True |
81
|
|
|
|
82
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
83
|
1 |
|
def is_list_element(self): |
84
|
|
|
""" |
85
|
|
|
Returns True. |
86
|
|
|
|
87
|
|
|
:rtype: bool |
88
|
|
|
""" |
89
|
1 |
|
return True |
90
|
|
|
|
91
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
92
|
1 |
|
def prepare_content_tree(self): |
93
|
|
|
""" |
94
|
|
|
Method which checks if all child nodes is phrasing. |
95
|
|
|
""" |
96
|
1 |
|
first = self.child_nodes[0] |
97
|
1 |
|
last = self.child_nodes[-1] |
98
|
|
|
|
99
|
1 |
|
for node_id in self.child_nodes: |
100
|
1 |
|
node = in_scope(node_id) |
101
|
|
|
|
102
|
1 |
|
if isinstance(node, TextNode): |
103
|
1 |
|
if node_id == first: |
104
|
1 |
|
node.prune_whitespace(leading=True) |
105
|
|
|
|
106
|
1 |
|
elif node_id == last: |
107
|
1 |
|
node.prune_whitespace(trailing=True) |
108
|
|
|
|
109
|
1 |
|
elif node_id == first and node_id == last: |
110
|
|
|
node.prune_whitespace(leading=True, trailing=True) |
111
|
|
|
|
112
|
|
|
# if not node.is_phrasing(): |
113
|
|
|
# raise RuntimeError("Node: id:%s, %s is not phrasing" % (str(node.id), node.name)) |
114
|
|
|
|
115
|
1 |
|
out_scope(node) |
116
|
|
|
|
117
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
118
|
1 |
|
@staticmethod |
119
|
|
|
def _increment_last_level(number): |
120
|
|
|
""" |
121
|
|
|
Increments the last level in number of the item node. |
122
|
|
|
|
123
|
|
|
:param str number: The number of last node. |
124
|
|
|
|
125
|
|
|
:rtype: str |
126
|
|
|
""" |
127
|
1 |
|
heading_numbers = number.split('.') |
128
|
1 |
|
heading_numbers[-1] = str(int(heading_numbers[-1]) + 1) |
129
|
|
|
|
130
|
1 |
|
return '.'.join(heading_numbers) |
131
|
|
|
|
132
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
133
|
1 |
|
@staticmethod |
134
|
|
|
def strip_start_point(number): |
135
|
|
|
""" |
136
|
|
|
Removes start point if it in the number. |
137
|
|
|
|
138
|
|
|
:param str number: The number of last node. |
139
|
|
|
|
140
|
|
|
:rtype: str |
141
|
|
|
""" |
142
|
1 |
|
return number.lstrip('.') |
143
|
|
|
|
144
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
145
|
1 |
|
def number(self, numbers): |
146
|
|
|
""" |
147
|
|
|
Sets number for item nodes. |
148
|
|
|
|
149
|
|
|
:param dict[str,str] numbers: The number of last node. |
150
|
|
|
""" |
151
|
1 |
|
numbers['item'] = self.strip_start_point(numbers['item']) |
152
|
1 |
|
numbers['item'] = self._increment_last_level(numbers['item']) |
153
|
|
|
|
154
|
1 |
|
self._options['number'] = numbers['item'] |
155
|
|
|
|
156
|
1 |
|
super().number(numbers) |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
160
|
|
|
NodeStore.register_inline_command('item', ItemNode) |
161
|
|
|
|