1
|
|
|
""" |
2
|
|
|
SDoc |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
9
|
|
|
import sdoc |
10
|
|
|
from sdoc.sdoc2 import in_scope, out_scope |
11
|
|
|
from sdoc.sdoc2.node.Node import Node |
12
|
|
|
from sdoc.sdoc2.node.TextNode import TextNode |
13
|
|
|
from sdoc.sdoc2.node.EndParagraphNode import EndParagraphNode |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class HeadingNode(Node): |
|
|
|
|
17
|
|
|
""" |
18
|
|
|
Abstract class for heading nodes. |
19
|
|
|
""" |
20
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
21
|
|
|
def __init__(self, name, options, argument): |
22
|
|
|
""" |
23
|
|
|
Object constructor. |
24
|
|
|
|
25
|
|
|
:param str name: The (command) name of this heading. |
26
|
|
|
:param dict[str,str] options: The options of this heading. |
27
|
|
|
:param str argument: The title of this heading. |
28
|
|
|
""" |
29
|
|
|
super().__init__(name, options, argument) |
30
|
|
|
|
31
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
32
|
|
|
def get_hierarchy_name(self): |
33
|
|
|
""" |
34
|
|
|
Returns 'sectioning'. |
35
|
|
|
|
36
|
|
|
:rtype: str |
37
|
|
|
""" |
38
|
|
|
return 'sectioning' |
39
|
|
|
|
40
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
41
|
|
|
def is_block_command(self): |
42
|
|
|
""" |
43
|
|
|
Returns False. |
44
|
|
|
|
45
|
|
|
:rtype: bool |
46
|
|
|
""" |
47
|
|
|
return False |
48
|
|
|
|
49
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
50
|
|
|
def is_inline_command(self): |
51
|
|
|
""" |
52
|
|
|
Returns True. |
53
|
|
|
|
54
|
|
|
:rtype: bool |
55
|
|
|
""" |
56
|
|
|
return True |
57
|
|
|
|
58
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
59
|
|
|
@staticmethod |
60
|
|
|
def _trim_levels(string_of_numbers): |
61
|
|
|
""" |
62
|
|
|
Strips all starting '0's and add one starting '0' symbol if number starts from '0'. |
63
|
|
|
|
64
|
|
|
:param str string_of_numbers: String with header node number. |
65
|
|
|
|
66
|
|
|
:rtype: str |
67
|
|
|
""" |
68
|
|
|
if string_of_numbers.startswith('0'): |
69
|
|
|
string_of_numbers = string_of_numbers.lstrip('0.') |
70
|
|
|
string_of_numbers = '0.{0!s}'.format(string_of_numbers) |
71
|
|
|
|
72
|
|
|
return string_of_numbers |
73
|
|
|
|
74
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
75
|
|
|
@staticmethod |
76
|
|
|
def get_numeration(enumerable_numbers, level): |
77
|
|
|
""" |
78
|
|
|
Returns the current enumeration of headings at a heading level. |
79
|
|
|
|
80
|
|
|
:param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes. |
81
|
|
|
:param int level: The level of nested heading. |
82
|
|
|
|
83
|
|
|
:rtype: str |
84
|
|
|
""" |
85
|
|
|
if 'heading' not in enumerable_numbers: |
86
|
|
|
heading_numbers = [] |
87
|
|
|
|
88
|
|
|
for _ in range(level): |
89
|
|
|
heading_numbers.append('0') |
90
|
|
|
else: |
91
|
|
|
heading_numbers = enumerable_numbers['heading'].split('.') |
92
|
|
|
|
93
|
|
|
if level > len(heading_numbers): |
94
|
|
|
for _ in range(level-len(heading_numbers)): |
95
|
|
|
heading_numbers.append('0') |
96
|
|
|
|
97
|
|
|
return '.'.join(heading_numbers[:level]) |
98
|
|
|
|
99
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
100
|
|
|
@staticmethod |
101
|
|
|
def _increment_last_level(enumerable_numbers): |
102
|
|
|
""" |
103
|
|
|
Increments the last level in number of a heading number. |
104
|
|
|
|
105
|
|
|
:param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes. |
106
|
|
|
|
107
|
|
|
:rtype: str |
108
|
|
|
""" |
109
|
|
|
heading_numbers = enumerable_numbers['heading'].split('.') |
110
|
|
|
heading_numbers[-1] = str(int(heading_numbers[-1]) + 1) |
111
|
|
|
|
112
|
|
|
return '.'.join(heading_numbers) |
113
|
|
|
|
114
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
115
|
|
|
def number(self, enumerable_numbers): |
116
|
|
|
""" |
117
|
|
|
Sets number of heading nodes. |
118
|
|
|
|
119
|
|
|
:param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes. |
120
|
|
|
""" |
121
|
|
|
enumerable_numbers['heading'] = self.get_numeration(enumerable_numbers, self.get_hierarchy_level()) |
122
|
|
|
enumerable_numbers['heading'] = self._increment_last_level(enumerable_numbers) |
123
|
|
|
|
124
|
|
|
self._options['number'] = self._trim_levels(enumerable_numbers['heading']) |
125
|
|
|
|
126
|
|
|
super().number(enumerable_numbers) |
127
|
|
|
|
128
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
129
|
|
|
def prepare_content_tree(self): |
130
|
|
|
""" |
131
|
|
|
Prepares the content tree. Create paragraph nodes. |
132
|
|
|
""" |
133
|
|
|
|
134
|
|
|
super().prepare_content_tree() |
135
|
|
|
|
136
|
|
|
# Adding the id's of splitted text in 'new_child_nodes1' list. |
137
|
|
|
self.split_text_nodes() |
138
|
|
|
|
139
|
|
|
# Creating paragraphs and add all id's in 'new_child_nodes2' list. |
140
|
|
|
self.create_paragraphs() |
141
|
|
|
|
142
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
143
|
|
|
def split_text_nodes(self): |
144
|
|
|
""" |
145
|
|
|
Replaces single text nodes that contains a paragraph separator (i.e. a double new line) with multiple text nodes |
146
|
|
|
without paragraph separator. |
147
|
|
|
""" |
148
|
|
|
new_child_nodes = [] |
149
|
|
|
|
150
|
|
|
for node_id in self.child_nodes: |
151
|
|
|
node = in_scope(node_id) |
152
|
|
|
|
153
|
|
|
if isinstance(node, TextNode): |
154
|
|
|
list_ids = node.split_by_paragraph() |
155
|
|
|
for ids in list_ids: |
156
|
|
|
new_child_nodes.append(ids) |
157
|
|
|
else: |
158
|
|
|
new_child_nodes.append(node.id) |
159
|
|
|
|
160
|
|
|
out_scope(node) |
161
|
|
|
|
162
|
|
|
self.child_nodes = new_child_nodes |
163
|
|
|
|
164
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
165
|
|
|
def create_paragraphs(self): |
166
|
|
|
""" |
167
|
|
|
Create paragraph nodes. |
168
|
|
|
|
169
|
|
|
A paragraph consists of phrasing nodes only. Each continuous slice of phrasing child nodes is move to a |
170
|
|
|
paragraph node. |
171
|
|
|
""" |
172
|
|
|
new_child_nodes = [] |
173
|
|
|
paragraph_node = None |
174
|
|
|
|
175
|
|
|
for node_id in self.child_nodes: |
176
|
|
|
node = in_scope(node_id) |
177
|
|
|
|
178
|
|
|
if node.is_phrasing(): |
179
|
|
|
if not paragraph_node: |
180
|
|
|
paragraph_node = sdoc.sdoc2.node_store.create_inline_node('paragraph') |
181
|
|
|
new_child_nodes.append(paragraph_node.id) |
182
|
|
|
|
183
|
|
|
paragraph_node.append_child_node(node) |
184
|
|
|
else: |
185
|
|
|
if paragraph_node: |
186
|
|
|
paragraph_node.prune_whitespace() |
187
|
|
|
sdoc.sdoc2.node_store.store_node(paragraph_node) |
188
|
|
|
paragraph_node = None |
189
|
|
|
|
190
|
|
|
# End paragraph nodes are created temporary to separate paragraphs in a flat list of (text) node. There |
191
|
|
|
# role ae replaced by the content hierarchy now. So, we must no store end paragraph nodes. |
192
|
|
|
if not isinstance(node, EndParagraphNode): |
193
|
|
|
new_child_nodes.append(node.id) |
194
|
|
|
|
195
|
|
|
out_scope(node) |
196
|
|
|
|
197
|
|
|
if paragraph_node: |
198
|
|
|
paragraph_node.prune_whitespace() |
199
|
|
|
sdoc.sdoc2.node_store.store_node(paragraph_node) |
200
|
|
|
# paragraph_node = None |
201
|
|
|
|
202
|
|
|
# Setting child nodes. |
203
|
|
|
self.child_nodes = new_child_nodes |
204
|
|
|
|
205
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
206
|
|
|
|
Methods which raise
NotImplementedError
should be overridden in concrete child classes.