vim-literate-markdown

A Vim plugin to replicate a subset of Org mode's literate programming, for Markdown files.
git clone git://git.alex.balgavy.eu/vim-literate-markdown.git
Log | Files | Refs | README

commit 9539f297006e2c7c9d70c739b24f3c62248f29df
parent c35f8ce6a8098cb8048cc849ec193933735c4a68
Author: Alex Balgavy <alex@balgavy.eu>
Date:   Thu, 17 Mar 2022 15:14:34 +0100

Added basic test for block execution

Diffstat:
Mexamples/literate_markdown_autoload.md | 5++++-
Atests/exec_block.md | 12++++++++++++
Mtests/run.sh | 22+++++++++++++++++++++-
3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/examples/literate_markdown_autoload.md b/examples/literate_markdown_autoload.md @@ -1,6 +1,8 @@ <!-- :Tangle(vim) ../autoload/literate_markdown.vim --> # vim-literate-markdown: autoloaded functions This document specifies the functionality of the vim-literate-markdown plugin. +While in the case of a larger project like this, it is perhaps not ideal and may arguably hinder readability, it serves as a proof-of-concept to showcase the power of this plugin. + Commands and bindings are defined in the [ftplugin](literate_markdown_ftplugin.md). The general structure of the file is: @@ -36,7 +38,7 @@ Tangling is when you combine several blocks of code from a literate file into on A code block in markdown is delimited by three backticks at the start (followed optionally by the name of a language), and three backticks at the end. We'll only tangle code that includes a language, because if there's no language set, the code may not be executable. -Let's define the start and end delimiters as variables containin regular expressions: +Let's define the start and end delimiters as variables containing regular expressions: <!-- :Tangle(vim) <constants> --> ```vim @@ -111,6 +113,7 @@ First we take the current line, containing the tangle directive, and split it in let parsedline = s:ParseTangleDirective(curline) let [last_set_interp, should_expand_macros, macro_group, curfile] = parsedline ``` + The directive looks like this: `<!-- :Tangle(language) <> <macro name> /path/to/file -->`. The language is optional; if it's not specified, the block is tangled into a 'generic' file (specified in `/path/to/file`). The diamond (`<>`) is optional, and if it's included, it means that the block contains additional macros that should also be expanded. diff --git a/tests/exec_block.md b/tests/exec_block.md @@ -0,0 +1,12 @@ +# Execute block test +This test checks the basic stateless execution of blocks. + +```sh +i=4 +i=$((i+5)) +[ "$i" -eq 9 ] && printf "Correct!\n" +``` + +```sh +ps ux | grep vim +``` diff --git a/tests/run.sh b/tests/run.sh @@ -21,6 +21,26 @@ run_test() { done } +test_block_exec() { + die() { printf '%s\n' "$1" >&2 && exit 1; } + + testdir="$tempdir"/exec_block + mkdir "$testdir" + cp exec_block.md "$testdir"/exec_block.md + + # Run both blocks and check for expected output + vim -c 'silent 9 | ExecPrevBlock | $ | ExecPrevBlock | wqall' "$testdir"/exec_block.md + [ "$(sed -n 12p "$testdir"/exec_block.md)" = 'Correct!' ] || die "Test exec_block FAILED. Did not find 'Correct!'" + grep "^$(whoami).*grep vim" "$testdir"/exec_block.md >/dev/null 2>&1 || die "Test exec_block FAILED. Did not find 'grep vim' in process list" + + # Change the second block & test for expected output + vim -c '17norm A | grep -v grep' -c '17 | ExecPrevBlock | wqall' "$testdir"/exec_block.md + grep "^$(whoami).*grep vim" "$testdir"/exec_block.md >/dev/null 2>&1 && die "Test exec_block FAILED. Found 'grep vim' in process list when running second time" +} + +test_block_exec + for i in all_in_one_file only_specific_language two_different_languages simple_macros example_1 example_2 example_3; do run_test "$i"; done -# rm -r "$tempdir" + +rm -r "$tempdir" trap - INT TERM EXIT