Skip to content

Instantly share code, notes, and snippets.

@tomjnixon
Created December 24, 2010 14:59
Show Gist options
  • Save tomjnixon/754315 to your computer and use it in GitHub Desktop.
Save tomjnixon/754315 to your computer and use it in GitHub Desktop.
import Control.Monad.State
-- Append a string to the state.
string :: String -> State [String] ()
string s = State $ \ss -> ((), ss ++ [s])
-- Get the string out of the state.
to_str (State ss) = concat $ b
where (a, b) = ss []
-- HTML stuff.
-- Format a html argument list.
format_arglist ((key, val) : xs) = " " ++ key ++ "=\"" ++ val ++ "\"" ++ format_arglist xs
format_arglist [] = ""
-- Format a tag with a name, some arguments and a body.
tag name args contents = do
string $ "<" ++ name ++ format_arglist args ++ ">"
contents
string $ "</" ++ name ++ ">"
-- Some example tags.
ul = tag "ul"
li = tag "li"
emph = tag "emph" []
-- A 'normal' html page with a title and a body.
html title body = do
tag "html" [] $ do
tag "head" [] $ do
tag "title" [] $ string title
tag "body" [] body
-- An example
my_html = to_str $ html "test page" $ do
string "Here is a list:"
ul [("class", "zomg")] $ do
li [] $ string "foo"
li [] $ do
string "some "
emph $ string "bold"
string " text"
li [] $ string "baz"
string "done!"
-- This should print:
-- <html><head><title>test page</title></head><body>Here is a list:
-- <ul class="zomg"><li>foo</li><li>some <emph>bold</emph> text</li>
-- <li>baz</li></ul>done!</body></html>
main = putStrLn my_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment