Completed
Push — master ( d0bff5...613540 )
by P.R.
02:06
created

FigureNode.setup_label()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 1
cts 2
cp 0.5
rs 9.4285
c 1
b 0
f 0
cc 1
crap 1.125
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.CaptionNode import CaptionNode
12 1
from sdoc.sdoc2.node.LabelNode import LabelNode
13 1
from sdoc.sdoc2.node.Node import Node
14
15
16 1
class FigureNode(Node):
17
    """
18
    A stub for SDoc2 node for figures.
19
    """
20
21
    # ------------------------------------------------------------------------------------------------------------------
22 1
    def __init__(self, io, options):
23
        """
24
        Object constructor.
25
26
        :param None|cleo.styles.output_style.OutputStyle io: The IO object.
27
        :param dict[str,str] options: The options of this figure.
28
        """
29 1
        super().__init__(io, 'figure', options)
30
31 1
        self.caption = None
32 1
        """
33
        The caption for the figure.
34
35
        :type: None|str
36
        """
37
38
    # ------------------------------------------------------------------------------------------------------------------
39 1
    def get_command(self):
40
        """
41
        Returns the command of this node, i.e. smile.
42
43
        :rtype: str
44
        """
45 1
        return 'figure'
46
47
    # ------------------------------------------------------------------------------------------------------------------
48 1
    def is_block_command(self):
49
        """
50
        Returns False.
51
52
        :rtype: bool
53
        """
54 1
        return True
55
56
    # ------------------------------------------------------------------------------------------------------------------
57 1
    def is_inline_command(self):
58
        """
59
        Returns True.
60
61
        :rtype: bool
62
        """
63
        return False
64
65
    # ------------------------------------------------------------------------------------------------------------------
66 1
    def prepare_content_tree(self):
67
        """
68
        Prepares this node for further processing.
69
        """
70 1
        for node_id in self.child_nodes:
71
            node = in_scope(node_id)
72
73
            if isinstance(node, CaptionNode):
74
                self.caption = node.argument
75
76
            if isinstance(node, LabelNode):
77
                self.setup_label(node)
78
79
            # node.prepare_content_tree()
80
81
            out_scope(node)
82
83
    # ------------------------------------------------------------------------------------------------------------------
84 1
    def setup_label(self, node):
85
        """
86
        Sets the data of a label to current table.
87
88
        :param sdoc.sdoc2.node.LabelNode.LabelNode node: The label node.
89
        """
90
        self._options['id'] = node.argument
91
92
    # ------------------------------------------------------------------------------------------------------------------
93 1
    @staticmethod
94
    def _get_numeration(enumerable_numbers):
95
        """
96
        Returns the current enumeration of figures.
97
98
        :param dict[str, sdoc.sdoc2.helper.Enumerable.Enumerable] enumerable_numbers:
99
        """
100 1
        if 'heading' in enumerable_numbers and enumerable_numbers['heading'].get_level(1):
101 1
            chapter = enumerable_numbers['heading'].get_level(1)
102
        else:
103 1
            chapter = 0
104
105 1
        if 'figures' not in enumerable_numbers:
106 1
            enumerable_numbers['figures'] = '{0!s}.{1!s}'.format(chapter, '0')
107
108
        else:
109 1
            numbers_level = enumerable_numbers['figures'].split('.')
110 1
            if chapter > int(numbers_level[0]):
111 1
                numbers_level[0] = chapter
112 1
                numbers_level[-1] = '0'
113
114 1
            enumerable_numbers['figures'] = '.'.join(map(str, numbers_level))
0 ignored issues
show
introduced by
Used builtin function 'map'
Loading history...
115
116
    # ------------------------------------------------------------------------------------------------------------------
117 1
    @staticmethod
118
    def _increment_last_level(enumerable_numbers):
119
        """
120
        Increments the last level of figures enumeration.
121
122
        :param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes.
123
        """
124 1
        heading_numbers = enumerable_numbers['figures'].split('.')
125 1
        heading_numbers[-1] = str(int(heading_numbers[-1]) + 1)
126
127 1
        enumerable_numbers['figures'] = '.'.join(heading_numbers)
128
129
    # ------------------------------------------------------------------------------------------------------------------
130 1
    def number(self, enumerable_numbers):
131
        """
132
        Sets the number of this figure node.
133
134
        :param dict[str,str] enumerable_numbers: The current numbers of enumerable nodes.
135
        """
136 1
        self._get_numeration(enumerable_numbers)
137 1
        self._increment_last_level(enumerable_numbers)
138
139 1
        self._options['number'] = enumerable_numbers['figures']
140
141 1
        super().number(enumerable_numbers)
142
143
144
# ----------------------------------------------------------------------------------------------------------------------
145
NodeStore.register_block_command('figure', FigureNode)
146