o ?] R@s|dZdZddlZddlZddlmZddlmZGdddeZGdd d e 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. reStructuredTextN) 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|g|RdS)Noffsetr)poprr__init__)selfargskwargsr B/usr/lib/python3/dist-packages/docutils/parsers/rst/tableparser.pyr &szTableMarkupError.__init__N)__name__ __module__ __qualname____doc__r r r r rrs rc@s,eZdZdZdZ dZ ddZddZdS) 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 block structurer r rparse7s zTableParser.parsecCstt|jD]+}|j|}|j|r2|jr&td|jd|df|d||_|dd|j|<q|jdksB|jt|jdkrHtd|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 rrGs(  zTableParser.find_head_body_sep)rrrrr"double_width_pad_charrrr r r rr+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 disconnectr!bottomrightr$donecellsrowsepscolsepsr rr r rrs  zGridTableParser.setupc Csdg}|ru|d\}}||jks||jks||j|krq|||}|s'q|\}}}}t|j|t|j|||||||j |d|d||} | | |j d|j||||| f|||f||fg||s|s}tddS)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. )rrrrz"Malformed table; parse incomplete.N)rr,r-r. scan_cellupdate_dict_of_listsr0r1 mark_doner get_2D_blockr+r%r(r/appendextendsortcheck_parse_completer) r cornerstopleftresultr,r-r0r1 cellblockr r rrs2     zGridTableParser.parse_tablecCs@|d}|d}t||D]}|j||ksJ||j|<q dS)z@For keeping track of how much of each text column has been seen.rN)r r.)r r=r>r,r-beforeaftercolr r rr6s  zGridTableParser.mark_donecCs2|jd}t|jD] }|j||krdSq dS)z2Each text column should have been completely seen.rFT)r,r r-r.)r lastrCr r rr;s z$GridTableParser.check_parse_completecCs&|j||dks J|||}|S)z:Starting at the top-left corner, start tracing out a cell.+)r scan_right)r r=r>r?r r rr4s zGridTableParser.scan_cellc Csi}|j|}t|d|jdD]2}||dkr:|g||<||||}|r9|\}}} t|| ||||fSq||dkrCdSqdS)zr Look for the top-right corner of the cell, and make note of all column boundaries ('+'). rrErN)rr r- scan_downr5) r r=r>r1r'r&r?r,r0 newcolsepsr r rrFs      zGridTableParser.scan_rightc Csi}t|d|jdD]7}|j||dkr7|g||<|||||}|r6|\}}t|||||fSq |j||dkrCdSq dS)zj Look for the bottom-right corner of the cell, making note of all row boundaries. rrE|N)r r,r scan_leftr5) r r=r>r-r0r&r? newrowsepsr1r r rrGs  zGridTableParser.scan_downc Csi}|j|}t|d|dD]}||dkr|g||<q||dkr&dSq||dkr/dS|||||}|durA|} | |fSdS)z Noting column boundaries, look for the bottom-left corner of the cell. It must line up with the starting point. rr*rErN)rr scan_up) r r=r>r,r-r1r'r&r?r0r r rrJs     zGridTableParser.scan_leftcCsTi}t|d|dD]}|j||dkr|g||<q |j||dkr'dSq |S)zT Noting row boundaries, see if we can return to the starting point. rr*rErIN)r r)r r=r>r,r-r0r&r r rrLs zGridTableParser.scan_upcst|j}i}tt|D]}||||<qt|ji}ttD]}|||<q'fddtdtD}ddttdDfddtt|dD}t|dtd}|jD]I\}} } } } ||} || }|| |dusJd| d|df|| | d}|| |d}||d|d8}|||d| f|| |<qi|dksJd |jr||j}|d|}||d}ng}|}|||fS) zh From the data collected by `scan_cell()`, convert to the final data structure. cs$g|]}||ddqS)rr .0r&)r1r r )sz8GridTableParser.structure_from_cells..rcSsg|]}dqSNr rMr r rrO,scsg|]}ddqSrPr rM)onerowr rrO-Nz&Cell (row %s, column %s) already used.rzUnused cells remaining.)sortedr0keysr r!r1r/r$)r r0rowindexr&colindexcolspecsrows remainingr=r>r,r-rrownumcolnummorerowsmorecols numheadrowsheadrowsbodyrowsr )r1rQrrsD     z$GridTableParser.structure_from_cellsN)rrrrrecompiler"rrr6r;r4rFrGrJrLrr r r rr)[s 4 # 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_endtabler!r.r0r1r2r r rrxs  zSimpleTableParser.setupcCs||jdd|_|jdd|_|jd\}}d}d}d}|t|jkry|j|}|j|rH||j|||||f|d}d}n$||| rf|ra||kra||j||||}d}n|sl|d}|d7}|t|jks&dSdS)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_columnsrrdrer!span_patr# parse_rowrstripstrip)r firststartfirstendrstart text_foundr'r r rrs.    zSimpleTableParser.parse_tablecCsg}d} |d|}|d|}|dkrn|dkrt|}|||fq|jrK|dd|jkr.rrr*z'Text in column margin in table line %s.r) r8sysmaxsizer!r rkrjrdmaxrr)r r| first_linerdlastcolr&rnrs nextstartrr'textnew_end main_startmain_endr r rr{s2      zSimpleTableParser.check_columnscCsldd|jD}d}|jr'tt|jD]}|j|dd|jkr&|}nq||jd||j|dfS)NcSsg|]\}}||qSr r )rNrnrsr r rrO rRz:SimpleTableParser.structure_from_cells..rru)rdr$r r!rf)r rWfirst_body_rowr&r r rr s z&SimpleTableParser.structure_from_cellsrP)rrrrrarbr"rhrrrgryrir{rr r r rrcFs -    rccCs(|D] \}}||g|qdS)z Extend the list values of `master` with those from `newdata`. Both parameters must be dictionaries containing list values. N)items setdefaultr9)masternewdatakeyvaluesr r rr5sr5)r __docformat__rardocutilsrdocutils.utilsrrobjectrr)rcr5r r r rs  0l R