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

FigureHtmlFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
dl 0
loc 43
ccs 3
cts 12
cp 0.25
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B write_into_file() 0 25 2
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
        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