1
|
1 |
|
from typing import Any, Dict |
2
|
|
|
|
3
|
1 |
|
from sdoc.helper.Html import Html |
4
|
1 |
|
from sdoc.sdoc2.formatter.html.HtmlFormatter import HtmlFormatter |
5
|
1 |
|
from sdoc.sdoc2.node.TocNode import TocNode |
6
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
View Code Duplication |
class TocHtmlFormatter(HtmlFormatter): |
|
|
|
|
10
|
|
|
""" |
11
|
|
|
HtmlFormatter for generating HTML code for table of contents. |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
15
|
1 |
|
def generate(self, node: TocNode, file: Any) -> None: |
16
|
|
|
""" |
17
|
|
|
Generates the HTML code for a table of contents node. |
18
|
|
|
|
19
|
|
|
:param TocNode node: The table of contents node. |
20
|
|
|
:param any file: The output file. |
21
|
|
|
""" |
22
|
|
|
self.write_into_file(node, file) |
23
|
|
|
|
24
|
|
|
HtmlFormatter.generate(self, node, file) |
25
|
|
|
|
26
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
27
|
1 |
|
@staticmethod |
28
|
1 |
|
def write_into_file(node: TocNode, file: Any) -> None: |
29
|
|
|
""" |
30
|
|
|
Writes data into opened file. |
31
|
|
|
|
32
|
|
|
:param TocNode node: The table of contents node. |
33
|
|
|
:param any file: The output file. |
34
|
|
|
""" |
35
|
|
|
attributes = {'role': 'navigation', 'class': 'table-of-contents'} |
36
|
|
|
|
37
|
|
|
file.write(Html.generate_tag('nav', attributes)) |
38
|
|
|
file.write(Html.generate_element('h3', {}, node.argument)) |
39
|
|
|
|
40
|
|
|
file.write('<ul>') |
41
|
|
|
TocHtmlFormatter.write_contents(node, file) |
42
|
|
|
file.write('</ul>') |
43
|
|
|
|
44
|
|
|
file.write('</nav>') |
45
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
47
|
1 |
|
@staticmethod |
48
|
1 |
|
def write_contents(node: TocNode, file: Any) -> None: |
49
|
|
|
""" |
50
|
|
|
Writes the contents into file. |
51
|
|
|
|
52
|
|
|
:param TocNode node: The table of contents node. |
53
|
|
|
:param any file: The output file. |
54
|
|
|
""" |
55
|
|
|
depth = node.get_option_value('depth') |
56
|
|
|
|
57
|
|
|
for item in node.get_option_value('ids'): |
58
|
|
|
if depth and item['level'] <= int(depth): |
59
|
|
|
TocHtmlFormatter.write_elements(item, file) |
60
|
|
|
|
61
|
|
|
elif not depth: |
62
|
|
|
TocHtmlFormatter.write_elements(item, file) |
63
|
|
|
|
64
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
65
|
1 |
|
@staticmethod |
66
|
1 |
|
def write_elements(item: Dict[str, str], file: Any) -> None: |
67
|
|
|
""" |
68
|
|
|
Write the containing elements. |
69
|
|
|
|
70
|
|
|
:param dict[str,str] item: The item which we outputs. |
71
|
|
|
:param any file: The output file. |
72
|
|
|
""" |
73
|
|
|
class_attr = 'level{}'.format(item['level']) |
74
|
|
|
file.write(Html.generate_tag('li', {'class': class_attr})) |
75
|
|
|
|
76
|
|
|
file.write(Html.generate_tag('a', {'href': '#{}'.format(item['id'])})) |
77
|
|
|
|
78
|
|
|
number = item['number'] if item['numbering'] else None |
79
|
|
|
if number: |
80
|
|
|
file.write(Html.generate_element('span', {}, str(number))) |
81
|
|
|
|
82
|
|
|
file.write(' {}'.format(item['arg'])) |
83
|
|
|
file.write('</a>') |
84
|
|
|
file.write('</li>') |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
88
|
|
|
NodeStore.register_formatter('toc', 'html', TocHtmlFormatter) |
89
|
|
|
|