Created
February 6, 2012 06:08
-
-
Save paf31/1750145 to your computer and use it in GitHub Desktop.
lhs2html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 '<' "<") . | |
| > (replaceChar '>' ">") . | |
| > (replaceChar '&' "&") | |
| > 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