Completed
Push — master ( 172351...9797b3 )
by P.R.
03:28
created

HeadingNode.set_toc_id()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

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