ó 9(Zc@sµdZdZddlZddlZddlmZddlmZmZm Z ddl m Z dej j fd„ƒYZ d efd „ƒYZd efd „ƒYZd „ZdS(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). treStructuredTexti’’’’N(tstates(tfrontendtnodest Component(t universaltParsercBsŖeZdZdKZddLdd gid d 6ejd 6fd dgidd6dd6ejd 6fddgidd6dd6fddgid d 6ejd 6fddgidd6dd6ejd 6fddgidd6dd6d d6ejd 6fd!d"gid d 6ejd 6fd#d$gid%d 6d&d'6fd(d)gid%d 6d*d6d+d'6ejd 6fd,d-gid d 6fd.d/gid%d 6d*d6d0d'6ejd 6fd1d2gid d 6fd3d4gid5d6d7gd86d5d6d9d6fd:d;gie d6d<d6ej d 6fd=d>gid?d6d@d 6ej d 6fdAdBgid%d 6dCd'6fdDdEgid d 6e d6dCd'6fffZ dFZ dMZe dLdH„ZdI„ZdJ„ZRS(NsThe reStructuredText parser.trestructuredtexttrsttresttrestxtrtxttrstxsreStructuredText Parser OptionssARecognize and link to standalone PEP references (like "PEP 258").s--pep-referencest store_truetactiont validatorsHBase URL for PEP references (default "http://www.python.org/dev/peps/").s--pep-base-urlstmetavarshttp://www.python.org/dev/peps/tdefaults7Template for PEP file part of URL. (default "pep-%04d")s--pep-file-url-templatespep-%04dsARecognize and link to standalone RFC references (like "RFC 822").s--rfc-referencessDBase URL for RFC references (default "http://tools.ietf.org/html/").s--rfc-base-urlshttp://tools.ietf.org/html/s3Set number of spaces for tab expansion (default 8).s --tab-widthstintttypeis)Remove spaces before footnote references.s--trim-footnote-reference-spaces(Leave spaces before footnote references.s --leave-footnote-reference-spacet store_falsettrim_footnote_reference_spacetdests{Disable directives that insert the contents of external file ("include" & "raw"); replaced with a "warning" system message.s--no-file-insertionitfile_insertion_enabledseEnable directives that insert the contents of external file ("include" & "raw"). Enabled by default.s--file-insertion-enabledsGDisable the "raw" directives; replaced with a "warning" system message.s--no-rawt raw_enableds0Enable the "raw" directive. Enabled by default.s --raw-enabledsqToken name set for parsing code with Pygments: one of "long", "short", or "none (no parsing)". Default is "long".s--syntax-highlighttlongtshorttnonetchoicessshChange straight quotation marks to typographic form: one of "yes", "no", "alt[ernative]" (default "no").s--smart-quotess s4Characters to use as "smart quotes" for . s--smartquotes-localess'tappends±Inline markup recognized at word boundaries only (adjacent to punctuation or whitespace). Force character-level inline markup recognition with "\ " (backslash + space). Default.s--word-level-inline-markuptcharacter_level_inline_markups¼Inline markup recognized anywhere, regardless of surrounding characters. Backslash-escapes must be used to avoid unwanted markup recognition. Useful for East Asian languages. Experimental.s--character-level-inline-markupsrestructuredtext parsertparserscCs4|rd|_n d|_tj|_||_dS(Nt RFC2822BodytBody(t initial_stateRt state_classestinliner(tselftrfc2822R$((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyt__init__©s    cCstj|ƒtjgS(N(Rtget_transformsRt SmartQuotes(R%((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR(±s cCs‹|j||ƒtjd|jd|jd|jjƒ|_tjj |d|j j dt ƒ}|jj ||d|jƒ|jƒdS(s=Parse `inputstring` and populate `document`, a document tree.R#R"tdebugt tab_widthtconvert_whitespaceR$N(t setup_parseRtRSTStateMachineR#R"treportert debug_flagt statemachinetdocutilst string2linestsettingsR+tTruetrunR$t finish_parse(R%t inputstringtdocumentt inputlines((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytparseµs     (RRsrestR R R N(sparsers(t__name__t __module__t__doc__t supportedtNoneRtvalidate_booleantvalidate_url_trailing_slashtvalidate_nonnegative_inttFalsetvalidate_ternarytvalidate_smartquotes_localest settings_spectconfig_sectiontconfig_section_dependenciesR'R(R;(((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyRPsŽ           tDirectiveErrorcBseZdZd„ZRS(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! cCs#tj|ƒ||_||_dS(sSet error `message` and `level`N(t ExceptionR'tleveltmsg(R%RLtmessage((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR'Īs  (R<R=R>R'(((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyRJĆs t DirectivecBs†eZdZdZdZeZd ZeZ d„Z d„Z d„Z d„Z d„Zd„Zd„Zd „Zd „Zd „ZRS( 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. - ``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] ic CsU||_||_||_||_||_||_||_||_| |_dS(N( tnamet argumentstoptionstcontenttlinenotcontent_offsett block_texttstatet state_machine( R%RPRQRRRSRTRURVRWRX((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR'@s        cCstdƒ‚dS(Ns Must override run() is subclass.(tNotImplementedError(R%((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR6LscCs 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. (RJ(R%RLRN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytdirective_errorQs cCs|jd|ƒS(Ni(RZ(R%RN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR*`scCs|jd|ƒS(Ni(RZ(R%RN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytinfocscCs|jd|ƒS(Ni(RZ(R%RN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytwarningfscCs|jd|ƒS(Ni(RZ(R%RN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyterroriscCs|jd|ƒS(Ni(RZ(R%RN((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytseverelscCs&|js"|jd|jƒ‚ndS(se Throw an ERROR-level DirectiveError if the directive doesn't have contents. s:Content block expected for the "%s" directive; none found.N(RSR]RP(R%((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytassert_has_contentqs  cCsnd|jkrjtj|jjdƒƒ}d|kr@|d=n|dj|ƒ|jjj||ƒndS(sAppend self.options['name'] to node['names'] if it exists. Also normalize the name string and register it as explicit target. RPtnamesN(RRRtfully_normalize_nametpopRRWR9tnote_explicit_target(R%tnodeRP((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytadd_namezs   N(R<R=R>trequired_argumentstoptional_argumentsRDtfinal_argument_whitespaceR@t option_spect has_contentR'R6RZR*R[R\R]R^R_Re(((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyROÕs V        cs dtf‡fd†ƒY}|S(sˆ Define & return a directive class generated from `directive_fn`. `directive_fn` uses the old-style, functional interface. tFunctionalDirectivecseeZeˆddƒZeˆdeƒZeˆdddefƒZe\ZZ Z ‡fd†Z RS(RRRSRQic s=ˆ|j|j|j|j|j|j|j|j|jƒ S(N( RPRQRRRSRTRURVRWRX(R%(t directive_fn(s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyR6–sN( R<R=tgetattrR@RiRDRjt_argument_specRfRgRhR6((Rl(s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pyRkŽs (RO(RlRk((Rls?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytconvert_directive_function‡s(R>t __docformat__tdocutils.parsersR2tdocutils.statemachinetdocutils.parsers.rstRRRRtdocutils.transformsRRRRKRJtobjectRORo(((s?/tmp/pip-build-wDUJoH/docutils/docutils/parsers/rst/__init__.pytDs  s²