module Anansi.Loom.HTML (loomHTML) where
import qualified Data.Text.Lazy as TL
import Control.Monad (forM_)
import Control.Monad.Writer (tell)
import Anansi.Types
import Anansi.Loom
loomHTML :: Loom
loomHTML = Loom "html" $ mapM_ putBlock where
putBlock b = case b of
BlockText text -> tell text
BlockFile path content -> let
label = TL.concat ["<b>» ", escape path, "</b>"]
in putContent label content
BlockDefine name content -> let
label = TL.concat ["<b>«", escape name, "»</b>"]
in putContent label content
putContent label cs = do
tell "<pre>"
tell label
tell "\n"
forM_ cs $ \c -> case c of
ContentText _ text -> tell . escape $ TL.append text "\n"
ContentMacro _ indent name -> tell $ formatMacro indent name
tell "</pre>"
formatMacro :: TL.Text -> TL.Text -> TL.Text
formatMacro indent name = TL.concat [indent, "<i>«", escape name, "»</i>\n"]
escape :: TL.Text -> TL.Text
escape = TL.concatMap $ \c -> case c of
'&' -> "&"
'<' -> "<"
'>' -> ">"
'"' -> """
'\'' -> "'"
_ -> TL.singleton c