@jtbnntt/xmd
v1.6.0
Published
`xmd` (execute Markdown) extracts code snippets from a Markdown document and executes them. In effect, it can be viewed as a tool to run a README file.
Downloads
2
Readme
xmd
xmd
(execute Markdown) extracts code snippets from a Markdown document and executes them. In effect, it can be viewed as a tool to run a README file.
The initial use case was automation of a complex project setup. Instead of creating a separate script to execute the steps, the necessary commands were included in the documentation. This made it easy to see the context of each step.
Installation
xmd
can be installed globally with npm install -g @jtbnntt/xmd
.
Running
To run xmd
, just supply the path to the executable Markdown file, e.g.,
xmd README.md
To read from stdin, provide -
in place of the filename:
xmd - < README.md
How prepare the Markdown document
The document must contain two types of information:
- An execution tree
- Named code snippets
The execution tree
The execution tree describe the hierarchy of steps that must run in order to complete the process. Children are considered to be dependencies of their parents, so all child nodes must complete before the parent runs.
The execution tree is created by writing a code block with a language of xmd
, like so:
```xmd
parent
child1
grandchild1
child2
child3
```
Levels of indentation are used to create the hierarchy.
A node may exist for organizational purposes only (that is, not refer to code that should be run) or may refer to a snippet. In the example above, parent
might not have its own code, but will not be considered complete until all the children have run.
The longest form for a node uses this format:
<name>: <code-reference> (<command>)
For an organizational node, only the name is necessary. If the name matches a code snippet's name, it will also be considered a code reference. The optional command can be used to control the command used to execute a snippet. Here are some examples of legal lines:
Some node
Some node: some-node
Some Node (/bin/bash)
Some Node: some-node (/bin/bash)
some-node
some-node (/bin/bash)
Environment variables to be supplied to each script may be specified in the form KEY=VALUE
.
Code snippets
Code snippets are the blocks of code that should be executed. If a language is specified using the standard Markdown style, it will be used to find the appropriate executor (unless overridden by the command in the execution tree).
Here's an example of a code snippet:
```sh (write-date)
date > date-file
```
Imagine the execution tree looks like this:
```xmd
Write date to file date.txt: write-date
```
When run, an executor registered as sh
will be looked up and used to run the script. The sh
executor runs /bin/sh
.
When running xmd
, snippets may be skipped by specifying them with the -s/--skip
option. The execution tree below them will be skipped as well.
Executors
Currently, the following type to command mappings exist:
sh
=>/bin/sh
js
=>node
Other commands, if they do not match the type, will need to be specified in the execution tree.
Envisioned improvements
- Allow a node to reference another part of the tree as a dependency
- Improve error handling
- Improve documentation
- Allow executor mappings to be specified as command line arguments
- Allow specified nodes to be run
Example
To see the output of this example, run
xmd ./README.md
HEADING_START=This is
This node must wait for its children to complete
Node using /bin/sh: child1
Node run as executable file: child2
Node running file with node: child3 (node)
Nothing to see here...
Or here...
Or here...
echo ==============================================================================
echo $HEADING_START child1 "(sh)"
echo
echo The type sh is mapped to /bin/sh, which is used to run this node.
echo ==============================================================================
#!/usr/bin/env python
import os
print('==============================================================================')
print(os.environ['HEADING_START'] + ' child2 (python)')
print('')
print('This node runs as an executable file and therefore relies on the shebang to')
print('specify the command.')
print('==============================================================================')
console.log('==============================================================================')
console.log(process.env.HEADING_START + ' child3 (node)');
console.log('');
console.log('This node runs with the command set explicitly in the execution tree');
console.log('definition.');
console.log('==============================================================================');