/** * TRP classes for (generic document) layout analysis objects * * See: https://docs.aws.amazon.com/textract/latest/dg/layoutresponse.html */ import { ApiBlockType, ApiRelationshipType } from "./api-models/base"; import { ApiBlock } from "./api-models/document"; import { ApiLayoutBlock, ApiLayoutFigureBlock, ApiLayoutFooterBlock, ApiLayoutHeaderBlock, ApiLayoutKeyValueBlock, ApiLayoutListBlock, ApiLayoutPageNumberBlock, ApiLayoutSectionHeaderBlock, ApiLayoutTableBlock, ApiLayoutTextBlock, ApiLayoutTitleBlock } from "./api-models/layout"; import { ApiObjectWrapper, IApiBlockWrapper, IBlockManager, IBlockTypeFilterOpts, INestedListOpts, IRenderable, IRenderOpts, IWithParentPage, IWithRelatedItems, PageHostedApiBlockWrapper } from "./base"; import { IWithContent, LineGeneric } from "./content"; import { FieldGeneric, IWithForm } from "./form"; import { Geometry, IWithGeometry } from "./geometry"; import { IWithTables, TableGeneric } from "./table"; /** * Standard interface for parsed Layout items */ export interface ILayoutItem & IRenderable, TPage extends IApiBlockWrapper & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables, TGeometryHost extends ApiObjectWrapper> extends IApiBlockWrapper, IRenderable, IWithContent, IWithParentPage, IWithGeometry { /** * 0-100 based confidence of the layout model for this element (*separate* from OCR confidence!) */ confidence: number; /** * Position of this layout element on the input image/page */ geometry: Geometry; /** * Number of layout items (any layout blocks) linked as direct children of this item * * Note this does *not* include any nested children. At the time of writing, only * LAYOUT_LIST items link to LAYOUT_TEXT children - so no nesting should be present anyway. */ get nLayoutChildrenDirect(): number; /** * *Total* number of layout items (any layout blocks) linked as direct or indirect children * * This includes any nested children. At the time of writing, only LAYOUT_LIST items link to * LAYOUT_TEXT children - so no nesting should be present anyway. */ get nLayoutChildrenTotal(): number; /** * Number of text `Line`s in this object */ get nTextLines(): number; /** * Parsed page layout collection that this element belongs to */ parentLayout: LayoutGeneric; html(opts?: IBlockTypeFilterOpts): string; /** * Layout items (any layout block types) linked as children to this item * * Supports recursing via `opts.deep`, but at the time of writing only LAYOUT_LIST items link to * LAYOUT_TEXT children - so no nesting should be present anyway. */ iterLayoutChildren(opts: IBlockTypeFilterOpts & INestedListOpts): Iterable & IRenderable, TPage, ApiObjectWrapper>>; /** * Iterate through the text `Line`s in this object * @example * for (const line of layItem.iterTextLines()) { * console.log(line.text); * } * @example * [...layItem.iterTextLines()].forEach( * (line) => console.log(line.text) * ); */ iterTextLines(): Iterable>; /** * Layout items (any layout block types) linked as children to this item * * Supports recursing via `opts.deep`, but at the time of writing only LAYOUT_LIST items link to * LAYOUT_TEXT children - so no nesting should be present anyway. */ listLayoutChildren(opts?: IBlockTypeFilterOpts & INestedListOpts): Array & IRenderable, TPage, ApiObjectWrapper>>; /** * List the text `Line` items in this object */ listTextLines(): Array>; } /** * Generic class on which layout items are based */ declare class LayoutItemBaseGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends PageHostedApiBlockWrapper implements IWithGeometry>, IWithParentPage { _geometry: Geometry>; _parentLayout: LayoutGeneric; constructor(block: TBlock, parentLayout: LayoutGeneric); get confidence(): number; get geometry(): Geometry>; get nLayoutChildrenDirect(): number; get nLayoutChildrenTotal(): number; get parentLayout(): LayoutGeneric; iterLayoutChildren(opts?: IBlockTypeFilterOpts & INestedListOpts): Iterable & IRenderable, TPage, ApiObjectWrapper>>; listLayoutChildren({ deep, includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts & INestedListOpts): Array & IRenderable, TPage, ApiObjectWrapper>>; } declare const LayoutLineContainerItem_base: { new (...args: any[]): { getText(opts?: IBlockTypeFilterOpts | undefined): string; iterContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Iterable>; listContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): LineGeneric[]; readonly nContentItems: number; get text(): string; readonly id: string; readonly blockType: ApiBlockType; readonly childBlockIds: string[]; relatedBlockIdsByRelType(relType: ApiRelationshipType | ApiRelationshipType[]): string[]; _dict: ApiBlock; readonly dict: ApiBlock; parentPage: IBlockManager; iterRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): Iterable>; listRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): IApiBlockWrapper[]; }; } & typeof LayoutItemBaseGeneric; /** * Common base class for Layout items whose `Child`ren are always text LINEs * * Not a big fan of relying heavily on this level of hierarchy because of its fragility... But at * the moment it seems like almost every Layout item contains only text `LINE` blocks, and it'd be * better to represent the `.text` of these as newline-joined rather than the default space-joined * from the mixin... So for now this helps us reduce some code duplication. */ declare class LayoutLineContainerItem & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem_base { getText(opts?: IBlockTypeFilterOpts): string; /** * Iterate through the text `Line`s in this object * * (Should be equivalent to iterContent, for LayoutLineContainer items that only contain text * LINE objects) * * @example * for (const line of layItem.iterTextLines()) { * console.log(line.text); * } * @example * [...layItem.iterTextLines()].forEach( * (line) => console.log(line.text) * ); */ iterTextLines(): Iterable>; /** * List the text `Line` items in this object * * (Should be equivalent to listContent, for LayoutLineContainer items that only contain text * LINE objects) */ listTextLines(): Array>; /** * Number of text `Line`s in this object * * (Should be equivalent to nContentItems, for LayoutLineContainer items that only contain text * LINE objects) */ get nTextLines(): number; } /** * Generic base class for a layout entity describing a diagram, figure, or image * * If you're consuming this library, you probably just want to use `document.ts/LayoutFigure`. */ export declare class LayoutFigureGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a figure is a
of class `figure` * * Detected text within the figure (if any), is included inside the div */ html(opts?: IBlockTypeFilterOpts): string; /** * The str() representation of a figure allows including text, but usually doesn't have any */ str(): string; } /** * Generic base class for a layout entity describing a page footer element * * If you're consuming this library, you probably just want to use `document.ts/LayoutFooter`. */ export declare class LayoutFooterGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a Footer element is a
of class `footer-el` * * Note that there might be multiple footer elements on a page (e.g. horizontal columns) */ html(opts?: IBlockTypeFilterOpts): string; str(): string; } /** * Generic base class for a layout entity describing a page header element * * If you're consuming this library, you probably just want to use `document.ts/LayoutHeader`. */ export declare class LayoutHeaderGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a Footer element is a
of class `footer-el` * * Note that there might be multiple footer elements on a page (e.g. horizontal columns) */ html(opts?: IBlockTypeFilterOpts): string; str(): string; } /** * Generic base class for a layout entity describing a key-value (form data) pair * * If you're consuming this library, you probably just want to use `document.ts/LayoutKeyValue`. */ export declare class LayoutKeyValueGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * Utility function to list all content (word, signature, etc) block IDs within a K-V Form Field * * Used to support mapping between Layout and K-V Forms objects e.g. in * `_mapPageContentToFormFields()` * * @param field Parsed TRP.js `Field` object from forms analysis * @returns Snapshot list of all the content (word, etc) block IDs in the Field's Key and Value */ protected _listContentIdsInFormField(field: FieldGeneric): string[]; /** * Utility function to generate a (point-in-time) mapping from field K/V content to field objects * * Since Textract JSON only stores one-way mappings from Key->WORD, Key->Value, and * Value->(WORD/SELECTION_ELEMENT/SIGNATURE), and from LAYOUT_KEY_VALUE->LINE->WORD, we need to * calculate some inverse mappings to enable searches to link from Layout to form Fields. * * TODO: If this has utility elsewhere, move it to FormGeneric and consider caching? */ protected _mapPageContentToFormFields(): { [blockId: string]: FieldGeneric; }; /** * Iterate through the Form Fields spanned by this LayoutKeyValue item * * Note this is a non-trivial lookup that requires searching through associated WORD blocks, due * to the structure of Textract responses. If Textract FORMS analysis was not also enabled in the * API, this will return an empty list []. * * @example * for (const field of form.iterFields()) { * console.log(field?.key.text); * } * @example * const fields = [...form.iterFields()]; */ iterFields(): Iterable>; /** * List the Form Fields spanned by this LayoutKeyValue item * * Note this is a non-trivial lookup that requires searching through associated WORD blocks, due * to the structure of Textract responses. If Textract FORMS analysis was not also enabled in the * API, this will return an empty list []. */ listFields(): FieldGeneric[]; /** * The semantic HTML representation for a key-value element is a
of class "key-value" * * Note this attempts to reconcile the contained content to Forms analysis' Key-Value pairs for * optimally semantic HTML, *if* the Forms feature was also enabled - but this is a non-trivial * operation. * * Since there's no guaranteed 1:1 correspondence between Layout K-V regions and detected form * fields, we loop through the plain text (from layout) but insert the semantic HTML for a whole * K-V Form Field at the first overlapping mention. */ html(opts?: IBlockTypeFilterOpts): string; /** * The human-readable `str()` representation for this element does not do KV Forms reconciliation * * It just returns the Layout object's `text` with some bookends for clarity. */ str(): string; } /** * Generic base class for a layout entity describing a page number annotation * * If you're consuming this library, you probably just want to use `document.ts/LayoutPageNumber`. */ export declare class LayoutPageNumberGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a page number element is a
of class "page-num" */ html(opts?: IBlockTypeFilterOpts): string; str(): string; } /** * Generic base class for a layout entity describing a section heading / title * * If you're consuming this library, you probably just want to use `document.ts/LayoutSectionHeader`. */ export declare class LayoutSectionHeaderGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a section heading is a

