ó V!¶\c@s¦dZdZddlZddlZddlmZddlmZdefd„ƒYZddd „ƒYZ d e fd „ƒYZ d e fd „ƒYZ d„Z dS(sË 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. treStructuredTextiÿÿÿÿN(t DataError(tstrip_combining_charstTableMarkupErrorcBseZdZd„ZRS(s  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)|jddƒ|_tj||ŒdS(Ntoffseti(tpopRRt__init__(tselftargstkwargs((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyR&s(t__name__t __module__t__doc__R(((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyRst TableParsercBs,eZdZdZdZd„Zd„ZRS(sR Abstract superclass for the common parts of the syntax-specific parsers. tcCs1|j|ƒ|jƒ|jƒ|jƒ}|S(sn 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. (tsetuptfind_head_body_sept parse_tabletstructure_from_cells(Rtblockt structure((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pytparse7s    cCsÛx”tt|jƒƒD]}}|j|}|jj|ƒr|jrntd|jd|dfd|ƒ‚q“||_|jddƒ|j|Look for a head/body row separator line; store the line index.sLMultiple head/body row separators (table lines %s and %s); only one allowed.iRt=t-isKThe head/body row separator may not be the first or last line of the table.N(trangetlenRthead_body_separator_pattmatcht head_body_sepRtreplace(Rtitline((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyRGs  $  !  N(R R R tNoneRtdouble_width_pad_charRR(((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyR +s  tGridTableParsercBsweZdZejdƒZd„Zd„Zd„Zd„Z d„Z d„Z d„Z d „Z d „Zd „ZRS( si 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. s\+=[=+]+=\+ *$cCs—||_|jjƒt|ƒd|_t|dƒd|_d|_dgt|dƒ|_g|_idgd6|_ idgd6|_ dS(Niiiÿÿÿÿ( Rt disconnectRtbottomtrightR Rtdonetcellstrowsepstcolseps(RR((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyR“s    c Csodg}xD|rO|jdƒ\}}||jks ||jks ||j|kr^q n|j||ƒ}|s|q n|\}}}}t|j|ƒt|j|ƒ|j||||ƒ|j j |d|d||ƒ} | j ƒ| j |j dƒ|jj||||| fƒ|j||f||fgƒ|jƒq W|jƒsktdƒ‚ndS(s  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. iits"Malformed table; parse incomplete.N(ii(RR$R%R&t scan_celltupdate_dict_of_listsR(R)t mark_doneRt get_2D_blockR#RR!R'tappendtextendtsorttcheck_parse_completeR( RtcornersttoptlefttresultR$R%R(R)t cellblock((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyRžs,     cCsX|d}|d}x=t||ƒD],}|j||ksCt‚||j||j|jddƒ|_|jdd|_|jd\}}d}d}d}xâ|t|jƒkr9|j|}|jj|ƒrÈ|j|j||!||j ƒ|fƒ|d}d}nd|||!j ƒr|r ||kr |j|j||!|ƒn|}d}n|s,|d}n|d7}qXWdS(sô 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. iiÿÿÿÿiN( t parse_columnsRRXRYR Rtspan_patRt parse_rowtrstriptstrip(Rt firststarttfirstendRtstartt text_foundR((sD/tmp/pip-install-usGedi/docutils/docutils/parsers/rst/tableparser.pyRˆs*     cCsâg}d}xltrz|jd|ƒ}|jd|ƒ}|dkrIPn|dkrdt|ƒ}n|j||fƒqW|jrÞ|dd|jkr¸td|dd|ƒ‚n|dd|jddf|ds  0íÑ