Completed
Push — master ( 57e7bf...8a74a7 )
by P.R.
01:36
created

ItemNode.prepare_content_tree()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 3
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
from sdoc.sdoc2 import node_store, in_scope, out_scope
10
from sdoc.sdoc2.node.Node import Node
11
from sdoc.sdoc2.node.TextNode import TextNode
12
13
14
class ItemNode(Node):
15
    """
16
    SDoc2 node for items.
17
    """
18
19
    # ------------------------------------------------------------------------------------------------------------------
20
    def __init__(self, options, argument):
21
        """
22
        Object constructor.
23
24
        :param dict[str,str] options: The options of this item.
25
        :param str argument: Not used.
26
        """
27
        super().__init__('item', options, argument)
28
29
        self._hierarchy_level = 0
30
31
    # ------------------------------------------------------------------------------------------------------------------
32
    def get_command(self):
33
        """
34
        Returns the command of this node, i.e. item.
35
36
        :rtype: str
37
        """
38
        return 'item'
39
40
    # ------------------------------------------------------------------------------------------------------------------
41
    def get_hierarchy_level(self, parent_hierarchy_level=-1):
42
        """
43
        Returns parent_hierarchy_level.
44
45
        :param int parent_hierarchy_level: The level of the parent in the hierarchy.
46
47
        :rtype: int
48
        """
49
        self._hierarchy_level = parent_hierarchy_level + 1
50
51
        return self._hierarchy_level
52
53
    # ------------------------------------------------------------------------------------------------------------------
54
    def get_hierarchy_name(self):
55
        """
56
        Returns 'item'
57
58
        :rtype: str
59
        """
60
        return 'item'
61
62
    # ------------------------------------------------------------------------------------------------------------------
63
    def is_block_command(self):
64
        """
65
        Returns False.
66
67
        :rtype: bool
68
        """
69
        return False
70
71
    # ------------------------------------------------------------------------------------------------------------------
72
    def is_inline_command(self):
73
        """
74
        Returns True.
75
76
        :rtype: bool
77
        """
78
        return True
79
80
    # ------------------------------------------------------------------------------------------------------------------
81
    def is_list_element(self):
82
        """
83
        Returns True.
84
85
        :rtype: bool
86
        """
87
        return True
88
89
    # ------------------------------------------------------------------------------------------------------------------
90
    def prepare_content_tree(self):
91
        """
92
        Method which checks if all child nodes is phrasing.
93
        """
94
        for node_id in self.child_nodes:
95
            node = in_scope(node_id)
96
97
            if isinstance(node, TextNode):
98
                node.prune_whitespace()
99
100
            # if not node.is_phrasing():
101
            #    raise RuntimeError("Node: id:%s, %s is not phrasing" % (str(node.id), node.name))
102
103
            out_scope(node)
104
105
    # ------------------------------------------------------------------------------------------------------------------
106
    @staticmethod
107
    def _increment_last_level(number):
108
        """
109
        Increments the last level in number of the item node.
110
111
        :param str number: The number of last node.
112
113
        :rtype: str
114
        """
115
        heading_numbers = number.split('.')
116
        heading_numbers[-1] = str(int(heading_numbers[-1]) + 1)
117
118
        return '.'.join(heading_numbers)
119
120
    # ------------------------------------------------------------------------------------------------------------------
121
    def strip_start_point(self, number):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
122
        """
123
        Removes start point if it in the number.
124
125
        :param str number: The number of last node.
126
127
        :rtype: str
128
        """
129
        return number.lstrip('.')
130
131
    # ------------------------------------------------------------------------------------------------------------------
132
    def number(self, numbers):
133
        """
134
        Sets number for item nodes.
135
136
        :param dict[str,str] numbers: The number of last node.
137
        """
138
        numbers['item'] = self.strip_start_point(numbers['item'])
139
        numbers['item'] = self._increment_last_level(numbers['item'])
140
141
        self._options['number'] = numbers['item']
142
143
        super().number(numbers)
144
145
146
# ----------------------------------------------------------------------------------------------------------------------
147
node_store.register_inline_command('item', ItemNode)
148