| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Transforms related to document parts. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
__docformat__ = 'reStructuredText' |
| 12 |
|
| 13 |
import sys |
| 14 |
from docutils import nodes |
| 15 |
from docutils.transforms import Transform |
| 16 |
|
| 17 |
|
| 18 |
class SectNum(Transform): |
| 19 |
|
| 20 |
""" |
| 21 |
Automatically assigns numbers to the titles of document sections. |
| 22 |
|
| 23 |
It is possible to limit the maximum section level for which the numbers |
| 24 |
are added. For those sections that are auto-numbered, the "auto" |
| 25 |
attribute is set, informing the contents table generator that a different |
| 26 |
form of the TOC should be used. |
| 27 |
""" |
| 28 |
|
| 29 |
default_priority = 710 |
| 30 |
"""Should be applied before `Contents`.""" |
| 31 |
|
| 32 |
def apply(self) -> None: |
| 33 |
self.maxdepth = self.startnode.details.get('depth', None) |
| 34 |
self.startvalue = self.startnode.details.get('start', 1) |
| 35 |
self.prefix = self.startnode.details.get('prefix', '') |
| 36 |
self.suffix = self.startnode.details.get('suffix', '') |
| 37 |
self.startnode.parent.remove(self.startnode) |
| 38 |
if self.document.settings.sectnum_xform: |
| 39 |
if self.maxdepth is None: |
| 40 |
self.maxdepth = sys.maxsize |
| 41 |
self.update_section_numbers(self.document) |
| 42 |
else: |
| 43 |
self.document.settings.sectnum_depth = self.maxdepth |
| 44 |
self.document.settings.sectnum_start = self.startvalue |
| 45 |
self.document.settings.sectnum_prefix = self.prefix |
| 46 |
self.document.settings.sectnum_suffix = self.suffix |
| 47 |
|
| 48 |
def update_section_numbers(self, node, prefix=(), depth=0) -> None: |
| 49 |
depth += 1 |
| 50 |
if prefix: |
| 51 |
sectnum = 1 |
| 52 |
else: |
| 53 |
sectnum = self.startvalue |
| 54 |
for child in node: |
| 55 |
if isinstance(child, nodes.section): |
| 56 |
numbers = prefix + (str(sectnum),) |
| 57 |
title = child[0] |
| 58 |
|
| 59 |
generated = nodes.generated( |
| 60 |
'', (self.prefix + '.'.join(numbers) + self.suffix |
| 61 |
+ '\u00a0' * 3), |
| 62 |
classes=['sectnum']) |
| 63 |
title.insert(0, generated) |
| 64 |
title['auto'] = 1 |
| 65 |
if depth < self.maxdepth: |
| 66 |
self.update_section_numbers(child, numbers, depth) |
| 67 |
sectnum += 1 |
| 68 |
|
| 69 |
|
| 70 |
class Contents(Transform): |
| 71 |
""" |
| 72 |
Generate a table of contents (ToC) |
| 73 |
|
| 74 |
This transform generates a table of contents from the entire document tree |
| 75 |
or from a single branch. It locates <section> elements and builds them |
| 76 |
into a nested bullet list, which is placed within a <topic> created by the |
| 77 |
contents directive. A title is either explicitly specified, taken from |
| 78 |
the appropriate language module, or omitted (local table of contents). |
| 79 |
The depth may be specified. Two-way references between the table of |
| 80 |
contents and section titles are generated (requires Writer support). |
| 81 |
|
| 82 |
This transform requires a startnode, a <pending> element which contains |
| 83 |
generation options and provides the location for the generated ToC (the |
| 84 |
startnode is replaced by the table of contents <bullet_list>). |
| 85 |
""" |
| 86 |
|
| 87 |
default_priority = 720 |
| 88 |
|
| 89 |
def apply(self) -> None: |
| 90 |
|
| 91 |
self.toc_id = self.document.set_id(self.startnode.parent) |
| 92 |
|
| 93 |
toc_by_writer = getattr(self.document.settings, 'use_latex_toc', False) |
| 94 |
|
| 95 |
if toc_by_writer: |
| 96 |
return |
| 97 |
|
| 98 |
details = self.startnode.details |
| 99 |
if 'local' in details: |
| 100 |
|
| 101 |
startnode = self.startnode.parent.parent |
| 102 |
while not isinstance(startnode, (nodes.section, nodes.document)): |
| 103 |
startnode = startnode.parent |
| 104 |
else: |
| 105 |
startnode = self.document |
| 106 |
if 'backlinks' in details: |
| 107 |
self.backlinks = details['backlinks'] |
| 108 |
else: |
| 109 |
self.backlinks = self.document.settings.toc_backlinks |
| 110 |
contents = self.build_contents(startnode) |
| 111 |
if len(contents): |
| 112 |
self.startnode.replace_self(contents) |
| 113 |
else: |
| 114 |
self.startnode.parent.parent.remove(self.startnode.parent) |
| 115 |
|
| 116 |
def build_contents(self, node, level=0): |
| 117 |
level += 1 |
| 118 |
sections = [sect for sect in node if isinstance(sect, nodes.section)] |
| 119 |
entries = [] |
| 120 |
depth = self.startnode.details.get('depth', sys.maxsize) |
| 121 |
for section in sections: |
| 122 |
title = section[0] |
| 123 |
auto = title.get('auto') |
| 124 |
entrytext = self.copy_and_filter(title) |
| 125 |
reference = nodes.reference('', '', refid=section['ids'][0], |
| 126 |
*entrytext) |
| 127 |
ref_id = self.document.set_id(reference, |
| 128 |
suggested_prefix='toc-entry') |
| 129 |
entry = nodes.paragraph('', '', reference) |
| 130 |
item = nodes.list_item('', entry) |
| 131 |
if (self.backlinks in ('entry', 'top') |
| 132 |
and title.next_node(nodes.reference) is None): |
| 133 |
if self.backlinks == 'entry': |
| 134 |
title['refid'] = ref_id |
| 135 |
elif self.backlinks == 'top': |
| 136 |
title['refid'] = self.toc_id |
| 137 |
if level < depth: |
| 138 |
subsects = self.build_contents(section, level) |
| 139 |
item += subsects |
| 140 |
entries.append(item) |
| 141 |
if entries: |
| 142 |
contents = nodes.bullet_list('', *entries) |
| 143 |
if auto: |
| 144 |
contents['classes'].append('auto-toc') |
| 145 |
return contents |
| 146 |
else: |
| 147 |
return [] |
| 148 |
|
| 149 |
def copy_and_filter(self, node): |
| 150 |
"""Return a copy of a title, with references, images, etc. removed.""" |
| 151 |
visitor = ContentsFilter(self.document) |
| 152 |
node.walkabout(visitor) |
| 153 |
return visitor.get_entry_text() |
| 154 |
|
| 155 |
|
| 156 |
class ContentsFilter(nodes.TreeCopyVisitor): |
| 157 |
|
| 158 |
def get_entry_text(self): |
| 159 |
return self.get_tree_copy().children |
| 160 |
|
| 161 |
def visit_citation_reference(self, node): |
| 162 |
raise nodes.SkipNode |
| 163 |
|
| 164 |
def visit_footnote_reference(self, node): |
| 165 |
raise nodes.SkipNode |
| 166 |
|
| 167 |
def visit_image(self, node): |
| 168 |
if node.hasattr('alt'): |
| 169 |
self.parent.append(nodes.Text(node['alt'])) |
| 170 |
raise nodes.SkipNode |
| 171 |
|
| 172 |
def ignore_node_but_process_children(self, node): |
| 173 |
raise nodes.SkipDeparture |
| 174 |
|
| 175 |
visit_problematic = ignore_node_but_process_children |
| 176 |
visit_reference = ignore_node_but_process_children |
| 177 |
visit_target = ignore_node_but_process_children |