0 | ||| Runtime HTML elements constructors.
  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.Elements.Runtime
  7 |
  8 | import Data.Either
  9 | import Language.Reflection
 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.Validation
 18 | import Text.Html.Elements.Types
 19 | import Text.Html.Types
 20 | import Text.Html.Utils
 21 | import Text.Html.Values
 22 |
 23 | %default total
 24 | %language ElabReflection
 25 |
 26 | ||| Generates a normal element either function using the element's name.
 27 | |||
 28 | ||| The function will be named `either_<name>` and will create a `<name>`
 29 | ||| element.
 30 | |||
 31 | ||| The second argument is the name of the validation function for the
 32 | ||| attributes.
 33 | |||
 34 | ||| The third argument is the name of the validation function for the child
 35 | ||| elements.
 36 | |||
 37 | ||| The type of the generated function is:
 38 | ||| `List Attribute -> List Element -> Either String (List Element)`
 39 | |||
 40 | ||| The following example:
 41 | |||
 42 | ||| ```idris2 example
 43 | ||| %runElab mkNormalElem "article" "standard" "flowContentElem"
 44 | ||| ```
 45 | |||
 46 | ||| generates the below code:
 47 | |||
 48 | ||| ```idris2
 49 | ||| public export
 50 | ||| either_article : List Attribute -> List Element -> Either String (List Element)
 51 | ||| either_article attrs elems =
 52 | |||      standard attrs
 53 | |||   *> flowContentElems elems
 54 | |||   *> Right (raw_article attrs elems)
 55 | ||| ```
 56 | private
 57 | mkEitherNormalElem :
 58 |      (name : String)
 59 |   -> (attrValidation : String)
 60 |   -> (elemValidation : String)
 61 |   -> Elab ()
 62 | mkEitherNormalElem name attrValidation elemValidation =
 63 |   let
 64 |     functionName := UN . Basic $ "either_" ++ name
 65 |     rawFuncName  := UN . Basic $ "raw_" ++ name
 66 |     attrValFunc  := UN . Basic $ attrValidation
 67 |     elemValFunc  := UN . Basic $ elemValidation
 68 |     argAttr      := arg $ var "List" .$ var "Attribute"
 69 |     argList      := arg $ var "List" .$ var "Element"
 70 |   in
 71 |   declare
 72 |    [ public' functionName $ argAttr .-> argList .-> `(Either String (List Element))
 73 |    , def functionName [var functionName .$ var "attrs" .$ var "elems" .=
 74 |      var "*>"
 75 |        .$ (var attrValFunc .$ var "attrs")
 76 |        .$ (var "*>"
 77 |          .$ (var elemValFunc .$ var "elems")
 78 |          .$ (var "Right" .$ (var rawFuncName .$ var "attrs" .$ var "elems"))
 79 |        )
 80 |      ]
 81 |    ]
 82 |
 83 | ||| Generates a void element either function using the element's name.
 84 | |||
 85 | ||| The function will be named `either_<name>` and will create a `<name>`
 86 | ||| element.
 87 | |||
 88 | ||| The second argument is the name of the validation function for the
 89 | ||| attributes.
 90 | |||
 91 | ||| The type of the generated function is:
 92 | ||| `List Attribute -> Either String (List Element)`
 93 | |||
 94 | ||| The following example:
 95 | |||
 96 | ||| ```idris2 example
 97 | ||| %runElab mkVoidElem "br" "standard"
 98 | ||| ```
 99 | |||
100 | ||| generates the below code:
101 | |||
102 | ||| ```idris2
103 | ||| public export
104 | ||| either_br : List Attribute -> Either String (List Element)
105 | ||| either_br attrs =
106 | |||      standard attrs
107 | |||   *> Right (raw_br attrs)
108 | ||| ```
109 | private
110 | mkEitherVoidElem : (name : String) -> (attrValidation : String) -> Elab ()
111 | mkEitherVoidElem name attrValidation =
112 |   let
113 |     functionName := UN . Basic $ "either_" ++ name
114 |     rawFuncName  := UN . Basic $ "raw_" ++ name
115 |     attrValFunc  := UN . Basic $ attrValidation
116 |     argAttr      := arg $ var "List" .$ var "Attribute"
117 |   in
118 |   declare
119 |    [ public' functionName $ argAttr .-> `(Either String (List Element))
120 |    , def functionName [var functionName .$ var "attrs" .=
121 |      var "*>"
122 |        .$ (var attrValFunc .$ var "attrs")
123 |        .$ (var "Right" .$ (var rawFuncName .$ var "attrs"))
124 |      ]
125 |    ]
126 |
127 | ||| Generates element either functions using the elements' names.
128 | |||
129 | ||| The functions will be named `either_<name>` and will create `<name>`
130 | ||| elements.
131 | private
132 | mkEitherElems : (elems : List Config) -> Elab ()
133 | mkEitherElems configs = mconcatMapM eitherFunc configs
134 |   where
135 |     eitherFunc : Config -> Elab ()
136 |     eitherFunc config =
137 |       if config.children
138 |       then mkEitherNormalElem config.name config.attributesFunc config.elemsFunc
139 |       else mkEitherVoidElem config.name config.attributesFunc
140 |
141 | %runElab mkEitherElems elems
142 |
143 | ||| Both a doctype preambule and a **html** element.
144 | public export
145 | either_html5 : List Attribute -> List Element -> Either String Html
146 | either_html5 attrs elems =
147 |      either_html attrs elems
148 |   *> Right (raw_html5 attrs elems)
149 |
150 | ||| A **title** element.
151 | public export
152 | either_title :
153 |      List Attribute
154 |   -> (text : NonWhitespace)
155 |   -> Either String (List Element)
156 | either_title attrs text = map (flip raw_title text) (A.standard attrs)
157 |