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