Completed
Push — master ( 793cf7...172351 )
by P.R.
04:33
created

FigureHtmlFormatter.generate()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 1
cts 3
cp 0.3333
rs 9.4285
c 2
b 0
f 0
cc 1
crap 1.2963
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 import node_store
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 1
    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
        HtmlFormatter.generate(self, node, file)
29
30
    # ------------------------------------------------------------------------------------------------------------------
31 1
    @staticmethod
32
    def write_into_file(node, file):
33
        """
34
        Writes data into opened HTML file.
35
36
        :param sdoc.sdoc2.node.FigureNode.FigureNode node: The figure node.
37
        :param file file: The output file.
38
        """
39
        # Creating dicts with attributes for each type of element.
40
        img_attributes = {'src': node.get_option_value('filename'),
41
                          'width': node.get_option_value('width'),
42
                          'height': node.get_option_value('height'),
43
                          'alt': node.get_option_value('caption')}
44
        div_attributes = {'class': node.get_option_value('class')}
45
46
        # Creating elements.
47
        img_element = Html.generate_void_element('img', img_attributes)
48
        div_img_element = Html.generate_element('div', div_attributes, img_element, True)
49
50
        # Write elements into html file.
51
        file.write(div_img_element)
52
53
54
# ----------------------------------------------------------------------------------------------------------------------
55
node_store.register_formatter('figure', 'html', FigureHtmlFormatter)
56