ó
|	Qc           @   sµ   d  Z  d Z d d l Z d d l Z d d l m Z d d l m Z m Z m	 Z	 d d l
 m Z d e j j f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d S(   s	  
This is ``docutils.parsers.rst`` package. It exports a single class, `Parser`,
the reStructuredText parser.


Usage
=====

1. Create a parser::

       parser = docutils.parsers.rst.Parser()

   Several optional arguments may be passed to modify the parser's behavior.
   Please see `Customizing the Parser`_ below for details.

2. Gather input (a multi-line string), by reading a file or the standard
   input::

       input = sys.stdin.read()

3. Create a new empty `docutils.nodes.document` tree::

       document = docutils.utils.new_document(source, settings)

   See `docutils.utils.new_document()` for parameter details.

4. Run the parser, populating the document tree::

       parser.parse(input, document)


Parser Overview
===============

The reStructuredText parser is implemented as a state machine, examining its
input one line at a time. To understand how the parser works, please first
become familiar with the `docutils.statemachine` module, then see the
`states` module.


Customizing the Parser
----------------------

Anything that isn't already customizable is that way simply because that type
of customizability hasn't been implemented yet.  Patches welcome!

When instantiating an object of the `Parser` class, two parameters may be
passed: ``rfc2822`` and ``inliner``.  Pass ``rfc2822=True`` to enable an
initial RFC-2822 style header block, parsed as a "field_list" element (with
"class" attribute set to "rfc2822").  Currently this is the only body-level
element which is customizable without subclassing.  (Tip: subclass `Parser`
and change its "state_classes" and "initial_state" attributes to refer to new
classes. Contact the author if you need more details.)

The ``inliner`` parameter takes an instance of `states.Inliner` or a subclass.
It handles inline markup recognition.  A common extension is the addition of
further implicit hyperlinks, like "RFC 2822".  This can be done by subclassing
`states.Inliner`, adding a new method for the implicit markup, and adding a
``(pattern, method)`` pair to the "implicit_dispatch" attribute of the
subclass.  See `states.Inliner.implicit_inline()` for details.  Explicit
inline markup can be customized in a `states.Inliner` subclass via the
``patterns.initial`` and ``dispatch`` attributes (and new methods as
appropriate).
t   reStructuredTextiÿÿÿÿN(   t   states(   t   frontendt   nodest	   Component(   t	   universalt   Parserc           B   s;  e  Z d  Z dA Z d dB d d	 g i d
 d 6e j d 6f d d g i d d 6d d 6e j d 6f d d g i d d 6d d 6f d d g i d
 d 6e j d 6f d d g i d d 6d d 6e j d 6f d d g i d d 6d d 6d  d 6e j d 6f d! d" g i d
 d 6e j d 6f d# d$ g i d% d 6d& d' 6f d( d) g i d% d 6d* d 6d+ d' 6e j d 6f d, d- g i d
 d 6f d. d/ g i d% d 6d* d 6d0 d' 6e j d 6f d1 d2 g i d
 d 6f d3 d4 g i d5 d6 d7 g d8 6d5 d 6d9 d 6f d: d; g i e	 d 6e j
 d 6f f f Z d< Z dC Z e	 dB d> „ Z d? „  Z d@ „  Z RS(D   s   The reStructuredText parser.t   restructuredtextt   rstt   restt   restxt   rtxtt   rstxs   reStructuredText Parser OptionssA   Recognize and link to standalone PEP references (like "PEP 258").s   --pep-referencest
   store_truet   actiont	   validatorsH   Base URL for PEP references (default "http://www.python.org/dev/peps/").s   --pep-base-urls   <URL>t   metavars   http://www.python.org/dev/peps/t   defaults7   Template for PEP file part of URL. (default "pep-%04d")s   --pep-file-url-templates   pep-%04dsA   Recognize and link to standalone RFC references (like "RFC 822").s   --rfc-referencessB   Base URL for RFC references (default "http://www.faqs.org/rfcs/").s   --rfc-base-urls   http://www.faqs.org/rfcs/s3   Set number of spaces for tab expansion (default 8).s   --tab-widths   <width>t   intt   typei   s)   Remove spaces before footnote references.s   --trim-footnote-reference-spaces(   Leave spaces before footnote references.s    --leave-footnote-reference-spacet   store_falset   trim_footnote_reference_spacet   dests{   Disable directives that insert the contents of external file ("include" & "raw"); replaced with a "warning" system message.s   --no-file-insertioni   t   file_insertion_enabledse   Enable directives that insert the contents of external file ("include" & "raw").  Enabled by default.s   --file-insertion-enabledsG   Disable the "raw" directives; replaced with a "warning" system message.s   --no-rawt   raw_enableds0   Enable the "raw" directive.  Enabled by default.s   --raw-enabledsq   Token name set for parsing code with Pygments: one of "long", "short", or "none (no parsing)". Default is "long".s   --syntax-highlightt   longt   shortt   nonet   choicess   <format>sh   Change straight quotation marks to typographic form: one of "yes", "no", "alt[ernative]" (default "no").s   --smart-quotess   restructuredtext parsert   parsersc         C   s4   | r d |  _  n	 d |  _  t j |  _ | |  _ d  S(   Nt   RFC2822Bodyt   Body(   t   initial_stateR   t   state_classest   inliner(   t   selft   rfc2822R"   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   __init__–   s
    	c         C   s   t  j |  ƒ t j g S(   N(   R   t   get_transformsR   t   SmartQuotes(   R#   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR&   ž   s    c         C   s‹   |  j  | | ƒ t j d |  j d |  j d | j j ƒ |  _ t j j	 | d | j
 j d t ƒ} |  j j | | d |  j ƒ|  j ƒ  d S(   s=   Parse `inputstring` and populate `document`, a document tree.R!   R    t   debugt	   tab_widtht   convert_whitespaceR"   N(   t   setup_parseR   t   RSTStateMachineR!   R    t   reportert
   debug_flagt   statemachinet   docutilst   string2linest   settingsR)   t   Truet   runR"   t   finish_parse(   R#   t   inputstringt   documentt
   inputlines(    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   parse¢   s    					(   R   R   s   restR
   R   R   N(   s   parsers(   t   __name__t
   __module__t   __doc__t	   supportedt   NoneR   t   validate_booleant   validate_url_trailing_slasht   validate_nonnegative_intt   Falset   validate_ternaryt   settings_spect   config_sectiont   config_section_dependenciesR%   R&   R9   (    (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR   P   st    	t   DirectiveErrorc           B   s   e  Z d  Z d „  Z RS(   s¶   
    Store a message and a system message level.

    To be thrown from inside directive code.

    Do not instantiate directly -- use `Directive.directive_error()`
    instead!
    c         C   s#   t  j |  ƒ | |  _ | |  _ d S(   s   Set error `message` and `level`N(   t	   ExceptionR%   t   levelt   msg(   R#   RI   t   message(    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR%   »   s    	(   R:   R;   R<   R%   (    (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyRG   °   s   	t	   Directivec           B   s†   e  Z d  Z d Z d Z e Z d Z e Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s  
    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.

    - ``src`` is the name (or path) of the rst source of the directive.

    - ``srcline`` is the line number of the first line of the directive
      in its source. It may differ from ``lineno``, if the main source
      includes other sources with the ``.. include::`` 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]
    i    c
   
      C   sU   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ d  S(   N(	   t   namet	   argumentst   optionst   contentt   linenot   content_offsett
   block_textt   statet   state_machine(
   R#   RM   RN   RO   RP   RQ   RR   RS   RT   RU   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR%   3  s    								c         C   s   t  d ƒ ‚ d  S(   Ns    Must override run() is subclass.(   t   NotImplementedError(   R#   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR4   ?  s    c         C   s   t  | | ƒ S(   s  
        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.
        (   RG   (   R#   RI   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   directive_errorD  s    c         C   s   |  j  d | ƒ S(   Ni    (   RW   (   R#   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR(   S  s    c         C   s   |  j  d | ƒ S(   Ni   (   RW   (   R#   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   infoV  s    c         C   s   |  j  d | ƒ S(   Ni   (   RW   (   R#   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   warningY  s    c         C   s   |  j  d | ƒ S(   Ni   (   RW   (   R#   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   error\  s    c         C   s   |  j  d | ƒ S(   Ni   (   RW   (   R#   RK   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   severe_  s    c         C   s&   |  j  s" |  j d |  j ƒ ‚ n  d S(   se   
        Throw an ERROR-level DirectiveError if the directive doesn't
        have contents.
        s:   Content block expected for the "%s" directive; none found.N(   RP   RZ   RM   (   R#   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   assert_has_contentd  s    		c         C   sn   d |  j  k rj t j |  j  j d ƒ ƒ } d | k r@ | d =n  | d j | ƒ |  j j j | | ƒ n  d S(   s   Append self.options['name'] to node['names'] if it exists.

        Also normalize the name string and register it as explicit target.
        RM   t   namesN(   RO   R   t   fully_normalize_namet   popt   appendRT   R7   t   note_explicit_target(   R#   t   nodeRM   (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   add_namem  s    
N(   R:   R;   R<   t   required_argumentst   optional_argumentsRB   t   final_argument_whitespaceR>   t   option_spect   has_contentR%   R4   RW   R(   RX   RY   RZ   R[   R\   Rc   (    (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyRL   Â   s    \										c            s    d t  f ‡  f d †  ƒ  Y} | S(   sˆ   
    Define & return a directive class generated from `directive_fn`.

    `directive_fn` uses the old-style, functional interface.
    t   FunctionalDirectivec              se   e  Z e ˆ  d  d ƒ Z e ˆ  d e ƒ Z e ˆ  d d d e f ƒ Z e \ Z Z	 Z
 ‡  f d †  Z RS(   RO   RP   RN   i    c      
      s=   ˆ  |  j  |  j |  j |  j |  j |  j |  j |  j |  j ƒ	 S(   N(	   RM   RN   RO   RP   RQ   RR   RS   RT   RU   (   R#   (   t   directive_fn(    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyR4   ‰  s    N(   R:   R;   t   getattrR>   Rg   RB   Rh   t   _argument_specRd   Re   Rf   R4   (    (   Rj   (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyRi     s
   (   RL   (   Rj   Ri   (    (   Rj   sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   convert_directive_functionz  s    (   R<   t   __docformat__t   docutils.parsersR0   t   docutils.statemachinet   docutils.parsers.rstR   R   R   R   t   docutils.transformsR   R   R   RH   RG   t   objectRL   Rm   (    (    (    sA   /usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.pyt   <module>D   s   `¸