0 | ||| Runtime HTML elements constructors without "either_" prefix.
  1 | |||
  2 | ||| Use this flavour if you want all test validation checks to be carried out
  3 | ||| carried out at runtime.
  4 | |||
  5 | ||| All those constructors return an `Either` type where the:
  6 | |||
  7 | ||| - `Left` value is a message in case of error; and
  8 | ||| - the `Right` value a list with, as single item, the resulting element.
  9 | module Text.Html.Runtime
 10 |
 11 | import Language.Reflection
 12 | import Language.Reflection.Syntax
 13 | import Language.Reflection.Syntax.Ops
 14 |
 15 | import Text.Html.Attributes.Validation as A
 16 | import Text.Html.Elements.Elab
 17 | import Text.Html.Elements.Raw
 18 |
 19 | import public Data.Either
 20 |
 21 | import public Text.Html.Attributes
 22 | import public Text.Html.Attributes.Types
 23 | import public Text.Html.Attributes.Validation
 24 | import public Text.Html.Elements
 25 | import public Text.Html.Elements.NoAttr
 26 | import public Text.Html.Elements.Runtime
 27 | import public Text.Html.Elements.Types
 28 | import public Text.Html.Elements.Validation
 29 | import public Text.Html.Runtime.NoAttr
 30 | import public Text.Html.Types
 31 | import public Text.Html.Utils
 32 | import public Text.Html.Values
 33 |
 34 | %default total
 35 | %language ElabReflection
 36 |
 37 | private
 38 | mkEitherNormalElem :
 39 |      (name : String)
 40 |   -> (attrValidation : String)
 41 |   -> (elemValidation : String)
 42 |   -> Elab ()
 43 | mkEitherNormalElem name attrValidation elemValidation =
 44 |   let
 45 |     functionName := UN . Basic $ name
 46 |     rawFuncName  := UN . Basic $ "raw_" ++ name
 47 |     attrValFunc  := UN . Basic $ attrValidation
 48 |     elemValFunc  := UN . Basic $ elemValidation
 49 |     argAttr      := arg $ var "List" .$ var "Attribute"
 50 |     argList      := arg $ `(Either String (List Element))
 51 |   in
 52 |   declare
 53 |    [ export' functionName $ argAttr .-> argList .-> `(Either String (List Element))
 54 |    , def functionName
 55 |      [ var functionName .$ var "attrs" .$ (var "Left" .$ var "e") .=
 56 |          var "Left" .$ var "e"
 57 |      , var functionName .$ var "attrs" .$ (var "Right" .$ var "elems") .=
 58 |          var "*>"
 59 |            .$ (var attrValFunc .$ var "attrs")
 60 |            .$ (var "*>"
 61 |              .$ (var elemValFunc .$ var "elems")
 62 |              .$ (var "Right" .$ (var rawFuncName .$ var "attrs" .$ var "elems"))
 63 |            )
 64 |
 65 |      ]
 66 |    ]
 67 |
 68 | private
 69 | mkEitherVoidElem :
 70 |      (name : String)
 71 |   -> (attrValidation : String)
 72 |   -> Elab ()
 73 | mkEitherVoidElem name attrValidation =
 74 |   let
 75 |     functionName := UN . Basic $ name
 76 |     rawFuncName  := UN . Basic $ "raw_" ++ name
 77 |     attrValFunc  := UN . Basic $ attrValidation
 78 |     argAttr      := arg $ var "List" .$ var "Attribute"
 79 |   in
 80 |   declare
 81 |    [ export' functionName $ argAttr .-> `(Either String (List Element))
 82 |    , def functionName [var functionName .$ var "attrs" .=
 83 |      var "*>"
 84 |        .$ (var attrValFunc .$ var "attrs")
 85 |        .$ (var "Right" .$ (var rawFuncName .$ var "attrs"))
 86 |      ]
 87 |    ]
 88 |
 89 | ||| Generates element either functions using the elements' names.
 90 | |||
 91 | ||| The functions will be named `<name>` and will create `<name>` elements.
 92 | private
 93 | mkEitherElems : (elems : List Config) -> Elab ()
 94 | mkEitherElems configs = mconcatMapM eitherFunc configs
 95 |   where
 96 |     eitherFunc : Config -> Elab ()
 97 |     eitherFunc config =
 98 |       if config.children
 99 |       then mkEitherNormalElem config.name config.attributesFunc config.elemsFunc
100 |       else mkEitherVoidElem config.name config.attributesFunc
101 |
102 | %runElab mkEitherElems elems
103 |
104 | ||| Plain text which can be used inside a HTML element.
105 | |||
106 | ||| Note: HTML code is escaped.
107 | export
108 | plainText : String -> Either String (List Element)
109 | plainText = Right . pure . PlainText . escape
110 |
111 | ||| Script content which can be used inside a **script** element.
112 | |||
113 | ||| Note: the script code is escaped.
114 | export
115 | scriptContent : String -> Either String (List Element)
116 | scriptContent = Right . pure . Script . escapeScript
117 |
118 | ||| CSS content which can be used inside a **style** element.
119 | |||
120 | ||| Note: the CSS code is escaped.
121 | public export
122 | styleContent : String -> Either String (List Element)
123 | styleContent = Right . pure . Css . escapeCss
124 |
125 | ||| Alias for plain text allowing to use standard strings.
126 | export
127 | fromString : String -> Either String (List Element)
128 | fromString = plainText
129 |
130 | ||| Both a doctype preambule and a **html** element.
131 | export
132 | html5 : List Attribute -> Either String (List Element) -> Either String Html
133 | html5 _     (Left e)      = Left e
134 | html5 attrs (Right elems) =
135 |      html attrs (Right elems)
136 |   *> Right (raw_html5 attrs elems)
137 |
138 | ||| A **title** element.
139 | export
140 | title :
141 |      List Attribute
142 |   -> (text : NonWhitespace)
143 |   -> Either String (List Element)
144 | title attrs text = Prelude.map (flip raw_title text) (A.standard attrs)
145 |