U n^1R@szdZdZddlZddlZddlmZddlmZGdddeZGdd d Z Gd d d e Z Gd d d e Z ddZ 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. ZreStructuredTextN) DataErrorstrip_combining_charsc@seZdZdZddZdS)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)Noffsetr)poprr__init__)selfargskwargsr z/private/var/folders/sd/whlwsn6x1_qgglc0mjv25_695qk2gl/T/pip-install-4zq3fp6i/docutils/docutils/parsers/rst/tableparser.pyr&szTableMarkupError.__init__N)__name__ __module__ __qualname____doc__rr r r r rsrc@s(eZdZdZdZdZddZddZdS) TableParserzR Abstract superclass for the common parts of the syntax-specific parsers. NcCs&|||||}|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. )setupfind_head_body_sep parse_tablestructure_from_cells)r blockZ structurer r r parse7s zTableParser.parsecCstt|jD]X}|j|}|j|r|jrNtd|jd|df|dq||_|dd|j|<q|jdks|jt|jdkrtd|ddS) 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)rangelenrhead_body_separator_patmatch head_body_seprreplace)r iliner r r rGs$  zTableParser.find_head_body_sep)rrrrr double_width_pad_charrrr r r r r+s rc@sjeZdZdZedZddZddZddZ d d Z d d Z d dZ ddZ ddZddZddZdS)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|_|jt|d|_t|dd|_d|_dgt|d|_g|_ddgi|_ddgi|_ dS)Nrr) r disconnectrbottomrightr"donecellsrowsepscolsepsr rr r r rs  zGridTableParser.setupc Csdg}|r|d\}}||jks||jks||j|kr scan_right)r r:r;r<r r r r2s zGridTableParser.scan_cellc Csi}|j|}t|d|jdD]d}||dkrt|g||<||||}|r|\}}} t|| ||||fSq"||dkr"dSq"dS)zr Look for the top-right corner of the cell, and make note of all column boundaries ('+'). rrCrN)rrr+ scan_downr3) r r:r;r/r%r$r<r*r.Z newcolsepsr r r rDs      zGridTableParser.scan_rightc Csi}t|d|jdD]n}|j||dkrn|g||<|||||}|r|\}}t|||||fSq|j||dkrdSqdS)zj Look for the bottom-right corner of the cell, making note of all row boundaries. rrC|N)rr*r scan_leftr3) r r:r;r+r.r$r<Z newrowsepsr/r r r rEs  zGridTableParser.scan_downc Csi}|j|}t|d|dD].}||dkr:|g||<q||dkrdSq||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(rCrN)rrscan_up) r r:r;r*r+r/r%r$r<r.r r r rGs     zGridTableParser.scan_leftcCsTi}t|d|dD]:}|j||dkr6|g||<q|j||dkrdSq|S)zT Noting row boundaries, see if we can return to the starting point. rr(rCrFN)rr)r r:r;r*r+r.r$r r r rHs zGridTableParser.scan_upcst|j}|i}tt|D]}||||<q&t|ji}ttD]}|||<q^fddtdtD}ddttdDfddtt|dD}t|dtd}|jD]\}} } } } ||} || }|| |dks*td| d|df|| | d}|| |d}||d|d8}|||d| f|| |<q|dkstd |j r||j }|d|}||d}ng}|}|||fS) zh From the data collected by `scan_cell()`, convert to the final data structure. cs$g|]}||ddqS)rr .0r$)r/r r +sz8GridTableParser.structure_from_cells..rcSsg|]}dqSNr rIr r r rK.scsg|]}ddqSrLr rI)onerowr r rK/sNz&Cell (row %s, column %s) already used.rzUnused cells remaining.) listr.keysr8rrr/r-r>r")r r.Zrowindexr$ZcolindexcolspecsZrows remainingr:r;r*r+rZrownumcolnumZmorerowsmorecolsZ numheadrowsZheadrowsZbodyrowsr )r/rMr rsH    z$GridTableParser.structure_from_cellsN)rrrrrecompiler rrr4r9r2rDrErGrHrr r r r r'[s4  # r'c@s^eZdZdZedZedZddZddZ dd Z d d Z dd dZ ddZ ddZd 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|jddd|jd<|jddd|jd<d|_g|_d|_g|_dgt|d|_ddgi|_ ddgi|_ dS)Nrrrr() rr)r#r"columns border_endtablerr,r.r/r0r r r rzs  zSimpleTableParser.setupcCs||jdd|_|jdd|_|jd\}}d}d}d}|t|jkr|j|}|j|r||j|||||f|d}d}nH||| r|r||kr||j||||}d}n |s|d}|d7}q>dS)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_columnsrrWrXrspan_patr! parse_rowrstripstrip)r Z firststartZfirstendrstartZ text_foundr%r r r rs,    zSimpleTableParser.parse_tablecCsg}d}|d|}|d|}|dkr*qJ|dkr:t|}|||fq|jr|dd|jkrvtd|d|d|dd|jddf|d<|S)zU Given a column span underline, return a list of (begin, end) pairs. rr r(rz(Column span incomplete in table line %s.r)findrr6rWrXr)r r%rcolsendbeginr r r rZs$  zSimpleTableParser.parse_columnsc Csd}g}|D]\}}d}z>||j|dks0t||j|dkrT|d7}|d7}q0Wn0ttfk rtd|d|ddYnX|d||gg|d7}q |S)Nrrz/Column span alignment problem in table line %s.r)rWr> IndexErrorrr6)r Zcolspecrr$r-r_rcrSr r r init_rows$   zSimpleTableParser.init_rowNc Cs|s |s dS|r$|j|}|d}n|jdd}|}|||||||}tt|D]F}||\}}|d|t||} | | |j d| ||d<q\|j |dS)af Given the text `lines` of a row, parse it and append to `self.table`. The row is parsed according to the current column spec (either `spanline` if provided or `self.columns`). For each column, extract text from each line, and check for text in column margins. Finally, adjust for insignificant whitespace. Nrrr1) rZrW check_columnsrgrrr5r)r#r&rYr6) r linesr_ZspanlinerWZ span_offsetrowr$rcr=r r r r\s     zSimpleTableParser.parse_rowcCs|tjdft|d}dd|D}tt|dD]}||\}}||dd}d} |D]} ||kr| |dr| |d} |t| } |jd\} }|t|| f||<| |kr| | f|jd<n,| ||rt d|| d|| d | d7} qbq:| dS) z Check for text in column margins and text overflow in the last column. Raise TableMarkupError if anything but whitespace is in column margins. Adjust the end value for the last column if there is text overflow. NrecSsg|] }t|qSr r)rJr%r r r rKsz3SimpleTableParser.check_columns..rrr(z'Text in column margin in table line %s.r) r6sysmaxsizerrr^r]rWmaxrr)r rj first_linerWZlastcolr$r_rcZ nextstartrr%textZnew_endZ main_startZmain_endr r r ris.     zSimpleTableParser.check_columnscCsldd|jD}d}|jrNtt|jD]$}|j|dd|jkr(|}qNq(||jd||j|dfS)NcSsg|]\}}||qSr r )rJr_rcr r r rKsz:SimpleTableParser.structure_from_cells..rre)rWr"rrrY)r rPZfirst_body_rowr$r r r r s z&SimpleTableParser.structure_from_cells)N)rrrrrTrUr r[rrrZrgr\rirr r r r rVHs-     rVcCs,t|D]\}}||g|q dS)z Extend the list values of `master` with those from `newdata`. Both parameters must be dictionaries containing list values. N)rNitems setdefaultr7)Zmasternewdatakeyvaluesr r r r3sr3) rZ __docformat__rTrlZdocutilsrZdocutils.utilsrrrr'rVr3r r r r s  0nR