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

HtmlFormat   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
dl 0
loc 101
ccs 30
cts 36
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __init__() 0 31 1
A _read_configuration() 0 20 4
A enumerate() 0 8 1
B generate() 0 30 5
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9 1
from sdoc import sdoc2
10 1
from sdoc.error import SDocError
11 1
from sdoc.format.Format import Format
12
13
14 1
class HtmlFormat(Format):
15
    """
16
    Class for generating HTML
17
    """
18
19
    # ------------------------------------------------------------------------------------------------------------------
20 1
    def __init__(self, io, config):
21
        """
22
        Object constructor.
23
24
        :param cleo.styles.output_style.OutputStyle io: The IO object.
25
        :param configparser.SectionProxy config: The section in the config file for the target_format.
26
        """
27 1
        Format.__init__(self, io, config)
28
29 1
        self._enumerate = True
30
        """
31
        If set chapters, sections, etc. must be numbered.
32
33
        :type: bool
34
        """
35
36 1
        self._file_per_chapter = False
37
        """
38
        If set, will generate multiple .html files for each chapter.
39
40
        :type: bool
41
        """
42
43 1
        self._one_file = True
44
        """
45
        If set, will generate one .html file.
46
47
        :type: bool
48
        """
49
50 1
        self._read_configuration(config)
51
52
    # ------------------------------------------------------------------------------------------------------------------
53 1
    def _read_configuration(self, config):
54
        """
55
        Reads the configuration for this formatter.
56
57
        :param configparser.SectionProxy config: The section in the config file for the target_format.
58
        """
59 1
        try:
60 1
            self._enumerate = config.getboolean('enumerate', fallback=self._enumerate)
61
        except ValueError:
62
            raise SDocError("Option 'enumerate' not set correctly")
63
64 1
        try:
65 1
            self._file_per_chapter = config.getboolean('file_per_chapter', fallback=self._file_per_chapter)
66
        except ValueError:
67
            raise SDocError("Option 'file_per_chapter' not set correctly")
68
69 1
        try:
70 1
            self._one_file = config.getboolean('one_file', fallback=self._one_file)
71
        except ValueError:
72
            raise SDocError("Option 'one_file' not set correctly")
73
74
    # ------------------------------------------------------------------------------------------------------------------
75 1
    @property
76
    def enumerate(self):
77
        """
78
        Getter for enumerate attribute.
79
80
        :rtype: bool
81
        """
82 1
        return self._enumerate
83
84
    # ------------------------------------------------------------------------------------------------------------------
85 1
    def generate(self):
86
        """
87
        Starts generating HTML file.
88
89
        :rtype int: The error count.
90
        """
91
        # Activate numbering nodes.
92 1
        if self.enumerate:
93 1
            sdoc2.node_store.number_numerable()
94
95
        # Generate labels.
96 1
        sdoc2.node_store.parse_labels()
97
98
        # Generate table of contents.
99 1
        sdoc2.node_store.generate_toc()
100
101
        # Generate whole HTML output file.
102 1
        if self._one_file:
103 1
            file_name = 'output.html'
104 1
            self._io.writeln('Writing <fso>{0!s}</fso>'.format(file_name))
105 1
            with open(file_name, 'wt', encoding='utf8') as general_file:
106 1
                formatter = sdoc2.node_store.create_formatter(self._io, 'document')
107 1
                formatter.generate(sdoc2.node_store.nodes[1], general_file)
108 1
                self._errors += formatter.errors
109
110
        # Generate in mode 'output file on each chapter'.
111 1
        if self._file_per_chapter:
112
            raise NotImplementedError()
113
114 1
        return self._errors
115
116
# ----------------------------------------------------------------------------------------------------------------------
117