Module Weberizer
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>" - The argument
ml:content="v"(vis any valid OCaml variable name) tells that the content of the HTML tag must be replaced by the HTML contained in the OCaml variablev. ml:content="f v1 ... vN"does the same but uses the return value of the OCaml functionfon 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 asml:content="v"except that it addsml: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 functionfapplied to the list[v1; ...; vN].
- version
- 0.8
val compile_html : ?trailer_ml:string -> ?trailer_mli:string -> ?hide:string list -> ?module_name:string -> string -> unitcompile_html fnamereads the HTML template filefname(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 filesmodule_name.ml andmodule_name.mli.- parameter module_name
the name of the generated module, possibly preceded by a path to indicate where to save the module file. By default, it is the basename of
fnamewithout its extension.
- parameter trailer_ml
additional code to be appended to the .ml file. This code can use the functions of the interface (without the module prefix) to set variables of the template. You set a variable
vusing the value of a variablev', you should use the constructionSet.v tpl (fun t -> ... Get.v' t ...)(which returns a copy oftplwithvset) to ensure that the value ofv'at the time of rendering is used and not the one present intplwhenvis set. This is important to maintain the independence of variables which may be set in any order (even if documented, the fact that a variable depends on others will lead to confusion and errors). If you useGet.vinsideSet.v,Get.vwill return the previous value of the variablev.
- parameter trailer_mli
additional code to be appended to the .mli file.
- parameter hide
variables of the template that will not be present in the module interface. This is only interesting if these variables are used in
trailer_mlfunctions.
val compile : ?module_name:string -> string -> unitcompile fnamedoes the same ascompile_htmlexcept that trailer code is taken fromfname.ml andfname.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 *)infname.mli. Special annotations are added to the generated module implementation and interface so errors point back tofname.ml andfname.mli respectively.
module Binding : sig ... endval subst : ?base:string -> Binding.t -> html -> htmlsubst b htmlreturnhtmlwhere all variables are substituted according to the bindingsb.- parameter base
All relative file names in "include" directives are considered to be relative to
base. Default: the current working directory.
- raises Invalid_argument
if variable names are not valid or associated values do not correspond to their usage.
val read : ?base:string -> ?bindings:Binding.t -> string -> htmlread fnamereads the filefnameand returns its content in a structured form.- parameter base
All relative file names in "include" directives are considered to be relative to
base. Default: theFilename.dirnameoffname.
- parameter bindings
if provided, perform the substitutions it mandates. Otherwise, the "raw" HTML is returned (this is the default).
- raises Sys_error
if the file cannot be read.
Utilities
val write_html : ?doctype:bool -> ?perm:int -> html -> string -> unitwrite_html html fnamewrites the textual representation of thehtmlto the filefname.- parameter doctype
whether to output a doctype (default:
true).
- parameter perm
the permissions of the created file (default:
0o644). Whatever permissions you specify, the executable bits are removed.
val body_of : html -> htmlbody_of htmlreturns the body of the HTML document or the entire document if no body is found.
val title_of : html -> stringtitle_of htmlreturns the title contained inhtml(if no title is present, it will return"").
module Path : sig ... endval 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) -> unititer_html base fiteratesf lang fileon all HTML files underbase(the argument offis guaranteed to be a path to a file). The resulting HTML code is written under the directoryout_dir lang, the subpath begin the relative path of the file w.r.t.baseand the filename is the original one with the language removed.- raises Invalid_argument
if
baseis not a directory.
- parameter lang
the accepted languages. The first one being the default one (for files that do not specify a language).
- raises Invalid_argument
if languages are not lowercase only.
- parameter exts
the allowed file extensions. Defaut:
[".html"]. May be useful, ofr example, if you want to deal PHP pages.
- parameter filter
examine the file of dir iff the condition
filter rel_dir fholds on the relative pathrel_dirfromrootand final file or dirf. Default: accept all.htmlfiles. Files and dirs starting with a dot are always excluded.
- parameter out_dir
a function s.t.
out_dir langgives the outpout directory used for the languagelang. Default: the identity.
- parameter out_ext
a function to transform the output extension given the input one. Default: the identity.
val relative_url_are_from_base : Path.t -> html -> htmlrelative_url_are_from_base path htmlprefix all relative URLs inhtmlso that they are specified according to the directory given bypathinstead of the base path.
val email : ?args:(string * string) list -> ?content:html -> string -> htmlemail ereturn some HTML/javascript code to protect the emailefrom SPAM harvesters. The emailemay end with "?..." in order to specify options, e.g.?subject=....- parameter args
arguments to the <a> HTML tag. Default:
[].
- parameter content
Tells whether the content of the email link is the email itself (no
contentspecified, the default) or some other data.
val protect_emails : html -> htmlprotect_emails htmlchanges all emails hrefs inhtmlin order to make it more difficult for spammers to harvest them.
module Cache : sig ... endSimple cache with dependencies and timeout.