gdritter repos telml / cbd545c
implement the rest of the current basic tags Getty Ritter 1 year, 3 months ago
1 changed file(s) with 33 addition(s) and 10 deletion(s). Collapse all Expand all
11 {-# LANGUAGE DeriveAnyClass #-}
22 {-# LANGUAGE OverloadedStrings #-}
33
4 module Main where
4 module Main (main) where
55
66 import qualified Control.Exception.Base as Exn
77 import qualified Data.ByteString.Char8 as BS
189189
190190 standardTags :: Text.Text -> [Text.Text] -> LuaM Text.Text
191191 standardTags n ps =
192 case (n, ps) of
192 case n of
193193 -- \em to produce italics
194 ("em", [r]) -> pure ("<em>" <> r <> "</em>")
195 ("em", _) -> throw (BuiltinArityMismatch 1 (length ps) n)
196 -- \strong to produce bolding
197 ("strong", [r]) -> pure ("<strong>" <> r <> "</strong>")
198 ("strong", _) -> throw (BuiltinArityMismatch 1 (length ps) n)
199 -- \li to produce list items
200 ("li", [r]) -> pure ("<li>" <> r <> "</li>")
201 ("li", _) -> throw (BuiltinArityMismatch 1 (length ps) n)
194 "em" -> simpleTag n ps (\r -> "<em>" <> r <> "</em>")
195 "strong" -> simpleTag n ps (\r -> "<strong>" <> r <> "</strong>")
196 "h1" -> simpleTag n ps (\r -> "<h1>" <> r <> "</h1>")
197 "h2" -> simpleTag n ps (\r -> "<h2>" <> r <> "</h2>")
198 "p" -> simpleTag n ps (\r -> "<p class=\"para\">" <> r <> "</p>")
199 "blockquote" -> simpleTag n ps (\r -> "<blockquote>" <> r <> "</blockquote>")
200 "tt" -> simpleTag n ps (\r -> "<code>" <> r <> "</code>")
201 "code" -> simpleTag n ps (\r -> "<pre><code>" <> r <> "</code></pre>")
202 "center" -> simpleTag n ps (\r -> "<div class=\"center\">" <> r <> "</div>")
203 -- some of the variadic ones
204 "ul" ->
205 pure ("<ul>" <> mconcat ["<li>" <> p <> "</li>" | p <- ps] <> "</ul>")
206 "ol" ->
207 pure ("<ol>" <> mconcat ["<li>" <> p <> "</li>" | p <- ps] <> "</ol>")
208 "br" ->
209 pure "<br/>"
210 "comment" -> pure ""
211 "link" -> case ps of
212 [address, text] ->
213 pure ("<a href=\"" <> address <> "\">" <> text <> "</a>")
214 _ -> throw (BuiltinArityMismatch 2 (length ps) n)
215 "img" -> case ps of
216 [address] ->
217 pure ("<img src=\"" <> address <> "\">")
218 [address, altText] ->
219 pure ("<img src=\"" <> address <> "\" alt=\"" <> altText <> "\">")
220 _ -> throw (BuiltinArityMismatch 1 (length ps) n)
202221 _ -> throw (NoSuchTag n)
222
223 simpleTag :: Text.Text -> [Text.Text] -> (Text.Text -> Text.Text) -> LuaM Text.Text
224 simpleTag _ [item] result = pure (result item)
225 simpleTag name items _ = throw (BuiltinArityMismatch 1 (length items) name)
203226
204227 handleDoc :: TeLML.Document -> LuaM Text.Text
205228 handleDoc = fmap mconcat . sequence . map handleFrag