gdritter repos telml-markup / 2e8243f
Add some comments, fix a single typo Getty Ritter 9 years ago
1 changed file(s) with 11 addition(s) and 2 deletion(s). Collapse all Expand all
88 import Text.Blaze.Html5 hiding (map, head, html)
99 import Text.Blaze.Html5.Attributes hiding (name)
1010
11 -- | Render a TeLML document with an extra set of possible tags.
1112 renderWith :: [(String, Renderer)] -> Document -> Either String Html
1213 renderWith rs =
1314 fmap (void . sequence) . mapM (renderPara (basicTags ++ rs)) . gatherPara
1415
16 -- | Render a TeLML document with the default set of tags.
1517 render :: Document -> Either String Html
1618 render = renderWith []
1719
20 -- This is a gross function, but I'm not sure how to decompose it any
21 -- other way. It takes a Document---i.e. a set of Fragments---and
22 -- splits it apart whenever it comes across double newlines.
1823 gatherPara :: Document -> [Document]
1924 gatherPara = reverse . map reverse . go [[]]
2025 where go rs [] = rs
2631 ((Text (head xs):r) : rs)) ts
2732 go _ _ = error "[unreachable]"
2833
34 -- Split a string at double-newlines.
2935 splitString :: String -> [String]
3036 splitString = filter (/= "") . go
3137 where go ('\n':'\n':xs) = "\n":go xs
3238 go (x:xs) = let r:rs = go xs in ((x:r):rs)
33 go [] = [""]
39 go "" = [""]
3440
41 -- This is just to make type signatures shorter
3542 type HtmlE = Either String Html
3643
3744 type Renderer = (Fragment -> HtmlE, [Document]) -> HtmlE
3845
46 -- The built-in set of tags (subject to change)
3947 basicTags :: [(String, Renderer)]
4048 basicTags =
4149 [ ("em"
4856 )
4957 , ("code"
5058 , \case (f,[rs]) -> fmap (code . sequence_) (mapM f rs)
51 _ -> Left "wrong arity for strong/1"
59 _ -> Left "wrong arity for code/1"
5260 )
5361 , ("link"
5462 , \case (f,[[Text l],r]) -> let go h = a ! href (stringValue l) $ h
5866 )
5967 ]
6068
69 -- render a single paragraph
6170 renderPara :: [(String, Renderer)] -> Document -> Either String Html
6271 renderPara taglist ds = fmap (p . sequence_) (mapM go ds)
6372 where go (Text ts) = Right (toMarkup ts)