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

FigureHtmlFormatter._write_caption()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 8.6667

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 1
cts 7
cp 0.1429
rs 9.4285
cc 3
crap 8.6667
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
from sdoc.helper.Html import Html
10 1
from sdoc.sdoc2.NodeStore import NodeStore
11 1
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
12
13
14 1
class FigureHtmlFormatter(HtmlFormatter):
15
    """
16
    HtmlFormatter for generating HTML code for figures.
17
    """
18
19
    # ------------------------------------------------------------------------------------------------------------------
20 1
    def generate(self, node, file):
21
        """
22
        Generates the HTML code for a figure node.
23
24
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
25
        :param file file: The output file.
26
        """
27
        self.write_into_file(node, file)
28
29
        HtmlFormatter.generate(self, node, file)
30
31
    # ------------------------------------------------------------------------------------------------------------------
32 1
    @staticmethod
33
    def _write_caption(node, file):
34
        """
35
        Generates the caption for the table in HTML representation.
36
37
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
38
        :param file file: The output file.
39
        """
40
        if node.caption:
41
            figure_number = node.get_option_value('number')
42
43
            if figure_number:
44
                inner_text = 'Figuur {}: {}'.format(figure_number, node.caption)  # TODO Internationalization
1 ignored issue
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
45
            else:
46
                inner_text = node.caption
47
48
            file.write(Html.generate_element('figcaption', {}, inner_text))
49
50
    # ------------------------------------------------------------------------------------------------------------------
51 1
    @staticmethod
52
    def write_into_file(node, file):
53
        """
54
        Writes data into opened HTML file.
55
56
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
57
        :param file file: The output file.
58
        """
59
        # Creating dicts with attributes for each type of element.
60
        figure_attributes = {'id': node.get_option_value('id')}
61
62
        img_attributes = {'src':    node.get_option_value('src'),
63
                          'width':  node.get_option_value('width'),
64
                          'height': node.get_option_value('height'),
65
                          'alt':    node.caption}
66
67
        # Creating elements.
68
        file.write(Html.generate_tag('figure', figure_attributes))
69
70
        file.write(Html.generate_element('img', img_attributes))
71
72
        FigureHtmlFormatter._write_caption(node, file)
73
74
        file.write('</figure>')
75
76
77
# ----------------------------------------------------------------------------------------------------------------------
78
NodeStore.register_formatter('figure', 'html', FigureHtmlFormatter)
79