| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Directives for document parts. |
| 7 |
""" |
| 8 |
|
| 9 |
__docformat__ = 'reStructuredText' |
| 10 |
|
| 11 |
from docutils import nodes, languages |
| 12 |
from docutils.transforms import parts |
| 13 |
from docutils.parsers.rst import Directive |
| 14 |
from docutils.parsers.rst import directives |
| 15 |
from docutils.parsers.rst.directives.body import BasePseudoSection |
| 16 |
|
| 17 |
|
| 18 |
class Contents(Directive): |
| 19 |
|
| 20 |
""" |
| 21 |
Table of contents. |
| 22 |
|
| 23 |
The table of contents is generated in two passes: initial parse and |
| 24 |
transform. During the initial parse, a 'pending' element is generated |
| 25 |
which acts as a placeholder, storing the TOC title and any options |
| 26 |
internally. At a later stage in the processing, the 'pending' element is |
| 27 |
replaced by a 'topic' element, a title and the table of contents proper. |
| 28 |
""" |
| 29 |
|
| 30 |
backlinks_values = ('top', 'entry', 'none') |
| 31 |
|
| 32 |
def backlinks(arg): |
| 33 |
value = directives.choice(arg, Contents.backlinks_values) |
| 34 |
if value == 'none': |
| 35 |
return None |
| 36 |
else: |
| 37 |
return value |
| 38 |
|
| 39 |
optional_arguments = 1 |
| 40 |
final_argument_whitespace = True |
| 41 |
option_spec = {'depth': directives.nonnegative_int, |
| 42 |
'local': directives.flag, |
| 43 |
'backlinks': backlinks, |
| 44 |
'class': directives.class_option} |
| 45 |
|
| 46 |
def run(self): |
| 47 |
if (not isinstance(self.state_machine.node, |
| 48 |
(nodes.Root, nodes.section, nodes.sidebar)) |
| 49 |
and isinstance(self.state_machine.node, |
| 50 |
BasePseudoSection.invalid_parents)): |
| 51 |
raise self.error('The "%s" directive may not be used within ' |
| 52 |
'topics or body elements.' % self.name) |
| 53 |
document = self.state_machine.document |
| 54 |
language = languages.get_language(document.settings.language_code, |
| 55 |
document.reporter) |
| 56 |
if self.arguments: |
| 57 |
title_text = self.arguments[0] |
| 58 |
text_nodes, messages = self.state.inline_text(title_text, |
| 59 |
self.lineno) |
| 60 |
title = nodes.title(title_text, '', *text_nodes) |
| 61 |
else: |
| 62 |
messages = [] |
| 63 |
if 'local' in self.options: |
| 64 |
title = None |
| 65 |
else: |
| 66 |
title = nodes.title('', language.labels['contents']) |
| 67 |
topic = nodes.topic(classes=['contents']) |
| 68 |
topic['classes'] += self.options.get('class', []) |
| 69 |
topic.source, topic.line = self.state_machine.get_source_and_line() |
| 70 |
topic.line -= 1 |
| 71 |
if 'local' in self.options: |
| 72 |
topic['classes'].append('local') |
| 73 |
if title: |
| 74 |
name = title.astext() |
| 75 |
topic += title |
| 76 |
else: |
| 77 |
name = language.labels['contents'] |
| 78 |
name = nodes.fully_normalize_name(name) |
| 79 |
if not document.has_name(name): |
| 80 |
topic['names'].append(name) |
| 81 |
document.note_implicit_target(topic) |
| 82 |
pending = nodes.pending(parts.Contents, rawsource=self.block_text) |
| 83 |
pending.details.update(self.options) |
| 84 |
document.note_pending(pending) |
| 85 |
topic += pending |
| 86 |
return [topic] + messages |
| 87 |
|
| 88 |
|
| 89 |
class Sectnum(Directive): |
| 90 |
|
| 91 |
"""Automatic section numbering.""" |
| 92 |
|
| 93 |
option_spec = {'depth': int, |
| 94 |
'start': int, |
| 95 |
'prefix': directives.unchanged_required, |
| 96 |
'suffix': directives.unchanged_required} |
| 97 |
|
| 98 |
def run(self): |
| 99 |
pending = nodes.pending(parts.SectNum) |
| 100 |
pending.details.update(self.options) |
| 101 |
self.state_machine.document.note_pending(pending) |
| 102 |
return [pending] |
| 103 |
|
| 104 |
|
| 105 |
class Header(Directive): |
| 106 |
|
| 107 |
"""Contents of document header.""" |
| 108 |
|
| 109 |
has_content = True |
| 110 |
|
| 111 |
def run(self): |
| 112 |
self.assert_has_content() |
| 113 |
header = self.state_machine.document.get_decoration().get_header() |
| 114 |
self.state.nested_parse(self.content, self.content_offset, header) |
| 115 |
return [] |
| 116 |
|
| 117 |
|
| 118 |
class Footer(Directive): |
| 119 |
|
| 120 |
"""Contents of document footer.""" |
| 121 |
|
| 122 |
has_content = True |
| 123 |
|
| 124 |
def run(self): |
| 125 |
self.assert_has_content() |
| 126 |
footer = self.state_machine.document.get_decoration().get_footer() |
| 127 |
self.state.nested_parse(self.content, self.content_offset, footer) |
| 128 |
return [] |