mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 19:25:32 -05:00
html: hide some utils in a Helpers_ module
This commit is contained in:
parent
f1831f364e
commit
b61c03d379
1 changed files with 13 additions and 0 deletions
|
|
@ -334,6 +334,9 @@ type nary' = ?if_:bool -> attribute list -> sub_elt list -> elt
|
||||||
(** Element with children, represented as a list of {!sub_elt} to be flattened
|
(** Element with children, represented as a list of {!sub_elt} to be flattened
|
||||||
@param if_ if false, do not print anything (default true) *)
|
@param if_ if false, do not print anything (default true) *)
|
||||||
|
|
||||||
|
(**/**)
|
||||||
|
module Helpers_ = struct
|
||||||
|
|
||||||
(** Escape string so it can be safely embedded in HTML text. *)
|
(** Escape string so it can be safely embedded in HTML text. *)
|
||||||
let _str_escape (out:Out.t) (s:string) : unit =
|
let _str_escape (out:Out.t) (s:string) : unit =
|
||||||
String.iter (function
|
String.iter (function
|
||||||
|
|
@ -383,18 +386,28 @@ let _write_tag_attrs ~void (out:Out.t) (tag:string) (attrs:attribute list) : uni
|
||||||
_write_attrs out attrs;
|
_write_attrs out attrs;
|
||||||
if void then Out.add_string out "/>" else Out.add_string out ">"
|
if void then Out.add_string out "/>" else Out.add_string out ">"
|
||||||
|
|
||||||
|
end
|
||||||
|
open Helpers_
|
||||||
|
(**/**)
|
||||||
|
|
||||||
|
(** Sub-element with a single element inside. *)
|
||||||
let[@inline] sub_e (elt:elt) : sub_elt = `E elt
|
let[@inline] sub_e (elt:elt) : sub_elt = `E elt
|
||||||
|
|
||||||
|
(** Sub-element with a list of items inside. *)
|
||||||
let[@inline] sub_l (l:elt list) : sub_elt = `L l
|
let[@inline] sub_l (l:elt list) : sub_elt = `L l
|
||||||
|
|
||||||
|
(** Sub-element with a sequence ({!Seq.t}) of items inside. *)
|
||||||
let[@inline] sub_seq (l:elt Seq.t) : sub_elt = `S l
|
let[@inline] sub_seq (l:elt Seq.t) : sub_elt = `S l
|
||||||
|
|
||||||
|
(** Helper to build a {!Seq.t} from an array. *)
|
||||||
let seq_of_array (a:_ array) : _ Seq.t =
|
let seq_of_array (a:_ array) : _ Seq.t =
|
||||||
let rec loop i () =
|
let rec loop i () =
|
||||||
if i=Array.length a then Seq.Nil
|
if i=Array.length a then Seq.Nil
|
||||||
else Seq.Cons (a.(i), loop (i+1))
|
else Seq.Cons (a.(i), loop (i+1))
|
||||||
in loop 0
|
in loop 0
|
||||||
|
|
||||||
|
(** Sub-element with nothing inside. Useful in conditionals, when one
|
||||||
|
decides not to emit a sub-element at all. *)
|
||||||
let sub_empty : sub_elt = `Nil
|
let sub_empty : sub_elt = `Nil
|
||||||
|
|
||||||
(** Emit a string value, which will be escaped. *)
|
(** Emit a string value, which will be escaped. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue