Completed
Push — master ( 2f8739...45a29f )
by P.R.
02:09 queued 18s
created

HeadingNode.is_block_command()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
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
from sdoc.sdoc2.helper.Enumerable import Enumerable
15
16
17
class HeadingNode(Node):
0 ignored issues
show
Bug introduced by
The method get_command which was declared abstract in the super-class Node
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
18
    """
19
    Abstract class for heading nodes.
20
    """
21
    # ------------------------------------------------------------------------------------------------------------------
22
    def __init__(self, name, options, argument):
23
        """
24
        Object constructor.
25
26
        :param str name: The (command) name of this heading.
27
        :param dict[str,str] options: The options of this heading.
28
        :param str argument: The title of this heading.
29
        """
30
        super().__init__(name, options, argument)
31
32
    # ------------------------------------------------------------------------------------------------------------------
33
    def get_hierarchy_name(self):
34
        """
35
        Returns 'sectioning'.
36
37
        :rtype: str
38
        """
39
        return 'sectioning'
40
41
    # ------------------------------------------------------------------------------------------------------------------
42
    def is_block_command(self):
43
        """
44
        Returns False.
45
46
        :rtype: bool
47
        """
48
        return False
49
50
    # ------------------------------------------------------------------------------------------------------------------
51
    def is_inline_command(self):
52
        """
53
        Returns True.
54
55
        :rtype: bool
56
        """
57
        return True
58
59
    # ------------------------------------------------------------------------------------------------------------------
60
    def number(self, enumerable_numbers):
61
        """
62
        Sets number of heading nodes.
63
64
        :param dict[str,sdoc.sdoc2.helper.Enumerable.Enumerable] enumerable_numbers:
65
        """
66
        if 'heading' not in enumerable_numbers:
67
            enumerable_numbers['heading'] = Enumerable()
68
69
        enumerable_numbers['heading'].generate_numeration(self.get_hierarchy_level())
70
        enumerable_numbers['heading'].increment_last_level()
71
        enumerable_numbers['heading'].remove_starting_zeros()
72
73
        self._options['number'] = enumerable_numbers['heading'].get_string()
74
75
        super().number(enumerable_numbers)
76
77
    # ------------------------------------------------------------------------------------------------------------------
78
    def prepare_content_tree(self):
79
        """
80
        Prepares the content tree. Create paragraph nodes.
81
        """
82
83
        super().prepare_content_tree()
84
85
        # Adding the id's of splitted text in 'new_child_nodes1' list.
86
        self.split_text_nodes()
87
88
        # Creating paragraphs and add all id's in 'new_child_nodes2' list.
89
        self.create_paragraphs()
90
91
    # ------------------------------------------------------------------------------------------------------------------
92
    def split_text_nodes(self):
93
        """
94
        Replaces single text nodes that contains a paragraph separator (i.e. a double new line) with multiple text nodes
95
        without paragraph separator.
96
        """
97
        new_child_nodes = []
98
99
        for node_id in self.child_nodes:
100
            node = in_scope(node_id)
101
102
            if isinstance(node, TextNode):
103
                list_ids = node.split_by_paragraph()
104
                for ids in list_ids:
105
                    new_child_nodes.append(ids)
106
            else:
107
                new_child_nodes.append(node.id)
108
109
            out_scope(node)
110
111
        self.child_nodes = new_child_nodes
112
113
    # ------------------------------------------------------------------------------------------------------------------
114
    def create_paragraphs(self):
115
        """
116
        Create paragraph nodes.
117
118
        A paragraph consists of phrasing nodes only. Each continuous slice of phrasing child nodes is move to a
119
        paragraph node.
120
        """
121
        new_child_nodes = []
122
        paragraph_node = None
123
124
        for node_id in self.child_nodes:
125
            node = in_scope(node_id)
126
127
            if node.is_phrasing():
128
                if not paragraph_node:
129
                    paragraph_node = sdoc.sdoc2.node_store.create_inline_node('paragraph')
130
                    new_child_nodes.append(paragraph_node.id)
131
132
                paragraph_node.append_child_node(node)
133
            else:
134
                if paragraph_node:
135
                    paragraph_node.prune_whitespace()
136
                    sdoc.sdoc2.node_store.store_node(paragraph_node)
137
                    paragraph_node = None
138
139
                # End paragraph nodes are created temporary to separate paragraphs in a flat list of (text) node. There
140
                # role ae replaced by the content hierarchy now. So, we must no store end paragraph nodes.
141
                if not isinstance(node, EndParagraphNode):
142
                    new_child_nodes.append(node.id)
143
144
            out_scope(node)
145
146
        if paragraph_node:
147
            paragraph_node.prune_whitespace()
148
            sdoc.sdoc2.node_store.store_node(paragraph_node)
149
            # paragraph_node = None
150
151
        # Setting child nodes.
152
        self.child_nodes = new_child_nodes
153
154
# ----------------------------------------------------------------------------------------------------------------------
155