B “äî\1Rã@szdZdZddlZddlZddlmZddlmZGdd„deƒZGdd „d ƒZ Gd d „d e ƒZ Gd d „d e ƒZ dd„Z dS)aË This module defines table parser classes,which parse plaintext-graphic tables and produce a well-formed data structure suitable for building a CALS table. :Classes: - `GridTableParser`: Parse fully-formed tables represented with a grid. - `SimpleTableParser`: Parse simple tables, delimited by top & bottom borders. :Exception class: `TableMarkupError` :Function: `update_dict_of_lists()`: Merge two dictionaries containing list values. ZreStructuredTextéN)Ú DataError)Ústrip_combining_charsc@seZdZdZdd„ZdS)ÚTableMarkupErrorz  Raise if there is any problem with table markup. The keyword argument `offset` denotes the offset of the problem from the table's start line. cOs"| dd¡|_tj|f|žŽdS)NÚoffsetr)ÚpoprrÚ__init__)ÚselfÚargsÚkwargs©r úD/tmp/pip-build-uw_ogi45/docutils/docutils/parsers/rst/tableparser.pyr&szTableMarkupError.__init__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr r r r rsrc@s(eZdZdZdZdZdd„Zdd„ZdS)Ú TableParserzR Abstract superclass for the common parts of the syntax-specific parsers. NúcCs&| |¡| ¡| ¡| ¡}|S)an Analyze the text `block` and return a table data structure. Given a plaintext-graphic table in `block` (list of lines of text; no whitespace padding), parse the table, construct and return the data necessary to construct a CALS table or equivalent. Raise `TableMarkupError` if there is any problem with the markup. )ÚsetupÚfind_head_body_sepÚ parse_tableÚstructure_from_cells)rÚblockZ structurer r r Úparse7s zTableParser.parsecCsšxjtt|jƒƒD]X}|j|}|j |¡r|jrPtd|jd|df|d�‚q||_| dd¡|j|<qW|jdksŠ|jt|jƒdkr–td|d�‚dS) z>Look for a head/body row separator line; store the line index.zLMultiple head/body row separators (table lines %s and %s); only one allowed.é)rú=ú-rzKThe head/body row separator may not be the first or last line of the table.N)ÚrangeÚlenrÚhead_body_separator_patÚmatchÚ head_body_seprÚreplace)rÚiÚliner r r rGs  zTableParser.find_head_body_sep)r rrrrÚdouble_width_pad_charrrr r r r r+s rc@sjeZdZdZe d¡Zdd„Zdd„Zdd„Z d d „Z d d „Z d d„Z dd„Z dd„Zdd„Zdd„ZdS)ÚGridTableParserai Parse a grid table using `parse()`. Here's an example of a grid table:: +------------------------+------------+----------+----------+ | Header row, column 1 | Header 2 | Header 3 | Header 4 | +========================+============+==========+==========+ | body row 1, column 1 | column 2 | column 3 | column 4 | +------------------------+------------+----------+----------+ | body row 2 | Cells may span columns. | +------------------------+------------+---------------------+ | body row 3 | Cells may | - Table cells | +------------------------+ span rows. | - contain | | body row 4 | | - body elements. | +------------------------+------------+---------------------+ Intersections use '+', row separators use '-' (except for one optional head/body row separator, which uses '='), and column separators use '|'. Passing the above table to the `parse()` method will result in the following data structure:: ([24, 12, 10, 10], [[(0, 0, 1, ['Header row, column 1']), (0, 0, 1, ['Header 2']), (0, 0, 1, ['Header 3']), (0, 0, 1, ['Header 4'])]], [[(0, 0, 3, ['body row 1, column 1']), (0, 0, 3, ['column 2']), (0, 0, 3, ['column 3']), (0, 0, 3, ['column 4'])], [(0, 0, 5, ['body row 2']), (0, 2, 5, ['Cells may span columns.']), None, None], [(0, 0, 7, ['body row 3']), (1, 0, 7, ['Cells may', 'span rows.', '']), (1, 1, 7, ['- Table cells', '- contain', '- body elements.']), None], [(0, 0, 9, ['body row 4']), None, None, None]]) The first item is a list containing column widths (colspecs). The second item is a list of head rows, and the third is a list of body rows. Each row contains a list of cells. Each cell is either None (for a cell unused because of another cell's span), or a tuple. A cell tuple contains four items: the number of extra rows used by the cell in a vertical span (morerows); the number of extra columns used by the cell in a horizontal span (morecols); the line offset of the first line of the cell contents; and the cell contents, a list of lines of text. z\+=[=+]+=\+ *$cCst|dd…|_|j ¡t|ƒd|_t|dƒd|_d|_dgt|dƒ|_g|_ddgi|_ddgi|_ dS)Nrréÿÿÿÿ) rÚ disconnectrÚbottomÚrightr ÚdoneÚcellsÚrowsepsÚcolseps)rrr r r r“s  zGridTableParser.setupc Csdg}xä|rê| d¡\}}||jks||jks||j|kr>q| ||¡}|sPq|\}}}}t|j|ƒt|j|ƒ| ||||¡|j   |d|d||¡} |   ¡|   |j d¡|j ||||| f¡| ||f||fg¡| ¡qW| ¡sütdƒ‚dS)a  Start with a queue of upper-left corners, containing the upper-left corner of the table itself. Trace out one rectangular cell, remember it, and add its upper-right and lower-left corners to the queue of potential upper-left corners of further cells. Process the queue in top-to-bottom order, keeping track of how much of each text column has been seen. We'll end up knowing all the row and column boundaries, cell positions and their dimensions. )rrrrÚz"Malformed table; parse incomplete.N)rr(r)r*Ú scan_cellÚupdate_dict_of_listsr,r-Ú mark_donerÚ get_2D_blockr'r!r$r+ÚappendÚextendÚsortÚcheck_parse_completer) rZcornersÚtopÚleftÚresultr(r)r,r-Ú cellblockr r r ržs,      zGridTableParser.parse_tablecCsD|d}|d}x.t||ƒD] }|j||ks2t‚||j|<qWdS)z@For keeping track of how much of each text column has been seen.rN)rr*ÚAssertionError)rr7r8r(r)ÚbeforeÚafterÚcolr r r r1Ás zGridTableParser.mark_donecCs4|jd}x$t|jƒD]}|j||krdSqWdS)z2Each text column should have been completely seen.rFT)r(rr)r*)rÚlastr>r r r r6És  z$GridTableParser.check_parse_completecCs&|j||dkst‚| ||¡}|S)z:Starting at the top-left corner, start tracing out a cell.ú+)rr;Ú scan_right)rr7r8r9r r r r/Ñs zGridTableParser.scan_cellc CsŠi}|j|}xvt|d|jdƒD]^}||dkrr|g||<| |||¡}|r‚|\}}} t|| ƒ||||fSq$||dkr$dSq$WdS)zr Look for the top-right corner of the cell, and make note of all column boundaries ('+'). rr@rN)rrr)Ú scan_downr0) rr7r8r-r#r"r9r(r,Z newcolsepsr r r rA×s      zGridTableParser.scan_rightc CsŠi}x€t|d|jdƒD]h}|j||dkrl|g||<| ||||¡}|r‚|\}}t||ƒ|||fSq|j||dkrdSqWdS)zj Look for the bottom-right corner of the cell, making note of all row boundaries. rr@ú|N)rr(rÚ scan_leftr0) rr7r8r)r,r"r9Z newrowsepsr-r r r rBês   zGridTableParser.scan_downc Csˆi}|j|}x@t|d|dƒD],}||dkr<|g||<q ||dkr dSq W||dkr`dS| ||||¡}|dk r„|} | |fSdS)z‰ Noting column boundaries, look for the bottom-left corner of the cell. It must line up with the starting point. rr&r@rN)rrÚscan_up) rr7r8r(r)r-r#r"r9r,r r r rDüs     zGridTableParser.scan_leftcCsVi}xLt|d|dƒD]8}|j||dkr8|g||<q|j||dkrdSqW|S)zT Noting row boundaries, see if we can return to the starting point. rr&r@rCN)rr)rr7r8r(r)r,r"r r r rEs zGridTableParser.scan_upcsÔt|j ¡ƒ}| ¡i}x tt|ƒƒD]}||||<q(Wt|j ¡ƒ‰ˆ ¡i}x ttˆƒƒD]}||ˆ|<qdW‡fdd„tdtˆƒƒDƒ}dd„ttˆƒdƒDƒ‰‡fdd„tt|ƒdƒDƒ}t|ƒdtˆƒd}xž|jD]”\}} } } } ||} || }|| |dk�s4td| d|dfƒ‚|| | d}|| |d}||d|d8}|||d| f|| |<qìW|dk�s–td ƒ‚|j �rÂ||j }|d|…}||d…}ng}|}|||fS) zh From the data collected by `scan_cell()`, convert to the final data structure. cs$g|]}ˆ|ˆ|dd‘qS)rr )Ú.0r")r-r r ú +sz8GridTableParser.structure_from_cells..rcSsg|]}d‘qS)Nr )rFr"r r r rG.scsg|]}ˆdd…‘qS)Nr )rFr")Úonerowr r rG/sNz&Cell (row %s, column %s) already used.rzUnused cells remaining.) Úlistr,Úkeysr5rrr-r+r;r )rr,Zrowindexr"ZcolindexÚcolspecsZrowsÚ remainingr7r8r(r)rZrownumÚcolnumZmorerowsÚmorecolsZ numheadrowsZheadrowsZbodyrowsr )r-rHr rsB   z$GridTableParser.structure_from_cellsN)r rrrÚreÚcompilerrrr1r6r/rArBrDrErr r r r r%[s4  # r%c@s^eZdZdZe d¡Ze d¡Zdd„Zdd„Z dd „Z d d „Z dd d„Z dd„Z dd„Zd S)ÚSimpleTableParseraQ Parse a simple table using `parse()`. Here's an example of a simple table:: ===== ===== col 1 col 2 ===== ===== 1 Second column of row 1. 2 Second column of row 2. Second line of paragraph. 3 - Second column of row 3. - Second item in bullet list (row 3, column 2). 4 is a span ------------ 5 ===== ===== Top and bottom borders use '=', column span underlines use '-', column separation is indicated with spaces. Passing the above table to the `parse()` method will result in the following data structure, whose interpretation is the same as for `GridTableParser`:: ([5, 25], [[(0, 0, 1, ['col 1']), (0, 0, 1, ['col 2'])]], [[(0, 0, 3, ['1']), (0, 0, 3, ['Second column of row 1.'])], [(0, 0, 4, ['2']), (0, 0, 4, ['Second column of row 2.', 'Second line of paragraph.'])], [(0, 0, 6, ['3']), (0, 0, 6, ['- Second column of row 3.', '', '- Second item in bullet', ' list (row 3, column 2).'])], [(0, 1, 10, ['4 is a span'])], [(0, 0, 12, ['5']), (0, 0, 12, [''])]]) z=[ =]*$z-[ -]*$cCs�|dd…|_|j ¡|jd dd¡|jd<|jd dd¡|jd<d|_g|_d|_g|_dgt|dƒ|_ddgi|_ ddgi|_ dS)Nrrrr&) rr'r!r ÚcolumnsÚ border_endÚtablerr*r,r-)rrr r r rzs  zSimpleTableParser.setupcCsê| |jdd¡|_|jdd|_|jd\}}d}d}d}x¦|t|jƒkrä|j|}|j |¡r’| |j||…|| ¡|f¡|d}d}nH|||…  ¡rÎ|rÄ||krÄ| |j||…|¡|}d}n |sÚ|d}|d7}q@WdS)zô First determine the column boundaries from the top border, then process rows. Each row may consist of multiple lines; accumulate lines until a row is complete. Call `self.parse_row` to finish the job. rr&rN) Ú parse_columnsrrRrSrÚspan_patrÚ parse_rowÚrstripÚstrip)rZ firststartZfirstendrÚstartZ text_foundr#r r r rˆs*   zSimpleTableParser.parse_tablecCsœg}d}xD| d|¡}| d|¡}|dkr,P|dkr.rrr&z'Text in column margin in table line %s.)r) r3ÚsysÚmaxsizerrrYrXrRÚmaxrr)rreÚ first_linerRZlastcolr"rZr^Z nextstartrr#ÚtextZnew_endZ main_startZmain_endr r r rdís*      zSimpleTableParser.check_columnscCsndd„|jDƒ}d}|jrPx4tt|jƒƒD]"}|j|dd|jkr*|}Pq*W||jd|…|j|d…fS)NcSsg|]\}}||‘qSr r )rFrZr^r r r rGsz:SimpleTableParser.structure_from_cells..rr`)rRr rrrT)rrKZfirst_body_rowr"r r r r sz&SimpleTableParser.structure_from_cells)N)r rrrrOrPrrVrrrUrbrWrdrr r r r rQHs-     rQcCs0x*t| ¡ƒD]\}}| |g¡ |¡qWdS)z‰ Extend the list values of `master` with those from `newdata`. Both parameters must be dictionaries containing list values. N)rIÚitemsÚ setdefaultr4)ZmasterÚnewdataÚkeyÚvaluesr r r r0sr0) rZ __docformat__rOrgZdocutilsrZdocutils.utilsrrrr%rQr0r r r r Ús  0nR