Skip to content

Instantly share code, notes, and snippets.

@jneira
Created April 1, 2015 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneira/33be67ffe1265e12d9e8 to your computer and use it in GitHub Desktop.
Save jneira/33be67ffe1265e12d9e8 to your computer and use it in GitHub Desktop.
type WithST h r = Eff (st:: ST h | r)
add :: forall h r. STRef h String -> String -> WithST h r String
add ref str = modifySTRef ref (++ str)
renderST :: Element -> String
renderST = pureST <<< renderElemST
renderElemST :: forall h r. Element -> WithST h r String
renderElemST e = do
c <- newSTRef ""
addElement c e
addElement :: forall h r. STRef h String -> Element -> WithST h r String
addElement c (Element e) = do
add c $ "<"++e.name
for_ e.attribs $ \a -> do
add c " "
addAttribute c a
addContent c e.content e.name
addAttribute :: forall h r. STRef h String -> Attribute ->
WithST h r String
addAttribute ref (Attribute {key = k, value = (Just v)}) =
add ref (k++"=\""++v++"\"")
addContent :: forall h r. STRef h String -> Maybe (Content Unit) ->
String -> WithST h r String
addContent ref Nothing _ = add ref " />"
addContent ref (Just (Content content)) name = do
add ref ">"
goEff (addContentItem ref) content
add ref "</"
add ref name
add ref ">"
addContentItem :: forall h r a. STRef h String ->
ContentF (Free ContentF a) ->
WithST h r (Free ContentF a)
addContentItem ref (TextContent s a) = do
add ref s
return a
addContentItem ref (ElementContent e a) = do
addElement ref e
return a
addContentItem ref (CommentContent c a) = do
add ref "<!-- "
add ref c
add ref " -->"
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment