o f'ha@sdZdZddlZddlZddlmZmZddlmZm Z m Z ddlm Z m Z ddl mZmZddlmZdd lmZddlZdd lmZmZmZmZdd lmZdd lmZmZmZdd lm Z m!Z!ddlm"Z"ddl#Z#Gddde Z$Gddde Z%Gddde Z&Gddde Z'Gddde(Z)Gddde*Z+GdddeZ,GdddeZ-Gdd d eZ.dSd"d#Z/Gd$d%d%e*Z0e1d&d'fd(d)Z2e1d*d'fd+d,Z3d-d.Z4Gd/d0d0e.Z5Gd1d2d2e5Z6Gd3d4d4e5Z7Gd5d6d6e7Z8Gd7d8d8e7Z9Gd9d:d:e7Z:Gd;d<dd>e7ZGdCdDdDe7Z?GdEdFdFe7Z@GdGdHdHe5ZAGdIdJdJe.ZBGdKdLdLeBZCGdMdNdNeCZDGdOdPdPeCZEGdQdRdRe.ZFe5e8e9e:e;ee@eBeDeEeAe6e=fZGdS)Ta This is the ``docutils.parsers.rst.states`` module, the core of the reStructuredText parser. It defines the following: :Classes: - `RSTStateMachine`: reStructuredText parser's entry point. - `NestedStateMachine`: recursive StateMachine. - `RSTState`: reStructuredText State superclass. - `Inliner`: For parsing inline markup. - `Body`: Generic classifier of the first line of a block. - `SpecializedBody`: Superclass for compound element members. - `BulletList`: Second and subsequent bullet_list list_items - `DefinitionList`: Second+ definition_list_items. - `EnumeratedList`: Second+ enumerated_list list_items. - `FieldList`: Second+ fields. - `OptionList`: Second+ option_list_items. - `RFC2822List`: Second+ RFC2822-style fields. - `ExtensionOptions`: Parses directive option fields. - `Explicit`: Second+ explicit markup constructs. - `SubstitutionDef`: For embedded directives in substitution definitions. - `Text`: Classifier of second line of a text block. - `SpecializedText`: Superclass for continuation lines of Text-variants. - `Definition`: Second line of potential definition_list_item. - `Line`: Second line of overlined section title or transition marker. - `Struct`: An auxiliary collection class. :Exception classes: - `MarkupError` - `ParserError` - `MarkupMismatch` :Functions: - `escape2null()`: Return a string, escape-backslashes converted to nulls. - `unescape()`: Return a string, nulls removed or restored to backslashes. :Attributes: - `state_classes`: set of State classes used with `RSTStateMachine`. Parser Overview =============== The reStructuredText parser is implemented as a recursive 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. In the description below, references are made to classes defined in this module; please see the individual classes for details. Parsing proceeds as follows: 1. The state machine examines each line of input, checking each of the transition patterns of the state `Body`, in order, looking for a match. The implicit transitions (blank lines and indentation) are checked before any others. The 'text' transition is a catch-all (matches anything). 2. The method associated with the matched transition pattern is called. A. Some transition methods are self-contained, appending elements to the document tree (`Body.doctest` parses a doctest block). The parser's current line index is advanced to the end of the element, and parsing continues with step 1. B. Other transition methods trigger the creation of a nested state machine, whose job is to parse a compound construct ('indent' does a block quote, 'bullet' does a bullet list, 'overline' does a section [first checking for a valid section header], etc.). - In the case of lists and explicit markup, a one-off state machine is created and run to parse contents of the first item. - A new state machine is created and its initial state is set to the appropriate specialized state (`BulletList` in the case of the 'bullet' transition; see `SpecializedBody` for more detail). This state machine is run to parse the compound element (or series of explicit markup elements), and returns as soon as a non-member element is encountered. For example, the `BulletList` state machine ends as soon as it encounters an element which is not a list item of that bullet list. The optional omission of inter-element blank lines is enabled by this nested state machine. - The current line index is advanced to the end of the elements parsed, and parsing continues with step 1. C. The result of the 'text' transition depends on the next line of text. The current state is changed to `Text`, under which the second line is examined. If the second line is: - Indented: The element is a definition list item, and parsing proceeds similarly to step 2.B, using the `DefinitionList` state. - A line of uniform punctuation characters: The element is a section header; again, parsing proceeds as in step 2.B, and `Body` is still used. - Anything else: The element is a paragraph, which is examined for inline markup and appended to the parent element. Processing continues with step 1. reStructuredTextN) FunctionType MethodType)nodes statemachineutils)ApplicationError DataError)StateMachineWSStateWS)fully_normalize_name)whitespace_normalize_name) directives languages tableparserroles)en) escape2nullunescape column_width)punctuation_chars urischemes)split_escaped_whitespacec@ eZdZdS) MarkupErrorN__name__ __module__ __qualname__rr=/usr/lib/python3/dist-packages/docutils/parsers/rst/states.pyr{ rc@r)UnknownInterpretedRoleErrorNrrrrr r"|r!r"c@r)"InterpretedRoleNotImplementedErrorNrrrrr r#}r!r#c@r) ParserErrorNrrrrr r$~r!r$c@r)MarkupMismatchNrrrrr r%r!r%c@eZdZdZddZdS)Structz3Stores data attributes for dotted-attribute access.cKs|j|dSN)__dict__update)self keywordargsrrr __init__zStruct.__init__N)rrr__doc__r-rrrr r' r'c@seZdZdZ  dddZdS)RSTStateMachinezy reStructuredText's master StateMachine. The entry point to reStructuredText parsing is the `run()` method. rTNc Cst|jj|j|_||_|durt}||jt ||j|jgdd|d|_ ||_ | |j |j j|_||_tj||||dd}|gksLJdd|_|_ dS)z Parse `input_lines` and modify the `document` node in place. Extend `StateMachineWS.run()`: set up parse-global data and run the StateMachine. NrF)documentreporterlanguage title_styles section_levelsection_bubble_up_kludgeinlinersource) input_sourcez.RSTStateMachine.run() results should be empty!)r get_languagesettings language_coder3r4 match_titlesInlinerinit_customizationsr'memor2attach_observer note_sourcenoder run)r+ input_linesr2 input_offsetr>r8resultsrrr rEs0     zRSTStateMachine.run)rTNrrrr/rErrrr r1s r1c@seZdZdZdddZdS)NestedStateMachinezh StateMachine run from within other StateMachine runs, to parse nested document structures. TcCsZ||_||_|j|_||jj|j|_|j|_||_t |||}|gks+Jd|S)z Parse `input_lines` and populate a `docutils.nodes.document` instance. Extend `StateMachineWS.run()`: set up document-wide data. z1NestedStateMachine.run() results should be empty!) r>rAr2rBrCr3r4rDr rE)r+rFrGrArDr>rHrrr rEszNestedStateMachine.runNTrIrrrr rJsrJc@seZdZdZeZgZd ddZddZddZ d d Z d d Z  d!ddZ d idd d fddZ ddZddZddZddZddZddZddZd S)"RSTStatez` reStructuredText State superclass. Contains methods used by all State subclasses. FcCstdd|_t|||dS)NBody state_classes initial_state)rOnested_sm_kwargsr r-r+ state_machinedebugrrr r-szRSTState.__init__cCsZt||jj}||_|j|_|j|_|j|_|jj|_t |jds+|jj |j_ dSdS)Nget_source_and_line) r runtime_initrSrAr3r8r2rDparenthasattrrU)r+rArrr rVs   zRSTState.runtime_initcCs(z |j|WdStyYdSw)zT Jump to input line `abs_line_offset`, ignoring jumps past the end. N)rS goto_lineEOFError)r+abs_line_offsetrrr rYs  zRSTState.goto_linecCs*|jd|jj|||jjf|dgfS)zs Override `StateWS.no_match` to generate a system message. This code should never be run. zjInternal error: no transition pattern match. State: "%s"; transitions: %s; context: %s; current line: %r.N)r3severe __class__rrSline)r+context transitionsrrr no_matchs  zRSTState.no_matchcCsggfS)zCalled at beginning of file.rr+r_rrr bofsz RSTState.bofNc Csd}|dur |j}|d7}|dur|j}|d7}t|}d} |dkr3z|j} Wn ty2Ynw| s?|dd|ji|} | j|||j||d|dkrU|j | n| | } |j rrt||dkrr|j t||| S)zg Create a new StateMachine rooted at `node` and run it over the input `block`. rNrTrArDr>r) nested_smrQlennested_sm_cachepop IndexErrorrTrErAappendunlinkr[rWrS next_line) r+blockrGrDr>state_machine_classstate_machine_kwargs use_default block_lengthrS new_offsetrrr nested_parses:   zRSTState.nested_parsec Cs| dur|j} | dur|j} || d<| dd|ji| } |dur$|}|| j|_|D] \} } t| j|| | q.| j|||j ||d| j|j}| | |fS)z Create a new StateMachine rooted at `node` and run it over the input `block`. Also keep track of optional intermediate blank lines and the required final one. NrPrTrfr) rgrQcopyrTstates blank_finishitemssetattrrErArmr[)r+rorGrDrPrxblank_finish_stateextra_settingsr>rprqrSkeyvaluerrr nested_list_parse's&       zRSTState.nested_list_parsecCs$||||r||||dSdS)z=Check for a valid subsection and create one if it checks out.N)check_subsectionnew_subsection)r+titler9stylelinenomessagesrrr sectionEszRSTState.sectionc Cs|j}|j}|j}z ||d}Wn$ty6t||jkr(||YdS|j|||7_YdSw||krS||_t|dkrGd|_ |j t|dt ||dkr[dS|j|||7_dS)a Check for a valid subsection header. Return 1 (true) or None (false). When a new section is reached that isn't a subsection of the current section, back up the line count (use ``previous_line(-x)``), then ``raise EOFError``. The current StateMachine will finish, then the calling StateMachine can re-examine the title. This will work its way back up the calling chain until the correct section level isreached. @@@ Alternative: Evaluate the title, store the title info & level, and back up the chain until that level is reached. Store in memo? Or return in results? :Exception: `EOFError` when a sibling or supersection encountered. rdNreT) rAr5r6index ValueErrorrhrlrWtitle_inconsistentr7rS previous_linerZ)r+r9rrrAr5mylevellevelrrr rJs,    zRSTState.check_subsectioncCs|jjdtd||d}|S)NzTitle level inconsistent:r^)r3r\r literal_block)r+ sourcetextrerrorrrr rss  zRSTState.title_inconsistentcCs|j}|j}|jd7_t}|j|7_|||\}}tj|dg|R} t| } |d | || 7}||7}||7}|j |||j j d} |j d} |j|j j| d| |dd} || |j|krrt||_dS)z?Append new subsection to document tree. On return, check level.rdrnamesNT)rGrDr>)rAr6rrrW inline_textrnormalize_nameastextrlr2note_implicit_targetrS line_offsetr[rurFrYrZ)r+rrrrAr section_node textnodestitle_messages titlenodenameoffset absoffset newabsoffsetrrr rys.     zRSTState.new_subsectionc Csd|}td|r/t|dkrgdfS|ddvr&|dd}n|dd}d}n|}d }|||\}}tj|d g|R}|j |\|_ |_ |g||fS) zW Return a list (paragraph & messages) & a boolean: literal_block next?  z(?%(or_group)s)%(suffix)s) isinstancetuplerl build_regexprlocalsrcompileUNICODE) definitionrrprefixsuffixparts part_stringspartor_groupregexprrr rs     rc @seZdZdZddZddZddZdZd Zd Z d Z d Z d Z dZ dZdeZdZdZddZ d=ddZddZddZddZddZd>d!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd=d/d0Z d1d2Z!d3d4Z"d5d6Z#d7Z$d8d9Z%d:d;Z&eeeeeeee e!d< Z'd S)?r?z9 Parse inline markup; call the `parse()` method. cCs g|_dSr()implicit_dispatch)r+rrr r-szInliner.__init__cCs2t|ddr d}d}ndtjtjf}dtjtjtjf}t}|t |j d|ddd|j gd fd d|d |j d d ddd|j dd|j gfgfdd|j |j dgfgf}||_ ||_||_tt|t|jd|tjt|jd|tjtd|tjtjBtd|tjtjBt|jd|tjt|jd|tjt|jd|tjt|j|dtjtjBtd|jd|tjtjBtd |tjtjBtd!|tjtjBd" |_|j|jj|jf|jr|j|jj|j f|j!r|j|jj"|j#fdSdS)#Ncharacter_level_inline_markupFz (^|(?%s)(?P__?) footnotelabelz\[z(?P\]_)[0-9]+z\#(%s)?z\*z(?P%s) backquotez(?P(:%s:)?)z`(?!`)z(\*)z(\*\*)a %(non_unescaped_whitespace_escape_before)s ( ` (?P (?P:%(simplename)s:)? (?P__?)? ) ) %(end_string_suffix)s a ( (?:[ \n]+|^) # spaces or beginning of line/string < # open bracket %(non_whitespace_after)s (([^<>]|\x00[<>])+) # anything but unescaped angle brackets %(non_whitespace_escape_before)s > # close bracket ) $ # end of string z(``)z(`)z (\|_{0,2})$a5 %(start_string_prefix)s (?P (?P # absolute URI (?P # scheme (http, ftp, mailto) [a-zA-Z][a-zA-Z0-9.+-]* ) : ( ( # either: (//?)? # hierarchical URI %(uric)s* # URI characters %(uri_end)s # final URI char ) ( # optional query \?%(uric)s* %(uri_end)s )? ( # optional fragment \#%(uric)s* %(uri_end)s )? ) ) | # *OR* (?P # email address z] ) ) %(end_string_suffix)s a %(start_string_prefix)s ( (pep-(?P\d+)(.txt)?) # reference to source file | (PEP\s+(?P\d+)) # reference by name ) %(end_string_suffix)sz{ %(start_string_prefix)s (RFC(-|\s+)?(?P\d+)) %(end_string_suffix)s) initialemphasisstronginterpreted_or_phrase_ref embedded_linkliteraltargetsubstitution_refemailuripeprfc)$getattrropeners delimitersclosing_delimitersclosersrrvr*varsr]non_whitespace_after simplenamestart_string_prefixend_string_suffixrr'rrrnon_whitespace_escape_beforerVERBOSEnon_whitespace_before email_patternpatternsrrlrstandalone_uripep_referencesr pep_referencerfc_referencesr rfc_reference)r+r<rrargsrrrr r@s                V   zInliner.init_customizationscCs|j|_|j|_|j|_||_|jjj}|j}t|}g}g} g} |ri||} | rf| } || dp=| dp=| dp=| d} | || |\}}}}| || |7} |re|| d | |7}||7}g} nn|s#d | |}|rz|| ||7}|| fS)a Return 2 lists: nodes (text and inline elements), and system_messages. Using `self.patterns.initial`, a pattern which matches start-strings (emphasis, strong, interpreted, phrase reference, literal, substitution reference, and inline target) and complete constructs (simple reference, footnote reference), search for a candidate. When one is found, check for validity (e.g., not a quoted '*' character). If valid, search for the corresponding end string if applicable, and check it for validity. If not found or invalid, generate a warning and ignore the start-string. Implicit inline markup (e.g. standalone URIs) is found last. rrrefendfnendr) r3r2r4rWrrrdispatchr groupdictrlimplicit_inliner)r+rrrArWpattern_searchr remaining processed unprocessedrmatchgroupsmethodbeforeinlines sysmessagesrrr rgsJ  z Inliner.parsez(?]z[_~*/=+a-zA-Z0-9]z-(?:%(urilast)s|%(uric)s(?=%(uri_end_delim)s))z"[-_!~*'{|}/#?^`&=+$%a-zA-Z0-9\x00]z %(emailc)s+(?:\.%(emailc)s+)* # name (?6sz%Inliner.phrase_ref..refurizproblem with embedded link: %rr__r#r, anonymousr)rrrrrrendswithrrrrrr indirect_reference_namerr adjust_uri referencedr referencer r2 note_refnamerlnote_indirect_targetnote_explicit_targetrW)r+rafterr r rr unescapedrawtext aliastext rawaliastextunderscore_escaped aliastypealiasr alias_partsr#r4 node_listrrr r!s                  zInliner.phrase_refcCs|jj|}|r d|S|S)Nmailto:)rrr)r+rrrrr r2lszInliner.adjust_uric Csjt||j||j\}}|r||||||\}}|||fS|jjd||d} |||| g|| gfS)Nz#Unknown interpreted text role "%s".r)rrr4r3rr) r+r rrrrole_fnrr messages2r rrr rss  zInliner.interpretedcCs0|j|||jjtjdd\}}}}}||||fS)NT)r)rrrrrrrr rs  zInliner.literalc Cs||||jjtj\}}}}}|r;t|dtjr;t|dks"J|d}t|} |d| |j ||j ||||fS)Nrrdr) rrrrrrhrrrlr2r7rW) r+rrrrrrrrrrrr inline_internal_targets  zInliner.inline_internal_targetc Cs||||jjtj\}}}}}t|dkr]|d}t|tjr]|} |j || |dddkr]t d| |fd} |dddkrJd| d <n t | | d <|j | | |7} | g}||||fS) Nrdrrrz|%s%srr*r.r/r#) rrrrsubstitution_referencerhrrr2note_substitution_refr4rr5) r+rrrrrrr subref_node subref_textreference_noderrr rFs(        zInliner.substitution_referencec Cs|d}t|}|j}|d|d}||dd}|dr:tjd||d}|t|7}|j |nMt d|}|ddkrX|d d}d |d <|j |n|d krid }d |d <|j |n|t|7}|r|||d <|j |t|jjr|}||g|gfS)ze Handles `nodes.footnote_reference` and `nodes.citation_reference` elements. rNr citationlabelz[%s]_r"r#rdauto*rr#)rrrrrrcitation_referenceTextr2note_citation_reffootnote_referencenote_autofootnote_refnote_symbol_footnote_refnote_footnote_refrget_trim_footnote_ref_spacer<r) r+rrlabelr#rrrrefnoderrr rRs:      zInliner.footnote_referencec Cs|d}t|}tj||d|t|d}||d_|r$d|d<n ||d<|j||j}| d}| d} |d||g|| dgfS)Nr#rr-rrdr/r) rrrr4r r r2r5rrr) r+rrr/ referencenamer# referencenoderrr rrr r4s      zInliner.referencecCs|j||ddS)Nrd)r/)r4)r+rrrrr anonymous_referencer.zInliner.anonymous_referencecCsh|dr|dtjvr2|drd}nd}|d}|t|}tjt|d||d}|gSt)NschemerrBrrTr+)rlowerrschemesrrr4r%)r+rr addschemerr,r4rrr rs    zInliner.standalone_uricCsz|d}|drtt|d}n|dr#tt|d}nt|jjj|jjj|}t j t|d||dgS)Nrzpep-pepnum1PEPpepnum2Tr+) r startswithintrr%r2r< pep_base_urlpep_file_url_templaterr4)r+rrrpepnumrefrrr rs    zInliner.pep_referencez rfc%d.htmlcCsX|d}|drtt|d}|jjj|j|}ntt j t|d||dgS)NrRFCrfcnumTr+) rrcrdrr2r< rfc_base_urlrfc_urlr%rr4)r+rrrrjrhrrr rs  zInliner.rfc_referencec Cs|sgS|jD]5\}}||}|rZ)d?d@Z*dAdBZ+dCdDZ,ddFdGZ-dHdIZ.dJdKZ/dLdMZ0dNdOZ1dPdQZ2dRdSZ3dTdUZ4dVdWZ5dXdYZ6dZd[Z7d\d]Z8d^d_Z9d`daZ:dbdcZ;dddeZdjdkZ?dldmZ@dndoZAddpdqZBddrdsZCdtduZDeZE eedveFeGejHejBedweFeGejHejBedxeFeGejHejBdyeE_"dzd{ZId|d}ZJd~dZKddZLddZMddZNddZOddZPddZQddZRddZSddZTddZUddZVddZWddZXddZYeIedeGjZejHejBfeJedeGjZejHejBfeKedejHejBfePedejHejBfeRedeGjZejHejBfgeE_[ddZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbdES)rMz: Generic classifier of the first line of a block. ()rdr)rrrrrr.)parensrparenperiod)arabic loweralpha upperalpha lowerroman upperromanrz[a-z]z[A-Z]z [ivxlcdm]+z [IVXLCDM]+rz\+-[-+]+-\+ *$z =+( +=+)+ *$z=+[ =]*$z[!-/:-@[-`{-~]nonalphanum7bitz[a-zA-Z]alphaz [a-zA-Z0-9]alphanumz [a-zA-Z0-9_-] alphanumpluszJ(%(arabic)s|%(loweralpha)s|%(upperalpha)s|%(lowerroman)s|%(upperroman)s|#)enumz%(alphanum)s%(alphanumplus)s*optnamez%(%(alpha)s%(alphanumplus)s*|<[^<>]+>)optargz!(-|\+)%(alphanum)s( ?%(optarg)s)?shortoptz"(--|/)%(optname)s([ =]%(optarg)s)?longoptz(%(shortopt)s|%(longopt)s)optionz(?P<%s>%s%s%s)u[-+*•‣⁃]( +|$)z((%(parens)s|%(rparen)s|%(period)s)( +|$)z1:(?![: ])([^:\\]|\\.|:(?!([ `]|$)))*(?>>( +|$)z\|( +|$)z \.\.( +|$)z__( +|$)z(%(nonalphanum7bit)s)\1* *$) bullet enumerator field_marker option_markerdoctest line_blockgrid_table_topsimple_table_topexplicit_markupr/r^rc CsN|j\}}}}|||}|j|7_|s"|j|d7_||gfS)z Block quote.z Block quote)rS get_indented block_quoterWr) r+rr_ next_stateindentedindentrrxelementsrrr rs  z Body.indentc Csg}|rK|||\}}}}}t}|||||||r1|||\} } || 7}|| 7}|}|rI|dsI|dd}|d7}|rI|dr9|s|S)Nrrd)split_attributionrrrurlparse_attribution) r+rrrblockquote_linesattribution_linesattribution_offsetnew_line_offset blockquote attributionrrrr rs0     zBody.block_quoteu(---?(?!-)|—) *(?=[^ \n])c Csd}d}tt|D]R}||}|rZ|rW||dkrW|j|}|rW|||\}} |rW|||} | j|dd| j| dd|d|| |||d||fSd}q |}q |ddddfS)a Check for a block quote attribution and split it off: * First line after a blank line must begin with a dash ("--", "---", em-dash; matches `self.attribution_pattern`). * Every line after that must have consistent indentation. * Attributions must be preceded by block quote content. Return a tuple of: (block quote content lines, content offset, attribution lines, attribution offset, remaining indented lines). NFrd)r)rT)rangerhrattribution_patternrcheck_attribution trim_leftr) r+rrblank nonblank_seenir^rattribution_endra_linesrrr rs,      zBody.split_attributioncCsd}|d}t|dt|D]*}||}|sn#|dur*t|t|}qt|t||kr9dSq|d7}||pBdfS)zt Check attribution shape. Return the index past the end of the attribution, and the indent. Nrd)NNr)rrhrlstrip)r+rattribution_startrrr^rrr rs  zBody.check_attributioncCs\d|}|j|}|||\}}tj|dg|R}|j|\|_|_ ||fS)Nrr) rrrSrrrrrUr9r^)r+rrrrrrrDrrr rs zBody.parse_attributionc Cst}|j\|_|_|j|7_|jd|d<|| \}}||7}|jj d}|j |jj |d|j d|d|d\}}|||sV|j|d7_g|gfS)Bullet list item.rrrdN BulletListrGrDrPrxz Bullet list)r bullet_listrSrUr9r^rWr list_itemrrrrFr[rYr) r+rr_r bulletlistrrxrrrrr rs&    z Body.bulletcCsd|jj|dr|j|\}}}n |j|\}}}}td|}|r.|j|||d||fS)NrrGrD)rSr^get_known_indentedget_first_known_indentedrrrru)r+rrrrxlistitemrrr rs   zBody.list_itemc Cs>||\}}}}||||stdt}|j|7_|dkr)d|d<n||d<|jj|j |d<|jj|j |d<|dkrX||d<|j d ||f} |j| 7_| |\} } || 7}|jjd} |j|jj| d |jd|d | |||dkd d \} } || | s|j|d7_g|gfS)zEnumerated List ItemrrLrenumtyperrrdrz|}Wnt yLtd|w|dkrTd}n|dkrZd}|sr|jj D] }|jj||rmnq`td |dkryd }nz |jj ||}Wn t j yd }Ynw||||fS) aA Analyze an enumerator and return the results. :Return: - the enumerator format ('period', 'parens', or 'rparen'), - the sequence used ('arabic', 'loweralpha', 'upperroman', etc.), - the text of the enumerator, stripped of formatting, and - the ordinal value of the enumerator ('a' -> 1, 'ii' -> 2, etc.; ``None`` is returned for invalid enumerator text). The enumerator format has already been determined by the regular expression match. If `expected_sequence` is given, that sequence is tried first. If not, we check for Roman numeral 1. This way, single-character Roman numerals (which are also alphabetical) can be matched. If no sequence has been matched, all sequences are checked in order. rzenumerator format not matchedrLzunknown enumerator sequence: %srrIrzenumerator sequence not matchedrdN)rrformatsr$rrrsequenceregexpsrKeyError sequences convertersrInvalidRomanNumeralError)r+rexpected_sequencerrrrrrrr r1sP     zBody.parse_enumeratorcCs|durdSz|j}Wnty|jYdSw|j|dds,dS||d||}|rV|\}}z||sF||rIWdSWdStyUYdSwdS)z Check validity based on the ordinal value and the second line. Return true if the ordinal is valid and the second line is blank, indented, or starts with the next enumerator or an auto-enumerator. Nrd)rSrnrZrstripmake_enumeratorrc TypeError)r+rrrrnresultnext_enumeratorauto_enumeratorrrr rhs4     zBody.is_enumerated_list_itemcCs|dkrd}nW|dkrt|}nN|dr&|dkrdSt|tdd}n|dr>zt|}Wntjy=YdSwtd ||d rN| }n|d rX| }ntd ||j j |}|j ||jd }|j d|jd }||fS) z Construct and return the next enumerated list item marker, and an auto-enumerator ("#" instead of the regular enumerator). Return ``None`` for invalid (out of range) ordinals. rLrrNrurdrz!unknown enumerator sequence: "%s"r]rr$)strr0chrrxrtoRoman RomanErrorr$rcr]rrrrr)r+rrrrrrrrrr rs<        zBody.make_enumeratorc Cst}|j|7_||\}}||7}|jjd}|j|jj|d|jd|d|d\}}| ||sD|j| d7_g|gfS)zField list item.rdN FieldListrz Field list) r field_listrWfieldrSrrrFr[rYr) r+rr_rrrrxrrrrr rs    zBody.field_markercCs||}|j\}}|j}|j|\}}}} t} || _|| _ | ||\} } | tj |dg| R7} tj d |g| R} | | 7} |rT|||| | | fS)Nrr)parse_field_markerrSrUrrrrrr9r^r field_name field_bodyrparse_field_body)r+rrsrcsrclinerrrrrx field_node name_nodes name_messagesrrrr rs   z Body.fieldcCs&|dd}|d|d}|S)z6Extract & return field name from a field marker match.rdN:)rrfind)r+rrrrr rszBody.parse_field_markercCs|j|||ddS)Nr)ru)r+rrrDrrr rszBody.parse_field_bodyc Cs@t}|j\|_|_z ||\}}WnKtya}z?|j d|}|j |7_ |j | \} } } }| | | } |j | 7_ |sR|j |d7_ g|gfWYd}~Sd}~ww|j |7_ ||7}|jjd} |j|jj| d|jd|d|d\}}|||s|j |d7_ g|gfS)Option list item.zInvalid option list marker: %sz Option listNrd OptionListr)r option_listrSrUr9r^option_list_itemrr3rrWrrrrrrrFr[rY)r+rr_r optionlistrrxrr rrrrrrrrr rs>      zBody.option_markerc Cs|j}||}|j|\}}}}|s"||tdtj dg|R}t d |} t d|| } |rD|j ||| d| |fS)Nrrrr)rSr[parse_option_markerrrrYrrr option_group descriptionrrru) r+rroptionsrrrrxrrrrrr rs"     zBody.option_list_itemc Csg}|d}|D]}|}d}|ddd}t|dkr,||dd<d}n3t|ddkr_|ddrB|dd rI|dd r_|ddd|dddg|dd<d }t|dkr|dd r|d drd|ddg|dd<dt|krdkrnn+t|}|t |d|d7}t|dkr|tj |d|d|d7}| |q t dt||f|S)z Return a list of `node.option` and `node.option_argument` objects, parsed from an option marker match. :Exception: `MarkupError` for invalid option markers. z, r$r=rdNre-z--+r) delimiterz;wrong number of option tokens (=%s), should be 1 or 2: "%s") rrr&rhrcr0rrr option_stringoption_argumentrlr) r+roptlist optionstrings optionstringtokensrfirstoptrrrr rsH     (     zBody.parse_option_markercCs0d|j}|jt||7_g|gfS)Nr)rrSget_text_blockrWr doctest_block)r+rr_rrrrr r.s z Body.doctestc Cst}|j|7_|j}|||\}}}||7}|j|7_|sH|jjd} |j|jj| d|j d|ddd\} }| | |sY|j|j j d|dd7_t |rn|djdurid|d_||g|gfS)zFirst line of a line block.rdN LineBlockrrz%Line block ends without a blank line.r)rrrWrSrline_block_linerrrFr[rYr3rrhrnest_line_block_lines) r+rr_rrorr^rrxrrrrr r6s2         zBody.line_blockc Csv|jj|dd\}}}}d|}|||\}} tj|dg|R} |jdkr6t | dd| _ | | |fS)z(Return one line element of a line_block.T until_blankrrrrd) rSrrrrrr^rrrhrr) r+rrrrrrxr text_nodesrr^rrr r Ps   zBody.line_block_linecCsJtdt|D]}t||dddur||dj||_q||dS)Nrdr)rrhrrnest_line_block_segment)r+rorrrr r\s zBody.nest_line_block_linescCsdd|D}t|}g}t}|D]$}|j|kr ||qt|r2||||t}||qt|rF||||||dd<dS)NcSsg|]}|jqSr)r)r(itemrrr csz0Body.nest_line_block_segment..)minrrrrlrhr)r+roindentsleast new_items new_blockrrrr rbs        zBody.nest_line_block_segmentcC|||||jtjS)zTop border of a full table.) table_topisolate_grid_tablerGridTableParserr+rr_rrrr ru zBody.grid_table_topcCr)zTop border of a simple table.)risolate_simple_tablerSimpleTableParserrrrr r{rzBody.simple_table_topc CsT|||\}}|j|7_|s%|jjd|jdd}|j|7_g|gfS)zTop border of a generic table.z Blank line required after table.rdr)tablerWr3rrSr) r+rr_risolate_function parser_classr!rxr rrr rs  zBody.table_topc Cs|\}}}|rQz#|}||}|jt|d}|||} | g|} W| |fStjyP} z|j|d| j | j d|} WYd} ~ | |fSd} ~ ww|} | |fS)zParse a table.rdr$)rN) rrSrrh build_tablerTableMarkupErrormalformed_tablerrr) r+r#r$rorrxparser tabledata tableliner"r!errrrr r"s,    z Body.tablec Csg}d}z |jjdd}Wn'tjy4}z|j\}}}||jjd||dd}WYd}~nd}~ww|| |j t |d }t t |D]%}|| ||<||ddvrrd}|jt ||||d=nqM|j|dsd}t t |d ddD]}|j||r|jt ||d||dd=nq|||g||fSt t |D]!}t |||ks||ddvr|||g||fSq|||fS) NrdT flush_leftUnexpected indentation.r9r^rz+|rre)rSr rUnexpectedIndentationErrorrrlr3r disconnectpad_double_widthdouble_width_pad_charrhrrrgrid_table_top_patrextendr') r+rrxror+rrwidthrrrr rsN       zBody.isolate_grid_tablecCs|jj}|jj}t|d}t||}|jj}d}d}|d}||kr}||} || } | rut| |kr\|j||||||dd} g| ||kpZ||d fS|d7}|}|dksr||ksr||dsu|} n=|d7}||ks&|rd} |j|||||d}nd} |j||d||d}||d| } g| | fS|j| |||| d}| |j |g| |kp|| d fS)Nrdrz5Bottom/header table border does not match top border.rez$ or no blank line after table bottomrzNo bottom table border found%s.) rSrrFrhrsimple_table_border_patrrnr'r2r3)r+rrlimittoplen pattern_matchfoundfound_atrr^rrrextrarorrr r sP       zBody.isolate_simple_tablecCsf||jdd|}d}|jt|d}|r!|d|7}|jj|t ||||d}|gS)NrrzMalformed table.rdr) replacer3rrSrrhr3rrr)r+rodetailrrr startlinerrrr r's  zBody.malformed_tablecCs|\}}}t}|dkr|ddg7<n |r"|ddg7<tjt|d} || 7}|D]} tj| d} |rCd| jd<|d8}| | 7} q0|r_t} | | 7} |D] } | || |7} qTt}| |7} |D] } ||| |7}qi|S) NrMclasseszcolwidths-autozcolwidths-given)cols)colwidthrdstub) rr"tgrouprhcolspec attributestheadbuild_table_rowtbody)r+r)r* stub_columnswidths colwidthsheadrowsbodyrowsr"rErCrFrHrowrJrrr r%s0    zBody.build_tablec Cs~t}|D]6}|dur q|\}}}}i} |r|| d<|r!|| d<tjdi| } || 7}d|r<|j|||| dq|S)Nmorerowsmorecolsrrr)rrPentryrru) r+rowdatar*rPcellrQrRr cellblockrGrSrrr rIs$   zBody.build_table_rowa? ( _ # anonymous target | # *OR* (?!_) # no underscore at the beginning (?P`?) # optional open quote (?![ `]) # first char. not space or # backquote (?P # reference name .+? ) %(non_whitespace_escape_before)s (?P=quote) # close quote if open quote used ) (?%(simplename)s)_ | # *OR* ` # open backquote (?![ ]) # not space (?P.+?) # hyperlink phrase %(non_whitespace_escape_before)s `_ # close backquote, # reference mark ) $ # end of string a ( (?![ ]) # first char. not space (?P.+?) # substitution text %(non_whitespace_escape_before)s \| # close delimiter ) ([ ]+|$) # followed by whitespace )rr4 substitutionc Cs |j\}}|j|\}}}}|d}t|} td|} || _ || _ | ddkrJ| dd} d| d<| rC| d | |j | n&| dkr[d} d| d<|j | n| td|7} | d | |j | | rz|j | | n|j | | |r|j||| d | g|fS) NrdrrrLrMrrNrr)rSrUrrrrrfootnoterr9r^rlr2note_autofootnotenote_symbol_footnoterW note_footnoter7rru) r+rrrrrrrxrWrrXrrr rXXs6     z Body.footnotec Cs|j\}}|j|\}}}}|d}t|} td|} || _ || _ | t d|7} | d | |j | |j | | |rP|j||| d| g|fS)Nrdrrrr)rSrUrrrrrcitationrr9r^rWrlr2 note_citationr7ru) r+rrrrrrrxrWrr\rrr r\ws   z Body.citationc Cs|jjj}|j}|jj|ddd\}}}}|jd|d|}dd|D}|d} d} | | } | r=n| d7} z| || 7} Wn t yTt d wq5|d| =|dd | t | dd |d<||||| d } | g|fS) NTF)r strip_indentrcSg|]}t|qSrrr(r^rrr rz)Body.hyperlink_target..rrdzmalformed hyperlink target.r$r)explicitrrrSrrrrrrrkrrhr make_targetr) r+rrmrrorrrx blocktextr  blockindex targetmatchrrrr hyperlink_targets6       ,  zBody.hyperlink_targetcCs||||\}}|dkr*tj|dt|d}||_||d|||j||S|dkr>t|d}||||||S|S)Nr#rr"r,) parse_targetrrrr1 add_targetr2r6)r+ro block_textr target_name target_typerrrrr rds  zBody.make_targetcCsp|r#|ddddkr#ddd|D}||}|r#d|fStd|}ddd |D}d |fS) z Determine the type of reference of a target. :Return: A 2-tuple, one of: - 'refname' and the indirect reference name - 'refuri' and the URI - 'malformed' and a system_message node rNrr$cSsg|]}|qSr)rrarrr rrbz%Body.parse_target..r#css"|] }dt|VqdSr%)rrr&r'rrr r)sz$Body.parse_target..r,)rr is_referencer)r+rorkrr4r# ref_partsrrr ris   zBody.parse_targetcCs4|jjjt|}|sdSt|dp|dS)Nsimplephrase)rcrr4rr rr)r+r4rrrr rns zBody.is_referencecCs||_|r1tt|}|d||r'|j|}|r!||d<ntd||j||j dS|r7||d<d|d<|j |dS)Nrr,zproblem with URI: %rrdr/) r^rrrlr8r2rr2r7rWnote_anonymous_target)r+ targetnamer,rrrrrrr rjs    zBody.add_targetcCs|jjj}|j\}}|jj|dd\}}}}|jd|d|} | t |d } d} | | } | r?n| d7} z| dt ||  } Wn ty\tdwq7|d| =|d d| t| dd |d<|ds|d=|d7}|r|d  s||r|d  r| d } t| }||_||_|s|jjd | t| | ||d }|g|fS|d |d<|d t| |j|||d|d\}}d}|ddD]!}t|tjst|tjs|j ||7_ ||=q|d7}q|!tj"D],}|#|r8td|$ }|jj%d|j&|t| | ||d }|g|fSq t|dkrV|jjd| t| | ||d }|g|fS|j'(|| |j |g|fS)NFr^rrTrdr$z"malformed substitution definition.rrz.Substitution definition "%s" missing contents.r/rSubstitutionDefrrz6Substitution definition contains illegal element <%s>:z.Substitution definition "%s" empty or invalid.))rcrrWrSrUrrrrr1rrrrrkrrhrjrrsubstitution_definitionr9r^r3rrrlr rrInlinerPrWtraverseElement*disallowed_inside_substitution_definitionspformatrtagnamer2note_substitution_def)r+rrmrrrorrrxrer rf subdefmatchsubnamesubstitution_noder new_abs_offsetrrDr{rrr substitution_defs      ,            zBody.substitution_defcCs<|dst|tjr|dst|tjr|drdSdS)Nidsr/rMTF)rrr4getrR)r+rDrrr rz"s  z/Body.disallowed_inside_substitution_definitionscKsN|d}t||jj|j\}}|j|7_|r"|||||S||S)z?Returns a 2-tuple: list of nodes, and a "blank finish" boolean.rd) rr directiverAr4r2rW run_directiveunknown_directive)r+roption_presets type_namedirective_classrrrr r*s   zBody.directivec Cst|ttfrddlm}||}|j}|jj}|jj| dd\}} } } d |jj ||jjd} z| || ||\} }}}Wn,t yq}z |jjd|d |jft| | |d}|g| fWYd }~Sd }~ww||| ||||| ||j }z|}Wn+tjjjy}z|jj|j|j|d}|t| | 7}|g}WYd }~nd }~wwt|tsJd |tt|D]}t||tjsJd ||||fq|| p|jfS) a Parse a directive then run its directive function. Parameters: - `directive`: The class implementing the directive. Must be a subclass of `rst.Directive`. - `match`: A regular expression match object which matched the first line of the directive. - `type_name`: The directive name, as used in the source text. - `option_presets`: A dictionary of preset options, defaults for the directive options. Currently, only an "alt" option is passed by substitution definitions (value: the substitution name), which may be used by an embedded image directive. Returns a 2-tuple: list of nodes, and a "blank finish" boolean. r)convert_directive_function) strip_toprrdzError in "%s" directive: %s.r$rNz+Directive "%s" must return a list of nodes.z6Directive "%s" returned non-Node object (index %s): %r) rrrdocutils.parsers.rstrrSrrrrrrFparse_directive_blockrr3rrrrrEdocutilsparsersrstDirectiveErrorsystem_messagerr listrrhNodeis_next_line_blank)r+rrrrrrinitial_line_offsetrrrrxrk argumentsrcontentcontent_offsetr?rdirective_instancermsg_noderrrr r6sj            zBody.run_directivecCsr|j}|j}|r|ds||d7}|r*|ds*||r*|dr|r\|js4|js4|r\t|D] \}}|sBnq8|d7}|d|} ||dd} ||d} n|} |} g} |rn|||| \} } ni} | r|js|js| ||d} |} g} | r| ds| | d7} | r| dr|js|jr| || } ng} | r|st d| | | | fS)Nrrdrzno content permitted) option_spec has_contentr trim_starttrim_endrequired_argumentsoptional_arguments enumerateparse_directive_optionsparse_directive_argumentsr)r+rrrrrrrr^ arg_blockrrrrrrr rrs^      zBody.parse_directive_blockc Cs|}t|D]\}}ttjd|r#||d}|d|}nqg}|r?|||\}} |r;|| ||fSt| ||fS)Nr) rvrrrrMrparse_extension_optionsr*r) r+rrrrrr^ opt_blocksuccessrrrr rs"   zBody.parse_directive_optionscCs|j}|j}d|}|}t||krtd|t|ft|||krB|jr6|d||d}|Std||t|f|S)Nrz$%s argument(s) required, %s suppliedrdz+maximum %s argument(s) allowed, %s supplied)rrrr&rhrfinal_argument_whitespace)r+rrrequiredoptionalarg_textrrrr rs$   zBody.parse_directive_argumentsc Cst}|j|d|ddd\}}|t|krdSzt||}WnTty;}zdd|jdfWYd}~Sd}~wtt fyX}zddd |jfWYd}~Sd}~wtj yt}zdd d |jfWYd}~Sd}~ww|r{d |fSd S) a Parse `datalines` for a field list containing extension options matching `option_spec`. :Parameters: - `option_spec`: a mapping of option name to conversion function, which should raise an exception on bad input. - `datalines`: a list of input strings. :Return: - Success value, 1 or 0. - An option dictionary on success, an error string on failure. rExtensionOptionsT)rPrx)rzinvalid option blockzunknown option: "%s"Nzinvalid option value: %sr$zinvalid option data: %srd)rzoption data incompletely parsed) rrrrhrextract_extension_optionsrrrrrExtensionOptionError)r+r datalinesrDrrxrr?rrr rs,   zBody.parse_extension_optionsc CsT|j}|jjddd\}}}}d|}|jjd|t|||d}|g|fS)NrFrtrzUnknown directive type "%s".r)rSrrrr3rrr) r+rrrrrrxrrrrr rs    zBody.unknown_directivecCs|j|ds|jrtgdfS|j|\}}}}|r7|ds7||r7|dr+d |}t||g|fS)Nrdrr) rrrrSrrcommentrrr)r+rrrrrxrrrr rs  z Body.commenta \.\.[ ]+ # explicit markup start \[ ( # footnote label: [0-9]+ # manually numbered footnote | # *OR* \# # anonymous auto-numbered footnote | # *OR* \#%s # auto-number ed?) footnote label | # *OR* \* # auto-symbol footnote ) \] ([ ]+|$) # whitespace or end of line z \.\.[ ]+ # explicit markup start \[(%s)\] # citation label ([ ]+|$) # whitespace or end of line z \.\.[ ]+ # explicit markup start _ # target indicator (?![ ]|$) # first char. not space or EOL z \.\.[ ]+ # explicit markup start \| # substitution indicator (?![ ]|$) # first char. not space or EOL aK \.\.[ ]+ # explicit markup start (%s) # directive name [ ]? # optional space :: # directive delimiter ([ ]+|$) # whitespace or end of line cC0||\}}|j|7_||g|gfSz3Footnotes, hyperlink targets, directives, comments.)explicit_constructrW explicit_listr+rr_rr!rxrrr r%   zBody.explicit_markupc Csg}|jjD]>\}}||j}|rDz|||WStyC}z|j}d|j}| |j j ||dWYd}~nd}~wwq| |\} } | || fS)z>Determine which explicit construct this is, parse & return it.r$rN) rc constructsrrrrSrrrrlr3rr) r+rerrorsrrmexpmatchrrrr!rxrrr r, s     zBody.explicit_constructcCsl|jjd}|j|jj|d|jd|jd||jjd\}}|||s4|j|d7_dSdS)z Create a nested state machine for a series of explicit markup constructs (including anonymous hyperlink targets). rdNExplicit)rGrDrPrxr>zExplicit markup) rSrrrFr[rWr>rYr)r+rxrrrrr r< s   zBody.explicit_listcCrzAnonymous hyperlink targets.)anonymous_targetrWrrrrr r/L rzBody.anonymousc Csj|j}|jj|dd\}}}}|jd|d|}dd|D}||||d}|g|fS)NTrrcSr_rr`rarrr rY rbz)Body.anonymous_target..r)rSrrrrrrd) r+rrrorrrxrerrrr rS s    zBody.anonymous_targetcCs|jjr |jgdgfS|jdkrtdt|jdkr7|jjd|j d}|j |7_ td|jj }|jj dt |||j d}|j |7_ g|gfS)z,Section title overline or transition marker.Line::rzeUnexpected possible title overline or transition. Treating it as ordinary text because it's so short.rz'Unexpected section title or transition.)rSr>rrrrrhr3rrrWr^r\rr)r+rr_rr rerrr r^] s&    z Body.linecC|jgdgfS)z%Titles, definition lists, paragraphs.rPrrrrr rs z Body.textr()rr)rN)crrrr/r TableParserr3r'rrkeysrr sequencepatsrdr|rrrrrrrrrrr4simple_table_top_patr7patsrescaperrrinitial_transitionsrrrrrrrrrrrrrrrrrrrrrr rrrrrr"rr r'r%rIrcrr?rrXr\rhrdrirnrjrrzrrrrrrrrrrrrrr/rr^rrrrr rM.sd               # #7%)  & +      -E <.!       , rMc@sNeZdZdZejZded<ddejDZedddd Z d d Z d S) RFC2822Bodyz RFC2822 headers are only valid as the first constructs in documents. As soon as anything else appears, the `Body` state should take over. z[!-9;-~]+:( +|$)rfc2822cCsg|]}|dfqS)rMr)r(rrrr r s zRFC2822Body.r)rrMc Cstjdgd}|j|7_||\}}||7}|jjd}|j|jj|d|jd|d|d\}}| ||sG|j| d7_g|gfS)RFC2822-style field list item.r)rArdN RFC2822ListrzRFC2822-style field list) rrrW rfc2822_fieldrSrrrFr[rYr) r+rr_r fieldlistrrxrrrrr r s"     zRFC2822Body.rfc2822c Cs~|jd|jd}|jj|dd\}}}}t}|t||7}td |}||7}|r;|j |||d||fS)NrTrrr) rfindrSrrrrrrrru) r+rrrrrrx fieldnode fieldbodyrrr r s  zRFC2822Body.rfc2822_fieldN) rrrr/rMrrvrinsertrrrrrr rx s   rc@sNeZdZdZdddZeZeZeZeZeZ eZ eZ eZ eZ eZeZeZeZdS)SpecializedBodya Superclass for second and subsequent compound element members. Compound elements are lists and list-like constructs. All transition methods are disabled (redefined as `invalid_input`). Override individual methods in subclasses to re-enable. For example, once an initial bullet list item, say, is recognized, the `BulletList` subclass takes over, with a "bullet_list" node as its container. Upon encountering the initial bullet list item, `Body.bullet` calls its ``self.nested_list_parse`` (`RSTState.nested_list_parse`), which starts up a nested parsing session with `BulletList` as the initial state. Only the ``bullet`` transition method is enabled in `BulletList`; as long as only bullet list items are encountered, they are parsed and inserted into the container. The first construct which is *not* a bullet list item triggers the `invalid_input` method, which ends the nested parse and closes the container. `BulletList` needs to recognize input that is invalid in the context of a bullet list, which means everything *other than* bullet list items, so it inherits the transition list created in `Body`. NcCs|jtz8Not a compound element member. Abort this state machine.)rSrrZrrrr invalid_input s zSpecializedBody.invalid_inputNNN)rrrr/rrrrrrrrrrrr/r^rrrrr r s  rc@r&)rz-Second and subsequent bullet_list list_items.cCsL|jd|jdkr|||\}}|j|7_||_g|gfS)rrr)rrWrrrrx)r+rr_rrrxrrr r s  zBulletList.bulletN)rrrr/rrrrr r r0rc@r&)DefinitionListz,Second and subsequent definition_list_items.cCr)zDefinition lists. Definitionrrrrr r rzDefinitionList.textN)rrrr/rrrrr r r0rc@r&)rz1Second and subsequent enumerated_list list_items.c Cs|||jd\}}}}||jks.|dkr'||jdks.|js.||jdks.||||s2||dkr9d|_||\}} |j|7_| |_ ||_g|gfS)zEnumerated list item.rrLrd) rrWrrMrrrrrrx) r+rr_rrrrrrrxrrr r s$    zEnumeratedList.enumeratorN)rrrr/rrrrr r r0rc@r&)rz(Second and subsequent field_list fields.cC,||\}}|j|7_||_g|gfS)zField list field.)rrWrxr+rr_rrrxrrr r  zFieldList.field_markerN)rrrr/rrrrr r r0rc@r&)rz4Second and subsequent option_list option_list_items.cCsLz ||\}}Wn ty|Ynw|j|7_||_g|gfS)r)rrrrWrx)r+rr_rrrxrrr r s   zOptionList.option_markerN)rrrr/rrrrr r r0rc@s*eZdZdZejZejZddZej Z dS)rz6Second and subsequent RFC2822-style field_list fields.cCs,||\}}|j|7_||_gdgfS)rr)rrWrxrrrr r$ rzRFC2822List.rfc2822N) rrrr/rrrrrrrrrrr r s  rc@r&)rzz Parse field_list fields for extension options. No nested parsing is done (including inline markup parsing). cCsRg}t|dgD]}|r||q |r&d|}|t||7}g}q dS)z5Override `Body.parse_field_body` for simpler parsing.rrN)rrrlrrr)r+rrrDrr^rrrr r6 s  z!ExtensionOptions.parse_field_bodyN)rrrr/rrrrr r. s rc@seZdZdZejZddZdS)r z,Second and subsequent lines of a line_block.cCsJ|j}|||\}}}|j|7_|jj|7_||_g|gfS)zNew line of line block.)rSrr rWrx)r+rr_rrr^rrxrrr rH s  zLineBlock.line_blockN)rrrr/rrrrrrrr r B s r c@s&eZdZdZddZddZejZdS)rz0Second and subsequent explicit markup construct.cCrr)rrWrxrrrr rV rzExplicit.explicit_markupcCrr)rrWrxrrrr r/] rzExplicit.anonymousN) rrrr/rr/rrrrrrr rR s  rc@sBeZdZdZedejejddZ ddgZ ddZ d d Z d S) ruzG Parser for the contents of a substitution_definition element. z (%s)::( +|$)r)embedded_directiverrrcCs>|j||jddd\}}|j|7_|js||_t)Nrr)alt)rrWrSat_eofrxrZrrrr rs s   z"SubstitutionDef.embedded_directivecCs|js |j|_tr()rSrrrxrZrrrr r{ s  zSubstitutionDef.textN) rrrr/rrr?rrrrrrrrrr rug s ruc@szeZdZdZejdddZddgZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZedZddZdS)rPzs Classifier of second line of a text block. Could be a paragraph, a definition list item, or a title. r^r) underliner)rrM)rrMcCsH|||jd\}}|j|7_|r|j|7_gdgfS)zEnd of paragraph.rdrM)rrSrrWr)r+rr_rrrrrr r s z Text.blankcCs|r |d|dgSr()rrbrrr eof szText.eofc Cst}||\}}||7}|j|7_|jjd}|j|jj|d|jd|d|dd\}}| ||sE|j| d7_gdgfS)Definition list item.rdNrr)rGrDrPrxr{zDefinition listrM) rdefinition_listdefinition_list_itemrWrSrrrFr[rYr) r+rr_rdefinitionlistdefinitionlistitemrxrrrrr r s    z Text.indentcCsT|j}|d}|j}|d|}g}t|t|krZt|dkr>|jjr9|jjd|d} |j | 7_ t d|dd|jj } |jj dt| | |d} || |jjs|dd|jj } |j\} } |jjdt| | | | d } |j |7_ |j | 7_ g|gfS|d} g|d d <|||| |d |g|gfS) zSection title.rrrzfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.rrzTitle underline too short.zUnexpected section title.r/Nrd)rSrrrrrhr>r3rrWrrr^rrrrlrUr\r)r+rr_rrrrr9rr rerrrrrr r sB            zText.underlinec Cs|jd}d}z |jjdd}Wn"tjy4}z|j\}}} |jjd|| d}WYd}~nd}~ww|t|} | | |\} } |j | 7_ |j |7_ | rmz|j Wn t ycYnw|j | 7_ g|gfS)z Paragraph.rdNTr,r.r/)rSrr rr0rr3rrrrWrnrZr) r+rr_rr@r ror+rrrrrrrr r s.    z Text.textcCs|j\}}}}|r|ds||r|dr|s#|Sd|}t||}|j|d\|_ |_ |g}|sG| | d|S)zReturn a list of nodes.rrrdz Literal block) rSrrrquoted_literal_blockrrrrUr9r^rlr)r+rrrrxrrr!rrr r s    zText.literal_blockcCsR|j}|jj}t}|j|jj|d||dtfddd}|||j S)NFQuotedLiteralBlockrN)rGrDr>rq) rSr[rrryrurFrrYchildren)r+r[r parent_noderrrr r s  zText.quoted_literal_blockc Cs|j\}}}}td|t|}|jd}|j|\|_|_ | ||\}} ||7}tj dg| R} || 7}|ddddkrU| |j j d|dd7} |j||| d ||fS) Nrrdrrr*rz`Blank line missing before literal block (after the "::")? Interpreted as a definition list item.rr)rSrrrrrrrUr9r^termrr3rru) r+termlinerrrrxitemnodertermlistrrrrr r s(  zText.definition_list_itemz +: +c Cst|dksJ||d|\}}t|d}|j|\|_|_|g}tt|D]R}||}t |tj rv|j |} t| dkrM|d|7<q,| d } t | } |d| 7<| ddD]} |tt| d| qfq,|d|7<q,||fS)z9Return a definition_list's term and optional classifiers.rdrrNT)rhrrrrSrUr9r^rrrPclassifier_delimiterr&rrl classifierr) r+rrrr term_noderArrDrrtextnoderrrr r s.      z Text.termN)rrrr/rMrrrrrrrrrrrrrrrrrr rP s  (  rPc@s2eZdZdZddZdddZeZeZeZeZ dS)SpecializedTextz Superclass for second and subsequent lines of Text-variants. All transition methods are disabled. Override individual methods in subclasses to re-enable. cCsgS)zIncomplete construct.rrbrrr rA zSpecializedText.eofNcCstrrZrrrr rE rzSpecializedText.invalid_inputr) rrrr/rrrrrrrrrr r8 s rc@s eZdZdZddZddZdS)rz.Second line of potential definition_list_item.cCs|jdgS)zNot a definition.re)rSrrbrrr rS s zDefinition.eofcCs,||\}}|j|7_||_gdgfS)rr)rrWrx)r+rr_rrrxrrr rX rzDefinition.indentN)rrrr/rrrrrr rO s rc@sNeZdZdZdZ ddZddZddZeZd d Z dd d Z dd dZ dS)rzO Second line of over- & underlined section title or transition marker. rdcCs|d}|jjrd|j_n t|dkr|||jr;|j\}}tj |dd}||_ |d|_ |j |7_ d|_gS)z0Transition marker at end of section or document.rFrr rd) rrAr7rhstate_correctioneofcheckrSrUr transitionr9r^rW)r+r_markerrrrrrr ri s     zLine.eofcCsd|j\}}|d}t|dkr||tj|d}||_|d|_|j |7_ gdgfS)zTransition marker.rrrrdrM) rSrUrrhrrrr9r^rW)r+rr_rrrrrrrr r{ s      z Line.blankc Csj|jd}|d}|j}d}z|j}Wn;tyR|d|}t|dkr5||||dn|jj dt |||d} |j | 7_ gd gfYSYnwd |||f} |}|}|j d d|s|d|d|}t|dkr||||dnQ|jj d t | | |d} |j | 7_ gd gfS||kr|d|d|}t|dkr||||dn|jj d t | | |d} |j | 7_ gd gfS|}g} t|t|kr|d|d|}t|dkr||||dn|jjdt | | |d} | | |d|df} d|_||| | |d| d|_gd gfS)z#Potential over- & underlined title.rdrrrrrezIncomplete section title.rrMz%s %s %srz6Missing matching underline for section title overline.z$Title overline & underline mismatch.zTitle overline too short.)rSrrrnrZrhrshort_overliner3r\rrrWr`rrrrlrrr) r+rr_rroverlinerrrer r9rrrrr r sz           z Line.textcCsx|d}|d|jj}|jd}t|dkr#||||d|jjdt |||d}|j |7_ gdgfS)Nrrrdrz+Invalid section title or transition marker.rrM) rSr^rrhrrr3rrrrW)r+rr_rrrerr rrr r s  zLine.underlinecCs.|jjd|d}|j|7_|||dS)Nz`Possible incomplete section title. Treating the overline as ordinary text because it's so short.r)r3rrWr)r+r_rerrr rrr r s zLine.short_overlinecCs$|j|g|dd<tdd)NrMr)rSrrStateCorrection)r+r_rrrr r s   zLine.state_correctionN)rd) rrrr/rrrrrrrrrrrr r` s > rc@s^eZdZdZdejddZdZdddZdd Z d d Z d d Z ddZ ddZ ddZdS)rz Nested parse handler for quoted (unindented) literal blocks. Special-purpose. Not for inclusion in `state_classes`. z(%(nonalphanum7bit)s)r)initial_quotedrFcCst|||g|_d|_dSr()rLr-rinitial_linenorRrrr r- s zQuotedLiteralBlock.__init__cCs|rt||gfSr(rrrrr r s zQuotedLiteralBlock.blankcCs|r$|j|j\}}d|}t||}||_||_|j|7_n|j|j j d|j d7_|j |j|j 7_gS)Nrz#Literal block expected; none found.r)rSrUrrrrr9r^rWr3rrrr)r+r_rrrrrrr r s     zQuotedLiteralBlock.eofcCs8|sJd|j|jjd|jd|jt)Nz7QuotedLiteralBlock.indent: context should not be empty!r.rrrlr3rrSrrrZrrrr r s  zQuotedLiteralBlock.indentcCsZ|d|jd}tt|tj}|d||j|jj f|j |_ |jg|gfS)z7Match arbitrary quote character on the first line only.rrquoted) remove_transitionrrrrradd_transitionrr]rrSrr)r+rr_rquotermrrr r s   z!QuotedLiteralBlock.initial_quotedcCs||j||gfS)z,Match consistent quotes on subsequent lines.)rlrrrrr r s  zQuotedLiteralBlock.quotedcCs0|r|j|jjd|jd|jt)Nz#Inconsistent literal block quoting.rrrrrr r$ s zQuotedLiteralBlock.textNr)rrrr/rMrrrr-rrrrrrrrrr r s   rrK)Hr/ __docformat__sysrtypesrrrrrrrr docutils.statemachiner r docutils.nodesr rr rrrrrdocutils.parsers.rst.languagesr_fallback_language_moduledocutils.utilsrrrrrrrrr"r#r$ Exceptionr%objectr'r1rJrLrr?rxr|rrrMrrrrrrrrrr rrurPrrrrrOrrrr sb    ' nYT.,  8 K