tag */ html(opts?: IBlockTypeFilterOpts): string; str(): string; } /** * Generic base class for a layout entity describing a table * * If you're consuming this library, you probably just want to use `document.ts/LayoutTable`. */ export declare class LayoutTableGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * Utility function to list all content (word, signature, etc) block IDs within a parsed Table object * * Used to support mapping between Layout and Tables analysis objects e.g. in * `_mapPageContentToTables()` * * @param field Parsed TRP.js `Field` object from forms analysis * @returns Snapshot list of all the content (word, etc) block IDs in the Field's Key and Value */ protected _listContentIdsInTable(table: TableGeneric): string[]; /** * Utility function to generate a (point-in-time) mapping from table content to Table objects * * Since Textract JSON only stores one-way mappings from CELL/MERGED_CELL->WORD, TABLE->CELL etc, * we need to calculate some inverse mappings to enable searches to link from Layout to table * objects. * * TODO: If this has utility elsewhere, move it to TableGeneric and consider caching? */ protected _mapPageContentToTables(): { [blockId: string]: TableGeneric; }; /** * Iterate through the Table objects spanned by this LayoutTable item (usually just 1) * * Note this is a non-trivial lookup that requires searching through associated WORD blocks, due * to the structure of Textract responses. If Textract TABLES analysis was not also enabled in * the API, this will return an empty list []. * * @example * for (const field of form.iterFields()) { * console.log(field?.key.text); * } * @example * const fields = [...form.iterFields()]; */ iterTables(): Iterable>; /** * List the Table objects spanned by this LayoutTable item (usually just 1) * * Note this is a non-trivial lookup that requires searching through associated WORD blocks, due * to the structure of Textract responses. If Textract TABLES analysis was not also enabled in * the API, this will return an empty list []. */ listTables(): TableGeneric[]; /** * The outer semantic HTML representation for a key-value element is a
* * IF Textract TABLES analysis was also run on the document, this will attempt to reconcile the * tagged Layout content to extracted `Table`(s) - but this is a non-trivial operation. * * Since there's no guaranteed 1:1 correspondence between the Layout Table regions and detected * Table objects, populate the div content by looping through the plain text (from layout) but * inserting the semantic HTML for each whole `` at the first overlapping mention. */ html(opts?: IBlockTypeFilterOpts): string; /** * The human-readable `str()` representation for this element does not do Tables reconciliation * * It just returns the Layout object's `text` with some bookends for clarity. */ str(): string; } /** * Generic base class for a layout entity describing a paragraph of text * * If you're consuming this library, you probably just want to use `document.ts/LayoutText`. */ export declare class LayoutTextGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a text element is a

