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