|
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 TocHtmlFormatter(HtmlFormatter): |
|
15
|
|
|
""" |
|
16
|
|
|
HtmlFormatter for generating HTML code for table of contents. |
|
17
|
|
|
""" |
|
18
|
|
|
|
|
19
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
20
|
1 |
|
def generate(self, node, file): |
|
21
|
|
|
""" |
|
22
|
|
|
Generates the HTML code for a table of contents node. |
|
23
|
|
|
|
|
24
|
|
|
:param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents 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 file. |
|
36
|
|
|
|
|
37
|
|
|
:param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node. |
|
38
|
|
|
:param file file: The output file. |
|
39
|
|
|
""" |
|
40
|
|
|
attributes = {'role': 'navigation', 'class': 'table-of-contents'} |
|
41
|
|
|
|
|
42
|
|
|
file.write(Html.generate_tag('nav', attributes)) |
|
43
|
|
|
file.write(Html.generate_element('h1', {}, node.argument)) |
|
44
|
|
|
|
|
45
|
|
|
file.write('<ul>') |
|
46
|
|
|
TocHtmlFormatter.write_contents(node, file) |
|
47
|
|
|
file.write('</ul>') |
|
48
|
|
|
|
|
49
|
|
|
file.write('</nav>') |
|
50
|
|
|
|
|
51
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
52
|
1 |
|
@staticmethod |
|
53
|
|
|
def write_contents(node, file): |
|
54
|
|
|
""" |
|
55
|
|
|
Writes the contents into file. |
|
56
|
|
|
|
|
57
|
|
|
:param sdoc.sdoc2.node.TocNode.TocNode node: The table of contents node. |
|
58
|
|
|
:param file file: The output file. |
|
59
|
|
|
""" |
|
60
|
|
|
depth = node.get_option_value('depth') |
|
61
|
|
|
|
|
62
|
|
|
for item in node.get_option_value('ids'): |
|
63
|
|
|
|
|
64
|
|
|
if depth and item['level'] <= int(depth): |
|
65
|
|
|
TocHtmlFormatter.write_elements(item, file) |
|
66
|
|
|
|
|
67
|
|
|
elif not depth: |
|
68
|
|
|
TocHtmlFormatter.write_elements(item, file) |
|
69
|
|
|
|
|
70
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
71
|
1 |
|
@staticmethod |
|
72
|
|
|
def write_elements(item, file): |
|
73
|
|
|
""" |
|
74
|
|
|
Write the containing elements. |
|
75
|
|
|
|
|
76
|
|
|
:param dict[str,str] item: The item which we outputs. |
|
77
|
|
|
:param file file: The output file. |
|
78
|
|
|
""" |
|
79
|
|
|
class_attr = 'level{}'.format(item['level']) |
|
80
|
|
|
file.write(Html.generate_tag('li', {'class': class_attr})) |
|
81
|
|
|
|
|
82
|
|
|
file.write(Html.generate_tag('a', {'href': '#{}'.format(item['id'])})) |
|
83
|
|
|
|
|
84
|
|
|
number = item['number'] |
|
85
|
|
|
file.write(Html.generate_element('span', {}, str(number))) |
|
86
|
|
|
|
|
87
|
|
|
file.write(' {}'.format(item['arg'])) |
|
88
|
|
|
file.write('</a>') |
|
89
|
|
|
file.write('</li>') |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
93
|
|
|
NodeStore.register_formatter('toc', 'html', TocHtmlFormatter) |
|
94
|
|
|
|