package htmlit

  1. Overview
  2. Docs

Module Htmlit.ElSource

HTML elements and fragments.

See the element constructors and minimal page generation.

HTML fragments

Sourcetype html

The type for HTML fragments. A fragment is either:

Elements

Sourcetype name = string

The type for element names.

Sourceval v : ?at:At.t list -> name -> html list -> html

v ?at n cs is an element with name n, attributes at and children cs. Favour element constructors whenever possible. Regarding at:

  • at defaults to [].
  • Except for At.class' and At.style attributes, at must not specify an attribute more than once. This is not checked by the module and what happens on multiple definitions is undefined.
  • For At.class' attributes, multiple specifications are gathered to form a single, space separated, attribute value for the class HTML attribute.
  • For At.style attributes, multiple specifications are gathered to form a single, semi-colon separated, attribute value for the style HTML attribute.

Text

Sourceval txt : string -> html

txt d is character data d.

Sourceval txt_of : ('a -> string) -> 'a -> html

txt_of f v is txt (f v). Cuts a bit on delimiter orgies.

Sourceval sp : html

sp is El.txt " ".

Sourceval nbsp : html

nbsp is El.txt "\u{00A0}".

Splices

Sourceval splice : ?sep:html -> html list -> html

splice ?sep hs is the list of fragments hs separated by sep (if any). When added to an element's children, the list is spliced in the element's children.

Unsafe raw data

Sourceval unsafe_raw : string -> html

unsafe_raw s is the raw string s without escaping markup delimiters. s must be well-formed HTML otherwise invalid markup is generated. This can be used to:

  • Include foreign markup, for example markup generated by another mechanism.
  • Avoid unpleasant surprises with the style element.
  • Let user-generated content create XSS attacks in your application.

Voids

Like void attributes, void fragments render nothing.

Sourceval void : html

void is splice []. See also is_void.

Sourceval is_void : html -> bool

is_void h is true iff h is an empty splice, an empty txt or an empty unsafe_raw. These fragments render nothing. See also void.

Rendering

Sourceval buffer_add : doctype:bool -> Buffer.t -> html -> unit

buffer_add ~doctype b h adds the HTML fragment h to b. If doc_type is true an HTML doctype declaration is prepended.

Sourceval to_string : doctype:bool -> html -> string

to_string is like buffer_add but returns directly a string.

Sourcemodule Low : sig ... end

Low level representation (unstable).

Page

There's more than one way to generate a basic minimal HTML page. The following provides good defaults for quick minimal pages.

Sourceval page : ?lang:string -> ?generator:string -> ?styles:string list -> ?scripts:string list -> ?more_head:html -> title:string -> html -> html

page ~lang ~title body is an El.html element with an At.lang attribute of lang (if specified and non-empty) containing a El.head element followed by body which must be a El.body element.

The children of the El.head element are in order:

  1. An El.meta charset with value utf-8, unconditional.
  2. An El.meta generator with value generator, if specified and non-empty.
  3. An El.meta viewport with value width=device-width, initial-scale=1, unconditional.
  4. For each non-empty element href of styles (defaults to []), an El.link with At.type' text/css and At.href value href. In order.
  5. For each non-empty element src of scripts (defaults to []), an El.script with At.defer, At.type' value text/javascript and At.src value src. In order.
  6. more_head fragment (defaults to El.void). Be careful if you add style tags with direct CSS source.
  7. An El.title with value title which is String.trimed. If the result is empty falls back to "Untitled". See also title_of_filepath to derive titles from file paths.
Sourceval title_of_filepath : string -> string

title_of_filepath f is a non-empty page title for filepath f. Either the basename of f without extension or if that results in "index" or "" the basename of the parent directory without extension or if that results in "" the value "Untitled". Directory separators can be '/' or '\\' regardless of the platform.

Element constructors

See the MDN HTML element reference.

Sourcetype cons = ?at:At.t list -> html list -> html

The type for element constructors. This is simply El.v with a pre-applied element name.

Sourcetype void_cons = ?at:At.t list -> unit -> html

The type for void element constructors. This is simply El.v with a pre-applied element name and without children.

Sourceval a : cons
Sourceval abbr : cons
Sourceval address : cons
Sourceval article : cons
Sourceval aside : cons
Sourceval audio : cons
Sourceval b : cons
Sourceval bdi : cons
Sourceval bdo : cons
Sourceval blockquote : cons
Sourceval body : cons
Sourceval button : cons
Sourceval canvas : cons
Sourceval caption : cons
Sourceval cite : cons
Sourceval code : cons
Sourceval colgroup : cons
Sourceval command : cons
Sourceval datalist : cons
Sourceval dd : cons
Sourceval del : cons
Sourceval details : cons
Sourceval dfn : cons
Sourceval div : cons
Sourceval dl : cons
Sourceval dt : cons
Sourceval em : cons
Sourceval fieldset : cons
Sourceval figcaption : cons
Sourceval figure : cons
Sourceval form : cons
Sourceval h1 : cons
Sourceval h2 : cons
Sourceval h3 : cons
Sourceval h4 : cons
Sourceval h5 : cons
Sourceval h6 : cons
Sourceval head : cons
Sourceval header : cons
Sourceval hgroup : cons
Sourceval html : cons
Sourceval i : cons
Sourceval iframe : cons
Sourceval ins : cons
Sourceval kbd : cons
Sourceval keygen : cons
Sourceval label : cons
Sourceval legend : cons
Sourceval li : cons
Sourceval main : cons
Sourceval map : cons
Sourceval mark : cons
Sourceval menu : cons
Sourceval meter : cons
Sourceval nav : cons
Sourceval noscript : cons
Sourceval object' : cons
Sourceval ol : cons
Sourceval optgroup : cons
Sourceval option : cons
Sourceval output : cons
Sourceval p : cons
Sourceval pre : cons
Sourceval progress : cons
Sourceval q : cons
Sourceval rp : cons
Sourceval rt : cons
Sourceval ruby : cons
Sourceval s : cons
Sourceval samp : cons
Sourceval script : cons
Sourceval section : cons
Sourceval select : cons
Sourceval small : cons
Sourceval span : cons
Sourceval strong : cons
Sourceval style : cons

style

Warning. If your style element contains CSS source, use unsafe_raw to specify it. Otherwise the CSS child combinator '>' gets escaped.

Sourceval sub : cons
Sourceval summary : cons
Sourceval sup : cons
Sourceval table : cons
Sourceval tbody : cons
Sourceval td : cons
Sourceval textarea : cons
Sourceval tfoot : cons
Sourceval th : cons
Sourceval thead : cons
Sourceval time : cons
Sourceval title : cons
Sourceval tr : cons
Sourceval u : cons
Sourceval ul : cons
Sourceval var : cons
Sourceval video : cons