Say you have a markdown file with code blocks and want to extract only these code blocks from the document. Use the standalone pantangle.py
script to achieve this:
# print to console
python pantangle.py MY-DOCUMENT
# write to file
python pantangle.py MY-DOCUMENT > MY-FILE
The script should work as is, so just copy it from the repo, no need to install any Python packages.
However, it does require pandoc and Python, so you need to have them on your system. To install pandoc, follow these instructions, for Python, look here.
The script is tested, and thus confirmed to work, with Python versions 3.7 to 3.11 and with pandoc version 2.19.
This should work on any document type supported by pandoc, so markdown, reStructuredText, org, asciidoc, etc. should work. However, it's only tested on markdown, reStructuredText, and jupyter notebooks so far.
The idea to "tangle" code from documents is taken from Emacs org mode, which also has this feature (it's just much more powerful).
Changes to the pantangle.py
script should not be made inside this script directly, but inside of pantangle.md
, which is treated as the source of truth. It is written using literate programming. The script is then created by calling itself on the modified markdown file.
So once pantangle.md
has been changed, run:
python pantangle.py pantangle.md > foo.py && mv foo.py pantangle.py
To additionally run the test, do:
python pantangle.py pantangle.md > foo.py && mv foo.py pantangle.py && python tests.py
Optionally, run mypy
on the resulting code:
mypy --strict pantangle.py