~ubuntu-branches/ubuntu/jaunty/python-docutils/jaunty

« back to all changes in this revision

Viewing changes to docutils/parsers/rst/directives/admonitions.py

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2008-07-24 10:39:53 UTC
  • mfrom: (1.1.4 upstream) (3.1.7 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080724103953-8gh4uezg17g9ysgy
Tags: 0.5-2
* Upload docutils 0.5 to unstable
* Update rst.el to upstream Subversion r5596, which apparently fixes
  all its performance problems (17_speed_up_rst_el.dpatch, closes: #474941)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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) $
 
1
# $Id: admonitions.py 4667 2006-07-12 21:40:56Z wiemann $
 
2
# Author: David Goodger <goodger@python.org>
5
3
# Copyright: This module has been placed in the public domain.
6
4
 
7
5
"""
11
9
__docformat__ = 'reStructuredText'
12
10
 
13
11
 
 
12
from docutils.parsers.rst import Directive
14
13
from docutils.parsers.rst import states, directives
15
14
from docutils import nodes
16
15
 
17
16
 
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
 
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