Can pandoc show the output of a codeblock (like jupyter notebook) when using the lhs mode?


#1

I am trying to use pandoc to create a html file that have embedded result of running code.

test.md

  ---
  title: test
  ---

  # Test

  this is a test

  ``` haskell
  print $ 1 + 1
  ```
$ pandoc test.md -f markdown+lhs -t html -s > out.html 

I want output to be 2 instead of print 1 + 1.

2019-05-26-181415_160x211_scrot
Can it be done with pandoc?


#2

Pandoc itself does not know how to execute Haskell code, but you can write your own program that combines Pandoc with a Haskell interpreter (say from the hint package).

I think Brent Yorgey did something like that a couple of years ago:

Perhaps you can look at or even use his code.


#3

Thanks, I only want to know if it can or cannot. It would be cool if the feature is added to pandoc.

For now, I would use jupyter + ihaskell as a solution for my case due to the time constrains.

BTW, adding to your answer; in case of anyone else interest, there is even the diagrams render for pandoc.


#4

Running programs is considered out of scope for pandoc and most likely won’t be added, at least not to “core” pandoc. It would be great, of course, if somebody takes the time to write a filter to do this. I set out to do that a few years back, but it proved to be too much work. And, as you note, jupyter, ihaskell, and Emacs Org mode already exist.


#5

Albert via Haskell Community discourse@haskell.org writes:

Running programs is considered out of scope for pandoc and most likely won’t be
added, at least not to “core” pandoc. It would be great, of course, if somebody
takes the time to write a filter to do this.

I’ve not followed this whole thread, but if you want a pandoc filter to
run the content of code blocks then you might try “panpipe”:

https://hackage.haskell.org/package/panpipe

It pipes the content of code blocks through arbitrary shell commands,
e.g.

```{pipe="runhaskell | tr l L"}
main = putStrLn "hello world"
```

will become:

```
heLLo worLd
```

It uses pandoc JSON, so any pandoc-supported format will work (markdown,
HTML, etc.). Another filter called “panhandle” can extract pandoc JSON
from a code block and splice it into the document:

https://hackage.haskell.org/package/panhandle

This way we can use panpipe to procedurally generate content, then use
panhandle to splice it into the document.

I’ve been using these to generate my web site for years. I’ve documented
them with a bunch of examples here:

http://chriswarbo.net/projects/activecode

Note that these both currently depend on pandoc 1.17.*, since pandoc’s
JSON format changed after that version. If that’s a problem for someone
then I don’t mind updating these filters; they’ve just been feature
complete for so long that I’ve not had a reason to make any changes.


#6

Not sure if this is any help but my knit-haskell library does this in the opposite direction, that is, it allows you to embed pandoc blocks in a haskell program.
But, I think, it may serve the same purpose, of allowing you to execute haskell code and put the results into a pandoc-created html document.


#7

This is rough around the edges, but inliterate seems promising. We did some basic tests and it works as it says on the tin.

AfC