0 | ||| HTML code generator.
  1 | |||
  2 | ||| This module offers functions to convert a `Html` type to a string.
  3 | ||| The two main functions are:
  4 | |||
  5 | ||| - `genHtml`: which renders the HTML on multiple lines with an indentation
  6 | |||    of two spaces for each level of the HTML tree to ensure good readability;
  7 | |||    and
  8 | ||| - `genHtmlMin`: which renders the HTML on one single line to minify the
  9 | |||    output.
 10 | |||
 11 | ||| Alternatively, you can provide your own configuration to the `genHtmlConf`
 12 | ||| function. This allows to use tabs instead of spaces or to set your own
 13 | ||| indentation, such as four spaces for example.
 14 | |||
 15 | ||| For convenience, the below functions are also exported, in case you would
 16 | ||| like to render a particular attribute or element:
 17 | |||
 18 | ||| - `genAttribute`
 19 | ||| - `genElement`
 20 | |||
 21 | ||| Note that you will need to provide your own configuration and, for
 22 | ||| `genElement`, provide the indentation.
 23 | module Text.Html.Generator
 24 |
 25 | import Data.Nat
 26 | import Data.String
 27 |
 28 | import Text.Html.Attributes.Types
 29 | import Text.Html.Elements.Types
 30 | import Text.Html.Types
 31 | import Text.Html.Utils
 32 |
 33 | %default total
 34 |
 35 | --------------------------------------------------------------------------------
 36 | -- Public
 37 | --------------------------------------------------------------------------------
 38 |
 39 | ||| The generation mode.
 40 | |||
 41 | ||| It can be the default one on multiple lines or the minify version on a
 42 | ||| single line.
 43 | public export
 44 | data Mode = Default | Minify
 45 |
 46 | ||| The indentation character to use (space or tab).
 47 | public export
 48 | data IndentChar = Space | Tab
 49 |
 50 | ||| A generation configuration.
 51 | public export
 52 | record Config where
 53 |   constructor MkConfig
 54 |
 55 |   ||| The indentation character (`Space` or `Tab`).
 56 |   indentChar : IndentChar
 57 |
 58 |   ||| The number of characters for each indentation.
 59 |   indentSize : Nat
 60 |
 61 |   ||| The generation mode (`Default` or `Minify`).
 62 |   mode : Mode
 63 |
 64 | ||| Returns a space for the indentation.
 65 | private
 66 | space : (indent : Nat) -> IndentChar -> String
 67 | space indent Space = replicate indent ' '
 68 | space indent Tab   = replicate indent '\t'
 69 |
 70 | ||| Returns a return line in normal mode or an empty string in minify mode.
 71 | private
 72 | returnLine : Mode -> String
 73 | returnLine Default = "\n"
 74 | returnLine Minify  = ""
 75 |
 76 | ||| Generates a doctype.
 77 | private
 78 | genDoctype : Doctype -> String
 79 | genDoctype Html5 = "<!doctype html>"
 80 |
 81 | ||| Generates an attribute.
 82 | export
 83 | genAttribute : Attribute -> String
 84 | genAttribute (MkAttribute name (Just val)) = "\{name}=\"\{show val}\""
 85 | genAttribute (MkAttribute name Nothing)    = "\{name}"
 86 |
 87 | ||| Generates a list of attributes.
 88 | private
 89 | genAttributes : Config -> List Attribute -> String
 90 | genAttributes config attrs = joinBy " " (map genAttribute attrs)
 91 |
 92 | ||| Generates a plain text.
 93 | private
 94 | genText : Config -> (indent: Nat) -> EscapedText -> String
 95 | genText (MkConfig _ _ Minify) _ text = show text
 96 | genText config indent text =
 97 |   let
 98 |     char = config.indentChar
 99 |     str = replaceStr "\n" ("\n" <+> space indent char) (show text)