paragraph tag */ html(opts?: IBlockTypeFilterOpts): string; /** * The human-readable string representation for a text element is just the text itself. */ str(): string; } /** * Generic base class for a layout entity describing a top-level document title * * If you're consuming this library, you probably just want to use `document.ts/LayoutTitle`. */ export declare class LayoutTitleGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutLineContainerItem implements ILayoutItem, TPage, LayoutItemBaseGeneric> { /** * The semantic HTML representation for a top-level title is a

tag */ html(opts?: IBlockTypeFilterOpts): string; str(): string; } declare const LayoutListGeneric_base: { new (...args: any[]): { getText(opts?: IBlockTypeFilterOpts | undefined): string; iterContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): Iterable & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables>>; listContent({ includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts): LayoutTextGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables>[]; readonly nContentItems: number; get text(): string; readonly id: string; readonly blockType: ApiBlockType; readonly childBlockIds: string[]; relatedBlockIdsByRelType(relType: ApiRelationshipType | ApiRelationshipType[]): string[]; _dict: ApiBlock; readonly dict: ApiBlock; parentPage: IBlockManager; iterRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): Iterable>; listRelatedItemsByRelType(relType: ApiRelationshipType | ApiRelationshipType[], opts?: IBlockTypeFilterOpts | undefined): IApiBlockWrapper[]; }; } & typeof LayoutItemBaseGeneric; /** * Generic base class for a layout entity describing a bulleted or numbered list * * If you're consuming this library, you probably just want to use `document.ts/LayoutList`. */ export declare class LayoutListGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> extends LayoutListGeneric_base implements ILayoutItem & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables>, TPage, LayoutItemBaseGeneric> { /** * Render the list as HTML * * TODO: Support ordered/numbered lists with
    , if we can infer when to use it? * TODO: innerHTML option on LayoutText to get rid of the

    tags maybe? * * @param opts Optional configuration for filtering rendering to certain content types */ html(opts?: IBlockTypeFilterOpts): string; /** * Iterate through the text `Line`s in this object * * @example * for (const item of layList.iterTextLines()) { * console.log(item.text); * } * @example * [...layList.iterTextLines()].forEach( * (line) => console.log(line.text) * ); */ iterTextLines(): Iterable>; /** * List the text `Line` items in this object */ listTextLines(): Array>; /** * The human-readable `str()` representation of a layout list is same as the (bulleted) `.text` */ str(): string; /** * Number of text `Line`s in this object */ get nTextLines(): number; /** * Text for LayoutList is rendered with '-' bullet points at 2/4-space indentation */ get text(): string; } /** * TypeScript type collecting all possible TRP parsed objects corresponding to layout elements */ export type LayoutItemGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> = LayoutFigureGeneric | LayoutFooterGeneric | LayoutHeaderGeneric | LayoutKeyValueGeneric | LayoutListGeneric | LayoutPageNumberGeneric | LayoutSectionHeaderGeneric | LayoutTableGeneric | LayoutTextGeneric | LayoutTitleGeneric; /** * Generic base class for a page-level Layout analysis container * * If you're consuming this library, you probably just want to use `document.ts/Layout`. */ export declare class LayoutGeneric & IBlockManager & IWithForm & IWithRelatedItems> & IWithTables> implements IRenderable, IWithParentPage { _parentPage: TPage; constructor(parentPage: TPage); /** * *Total* number of layout elements detected on the page, including nested items * * @deprecated Migrate to `.nItemsTotal` for clarity. */ get nItems(): number; /** * Number of *top-level* layout elements detected on the page * * Some LAYOUT_* blocks may point to others as children, and this count will only include * top-level items. */ get nItemsDirect(): number; /** * *Total* number of layout elements detected on the page, including nested items * * Some LAYOUT_* blocks may point to others as children, and this count will include all layout * items on the page, not just those at the top level. */ get nItemsTotal(): number; /** * Parsed TRP.js page to which this Layout corresponds */ get parentPage(): TPage; /** * Collects the plain text content of all layout items, connected by \n\n */ get text(): string; /** * Concatenate the HTML representations of items in the layout * * Since this class is just a collection and not an API object wrapper, we don't wrap the content * HTML in anything: Leave that up to `Page`, `Document`, etc. */ html(opts?: IRenderOpts): string; /** * Iterate through (just the top level, or all) the Items in the Layout. */ iterItems({ deep, includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts & INestedListOpts): Iterable>; /** * List (just the top level, or all) the Items in the Layout. */ listItems({ deep, includeBlockTypes, onUnexpectedBlockType, skipBlockTypes, }?: IBlockTypeFilterOpts & INestedListOpts): LayoutItemGeneric[]; /** * The human-readable `str()` representation for a page layout * * This includes contained items' `str()`s, with bookends for clarity */ str(): string; } export {};