Completed
Pull Request — master (#56)
by Oleg
02:13
created

FigureHtmlFormatter.write_into_file()   B

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.6796

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 25
ccs 1
cts 8
cp 0.125
rs 8.8571
c 1
b 1
f 0
cc 2
crap 4.6796
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_into_file(node, file):
34
        """
35
        Writes data into opened HTML file.
36
37
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
38
        :param file file: The output file.
39
        """
40
        # Creating dicts with attributes for each type of element.
41
        figure_attributes = {'id': node.get_option_value('id')}
42
43
        img_attributes = {'src':    node.get_option_value('src'),
44
                          'width':  node.get_option_value('width'),
45
                          'height': node.get_option_value('height'),
46
                          'alt':    node.caption}
47
48
        # Creating elements.
49
        file.write(Html.generate_tag('figure', figure_attributes))
50
51
        if node.caption:
52
            file.write(Html.generate_element('figcaption', {}, node.caption))
53
54
        file.write(Html.generate_element('img', img_attributes))
55
56
        file.write('</figure>')
57
58
59
# ----------------------------------------------------------------------------------------------------------------------
60
NodeStore.register_formatter('figure', 'html', FigureHtmlFormatter)
61