100 |   in
101 |   space indent char <+> str
102 |
103 | ||| Generates a script content.
104 | private
105 | genScript : Config -> (indent: Nat) -> EscapedScript -> String
106 | genScript (MkConfig _ _ Minify) _ text = show text
107 | genScript config indent text =
108 |   let
109 |     char = config.indentChar
110 |     str = replaceStr "\n" ("\n" <+> space indent char) (show text)
111 |   in
112 |   space indent char <+> str
113 |
114 | ||| Generates a CSS content.
115 | private
116 | genCss : Config -> (indent: Nat) -> EscapedCss -> String
117 | genCss (MkConfig _ _ Minify) _ text = show text
118 | genCss config indent text =
119 |   let
120 |     char = config.indentChar
121 |     str = replaceStr "\n" ("\n" <+> space indent char) (show text)
122 |   in
123 |   space indent char <+> str
124 |
125 | mutual
126 |
127 |   ||| Generates a list of elements.
128 |   export
129 |   genElementsConf : Config -> (indent : Nat) -> List Element -> String
130 |   genElementsConf config indent elems =
131 |     -- The call is total as `genElementsConf` is called on the element's children
132 |     -- and not on the element itself within `genElement`.
133 |     assert_total $ foldMap f elems
134 |     where
135 |       f : Element -> String
136 |       f elem =
137 |             returnLine config.mode
138 |         <+> genElementConf config indent elem
139 |
140 |   ||| Generates an element.
141 |   |||
142 |   ||| For plain text, in default mode, if the text is longer than 80 characters
143 |   ||| or it contains a return line character, then it's displayed below the HTML
144 |   ||| tag and indented. Otherwise, it's displayed on the same line as the
145 |   ||| opening and closing tags.
146 |   export
147 |   genElementConf : Config -> (indent : Nat) -> Element -> String
148 |   genElementConf config indent (NormalElement name attrs [PlainText text]) =
149 |     let
150 |       char = config.indentChar
151 |       indent' = indent + config.indentSize
152 |       str = if length (show text) > 80 || isInfixOf "\n" (show text)
153 |             then
154 |                   returnLine config.mode <+> space 0 char
155 |               <+> genText config indent' text
156 |               <+> returnLine config.mode <+> space indent char
157 |             else genText config 0 text
158 |     in
159 |         space indent char <+> "<\{name}"
160 |     <+> case attrs of
161 |           [] => ">"
162 |           xs => " \{genAttributes config xs}>"
163 |     <+> str
164 |     <+> "</" <+> name <+> ">"
165 |   genElementConf config indent (NormalElement name attrs children) =
166 |     let
167 |       indent' = indent + config.indentSize
168 |       char    = config.indentChar
169 |     in
170 |     space indent char <+> "<\{name}"
171 |     <+> case attrs of
172 |           [] => ">"
173 |           xs => " \{genAttributes config xs}>"
174 |     <+> genElementsConf config indent' children
175 |     <+> returnLine config.mode
176 |     <+> space indent char <+> "</" <+> name <+> ">"
177 |   genElementConf config indent (VoidElement name []) =
178 |     space indent config.indentChar <+> "<\{name}>"
179 |   genElementConf config indent (VoidElement name attrs) =
180 |     space indent config.indentChar <+> "<\{name} \{genAttributes config attrs}>"
181 |   genElementConf config indent (PlainText text) =
182 |     space indent config.indentChar <+> genText config 0 text
183 |   genElementConf config indent (Script text) =
184 |     space indent config.indentChar <+> genScript config 0 text
185 |   genElementConf config indent (Css text) =
186 |     space indent config.indentChar <+> genCss config 0 text
187 |   genElementConf config indent (HtmlCode str) =
188 |     space indent config.indentChar <+> str
189 |
190 | ||| Default config using 2 spaces for the indentation.
191 | private
192 | defaultConfig : Config
193 | defaultConfig = MkConfig Space 2 Default
194 |
195 | ||| One liner config.
196 | private
197 | minConfig : Config
198 | minConfig = MkConfig Space 0 Minify
199 |
200 | ||| Generates HTML code based on the provided element.
201 | |||
202 | ||| Each child is displayed on its own line and 2 spaces are used for the
203 | ||| indentation.
204 | export
205 | genElement : Element -> String
206 | -- Total as genElementConf will always generate a non-empty string.
207 | genElement elem = assert_total $ strTail $ genElementConf defaultConfig 0 elem
208 |
209 | ||| Generates HTML code based on the provided element as a one liner.
210 | export
211 | genElementMin : Element -> String
212 | genElementMin = genElementConf minConfig 0
213 |
214 | ||| Generates HTML code based on the provided list of elements.
215 | |||
216 | ||| Each element is displayed on its own line and 2 spaces are used for the
217 | ||| indentation.
218 | export
219 | genElements : List Element -> String
220 | genElements []    = ""
221 | genElements elems =
222 |   -- Total, as for non-empty list genElementsConf will always generate a
223 |   -- non-empty string.
224 |   assert_total $ strTail $ genElementsConf defaultConfig 0 elems
225 |
226 | ||| Generates HTML code based on the provided list of elements as a one liner.
227 | export
228 | genElementsMin : List Element -> String
229 | genElementsMin = genElementsConf minConfig 0
230 |
231 | ||| Generates the HTML based on the provided configuration.
232 | export
233 | genHtmlConf : Config -> Html -> String
234 | genHtmlConf config html =
235 |       genDoctype html.doctype
236 |   <+> returnLine config.mode
237 |   <+> genElementConf config 0 html.htmlElement
238 |   <+> returnLine config.mode
239 |
240 | ||| Generates the HTML using a default of 2 spaces for the indentation.
241 | export
242 | genHtml : Html -> String
243 | genHtml = genHtmlConf defaultConfig
244 |
245 | ||| Generates a one liner HTML.
246 | export
247 | genHtmlMin : Html -> String
248 | genHtmlMin = genHtmlConf minConfig
249 |