Verified Commit 2b3ec4fc authored by Jakob Moser's avatar Jakob Moser
Browse files

Add rudimentary table of contents

parent ca1606f5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
[project]
name = "coliverter"
version = "0.3.0"
version = "0.3.1"
authors = [
  { name="Jakob Moser", email="moser@cl.uni-heidelberg.de" },
]
+25 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import importlib.resources as resources
from pathlib import Path

import pandoc
from pandoc.types import Block, BulletList, Header, Pandoc, Plain, Para, Str, Emph


def _get_filter(name: str) -> Path:
@@ -18,6 +19,28 @@ PANDOC_OPTIONS = (
    "--variable=lang=de",
    f"--lua-filter={_get_filter('heading-to-title')}",
)
TOC_PLACEHOLDER = Para([Str("[["), Emph([Str("TOC")]), Str("]]")])


def _get_table_of_contents(document: Pandoc) -> BulletList:
    toc = BulletList(
        [
            [Plain(element[2])]
            for element in pandoc.iter(document)
            if isinstance(element, Header)
        ]
    )

    return toc


def _replace(document: Pandoc, old: Block, new: Block) -> None:
    old_locations = tuple(
        path[-1] for element, path in pandoc.iter(document, path=True) if element == old
    )

    for parent, index in old_locations:
        parent[index] = new


def md_to_html(markdown: str) -> str:
@@ -31,4 +54,6 @@ def md_to_html(markdown: str) -> str:
        source=markdown, format=f"commonmark_x+{'+'.join(ACTIVE_MARKDOWN_EXTENSIONS)}"
    )

    _replace(document, TOC_PLACEHOLDER, _get_table_of_contents(document))

    return pandoc.write(document, format="html", options=PANDOC_OPTIONS)
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ wheels = [

[[package]]
name = "coliverter"
version = "0.2.1"
version = "0.3.1"
source = { editable = "." }
dependencies = [
    { name = "pandoc" },