class Directive(object): (source)
Known subclasses: docutils.parsers.rst.directives.admonitions.BaseAdmonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.BlockQuote
, docutils.parsers.rst.directives.body.CodeBlock
, docutils.parsers.rst.directives.body.Compound
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.LineBlock
, docutils.parsers.rst.directives.body.MathBlock
, docutils.parsers.rst.directives.body.ParsedLiteral
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.html.Meta
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.Date
, docutils.parsers.rst.directives.misc.DefaultRole
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.Replace
, docutils.parsers.rst.directives.misc.Role
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.misc.Title
, docutils.parsers.rst.directives.misc.Unicode
, docutils.parsers.rst.directives.parts.Contents
, docutils.parsers.rst.directives.parts.Footer
, docutils.parsers.rst.directives.parts.Header
, docutils.parsers.rst.directives.parts.Sectnum
, docutils.parsers.rst.directives.references.TargetNotes
, docutils.parsers.rst.directives.tables.Table
Constructor: Directive(name, arguments, options, content, ...)
Base class for reStructuredText directives.
The following attributes may be set by subclasses. They are interpreted by the directive parser (which runs the directive class):
required_arguments
: The number of required arguments (default: 0).optional_arguments
: The number of optional arguments (default: 0).final_argument_whitespace
: A boolean, indicating if the final argument may contain whitespace (default: False).option_spec
: A dictionary, mapping known option names to conversion functions such asint
orfloat
(default: {}, no options). Several conversion functions are defined in the directives/__init__.py module.Option conversion functions take a single parameter, the option argument (a string or None), validate it and/or convert it to the appropriate form. Conversion functions may raise
ValueError
andTypeError
exceptions.has_content
: A boolean; True if content is allowed. Client code must handle the case where content is required but not supplied (an empty content list will be supplied).
Arguments are normally single whitespace-separated words. The
final argument may contain whitespace and/or newlines if
final_argument_whitespace
is True.
If the form of the arguments is more complex, specify only one
argument (either required or optional) and set
final_argument_whitespace
to True; the client code must do any
context-sensitive parsing.
When a directive implementation is being run, the directive class
is instantiated, and the run()
method is executed. During
instantiation, the following instance variables are set:
- name is the directive type or name (string).
- arguments is the list of positional arguments (strings).
- options is a dictionary mapping option names (strings) to
values (type depends on option conversion functions; see
option_spec
above). - content is a list of strings, the directive content line by line.
- lineno is the absolute line number of the first line of the directive.
- content_offset is the line offset of the first line of the content from the beginning of the current input. Used when initiating a nested parse.
- block_text is a string containing the entire directive.
- state is the state which called the directive function.
- state_machine is the state machine which controls the state which called the directive function.
Directive functions return a list of nodes which will be inserted into the document tree at the point where the directive was encountered. This can be an empty list if there is nothing to insert.
For ordinary directives, the list must contain body elements or
structural elements. Some directives are intended specifically
for substitution definitions, and must return a list of Text
nodes and/or inline elements (suitable for inline insertion, in
place of the substitution reference). Such directives must verify
substitution definition context, typically using code like this:
if not isinstance(state, states.SubstitutionDef): error = state_machine.reporter.error( 'Invalid context: the "%s" directive can only be used ' 'within a substitution definition.' % (name), nodes.literal_block(block_text, block_text), line=lineno) return [error]
Method | __init__ |
Undocumented |
Method | add |
Append self.options['name'] to node['names'] if it exists. |
Method | assert |
Throw an ERROR-level DirectiveError if the directive doesn't have contents. |
Method | debug |
Undocumented |
Method | directive |
Return a DirectiveError suitable for being thrown as an exception. |
Method | error |
Undocumented |
Method | info |
Undocumented |
Method | run |
Undocumented |
Method | severe |
Undocumented |
Method | warning |
Undocumented |
Class Variable | final |
May the final argument contain whitespace? |
Class Variable | has |
May the directive have content? |
Class Variable | option |
Mapping of option names to validator functions. |
Class Variable | optional |
Number of optional arguments after the required arguments. |
Class Variable | required |
Number of required directive arguments. |
Instance Variable | arguments |
Undocumented |
Instance Variable | block |
Undocumented |
Instance Variable | content |
Undocumented |
Instance Variable | content |
Undocumented |
Instance Variable | lineno |
Undocumented |
Instance Variable | name |
Undocumented |
Instance Variable | options |
Undocumented |
Instance Variable | state |
Undocumented |
Instance Variable | state |
Undocumented |
Undocumented
Append self.options['name'] to node['names'] if it exists.
Also normalize the name string and register it as explicit target.
Return a DirectiveError suitable for being thrown as an exception.
Call "raise self.directive_error(level, message)" from within
a directive implementation to return one single system message
at level level
, which automatically gets the directive block
and the line number added.
Preferably use the debug
, info
, warning
, error
, or severe
wrapper methods, e.g. self.error(message) to generate an
ERROR-level directive error.
docutils.parsers.rst.directives.admonitions.BaseAdmonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.BlockQuote
, docutils.parsers.rst.directives.body.CodeBlock
, docutils.parsers.rst.directives.body.Compound
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.LineBlock
, docutils.parsers.rst.directives.body.MathBlock
, docutils.parsers.rst.directives.body.ParsedLiteral
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.html.Meta
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.Date
, docutils.parsers.rst.directives.misc.DefaultRole
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.Replace
, docutils.parsers.rst.directives.misc.Role
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.misc.Title
, docutils.parsers.rst.directives.misc.Unicode
, docutils.parsers.rst.directives.parts.Contents
, docutils.parsers.rst.directives.parts.Footer
, docutils.parsers.rst.directives.parts.Header
, docutils.parsers.rst.directives.parts.Sectnum
, docutils.parsers.rst.directives.references.TargetNotes
, docutils.parsers.rst.directives.tables.CSVTable
, docutils.parsers.rst.directives.tables.ListTable
, docutils.parsers.rst.directives.tables.RSTTable
Undocumented
docutils.parsers.rst.directives.admonitions.BaseAdmonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.DefaultRole
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.misc.Title
, docutils.parsers.rst.directives.misc.Unicode
, docutils.parsers.rst.directives.parts.Contents
, docutils.parsers.rst.directives.tables.Table
May the final argument contain whitespace?
docutils.parsers.rst.directives.admonitions.BaseAdmonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.BlockQuote
, docutils.parsers.rst.directives.body.CodeBlock
, docutils.parsers.rst.directives.body.Compound
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.LineBlock
, docutils.parsers.rst.directives.body.MathBlock
, docutils.parsers.rst.directives.body.ParsedLiteral
, docutils.parsers.rst.directives.html.Meta
, docutils.parsers.rst.directives.images.Figure
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.Date
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.Replace
, docutils.parsers.rst.directives.misc.Role
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.parts.Footer
, docutils.parsers.rst.directives.parts.Header
, docutils.parsers.rst.directives.tables.Table
May the directive have content?
docutils.parsers.rst.directives.admonitions.BaseAdmonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.CodeBlock
, docutils.parsers.rst.directives.body.Compound
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.LineBlock
, docutils.parsers.rst.directives.body.MathBlock
, docutils.parsers.rst.directives.body.ParsedLiteral
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.misc.Unicode
, docutils.parsers.rst.directives.parts.Contents
, docutils.parsers.rst.directives.parts.Sectnum
, docutils.parsers.rst.directives.references.TargetNotes
, docutils.parsers.rst.directives.tables.Table
Mapping of option names to validator functions.
docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.CodeBlock
, docutils.parsers.rst.directives.body.Container
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.DefaultRole
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.TestDirective
, docutils.parsers.rst.directives.misc.Title
, docutils.parsers.rst.directives.misc.Unicode
, docutils.parsers.rst.directives.parts.Contents
, docutils.parsers.rst.directives.tables.Table
Number of optional arguments after the required arguments.
docutils.parsers.rst.directives.admonitions.Admonition
, docutils.parsers.rst.directives.body.BasePseudoSection
, docutils.parsers.rst.directives.body.Rubric
, docutils.parsers.rst.directives.images.Image
, docutils.parsers.rst.directives.misc.Class
, docutils.parsers.rst.directives.misc.Include
, docutils.parsers.rst.directives.misc.Raw
, docutils.parsers.rst.directives.misc.Title
, docutils.parsers.rst.directives.misc.Unicode
Number of required directive arguments.