Completed
Pull Request — master (#43)
by Oleg
02:07
created

HtmlFormat   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
dl 0
loc 117
ccs 18
cts 40
cp 0.45
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A enumerate() 0 8 1
B __init__() 0 31 1
A one_file() 0 8 1
A _read_configuration() 0 20 4
A file_per_chapter() 0 8 1
B generate() 0 26 4
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
        super().__init__(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
        return self._enumerate
83
84
    # ------------------------------------------------------------------------------------------------------------------
85 1
    @property
86
    def file_per_chapter(self):
87
        """
88
        Getter for file_per_chapter attribute.
89
90
        :rtype: bool
91
        """
92
        return self._file_per_chapter
93
94
    # ------------------------------------------------------------------------------------------------------------------
95 1
    @property
96
    def one_file(self):
97
        """
98
        Getter for one_file attribute.
99
100
        :rtype: bool
101
        """
102
        return self._one_file
103
104
    # ------------------------------------------------------------------------------------------------------------------
105 1
    def generate(self):
106
        """
107
        Starts generating HTML file.
108
        """
109
        # Activate numbering nodes.
110
        if self.enumerate:
111
            sdoc2.node_store.number_numerable()
112
113
        # Generate labels.
114
        sdoc2.node_store.parse_labels()
115
116
        # Generate table of contents.
117
        sdoc2.node_store.generate_toc()
118
119
        # Generate whole HTML output file.
120
        if self.one_file:
121
            file_name = 'output.html'
122
            self._io.writeln('Writing <fso>{0!s}</fso>'.format(file_name))
123
            general_file = open(file_name, 'wt', encoding='utf8')
124
            formatter = sdoc2.node_store.create_formatter(self._io, 'document')
125
            formatter.generate(sdoc2.node_store.nodes[1], general_file)
126
127
        # Generate in mode 'output file on each chapter'.
128
        if self.file_per_chapter:
129
            formatter = sdoc2.node_store.create_formatter(self._io, 'document')
130
            formatter.generate_chapter(sdoc2.node_store.nodes[1], None)
131
132
# ----------------------------------------------------------------------------------------------------------------------
133