| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Admonition directives. |
| 7 |
""" |
| 8 |
|
| 9 |
__docformat__ = 'reStructuredText' |
| 10 |
|
| 11 |
|
| 12 |
from docutils.parsers.rst import Directive |
| 13 |
from docutils.parsers.rst import directives |
| 14 |
from docutils.parsers.rst.roles import normalize_options |
| 15 |
from docutils import nodes |
| 16 |
|
| 17 |
|
| 18 |
class BaseAdmonition(Directive): |
| 19 |
|
| 20 |
final_argument_whitespace = True |
| 21 |
option_spec = {'class': directives.class_option, |
| 22 |
'name': directives.unchanged} |
| 23 |
has_content = True |
| 24 |
|
| 25 |
node_class = None |
| 26 |
"""Subclasses must set this to the appropriate admonition node class.""" |
| 27 |
|
| 28 |
def run(self): |
| 29 |
options = normalize_options(self.options) |
| 30 |
self.assert_has_content() |
| 31 |
text = '\n'.join(self.content) |
| 32 |
admonition_node = self.node_class(text, **options) |
| 33 |
self.add_name(admonition_node) |
| 34 |
admonition_node.source, admonition_node.line = \ |
| 35 |
self.state_machine.get_source_and_line(self.lineno) |
| 36 |
if self.node_class is nodes.admonition: |
| 37 |
title_text = self.arguments[0] |
| 38 |
textnodes, messages = self.state.inline_text(title_text, |
| 39 |
self.lineno) |
| 40 |
title = nodes.title(title_text, '', *textnodes) |
| 41 |
title.source, title.line = ( |
| 42 |
self.state_machine.get_source_and_line(self.lineno)) |
| 43 |
admonition_node += title |
| 44 |
admonition_node += messages |
| 45 |
if 'classes' not in options: |
| 46 |
admonition_node['classes'] += ['admonition-' |
| 47 |
+ nodes.make_id(title_text)] |
| 48 |
self.state.nested_parse(self.content, self.content_offset, |
| 49 |
admonition_node) |
| 50 |
return [admonition_node] |
| 51 |
|
| 52 |
|
| 53 |
class Admonition(BaseAdmonition): |
| 54 |
|
| 55 |
required_arguments = 1 |
| 56 |
node_class = nodes.admonition |
| 57 |
|
| 58 |
|
| 59 |
class Attention(BaseAdmonition): |
| 60 |
|
| 61 |
node_class = nodes.attention |
| 62 |
|
| 63 |
|
| 64 |
class Caution(BaseAdmonition): |
| 65 |
|
| 66 |
node_class = nodes.caution |
| 67 |
|
| 68 |
|
| 69 |
class Danger(BaseAdmonition): |
| 70 |
|
| 71 |
node_class = nodes.danger |
| 72 |
|
| 73 |
|
| 74 |
class Error(BaseAdmonition): |
| 75 |
|
| 76 |
node_class = nodes.error |
| 77 |
|
| 78 |
|
| 79 |
class Hint(BaseAdmonition): |
| 80 |
|
| 81 |
node_class = nodes.hint |
| 82 |
|
| 83 |
|
| 84 |
class Important(BaseAdmonition): |
| 85 |
|
| 86 |
node_class = nodes.important |
| 87 |
|
| 88 |
|
| 89 |
class Note(BaseAdmonition): |
| 90 |
|
| 91 |
node_class = nodes.note |
| 92 |
|
| 93 |
|
| 94 |
class Tip(BaseAdmonition): |
| 95 |
|
| 96 |
node_class = nodes.tip |
| 97 |
|
| 98 |
|
| 99 |
class Warning(BaseAdmonition): |
| 100 |
|
| 101 |
node_class = nodes.warning |