Passed
Branch master (0442a2)
by P.R.
02:00
created

FigureHtmlFormatter.write_into_file()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
from sdoc.helper.Html import Html
10
from sdoc.sdoc2 import node_store
11
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter
12
13
14
class FigureHtmlFormatter(HtmlFormatter):
15
    """
16
    HtmlFormatter for generating HTML code for figures.
17
    """
18
    # ------------------------------------------------------------------------------------------------------------------
19
    def generate(self, node, file):
20
        """
21
        Generates the HTML code for a figure node.
22
23
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
24
        :param file file: The output file.
25
        """
26
        self.write_into_file(node, file)
27
28
        super().generate(node, file)
29
30
    # ------------------------------------------------------------------------------------------------------------------
31
    def generate_chapter(self, node, file):
32
        """
33
        Generates the HTML code for a figure node.
34
35
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
36
        :param file file: The output file.
37
        """
38
        if file:
39
            self.write_into_file(node, file)
40
41
        super().generate_chapter(node, file)
42
43
    # ------------------------------------------------------------------------------------------------------------------
44
    @staticmethod
45
    def write_into_file(node, file):
46
        """
47
        Writes data into opened HTML file.
48
49
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
50
        :param file file: The output file.
51
        """
52
        # Creating dicts with attributes for each type of element.
53
        img_attributes = {'src': node.get_option_value('filename'),
54
                          'width': node.get_option_value('width'),
55
                          'height': node.get_option_value('height'),
56
                          'alt': node.get_option_value('caption')}
57
        div_attributes = {'class': node.get_option_value('class')}
58
59
        # Creating elements.
60
        img_element = Html.generate_void_element('img', img_attributes)
61
        div_img_element = Html.generate_element('div', div_attributes, img_element, True)
62
63
        # Write elements into html file.
64
        file.write(div_img_element)
65
66
67
# ----------------------------------------------------------------------------------------------------------------------
68
node_store.register_formatter('figure', 'html', FigureHtmlFormatter)
69