mò gã¸Ec@sdZdZdkZdkZdklZdefd„ƒYZdfd„ƒYZdefd „ƒYZd efd „ƒ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. treStructuredTextN(s DataErrortTableMarkupErrorcBstZRS(N(t__name__t __module__(((t?/data/zmath/zope/lib/python/docutils/parsers/rst/tableparser.pyRst TableParsercBs,tZdZeZdZd„Zd„ZRS(sR Abstract superclass for the common parts of the syntax-specific parsers. tcCs1|i|ƒ|iƒ|iƒ|iƒ}|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. N(tselftsetuptblocktfind_head_body_sept parse_tabletstructure_from_cellst structure(RR R ((Rtparse-s     cCsÎxŠtt|iƒƒD]s}|i|}|ii|ƒoM|iot d|i|fƒ‚q‰||_|i ddƒ|i|Look for a head/body row separator line; store the line index.sXMultiple head/body row separators in table (at line offset %s and %s); only one allowed.t=t-iisKThe head/body row separator may not be the first or last line of the table.N( trangetlenRR titlinethead_body_separator_pattmatcht head_body_sepRtreplace(RRR((RR =s   !-(RRt__doc__tNoneRtdouble_width_pad_charRR (((RR!s  tGridTableParsercBswtZdZeidƒ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›||_|iiƒt|ƒd|_t|dƒd|_d|_dgt|dƒ|_g|_ hddg<|_ hddg<|_ dS(Niiiÿÿÿÿ( R Rt disconnectRtbottomtrightRRtdonetcellstrowsepstcolseps(RR ((RRˆs    c Csydg}xL|oD|idƒ\}} ||ijp$| |ijp||i| joq n|i|| ƒ}|pq n|\}}}}t |i |ƒt |i |ƒ|i || ||ƒ|ii|d| d||ƒ}|iƒ|i|idƒ|ii|| |||fƒ|i||f|| fgƒ|iƒq W|iƒptdƒ‚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(tcornerstpopttoptleftRRRR t scan_celltresultR"R#tupdate_dict_of_listst mark_doneR t get_2D_blockt cellblockRRRR!tappendtextendtsorttcheck_parse_completeR( RRRR%R.R"R*R#R'R(((RR “s.  4   cCsZ|d}|d}x?t||ƒD].}|i ||jpt ‚||i |((RR:ßs    c CsÅh}|i|} xYt|d|dƒD]A}| |djo|g||