0 | ||| Functions to create HTML elements with compile time validation.
  1 | |||
  2 | ||| Each of the below function has the same name as in HTML.
  3 | |||
  4 | ||| The first parameter is the list of attributes.
  5 | ||| The second parameter, if applicable is the element's content, which is in
  6 | ||| most of the cases a list of children elements.
  7 | |||
  8 | ||| The return value is a list of element rather than a single element. This
  9 | ||| allows an easier composition of the elements.
 10 | module Text.Html.Elements
 11 |
 12 | import Data.Either
 13 | import Data.String
 14 | import Language.Reflection
 15 | import Language.Reflection.Syntax
 16 | import Language.Reflection.Syntax.Ops
 17 |
 18 | import Text.Html.Attributes.Types
 19 | import Text.Html.Attributes.Validation
 20 | import Text.Html.Elements.Elab
 21 | import Text.Html.Elements.Raw
 22 | import Text.Html.Elements.Runtime
 23 | import Text.Html.Elements.Types
 24 | import Text.Html.Elements.Validation
 25 | import Text.Html.Types
 26 | import Text.Html.Utils
 27 | import Text.Html.Values
 28 |
 29 | %default total
 30 | %language ElabReflection
 31 |
 32 | ||| Generates a normal element function using the element's name.
 33 | |||
 34 | ||| The function will be named `<name>` and will create a `<name>` element.
 35 | |||
 36 | ||| The type of the generated function is:
 37 | |||
 38 | ||| ```idris2
 39 | |||    (attrs : List Attribute)
 40 | ||| -> (elems : List Element)
 41 | ||| -> {auto 0 prf : IsRight (either_<name> attrs elems)}
 42 | ||| -> List Element
 43 | ||| ```
 44 | |||
 45 | ||| The following example:
 46 | |||
 47 | ||| ```idris2 example
 48 | ||| %runElab mkNormalElem "a"
 49 | ||| ```
 50 | |||
 51 | ||| generates the below code:
 52 | |||
 53 | ||| ```idris2
 54 | ||| public export
 55 | ||| a :
 56 | |||      (attrs : List Attribute)
 57 | |||   -> (elems : List Element)
 58 | |||   -> {auto 0 prf : IsRight (either_a attrs elems)}
 59 | |||   -> List Element
 60 | ||| a attrs elems = raw_a attrs elems
 61 | ||| ```
 62 | export
 63 | mkNormalElem : (name : String) -> Elab ()
 64 | mkNormalElem name =
 65 |   let
 66 |     funcName       := UN . Basic $ name
 67 |     rawFuncName    := UN . Basic $ "raw_" ++ name
 68 |     eitherFuncName := UN . Basic $ "either_" ++ name
 69 |     argAttr        := mkArg "attrs" "Attribute"
 70 |     argList        := mkArg "elems" "Element"
 71 |     prfFunc        := var "IsRight"
 72 |                         .$ (var eitherFuncName .$ var "attrs" .$ var "elems")
 73 |     argPrf         := MkArg M0 AutoImplicit (Just "prf") prfFunc
 74 |   in
 75 |   declare
 76 |     [ public' funcName $ argAttr .-> argList .-> argPrf .-> `(List Element)
 77 |     , def funcName [var funcName .$ var "attrs" .$ var "elems" .=
 78 |         var rawFuncName .$ var "attrs" .$ var "elems"]
 79 |     ]
 80 |   where
 81 |     mkArg : TT.Name -> TT.Name -> Arg
 82 |     mkArg argName typeName =
 83 |       MkArg MW ExplicitArg (Just argName) $ var "List" .$ var typeName
 84 |
 85 | ||| Generates a void element function using the element's name.
 86 | |||
 87 | ||| The function will be named `<name>` and will create a `<name>` element.
 88 | |||
 89 | ||| The type of the generated function is:
 90 | |||
 91 | ||| ```idris2
 92 | |||    (attrs : List Attribute)
 93 | ||| -> {auto 0 prf : IsRight (either_<name> attrs)}
 94 | ||| -> List Element
 95 | ||| ```
 96 | |||
 97 | ||| The following example:
 98 | |||
 99 | ||| ```idris2 example
100 | ||| %runElab mkNormalElem "br"
101 | ||| ```
102 | |||
103 | ||| generates the below code:
104 | |||
105 | ||| ```idris2
106 | ||| public export
107 | ||| a :
108 | |||      (attrs : List Attribute)
109 | |||   -> {auto 0 prf : IsRight (either_br attrs)}
110 | |||   -> List Element
111 | ||| a attrs = raw_br attrs
112 | ||| ```
113 | export
114 | mkVoidElem : (name : String) -> Elab ()
115 | mkVoidElem name =
116 |   let
117 |     funcName       := UN . Basic $ name
118 |     rawFuncName    := UN . Basic $ "raw_" ++ name
119 |     eitherFuncName := UN . Basic $ "either_" ++ name
120 |     argAttr        := mkArg "attrs" "Attribute"
121 |     prfFunc        := var "IsRight" .$ (var eitherFuncName .$ var "attrs" )
122 |     argPrf         := MkArg M0 AutoImplicit (Just "prf") prfFunc
123 |   in
124 |   declare
125 |     [ public' funcName $ argAttr .-> argPrf .-> `(List Element)
126 |     , def funcName [var funcName .$ var "attrs" .=
127 |         var rawFuncName .$ var "attrs"]
128 |     ]
129 |   where
130 |     mkArg : TT.Name -> TT.Name -> Arg
131 |     mkArg argName typeName =
132 |       MkArg MW ExplicitArg (Just argName) $ var "List" .$ var typeName
133 |
134 | ||| Creates the HTML elements from the provided configuration.
135 | private
136 | mkElems : (elems : List Config) -> Elab ()
137 | mkElems configs = do
138 |   mconcatMapM f configs
139 |   where
140 |     f : Config -> Elab ()
141 |     f config =
142 |       if config.children
143 |       then mkNormalElem config.name
144 |       else mkVoidElem config.name
145 |
146 | %runElab mkElems elems
147 |
148 | ||| Creates both a doctype preamble and a **html** element.
149 | |||
150 | ||| Accepted attributes (for the **html** element):
151 | |||
152 | ||| - Global
153 | |||
154 | ||| Accepted elements (for the **html** element):
155 | |||
156 | ||| - **head**
157 | ||| - **body**
158 | |||
159 | ||| Additional constraints (for the **html** element):
160 | |||
161 | ||| - The first element must be the **head** element.
162 | ||| - The second element must be the **body** element.
163 | ||| - There cannot be other elements.
164 | public export
165 | html5 :
166 |      (attrs : List Attribute)
167 |   -> (elems : List Element)
168 |   -> {auto 0 prf : IsRight (either_html attrs elems)}
169 |   -> Html
170 | html5 attrs elems = raw_html5 attrs elems
171 |
172 | ||| Creates a **title** element.
173 | |||
174 | ||| Accepted attributes:
175 | |||
176 | ||| - Global
177 | |||
178 | ||| Accepted values:
179 | |||
180 | ||| - A non-empty, not white space only string.
181 | public export
182 | title :
183 |      (attrs : List Attribute)
184 |   -> (text : NonWhitespace)
185 |   -> {auto 0 prf : IsRight (either_title attrs text)}
186 |   -> List Element
187 | title attrs text = raw_title attrs text
188 |