A Pandoc filter that identifies code blocks(Haskell), executes the code in GHCI and embeds the results in the returned Markdown.
Often a markdown(or any pandoc supported document) for any Haskell related documentation or a technical blog post involves code blocks. The code block could include definitions and also a demonstration of output with an interactive prompt. For, example, take this code block:
-- README.md -- definition increment:: Integer -> Integer increment x = x + 1 -- interactive prompt to demostrate the working of definitions so far >> increment 41 It would be nice if this code block was automatically evaluated and output of increment 41 is automatically recorded below >> increment 41, as follows:
-- README.md -- definition increment:: Integer -> Integer increment x = x + 1 -- interactive prompt to demostrate the working of definitions so far >> increment 41 42 Notice, that the 42 is automatically populated by this filter while transforming the original document.
To transform the document, we need to run the document through the pandoc filter, as follows:
-- set up pandoc_filter to the executable of this program (see Installation) pandoc -s -t json README.md | pandoc_filter | pandoc -f json -t markdown - [Stack](https://docs.haskellstack.org/en/stable/README/) Currently, this filter can be installed from the source (it will be available on Hackage once the tool is stable).
git clone https://github.com/gdevanla/pandoc-markdown-ghci-filter.git cd pandoc-markdown-ghci-filter stack build stack setup # Note, this command copies this tool to ~/.local/bin. # test it on a test_markdown file pandoc -s -t json README.md | pandoc-markdown-ghci-filter-exe | pandoc -f json -t markdown - All interactive statements (prefixed with
>>) need to be preceded by\nto let the filter respect original new line spacing. If this is not followed,\nmay be truncated. - The program internally wraps all commands inside the GHCi multi-line contruct
:{..:}. Therefore, the code segments should not have multiline constructs as part of code blocks. - If you want the filter to ignore a certain
codeblock, you can turn-off the filter by setting thecodeblock attribute as follows
{.haskell code_filter="Off"} -- do not run this code through GHCi >> putStrLn "This line will not be expanded by the filter" This line will not be expanded by the filter Note, the default value is "On"
- Attaching different formattting properties to
output. - As explained in
Usage Notes, allinteractivestatements should be preceded by an empty line, for the filter to maintain the\ncharacters as given by the input.