-
Notifications
You must be signed in to change notification settings - Fork 0
/
Html.hs
29 lines (25 loc) · 1.04 KB
/
Html.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Jago.Render.Html
( render
) where
import qualified Data.Text as T
import Jago.AST ( Attribute
, Document
, Html(..)
)
-- | Render Html attributes to used by an element
renderAttributes :: [Attribute] -> T.Text
renderAttributes = T.intercalate " " . map renderAttribute
where renderAttribute (k, v) = mconcat [k, "=", "\"", v, "\""]
-- | Render either literal text or an element
renderHtml :: Html -> T.Text
renderHtml (Text t ) = t
renderHtml (Element name attrs child) = mconcat [start, child', end]
where
start = case attrs of
[] -> mconcat ["<", name, ">"]
xs -> mconcat ["<", name, " ", renderAttributes xs, ">"]
end = mconcat ["</", name, ">"]
child' = mconcat $ map renderHtml child
-- | Render document
render :: Document -> T.Text
render doc = mconcat [doc', "\n"] where doc' = mconcat $ map renderHtml doc