class documentation

class Directive(object):

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.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.Meta, 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

View In Hierarchy

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 as int or float (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 and TypeError 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​_name Append self.options['name'] to node['names'] if it exists.
Method assert​_has​_content Throw an ERROR-level DirectiveError if the directive doesn't have contents.
Method debug Undocumented
Method directive​_error 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​_argument​_whitespace May the final argument contain whitespace?
Class Variable has​_content May the directive have content?
Class Variable option​_spec Mapping of option names to validator functions.
Class Variable optional​_arguments Number of optional arguments after the required arguments.
Class Variable required​_arguments Number of required directive arguments.
Instance Variable arguments Undocumented
Instance Variable block​_text Undocumented
Instance Variable content Undocumented
Instance Variable content​_offset Undocumented
Instance Variable lineno Undocumented
Instance Variable name Undocumented
Instance Variable options Undocumented
Instance Variable state Undocumented
Instance Variable state​_machine Undocumented
def __init__(self, name, arguments, options, content, lineno, content_offset, block_text, state, state_machine):

Undocumented

def add_name(self, node):

Append self.options['name'] to node['names'] if it exists.

Also normalize the name string and register it as explicit target.

def assert_has_content(self):
Throw an ERROR-level DirectiveError if the directive doesn't have contents.
def debug(self, message):

Undocumented

def directive_error(self, level, message):

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.

def error(self, message):

Undocumented

def info(self, message):

Undocumented

def severe(self, message):

Undocumented

def warning(self, message):

Undocumented

arguments =

Undocumented

block_text =

Undocumented

content =

Undocumented

content_offset =

Undocumented

lineno =

Undocumented

name =

Undocumented

options =

Undocumented

state =

Undocumented

state_machine =

Undocumented