frnmst/md-toc

Feature request: Support MD in argument instead of file

Closed this issue · 4 comments

First thank you for creating and maintaining this library.

We use it in Ansible, so we have written an ansible role around this library.

Today we generate the MD and write to disk in one Ansible task. We then run md_toc on the file in a second task (in python).
It works fine, but we would like to speed up the process by collapsing the two tasks in python and only write the final output to disk.

If you support the idea, I can provide a PR. I would move the parsing code to a separate function and call that from existing and new APIs. New API could use StringIO to create a stream.

Ok, thanks.

Which existing functions in md-toc version 8.0.0 need to be modified for this?

build_toc could either accept another argument with a Stream or the parsing could be moved out to a separate function build_toc_from_stream using a Stream as input. build_toc could then open the file and call build_toc_from_stream. I could then use build_toc_from_stream directly on my side.

Ok, you can create a separate function.

If possible add unit tests. You can open a PR on the dev branch when you are done.

I will close this issue again.
I discoverd your '-' filename feature and can override stdin with my StringIO:

    # Generate TOC from variable
    with StringIO(md_input) as md:
        stdin = sys.stdin
        sys.stdin = md
        toc = md_toc.build_toc('-', keep_header_levels=toc_levels, skip_lines=skip_lines)
        sys.stdin = stdin