Library
Module
Module type
Parameter
Class
Class type
Simple templating library. It reads HTML files "with variables" and can either output an OCaml module (reusable template with some static checks) or perform substitutions and return the HTML.
To parametrize the HTML, you can add the following arguments to any HTML tag:
ml:content="<ident>" ml:content="<ident> <arg1> ... <argN>" ml:strip="true" ml:strip="if empty" ml:replace="<ident>"
ml:content="v"
(v
is any valid OCaml variable name) tells that the content of the HTML tag must be replaced by the HTML contained in the OCaml variable v
.ml:content="f v1 ... vN"
does the same but uses the return value of the OCaml function f
on the list [v1; ...; vN]
as the replacement HTML.ml:strip="true"
says that the HTML tag should be removed and replaced by the content.ml:strip="if empty"
says that the HTML tag should be removed only if the replacement content is empty.ml:replace="v"
is the same as ml:content="v"
except that it adds ml:strip="true"
.A special value of <ident> is "include". It serves to include the files passed as arguments and does not define a new function.
Inside HTML arguments themselves, one can use
${<ident>}
which is replaced by the content (a string) of the variable <indent>;${f v1 ... vN}
which is replaced by the string returned by the function f
applied to the list [v1; ...; vN]
.type html = Nethtml.document list
val compile_html :
?trailer_ml:string ->
?trailer_mli:string ->
?hide:string list ->
?module_name:string ->
string ->
unit
compile_html fname
reads the HTML template file fname
(typically a file with extension ".html") and creates an OCaml module with functions to fill the variables of the template. The module will be in files module_name
.ml and module_name
.mli.
compile fname
does the same as compile_html
except that trailer code is taken from fname
.ml and fname
.mli for the implementation and interface respectively. Moreover, to hide the signature of a template variable, say "var", one can add a comment (* \@hide var *)
in fname
.mli. Special annotations are added to the generated module implementation and interface so errors point back to fname
.ml and fname
.mli respectively.
module Binding : sig ... end
subst b html
return html
where all variables are substituted according to the bindings b
.
read fname
reads the file fname
and returns its content in a structured form.
val write_html : ?doctype:bool -> ?perm:int -> html -> string -> unit
write_html html fname
writes the textual representation of the html
to the file fname
.
body_of html
returns the body of the HTML document or the entire document if no body is found.
val title_of : html -> string
title_of html
returns the title contained in html
(if no title is present, it will return ""
).
module Path : sig ... end
val iter_html :
?langs:string list ->
?exts:string list ->
?filter:(Path.t -> bool) ->
?perm:int ->
?out_dir:(string -> string) ->
?out_ext:(string -> string) ->
string ->
(string -> Path.t -> html) ->
unit
iter_html base f
iterates f lang file
on all HTML files under base
(the argument of f
is guaranteed to be a path to a file). The resulting HTML code is written under the directory out_dir
lang
, the subpath begin the relative path of the file w.r.t. base
and the filename is the original one with the language removed.
relative_url_are_from_base path html
prefix all relative URLs in html
so that they are specified according to the directory given by path
instead of the base path.
email e
return some HTML/javascript code to protect the email e
from SPAM harvesters. The email e
may end with "?..." in order to specify options, e.g. ?subject=...
.
protect_emails html
changes all emails hrefs in html
in order to make it more difficult for spammers to harvest them.
module Cache : sig ... end
Simple cache with dependencies and timeout.