Completed
Pull Request — master (#32)
by Oleg
01:55
created

HeadingNode._trim_levels()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
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'].get_numeration(self.get_hierarchy_level())
70
        enumerable_numbers['heading'].increment_last_level()
71
72
        self._options['number'] = enumerable_numbers['heading'].get_string()
73
74
        super().number(enumerable_numbers)
75
76
    # ------------------------------------------------------------------------------------------------------------------
77
    def prepare_content_tree(self):
78
        """
79
        Prepares the content tree. Create paragraph nodes.
80
        """
81
82
        super().prepare_content_tree()
83
84
        # Adding the id's of splitted text in 'new_child_nodes1' list.
85
        self.split_text_nodes()
86
87
        # Creating paragraphs and add all id's in 'new_child_nodes2' list.
88
        self.create_paragraphs()
89
90
    # ------------------------------------------------------------------------------------------------------------------
91
    def split_text_nodes(self):
92
        """
93
        Replaces single text nodes that contains a paragraph separator (i.e. a double new line) with multiple text nodes
94
        without paragraph separator.
95
        """
96
        new_child_nodes = []
97
98
        for node_id in self.child_nodes:
99
            node = in_scope(node_id)
100
101
            if isinstance(node, TextNode):
102
                list_ids = node.split_by_paragraph()
103
                for ids in list_ids:
104
                    new_child_nodes.append(ids)
105
            else:
106
                new_child_nodes.append(node.id)
107
108
            out_scope(node)
109
110
        self.child_nodes = new_child_nodes
111
112
    # ------------------------------------------------------------------------------------------------------------------
113
    def create_paragraphs(self):
114
        """
115
        Create paragraph nodes.
116
117
        A paragraph consists of phrasing nodes only. Each continuous slice of phrasing child nodes is move to a
118
        paragraph node.
119
        """
120
        new_child_nodes = []
121
        paragraph_node = None
122
123
        for node_id in self.child_nodes:
124
            node = in_scope(node_id)
125
126
            if node.is_phrasing():
127
                if not paragraph_node:
128
                    paragraph_node = sdoc.sdoc2.node_store.create_inline_node('paragraph')
129
                    new_child_nodes.append(paragraph_node.id)
130
131
                paragraph_node.append_child_node(node)
132
            else:
133
                if paragraph_node:
134
                    paragraph_node.prune_whitespace()
135
                    sdoc.sdoc2.node_store.store_node(paragraph_node)
136
                    paragraph_node = None
137
138
                # End paragraph nodes are created temporary to separate paragraphs in a flat list of (text) node. There
139
                # role ae replaced by the content hierarchy now. So, we must no store end paragraph nodes.
140
                if not isinstance(node, EndParagraphNode):
141
                    new_child_nodes.append(node.id)
142
143
            out_scope(node)
144
145
        if paragraph_node:
146
            paragraph_node.prune_whitespace()
147
            sdoc.sdoc2.node_store.store_node(paragraph_node)
148
            # paragraph_node = None
149
150
        # Setting child nodes.
151
        self.child_nodes = new_child_nodes
152
153
# ----------------------------------------------------------------------------------------------------------------------
154