1 | 1 | from typing import Dict |
|
2 | |||
3 | 1 | from cleo.io.io import IO |
|
4 | |||
5 | 1 | from sdoc.sdoc2 import node_store |
|
6 | 1 | from sdoc.sdoc2.node.HeadingNode import HeadingNode |
|
7 | 1 | from sdoc.sdoc2.node.Node import Node |
|
8 | 1 | from sdoc.sdoc2.node.ParagraphNode import ParagraphNode |
|
9 | 1 | from sdoc.sdoc2.NodeStore import NodeStore |
|
10 | |||
11 | |||
12 | 1 | View Code Duplication | class TocNode(Node): |
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
13 | """ |
||
14 | SDoc2 node for table of contents. |
||
15 | """ |
||
16 | |||
17 | # ------------------------------------------------------------------------------------------------------------------ |
||
18 | 1 | def __init__(self, io: IO, options: Dict[str, str], argument: str): |
|
19 | """ |
||
20 | Object constructor. |
||
21 | |||
22 | :param OutputStyle io: The IO object. |
||
23 | :param dict[str,str] options: The options of this table of contents. |
||
24 | :param str argument: The argument of this TOC. |
||
25 | """ |
||
26 | Node.__init__(self, io, 'toc', options, argument) |
||
27 | |||
28 | # ------------------------------------------------------------------------------------------------------------------ |
||
29 | 1 | def get_command(self) -> str: |
|
30 | """ |
||
31 | Returns the command of this node (i.e. toc). |
||
32 | """ |
||
33 | return 'toc' |
||
34 | |||
35 | # ------------------------------------------------------------------------------------------------------------------ |
||
36 | 1 | def is_block_command(self) -> bool: |
|
37 | """ |
||
38 | Returns False. |
||
39 | """ |
||
40 | return False |
||
41 | |||
42 | # ------------------------------------------------------------------------------------------------------------------ |
||
43 | 1 | def is_inline_command(self) -> bool: |
|
44 | """ |
||
45 | Returns True. |
||
46 | """ |
||
47 | return True |
||
48 | |||
49 | # ------------------------------------------------------------------------------------------------------------------ |
||
50 | 1 | def generate_toc(self) -> None: |
|
51 | """ |
||
52 | Generates the table of contents. |
||
53 | """ |
||
54 | self._options['ids'] = [] |
||
55 | |||
56 | for node in node_store.nodes.values(): |
||
57 | if not isinstance(node, ParagraphNode) and isinstance(node, HeadingNode): |
||
58 | node.set_toc_id() |
||
59 | |||
60 | data = {'id': node.get_option_value('id'), |
||
61 | 'arg': node.argument, |
||
62 | 'level': node.get_hierarchy_level(), |
||
63 | 'number': node.get_option_value('number'), |
||
64 | 'numbering': node.numbering} |
||
65 | |||
66 | self._options['ids'].append(data) |
||
67 | |||
68 | |||
69 | # ---------------------------------------------------------------------------------------------------------------------- |
||
70 | NodeStore.register_inline_command('toc', TocNode) |
||
71 |