Skip to content

ontocast.tool.chunk.contents

Tables of contents and lists of figures or tables, recognised from their content.

A contents entry is one line or one table row: a title, optional dotted leaders, and a page number at the end (up to three arabic digits, or lower-case roman). A table row is an entry only when its last cell is the page and every other cell is text. A table whose header row is not an entry names its columns, so it holds data, never contents.

A unit is a contents list when entries are at least :data:~ontocast.tool.chunk.bibliography.MIN_LIST_ENTRIES lines, make up at least :data:~ontocast.tool.chunk.bibliography.CONTENT_ONLY_SHARE of its lines, and their arabic page numbers ascend in that share of steps.

English and Latin-script conventions; a contents list it misses is extracted, never a section dropped.

Attributes

__all__ = ['looks_like_contents_list'] module-attribute

Functions:

looks_like_contents_list(text)

Whether the content alone makes a unit a table of contents or a list of figures.

Parameters:

Name Type Description Default
text str

Unit text (markdown or plain).

required

Returns:

Type Description
bool

True when the unit is mostly contents entries with ascending pages.

Source code in ontocast/tool/chunk/contents.py
def looks_like_contents_list(text: str) -> bool:
    """Whether the content alone makes a unit a table of contents or a list of figures.

    Args:
        text: Unit text (markdown or plain).

    Returns:
        True when the unit is mostly contents entries with ascending pages.
    """
    pages = _entry_pages(text)
    entries = [page for page in pages if page is not None]
    return (
        len(entries) >= MIN_LIST_ENTRIES
        and len(entries) / len(pages) >= CONTENT_ONLY_SHARE
        and _ascending(entries)
    )