Using TyXML with IOCamlJS

We are running using the full version so we dont need to load camlp4


In [2]:
#use "topfind"


- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
Out[2]:

Now load the TyXML library and syntax extension. We currently need to add some predicates to 'Topfind' so the syntax extension loads properly. A customised topfind for different iocamljs versions will eventually be needed.


In [3]:
Topfind.add_predicates["syntax";"camlp4o"]


Out[3]:
- : unit = ()

In [4]:
#require "tyxml.syntax"


/home/andyman/.opam/4.01.0-iocamljs/lib/ocaml/str.cma: loaded
/home/andyman/.opam/4.01.0-iocamljs/lib/tyxml: added to search path
/home/andyman/.opam/4.01.0-iocamljs/lib/tyxml/tyxml.cma: loaded
/home/andyman/.opam/4.01.0-iocamljs/lib/tyxml/pa_tyxml.cma: loaded
Out[4]:

Define a printing function which will display the HTML in the notebook.


In [5]:
let html q = 
    let b = Buffer.create 10 in
    Html5.P.print_list ~output:(Buffer.add_string b) q;
    Iocaml.display "text/html" (Buffer.contents b)


Out[5]:
value html : list (Html5.M.elt 'a) -> unit = <fun>

We can now write some HTML. Note the local binding of the Html5 module. If we do this globally we get a JavaScript stack overflow exception. I think this is due to printing a large module signature.


In [6]:
let x = 
    let module Html5 = Html5.M in 
    <:html5< <p> <i>hello</i> <b>world</b> </p> >>


Out[6]:
value x : Html5.M.elt [> Html5_types.p ] = <abstr>

Now we can display the html


In [7]:
html [x]


hello world

Out[7]:
- : unit = ()