0 | ||| Runtime HTML elements constructors without "either_" prefix nor attributes.
 1 | |||
 2 | ||| All those constructors return an `Either` type where the:
 3 | |||
 4 | ||| - `Left` value is a message in case of error; and
 5 | ||| - the `Right` value a list with, as single item, the resulting element.
 6 | module Text.Html.Runtime.NoAttr
 7 |
 8 | import Language.Reflection
 9 | import Language.Reflection.Pretty
10 | import Language.Reflection.Syntax
11 | import Language.Reflection.Syntax.Ops
12 |
13 | import Text.Html.Attributes.Types
14 | import Text.Html.Attributes.Validation as A
15 | import Text.Html.Elements.Elab
16 | import Text.Html.Elements.Raw
17 | import Text.Html.Elements.Runtime
18 | import Text.Html.Elements.Validation
19 | import Text.Html.Elements.Types
20 | import Text.Html.Types
21 | import Text.Html.Utils
22 | import Text.Html.Values
23 |
24 | %default total
25 | %language ElabReflection
26 |
27 | private
28 | mkEitherNormalElemNoAttr :
29 |      (name : String)
30 |   -> (elemValidation : String)
31 |   -> Elab ()
32 | mkEitherNormalElemNoAttr name elemValidation =
33 |   let
34 |     functionName := UN . Basic $ name
35 |     rawFuncName  := UN . Basic $ "raw_" ++ name
36 |     elemValFunc  := UN . Basic $ elemValidation
37 |     argList      := arg $ `(Either String (List Element))
38 |   in
39 |   declare
40 |    [ export' functionName $ argList .-> `(Either String (List Element))
41 |    , def functionName
42 |      [ var functionName .$ (var "Left" .$ var "e") .=
43 |          var "Left" .$ var "e"
44 |      , var functionName .$ (var "Right" .$ var "elems") .=
45 |          var "*>"
46 |            .$ (var elemValFunc .$ var "elems")
47 |            .$ (var "Right" .$ (var rawFuncName .$ var "Nil" .$ var "elems"))
48 |      ]
49 |    ]
50 |
51 | private
52 | mkEitherVoidElemNoAttr : (name : String) -> Elab ()
53 | mkEitherVoidElemNoAttr name =
54 |   let
55 |     functionName := UN . Basic $ name
56 |     rawFuncName  := UN . Basic $ "raw_" ++ name
57 |   in
58 |   declare
59 |    [ export' functionName `(Either String (List Element))
60 |    , def functionName [var functionName .=
61 |        var "Right" .$ (var rawFuncName .$ var "Nil")
62 |      ]
63 |    ]
64 |
65 | ||| Generates element either functions using the elements' names.
66 | |||
67 | ||| The functions will be named `<name>` and will create `<name>` elements.
68 | private
69 | mkEitherElemsNoAttr : (elems : List Config) -> Elab ()
70 | mkEitherElemsNoAttr configs = mconcatMapM eitherFunc configs
71 |   where
72 |     eitherFunc : Config -> Elab ()
73 |     eitherFunc config =
74 |       if config.children
75 |       then mkEitherNormalElemNoAttr config.name config.elemsFunc
76 |       else mkEitherVoidElemNoAttr config.name
77 |
78 | %runElab mkEitherElemsNoAttr elems
79 |
80 | ||| A **title** element.
81 | export
82 | title : (text : NonWhitespace) -> Either String (List Element)
83 | title text = Prelude.map (flip raw_title text) (Right [])
84 |