Completed
Pull Request — master (#59)
by Oleg
02:10
created

ItemNode   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
dl 0
loc 147
ccs 40
cts 42
cp 0.9524
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 16 1
A is_list_element() 0 7 1
A is_block_command() 0 7 1
A get_hierarchy_level() 0 11 1
A _increment_last_level() 0 13 1
C prepare_content_tree() 0 24 7
A number() 0 12 1
A get_hierarchy_name() 0 7 1
A strip_start_point() 0 10 1
A get_command() 0 7 1
A is_inline_command() 0 7 1
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
from sdoc.sdoc2 import in_scope, out_scope
10 1
from sdoc.sdoc2.NodeStore import NodeStore
11 1
from sdoc.sdoc2.node.Node import Node
12 1
from sdoc.sdoc2.node.TextNode import TextNode
13
14
15 1
class ItemNode(Node):
16
    """
17
    SDoc2 node for items.
18
    """
19
20
    # ------------------------------------------------------------------------------------------------------------------
21 1
    def __init__(self, io, options, argument):
22
        """
23
        Object constructor.
24
25
        :param None|cleo.styles.output_style.OutputStyle io: The IO object.
26
        :param dict[str,str] options: The options of this item.
27
        :param str argument: Not used.
28
        """
29 1
        super().__init__(io, 'item', options, argument)
30
31 1
        self._hierarchy_level = 0
32 1
        """
33
        The hierarchy level of the itemize.
34
35
        :type: int
36
        """
37
38
    # ------------------------------------------------------------------------------------------------------------------
39 1
    def get_command(self):
40
        """
41
        Returns the command of this node, i.e. item.
42
43
        :rtype: str
44
        """
45 1
        return 'item'
46
47
    # ------------------------------------------------------------------------------------------------------------------
48 1
    def get_hierarchy_level(self, parent_hierarchy_level=-1):
49
        """
50
        Returns parent_hierarchy_level.
51
52
        :param int parent_hierarchy_level: The level of the parent in the hierarchy.
53
54
        :rtype: int
55
        """
56 1
        self._hierarchy_level = parent_hierarchy_level + 1
57
58 1
        return self._hierarchy_level
59
60
    # ------------------------------------------------------------------------------------------------------------------
61 1
    def get_hierarchy_name(self):
62
        """
63
        Returns 'item'
64
65
        :rtype: str
66
        """
67 1
        return 'item'
68
69
    # ------------------------------------------------------------------------------------------------------------------
70 1
    def is_block_command(self):
71
        """
72
        Returns False.
73
74
        :rtype: bool
75
        """
76 1
        return False
77
78
    # ------------------------------------------------------------------------------------------------------------------
79 1
    def is_inline_command(self):
80
        """
81
        Returns True.
82
83
        :rtype: bool
84
        """
85
        return True
86
87
    # ------------------------------------------------------------------------------------------------------------------
88 1
    def is_list_element(self):
89
        """
90
        Returns True.
91
92
        :rtype: bool
93
        """
94 1
        return True
95
96
    # ------------------------------------------------------------------------------------------------------------------
97 1
    def prepare_content_tree(self):
98
        """
99
        Method which checks if all child nodes is phrasing.
100
        """
101 1
        first = self.child_nodes[0]
102 1
        last = self.child_nodes[-1]
103
104 1
        for node_id in self.child_nodes:
105 1
            node = in_scope(node_id)
106
107 1
            if isinstance(node, TextNode):
108 1
                if node_id == first:
109 1
                    node.prune_whitespace(leading=True)
110
111 1
                elif node_id == last:
112 1
                    node.prune_whitespace(trailing=True)
113
114 1
                elif node_id == first and node_id == last:
115
                    node.prune_whitespace(leading=True, trailing=True)
116
117
            # if not node.is_phrasing():
118
            #    raise RuntimeError("Node: id:%s, %s is not phrasing" % (str(node.id), node.name))
119
120 1
            out_scope(node)
121
122
    # ------------------------------------------------------------------------------------------------------------------
123 1
    @staticmethod
124
    def _increment_last_level(number):
125
        """
126
        Increments the last level in number of the item node.
127
128
        :param str number: The number of last node.
129
130
        :rtype: str
131
        """
132 1
        heading_numbers = number.split('.')
133 1
        heading_numbers[-1] = str(int(heading_numbers[-1]) + 1)
134
135 1
        return '.'.join(heading_numbers)
136
137
    # ------------------------------------------------------------------------------------------------------------------
138 1
    @staticmethod
139
    def strip_start_point(number):
140
        """
141
        Removes start point if it in the number.
142
143
        :param str number: The number of last node.
144
145
        :rtype: str
146
        """
147 1
        return number.lstrip('.')
148
149
    # ------------------------------------------------------------------------------------------------------------------
150 1
    def number(self, numbers):
151
        """
152
        Sets number for item nodes.
153
154
        :param dict[str,str] numbers: The number of last node.
155
        """
156 1
        numbers['item'] = self.strip_start_point(numbers['item'])
157 1
        numbers['item'] = self._increment_last_level(numbers['item'])
158
159 1
        self._options['number'] = numbers['item']
160
161 1
        super().number(numbers)
162
163
164
# ----------------------------------------------------------------------------------------------------------------------
165
NodeStore.register_inline_command('item', ItemNode)
166