[3] | 1 | # Author: David Goodger |
---|
| 2 | # Contact: goodger@users.sourceforge.net |
---|
| 3 | # Revision: $Revision: 3155 $ |
---|
| 4 | # Date: $Date: 2005-04-02 23:57:06 +0200 (Sat, 02 Apr 2005) $ |
---|
| 5 | # Copyright: This module has been placed in the public domain. |
---|
| 6 | |
---|
| 7 | """ |
---|
| 8 | Admonition directives. |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | __docformat__ = 'reStructuredText' |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | from docutils.parsers.rst import states, directives |
---|
| 15 | from docutils import nodes |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | def make_admonition(node_class, name, arguments, options, content, lineno, |
---|
| 19 | content_offset, block_text, state, state_machine): |
---|
| 20 | if not content: |
---|
| 21 | error = state_machine.reporter.error( |
---|
| 22 | 'The "%s" admonition is empty; content required.' % (name), |
---|
| 23 | nodes.literal_block(block_text, block_text), line=lineno) |
---|
| 24 | return [error] |
---|
| 25 | text = '\n'.join(content) |
---|
| 26 | admonition_node = node_class(text) |
---|
| 27 | if arguments: |
---|
| 28 | title_text = arguments[0] |
---|
| 29 | textnodes, messages = state.inline_text(title_text, lineno) |
---|
| 30 | admonition_node += nodes.title(title_text, '', *textnodes) |
---|
| 31 | admonition_node += messages |
---|
| 32 | if options.has_key('class'): |
---|
| 33 | classes = options['class'] |
---|
| 34 | else: |
---|
| 35 | classes = ['admonition-' + nodes.make_id(title_text)] |
---|
| 36 | admonition_node['classes'] += classes |
---|
| 37 | state.nested_parse(content, content_offset, admonition_node) |
---|
| 38 | return [admonition_node] |
---|
| 39 | |
---|
| 40 | def admonition(*args): |
---|
| 41 | return make_admonition(nodes.admonition, *args) |
---|
| 42 | |
---|
| 43 | admonition.arguments = (1, 0, 1) |
---|
| 44 | admonition.options = {'class': directives.class_option} |
---|
| 45 | admonition.content = 1 |
---|
| 46 | |
---|
| 47 | def attention(*args): |
---|
| 48 | return make_admonition(nodes.attention, *args) |
---|
| 49 | |
---|
| 50 | attention.content = 1 |
---|
| 51 | |
---|
| 52 | def caution(*args): |
---|
| 53 | return make_admonition(nodes.caution, *args) |
---|
| 54 | |
---|
| 55 | caution.content = 1 |
---|
| 56 | |
---|
| 57 | def danger(*args): |
---|
| 58 | return make_admonition(nodes.danger, *args) |
---|
| 59 | |
---|
| 60 | danger.content = 1 |
---|
| 61 | |
---|
| 62 | def error(*args): |
---|
| 63 | return make_admonition(nodes.error, *args) |
---|
| 64 | |
---|
| 65 | error.content = 1 |
---|
| 66 | |
---|
| 67 | def hint(*args): |
---|
| 68 | return make_admonition(nodes.hint, *args) |
---|
| 69 | |
---|
| 70 | hint.content = 1 |
---|
| 71 | |
---|
| 72 | def important(*args): |
---|
| 73 | return make_admonition(nodes.important, *args) |
---|
| 74 | |
---|
| 75 | important.content = 1 |
---|
| 76 | |
---|
| 77 | def note(*args): |
---|
| 78 | return make_admonition(nodes.note, *args) |
---|
| 79 | |
---|
| 80 | note.content = 1 |
---|
| 81 | |
---|
| 82 | def tip(*args): |
---|
| 83 | return make_admonition(nodes.tip, *args) |
---|
| 84 | |
---|
| 85 | tip.content = 1 |
---|
| 86 | |
---|
| 87 | def warning(*args): |
---|
| 88 | return make_admonition(nodes.warning, *args) |
---|
| 89 | |
---|
| 90 | warning.content = 1 |
---|