1 # $Id: admonitions.py 4667 2006-07-12 21:40:56Z wiemann $ |
|
2 # Author: David Goodger <goodger@python.org> |
|
3 # Copyright: This module has been placed in the public domain. |
|
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 states, directives |
|
14 from docutils import nodes |
|
15 |
|
16 |
|
17 class BaseAdmonition(Directive): |
|
18 |
|
19 required_arguments = 0 |
|
20 optional_arguments = 0 |
|
21 final_argument_whitespace = True |
|
22 option_spec = {} |
|
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 self.assert_has_content() |
|
30 text = '\n'.join(self.content) |
|
31 admonition_node = self.node_class(text) |
|
32 if self.arguments: |
|
33 title_text = self.arguments[0] |
|
34 textnodes, messages = self.state.inline_text(title_text, |
|
35 self.lineno) |
|
36 admonition_node += nodes.title(title_text, '', *textnodes) |
|
37 admonition_node += messages |
|
38 if self.options.has_key('class'): |
|
39 classes = self.options['class'] |
|
40 else: |
|
41 classes = ['admonition-' + nodes.make_id(title_text)] |
|
42 admonition_node['classes'] += classes |
|
43 self.state.nested_parse(self.content, self.content_offset, |
|
44 admonition_node) |
|
45 return [admonition_node] |
|
46 |
|
47 |
|
48 class Admonition(BaseAdmonition): |
|
49 |
|
50 required_arguments = 1 |
|
51 option_spec = {'class': directives.class_option} |
|
52 node_class = nodes.admonition |
|
53 |
|
54 |
|
55 class Attention(BaseAdmonition): |
|
56 |
|
57 node_class = nodes.attention |
|
58 |
|
59 |
|
60 class Caution(BaseAdmonition): |
|
61 |
|
62 node_class = nodes.caution |
|
63 |
|
64 |
|
65 class Danger(BaseAdmonition): |
|
66 |
|
67 node_class = nodes.danger |
|
68 |
|
69 |
|
70 class Error(BaseAdmonition): |
|
71 |
|
72 node_class = nodes.error |
|
73 |
|
74 |
|
75 class Hint(BaseAdmonition): |
|
76 |
|
77 node_class = nodes.hint |
|
78 |
|
79 |
|
80 class Important(BaseAdmonition): |
|
81 |
|
82 node_class = nodes.important |
|
83 |
|
84 |
|
85 class Note(BaseAdmonition): |
|
86 |
|
87 node_class = nodes.note |
|
88 |
|
89 |
|
90 class Tip(BaseAdmonition): |
|
91 |
|
92 node_class = nodes.tip |
|
93 |
|
94 |
|
95 class Warning(BaseAdmonition): |
|
96 |
|
97 node_class = nodes.warning |
|