Skip to main content

Generates ('tangles') source code from Markdown documents

Project description

app-badge Version License Format PyVer Downloads

md-tangle

This project is a result of wanting config and setup files to be part of a document explaining my setup. I originally used Org-mode and org-babel-tangle in Emacs to achieve this. I really like Org-mode and Emacs, but I'm not fond of being dependent on one editor. This is the reason I wanted a CLI, and a more widely used document markup language.

This way of programming is called literate programming. This programming paradigm was introduced by Donald Knuth. The idea is to write a program as an explanation of the program logic in a natural language interspersed with snippets of traditional source code. The source code can then be generated ("tangled") by using some tool.

As Markdown is used by most programmers, I saw that language fit for the task. Markdown is a plaintext-ish format popular with programmers. It's simple, easy and already has support for embedding code blocks using ``` or ~~~~, mostly for the purposes of syntax highlighting in documentation.

Installing

This CLI tool can be installed from PyPI using pip.

pip install md-tangle

For local development, you can install it in editable mode:

pip install -e .

Command

By adding the keyword tangle:<path/filename>, this tool will tangle tagged code blocks to given file. Supports ~ for home directory.

One can tangle the code block to multiple files by separating the files with chosen separator (default: ,).

If the file already exists, the user will be prompted with the option to overwrite, unless the -f/--force flag is added.

Tags

By adding the keyword tags:<tag> to a tangled code block, it will NOT be included in the resulting files if not included with the -i/--include flag.

Flags

  • -h/--help: Show help message and exit
  • --version: Show installed version
  • -f/--force: Force overwrite of files if the already exists
  • -v/--verbose: Show output
  • -d/--destination: Overwrite output destination
  • -i/--include: Include tagged code blocks (separator=',')
  • -s/--separator: Separator for tangle destinations (default=',')
  • -p/--block-padding: Add N newlines between code blocks when writing to file (default=0)

Usage

Take the following example:

HelloWorld.md

# Some title
Describing the following code... bla bla.

~~~~javascript tangle:helloWorld.js
console.log("Hello, ");
console.log("world");
~~~~

## Styling
Adding header for my css files:

~~~~css tangle:styles/button.css,styles/input.css
/* Styling for mye awesome app */
~~~~

By adding some css ... 

~~~~css tangle:styles/button.css
#button1 {
    border: none;
}
~~~~

~~~~css tangle:styles/input.css
#button1 {
    border: none;
}
~~~~

~~~~css tangle:styles/theme.css tags:theme
#button1 {
    border-color: red;
}
~~~~

By installing md-tangle with pip, one could simply produce files from this file by executing:

$ md-tangle -v HelloWorld.md 
helloWorld.js                                      2 lines
styles/button.css                                  4 lines
styles/input.css                                   4 lines
$ ls 
helloWorld.js HelloWorld.md styles

If you also want to include tagged code blocks, you can run

$ md-tangle -v -i theme HelloWorld.md
helloWorld.js                                      2 lines
styles/button.css                                  4 lines
styles/input.css                                   4 lines
styles/theme.css                                   3 lines
$ ls
helloWorld.js HelloWorld.md styles

Documentation

The documentation for md-tangle is of course written in Markdown, and tangles to the source code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md_tangle-2.0.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md_tangle-2.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file md_tangle-2.0.0.tar.gz.

File metadata

  • Download URL: md_tangle-2.0.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for md_tangle-2.0.0.tar.gz
Algorithm Hash digest
SHA256 feb19cd7be97c2f5abdf84645a3e29f740cbba97c516e513bbd611c7415efaa6
MD5 c9a4ccc8ba26197f98758cb58363a169
BLAKE2b-256 a1b67503dd4dfc883fe0a46589ba77fd6c277e004c313e6c5d9d7de420ca65c8

See more details on using hashes here.

File details

Details for the file md_tangle-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: md_tangle-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for md_tangle-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 919eebc2b4831f38ae5ea564caab2c4cfc9be9ff20a0ef1936ef3c0cb1d542c2
MD5 e548fc4cd9b89a4111504c1ea860257b
BLAKE2b-256 5de280812aa74e3b7ce1644277eb82758d5b7539cdf9b212d2a6096ca79b26a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page