0 | ||| Raw constructors for elements without any checks.
  1 | |||
  2 | ||| Those constructors do not carry any checks. They are therefore fast and
  3 | ||| and simple to use at run-time. On the other hand, they don't provide any
  4 | ||| form of guarantee when it comes to the correctness of the HTML code.
  5 | |||
  6 | ||| They are preceded by `raw_` to distinguish them from the standard
  7 | ||| elements functions with compile time checks. This is to avoid name clashes
  8 | ||| as this module must also be exported since those functions are used by the
  9 | ||| standard functions. 
 10 | module Text.Html.Elements.Raw
 11 |
 12 | import Language.Reflection
 13 | import Language.Reflection.Syntax
 14 | import Language.Reflection.Syntax.Ops
 15 |
 16 | import Text.Html.Attributes.Types
 17 | import Text.Html.Elements.Elab
 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 raw function using the element's name.
 27 | |||
 28 | ||| The function will be named `raw_<name>` and will create a `<name>` element.
 29 | |||
 30 | ||| The type of the generated function is:
 31 | ||| `List Attribute -> List Element -> List Element`
 32 | |||
 33 | ||| The following example:
 34 | |||
 35 | ||| ```idris2 example
 36 | ||| %runElab mkNormalElem "a"
 37 | ||| ```
 38 | |||
 39 | ||| generates the below code:
 40 | |||
 41 | ||| ```idris2
 42 | ||| public export
 43 | ||| raw_a : List Attribute -> List Element -> List Element
 44 | ||| raw_a attrs elems = pure $ NormalElement "a" attrs elems
 45 | ||| ```
 46 | export
 47 | mkRawNormalElem : (name : String) -> Elab ()
 48 | mkRawNormalElem name =
 49 |   let
 50 |     functionName := UN . Basic $ "raw_" ++ name
 51 |     argAttr      := arg $ var "List" .$ var "Attribute"
 52 |     argList      := arg $ var "List" .$ var "Element"
 53 |   in
 54 |   declare
 55 |    [ public' functionName $ argAttr .-> argList .-> `(List Element)
 56 |    , def functionName [var functionName .$ var "attrs" .$ var "elems" .=
 57 |       var "pure"
 58 |       .$ (var "NormalElement"
 59 |       .$ (var "fromString" .$ primVal (Str name))
 60 |       .$ var "attrs"
 61 |       .$ var "elems")]
 62 |    ]
 63 |
 64 | ||| Generates a void element raw function using the element's name.
 65 | |||
 66 | ||| The function will be named `raw_<name>` and will create a `<name>` element.
 67 | |||
 68 | ||| The type of the generated function is:
 69 | ||| `List Attribute -> List Element`
 70 | |||
 71 | ||| The following example:
 72 | |||
 73 | ||| ```idris2 example
 74 | ||| %runElab mkNormalElem "br"
 75 | ||| ```
 76 | |||
 77 | ||| generates the below code:
 78 | |||
 79 | ||| ```idris2
 80 | ||| public export
 81 | ||| raw_br : List Attribute -> List Element
 82 | ||| raw_br elems = pure $ VoidElement "br" attrs
 83 | ||| ```
 84 | export
 85 | mkRawVoidElem : (name : String) -> Elab ()
 86 | mkRawVoidElem name =
 87 |   let
 88 |     functionName := UN . Basic $ "raw_" ++ name
 89 |     argAttr      := arg $ var "List" .$ var "Attribute"
 90 |   in
 91 |   declare
 92 |    [ public' functionName $ argAttr .-> `(List Element)
 93 |    , def functionName [var functionName .$ var "attrs" .=
 94 |       var "pure"
 95 |       .$ (var "VoidElement"
 96 |       .$ (var "fromString" .$ primVal (Str name))
 97 |       .$ var "attrs")]
 98 |    ]
 99 |
100 | ||| Generates element raw functions using the elements' names.
101 | |||
102 | ||| The functions will be named `raw_<name>` and will create `<name>`
103 | ||| elements.
104 | export
105 | mkRawElems : (elems : List Config) -> Elab ()
106 | mkRawElems configs = mconcatMapM rawFunc configs
107 |   where
108 |     rawFun : Config -> Elab ()
109 |     rawFunc config =
110 |       if config.children
111 |       then mkRawNormalElem config.name
112 |       else mkRawVoidElem config.name
113 |
114 | %runElab mkRawElems elems
115 |
116 | ||| Unescaped HTML code.
117 | |||
118 | ||| This can be useful, if you need to inject some HTML code from another source
119 | ||| typically from a conversion from Markdown.
120 | |||
121 | ||| > [!WARNING]
122 | ||| > To use with caution of course, as the HTML will not be escaped. You must
123 | ||| > be certain of the source of this HTML code and that it cannot be
124 | ||| > malicious.
125 | public export
126 | htmlCode : String -> List Element
127 | htmlCode = pure . HtmlCode
128 |
129 | ||| Plain text which can be used inside a HTML element.
130 | |||
131 | ||| Note: HTML code is escaped.
132 | public export
133 | plainText : String -> List Element
134 | plainText = pure . PlainText . escape
135 |
136 | ||| Script content which can be used inside a **script** element.
137 | |||
138 | ||| Note: the script code is escaped.
139 | public export
140 | scriptContent : String -> List Element
141 | scriptContent = pure . Script . escapeScript
142 |
143 | ||| CSS content which can be used inside a **style** element.
144 | |||
145 | ||| Note: the CSS code is escaped.
146 | public export
147 | styleContent : String -> List Element
148 | styleContent = pure . Css . escapeCss
149 |
150 | ||| Both a doctype preambule and a **html** element.
151 | public export
152 | raw_html5 : (attrs: List Attribute) -> (elems: List Element) -> Html
153 | raw_html5 attrs elems = MkHtml Html5 (NormalElement "html" attrs elems)
154 |
155 | ||| The **title** element.
156 | public export
157 | raw_title : List Attribute -> (title : NonWhitespace) -> List Element
158 | raw_title attrs text = pure $ NormalElement "title" attrs (plainText $ show text)
159 |