Completed
Push — master ( b50573...8e7f82 )
by P.R.
01:58
created

HtmlFormat._read_configuration()   A

Complexity

Conditions 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.5726

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 7
cts 13
cp 0.5385
rs 9.2
c 1
b 0
f 0
cc 4
crap 5.5726
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 whole HTML output file.
117
        if self.one_file:
118
            file_name = 'output.html'
119
            self._io.writeln('Writing <fso>{0!s}</fso>'.format(file_name))
120
            general_file = open(file_name, 'wt', encoding='utf8')
121
            formatter = sdoc2.node_store.create_formatter(self._io, 'document')
122
            formatter.generate(sdoc2.node_store.nodes[1], general_file)
123
124
        # Generate in mode 'output file on each chapter'.
125
        if self.file_per_chapter:
126
            formatter = sdoc2.node_store.create_formatter(self._io, 'document')
127
            formatter.generate_chapter(sdoc2.node_store.nodes[1], None)
128
129
# ----------------------------------------------------------------------------------------------------------------------
130