UNPKG

docutils-ts

Version:

Port of the Python Docutils library to TypeScript

250 lines (239 loc) 9.46 kB
/** * 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. * */ import { DataError } from '../../exceptions.js'; import StringList from "../../stringList.js"; import { TableData } from './types.js'; export interface RowIndicies { [rowNum: number]: number; } /** * 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. * */ declare class TableMarkupError extends DataError { offset?: number; constructor(message: string, offset?: number); } /** * Abstract superclass for the common parts of the syntax-specific parsers. * */ declare abstract class TableParser { protected doubleWidthPadChar: string; protected block: StringList; protected headBodySep?: number; protected headBodySeparatorPat?: RegExp; constructor(); _init(): void; /** * 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. */ parse(block: StringList): TableData; abstract parse_table(): void; /** * Look for a head/body row separator line; store the line index. */ findHeadBodySep(): void; abstract structure_from_cells(): TableData; abstract setup(block: StringList): void; } /** 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. */ declare class GridTableParser extends TableParser { private cells; private colseps; private rowseps; private done; private bottom; private right; _init(): void; setup(block: StringList): void; /** Start with a queue of upper-left corners, containing the upper-left corner of the table itthis. 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. */ parse_table(): void; /** For keeping track of how much of each text column has been seen. */ mark_done(top: number, left: number, bottom: number, right: number): void; /** Each text column should have been completely seen. */ check_parse_complete(): boolean; /** Starting at the top-left corner, start tracing out a cell. */ scan_cell(top: number, left: number): any[] | null; /** Look for the top-right corner of the cell, and make note of all column boundaries ('+'). */ scan_right(top: number, left: number): any[] | null; /** Look for the bottom-right corner of the cell, making note of all row boundaries. */ scan_down(top: number, left: number, right: number): any[] | null; /** Noting column boundaries, look for the bottom-left corner of the cell. It must line up with the starting point. */ scan_left(top: number, left: number, bottom: number, right: number): any[] | null; /** Noting row boundaries, see if we can return to the starting point. */ scan_up(top: number, left: number, bottom: number, right: number): any; /** From the data collected by `scan_cell()`, convert to the final data structure. */ structure_from_cells(): TableData; } /** * 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, [''])]]) * */ declare class SimpleTableParser extends TableParser { private table; private spanPat; private columns; private border_end; private colseps; private rowseps; private done?; _init(): void; setup(block: StringList): void; /** 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 `this.parse_row` to finish the job. */ parse_table(): void; /** Given a column span underline, return a list of (begin, end) pairs. */ parse_columns(line: string, offset: number): any[][]; init_row(colspec: number[][], offset: number): [number, number, number, {}[]][]; /** Given the text `lines` of a row, parse it and append to `this.table`. The row is parsed according to the current column spec (either `spanline` if provided or `this.columns`). For each column, extract text from each line, and check for text in column margins. Finally, adjust for insignificant whitespace. */ parse_row(lines: StringList, start: number, spanline?: any[]): void; /** 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. */ check_columns(lines: any, firstLine: number, columns: any | any[]): void; structure_from_cells(): TableData; } export { GridTableParser, SimpleTableParser, TableMarkupError };