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