Skip to content

Instantly share code, notes, and snippets.

@paf31
Created February 6, 2012 06:08
Show Gist options
  • Select an option

  • Save paf31/1750145 to your computer and use it in GitHub Desktop.

Select an option

Save paf31/1750145 to your computer and use it in GitHub Desktop.
lhs2html
As my first venture into the world of Haskell as a scripting language, I decided to write a small command line tool to automate the task of producing HTML suitable for Blogger posts from Literate Haskell files.
The tool can be invoked on the command line. It takes Literate Haskell on standard input, which it assumes is formatted correctly, and formats the code as HTML on standard output.
> module Main where
> import Data.List ( intercalate )
> isComment ('>':' ':_) = False
> isComment _ = True
> subseqsWhere :: (a -> Bool) -> [a] -> [[a]]
> subseqsWhere p = subseqsWhere' [] [] where
> subseqsWhere' xss xs [] = if null xs then xss else xss ++ [xs]
> subseqsWhere' xss xs (x:xs')
> | p x = subseqsWhere' xss (xs ++ [x]) xs'
> | otherwise = subseqsWhere' (if null xs then xss else xss ++ [xs]) [] xs'
> formatLines :: [String] -> String
> formatLines lines =
> let encoded = map encodeHtml lines in
> case (isComment $ head $ lines) of
> True -> "<p>" ++ (intercalate "\n" encoded) ++ "</p>"
> False -> "<pre>" ++ (intercalate "<br />" encoded) ++ "</pre>"
> replaceChar x0 repl = flip (>>=) (\x -> if x == x0 then repl else [x])
> encodeHtml = (replaceChar '<' "&lt;") .
> (replaceChar '>' "&gt;") .
> (replaceChar '&' "&amp;")
> process = (map formatLines) . subseqsWhere (not . null)
> main = interact (unlines . process . lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment