UNPKG

docutils-ts

Version:

Port of the Python Docutils library to TypeScript

228 lines (220 loc) 10.6 kB
import RSTState from "./rstState.js"; import * as nodes from "../../../nodes.js"; import StringList from "../../../stringList.js"; import RSTStateMachine from "../rstStateMachine.js"; import { ContextArray, NodeInterface, ParseMethodReturnType, ParseResult, RegexpResult, StateInterface, StateType, IsolateTableResult } from "../../../types.js"; import { BodyState, ParserConstructor, DirectiveConstructor, TableData } from '../types.js'; export type RowData = [number, number, number, number, StringList]; export interface ExplicitConstructFunction { (match: RegExpExecArray): ParseResult; } export type ExplicitConstructTuple = [ExplicitConstructFunction, RegExp, RegExpExecArray]; export interface BodyPats { nonalphanum7bit?: string; alpha?: string; alphanum?: string; alphanumplus?: string; enum?: string; optname?: string; optarg?: string; shortopt?: string; longopt?: string; option?: string; rparen?: string; parens?: string; period?: string; } export interface EnumFormatInfo { prefix: string; suffix: string; start: number; end: number; } export interface EnumFormatInfos { parens: EnumFormatInfo; rparen: EnumFormatInfo; period: EnumFormatInfo; [name: string]: EnumFormatInfo; } export interface EnumSequencePats { arabic: string; loweralpha: string; upperalpha: string; lowerroman: string; upperroman: string; [name: string]: string; } export interface EnumConverters { arabic: (input: string) => number; loweralpha: (input: string) => number; upperalpha: (input: string) => number; lowerroman: (input: string) => number; upperroman: (input: string) => number; } export interface SequenceRegexps { arabic: (input: string) => RegExp; loweralpha: (input: string) => RegExp; upperalpha: (input: string) => RegExp; lowerroman: (input: string) => RegExp; upperroman: (input: string) => RegExp; } export interface EnumParseInfo { formatinfo?: EnumFormatInfos; formats?: string[]; sequences?: string[]; sequencepats?: EnumSequencePats; converters?: EnumConverters; sequenceregexps?: SequenceRegexps; } /** * Generic classifier of the first line of a block. */ declare class Body extends RSTState implements BodyState { private gridTableTopPat?; /** Enumerated list parsing information. */ private enum?; private attribution_pattern?; private simpleTableTopPat?; private pats; protected initialTransitions?: (string | string[])[]; constructor(stateMachine: RSTStateMachine, debug?: boolean); footnote(match: RegExpExecArray): ParseResult; citation(match: RegExpExecArray): ParseResult; hyperlink_target(match: RegExpExecArray): ParseResult; make_target(block: StringList, blockText: string, lineno: number, target_name: string): NodeInterface | undefined; /** Determine the type of reference of a target. :Return: A 2-tuple, one of: - 'refname' and the indirect reference name - 'refuri' and the URI - 'malformed' and a system_message node */ parse_target(block: StringList, blockText: string, lineno: number): [string, string, NodeInterface?]; is_reference(reference: string): string | undefined; add_target(targetname: string, refuri: string, target: NodeInterface, lineno: number): void; substitution_def(match: RegExpExecArray): ParseResult; disallowedInsideSubstitutionDefinitions(node: NodeInterface): boolean; /** Returns a 2-tuple: list of nodes, and a "blank finish" boolean. */ directive(match: RegExpExecArray, optionPresets: any): ParseResult; /** * Parse a directive then run its directive function. * * Parameters: * * - `directive`: The class implementing the directive. Must be * a subclass of `rst.Directive`. * * - `match`: A regular expression match object which matched the first * line of the directive. * * - `typeName`: The directive name, as used in the source text. * * - `option_presets`: A dictionary of preset options, defaults for the * directive options. Currently, only an "alt" option is passed by * substitution definitions (value: the substitution name), which may * be used by an embedded image directive. * * Returns a 2-tuple: list of nodes, and a "blank finish" boolean. **/ runDirective(directiveClass: DirectiveConstructor, match: RegExpExecArray, typeName: string, option_presets: any): ParseResult; unknown_directive(typeName: string): ParseResult; comment(match: RegexpResult): ParseResult; /** Footnotes, hyperlink targets, directives, comments. */ explicit_markup(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; /** Determine which explicit construct this is, parse & return it. */ explicit_construct(match: RegexpResult): ParseResult; /** * Create a nested state machine for a series of explicit markup * constructs (including anonymous hyperlink targets). */ explicit_list(blankFinish: boolean): void; /** Anonymous hyperlink targets. */ anonymous(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; anonymous_target(match: RegexpResult): ParseResult; indent(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; block_quote(indented: StringList, lineOffset: number): NodeInterface[]; split_attribution(indented: StringList, lineOffset: number): [StringList | undefined, StringList | undefined, number | undefined, StringList | undefined, number | undefined]; check_attribution(indented: StringList, attributionStart: any): any; /** Enumerated List Item */ enumerator(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; parse_attribution(indented: string[], lineOffset: number): [NodeInterface, NodeInterface[]]; bullet(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; list_item(indent: number): [NodeInterface, boolean]; /** Construct and return the next enumerated list item marker, and an auto-enumerator ("#" instead of the regular enumerator). Return ``None`` for invalid (out of range) ordinals. */ make_enumerator(ordinal: number, sequence: string, format: string): [string, string] | undefined; /** * Transition function for field_maker. Performs a nested list parse. */ field_marker(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; field(match: RegexpResult): [NodeInterface, boolean]; /** Extract & return field name from a field marker match. */ parse_field_marker(match: RegexpResult): string; parse_field_body(indented: StringList, offset: number, node: NodeInterface): void; /** Option list item. */ option_marker(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; option_list_item(match: RegexpResult): [NodeInterface, boolean]; /** * Return a list of `node.option` and `node.option_argument` objects, * parsed from an option marker match. * * :Exception: `MarkupError` for invalid option markers. */ parse_option_marker(match: RegexpResult): NodeInterface[]; doctest(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; /** First line of a line block. */ line_block(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; /** Return one line element of a line_block. */ line_block_line(match: RegexpResult, lineno: number): [NodeInterface, NodeInterface[], boolean]; nest_line_block_lines(block: NodeInterface): void; nest_line_block_segment(block: NodeInterface): void; /** Top border of a full table. */ grid_table_top(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; /** Top border of a simple table. */ simple_table_top(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; table_top(match: RegexpResult, context: ContextArray, nextState: StateType, isolate_function: () => IsolateTableResult, parser_class: ParserConstructor): ParseMethodReturnType; /** Parse a table. */ table(isolateFunction: () => IsolateTableResult, parserClass: ParserConstructor): ParseResult; isolate_grid_table(): IsolateTableResult; isolate_simple_table(): IsolateTableResult; malformed_table(block: StringList, detail?: string, offset?: number): NodeInterface[]; build_table(tabledata: TableData, tableline: number, stubColumns?: number, widths?: string): nodes.table; buildTableRow(rowdata: RowData, tableline: number): nodes.row; line(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; text(match: RegexpResult, context: ContextArray, nextState: StateInterface): ParseMethodReturnType; private isEnumeratedListItem; /** * Analyze an enumerator and return the results. :Return: - the enumerator format ('period', 'parens', or 'rparen'), - the sequence used ('arabic', 'loweralpha', 'upperroman', etc.), - the text of the enumerator, stripped of formatting, and - the ordinal value of the enumerator ('a' -> 1, 'ii' -> 2, etc.; ``None`` is returned for invalid enumerator text). The enumerator format has already been determined by the regular expression match. If `expected_sequence` is given, that sequence is tried first. If not, we check for Roman numeral 1. This way, single-character Roman numerals (which are also alphabetical) can be matched. If no sequence has been matched, all sequences are checked in order. */ private parseEnumerator; private parseDirectiveBlock; private parseDirectiveOptions; /** Parse `datalines` for a field list containing extension options matching `option_spec`. :Parameters: - `option_spec`: a mapping of option name to conversion function, which should raise an exception on bad input. - `datalines`: a list of input strings. :Return: - Success value, 1 or 0. - An option dictionary on success, an error string on failure. */ private parseExtensionOptions; private parseDirectiveArguments; } export default Body;