17

I happen to really like Markdown (probably because of SO) and I like programming in Haskell. I have recently discovered Literate Haskell (LHS) and I want to use Markdown and LHS together. Let me give you this dumb example:

Crazy Literate Haskell
======================

This is an example of some literate Haskell Code:

> module Main where

Look at that IO Monad work:

> main = return 2 >>= print

Wasn't that cool?

That is an example of a literate haskell file written to be parsed by Markdown later. However, I want the code to actually appear in html code blocks and without the > before them. Therefore I cannot merely indent all of the code lines by four because that would produce the markdown that you see above. Basically, I want the html to come out like this:

<h1>Crazy Literate Haskell</h1>

<p>This is an example of some literate Haskell Code:</p>

<pre><code>module Main where
</code></pre>

<p>Look at that IO Monad work:</p>

<pre><code>main = return 2 &gt;&gt;= print
</code></pre>

<p>Wasn't that cool?</p>

The thing to notice is that it has no > symbols. How would I do that?

Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
  • Now obviously I could throw the .lhs file through a sed 's_^> _ _' but I don't really want to. I would rather some built in function that I did not know about. – Robert Massaioli Aug 13 '10 at 02:39

1 Answers1

18

Use Pandoc. It has a markdown+lhs mode for using markdown in literal Haskell files, and if you don't like the html it produces, there is an api for modifying the document structure.

Install it with

cabal install pandoc
dave4420
  • 46,404
  • 6
  • 118
  • 152
  • 6
    Robert, The html pasted here http://hpaste.org/fastcgi/hpaste.fcgi/raw?id=29029 is the result of cutting your markdown and pasting it to `pandoc` with `pbpaste | pandoc -r markdown+lhs -w html -s | pbcopy` Just in case you take up Haskell blog-writing ... note that you *can* ask `pandoc` to write `literate html` with bird-tracks: `pbpaste | pandoc -r markdown+lhs -w html+lhs | pbpaste` -- then you'd get this: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=29030 The html is oddly, but legally, formatted; you can get rid of this by passing it through `tidy`. – applicative Aug 13 '10 at 23:43
  • That is an excellent answer, both Dave and applicative. Thankyou, this was exactly what I was looking for. Marking as answer. – Robert Massaioli Aug 14 '10 at 02:47
  • And to prepare it for publishing on StackOverflow (as normal markdown, without .lhs tricks), one can do `pandoc --from markdown+lhs --to markdown`! (Only the HTML-style comments in the source markdown get treated in a wrong way. I use them for storing Emacs variables at the end of the file.) – imz -- Ivan Zakharyaschev May 05 '15 at 08:58