Completed
Pull Request — master (#45)
by Oleg
02:04
created

FigureHtmlFormatter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 2
c 2
b 1
f 0
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A write_into_file() 0 21 1
A generate() 0 10 1
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 1
        self.write_into_file(node, file)
28
29 1
        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 1
        img_attributes = {'src':    node.get_option_value('filename'),
42
                          'width':  node.get_option_value('width'),
43
                          'height': node.get_option_value('height'),
44
                          'alt':    node.get_option_value('caption')}
45 1
        div_attributes = {'class': node.get_option_value('class')}
46
47
        # Creating elements.
48 1
        img_element = Html.generate_void_element('img', img_attributes)
49 1
        div_img_element = Html.generate_element('div', div_attributes, img_element, True)
50
51
        # Write elements into html file.
52 1
        file.write(div_img_element)
53
54
55
# ----------------------------------------------------------------------------------------------------------------------
56
NodeStore.register_formatter('figure', 'html', FigureHtmlFormatter)
57