Completed
Pull Request — master (#43)
by Oleg
02:07
created

HeadingNode   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 92.73%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
dl 0
loc 144
ccs 51
cts 55
cp 0.9273
rs 10
c 2
b 0
f 0

9 Methods

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