-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting [\Svg] Tyxml_svg.elt
to [\Svg] Js_of_ocaml_tyxml.Tyxml_js.Html.elt
#323
Comments
[
Svg] Tyxml_svg.elt to
[Svg] Js_of_ocaml_tyxml.Tyxml_js.Html.elt
`[\Svg] Tyxml_svg.elt
to [\Svg] Js_of_ocaml_tyxml.Tyxml_js.Html.elt
As I understand, one of the issues here is that TyXML is meant to produce well-typed HTML/SVG trees from an implementation module, not necessarily to convert these trees from implementations to implementations. As such This would mean that for my usage, I would need to parameterise the core of my program by a module The other issue I am facing here is that I don't understand how to correctly convert a |
For your second, subsidiary, question, the function you are looking for is For the more general question: there are no conversion between |
Thanks for the answer! After a lot of thousand-lines-long error messages (which is typical of functor error with very large module signature), I think that I got it right. The Here is the signature that I finally got: module Image (Xml : Xml_sigs.T with module W = Xml_wrap.NoWrap) (Svg : Svg_sigs.Make(Xml).T) : sig ... end Is it the expected one? |
Thanks for the suggestion to use the DOM functions to convert the object into a string! But it seems that it won't work in my setting: let me share some thoughts here ☺ There is no let%html displayed_img =
"<span class = 'output'>"
[Html.svg [img]]
"</span>" in
let displayed_img = To_dom.of_span displayed_img in
Dom.appendChild output displayed_img This works great! The SVG appears in the webpage. Thanks! I'm surprised that it needed a list by the way: I would have expected to write But I still have to convert it to SVG later on. I don't have a handle to the DOM equivalent of let file = Js.to_string displayed_img##.innerHTML in
let url = "data:image/svg+xml;utf8," ^ file in
let%html button = "<a href = "url" download = 'file.svg'></a>" in
(* ... *) Another option would be to use something like below, but in addition to be unsafe, it produces the string match Js.Opt.to_option displayed_img##.firstChild with
| None -> assert false
| Some img -> (Js.Unsafe.coerce img : <toString : Js.js_string Js.t Js.meth> Js.t)##toString I guess a proper option would be to use |
That looks about right, indeed ! I agree the wrap module is a bit difficult to wrap (!) your head around. You are poking at the innards a bit here, and such usage are well supported, but not well documented.
Indeed, it seems there are some functions missing. Note that you can always manufacture your own In general, it seems all the other remarks stem to some partial supports for SVG, which I must admit are not as well seasoned at the other parts of the Tyxml/Js_of_ocaml APIs. |
That was a side question here, but I tried to investigate on this:
I tried to build a minimal example for this, but it seems that I can't reproduce the issue. Maybe I'm misunderstanding something there. Here is my code: open Js_of_ocaml
open Tyxml
open Js_of_ocaml_tyxml
open Tyxml_js
let file = {|This is a test file with quotes and a backslash \'".|}
let url = "data:text/plain;charset=UTF-8," ^ file
let%html p =
{|
<p>
<a href = |}url{| download = "test.txt">Test</a>
</p>
|}
let () =
let p = To_dom.of_p p in
Dom.appendChild Dom_html.document##.body p ;
let pre = To_dom.of_pre [%html "<pre>" [Html.txt (Js.to_string p##.innerHTML)] "</pre>" ] in
Dom.appendChild Dom_html.document##.body pre The Just to double-check, using base Tyxml instead of Tyxml_js and outputting the output on a terminal does escape the quotes. So this looks fine. |
I'm easily lost in the documentation of TyXML, and in particular I would really appreciate some kind of diagram showing all the conversions that can be invoked between the different types of TyXML/Js_of_ocaml/etc. So it is possible that this issue is just a documentation issue. It might also be an issue belonging to another project (maybe
Js_of_ocaml_tyxml
?), as I'm missing subtleties between the separation of these projects.Here is my issue: the core of my program produces a representation of an SVG file in the type
[`Svg] Tyxml_svg.elt
(which is the type that comes out when I use thelet%svg
syntax withTyxml
openned). I have two interfaces for it, on terminal and web.In the terminal version, I need to produce a SVG file from it. This is done easily enough with
Format.fprintf fmt "%a@." (Tyxml.Svg.pp ~indent:true ()) svg
.In the web version, I use the module
Tyxml_js
ofJs_of_ocaml_tyxml
to produce my webpage, as this type is easy to manipulate and to convert into an actual webpage using one of theTo_dom.of_*
functions.The issue is that I can't insert as-is my image
svg : [`Svg] Tyxml_svg.elt
as these types are not compatible.I suspect that underneath, they both use
Xml.elt
as a representation, but that this type constraint is hidden in the module definition ofTyxml_svg
(for good reasons, as not allXml.elt
are valid SVG elements). I however have the impression that no conversion function has been provided before hiding theelt
type under the module signature, so we are stuck withinTyxml_svg.elt
.Maybe I am doing things in the wrong way here: do tell me if I should use other types for this usage ☺ TyXML is really useful for building typesafe SVG documents and webpages, but I still have trouble converting from types to types (which is why a recapitulating diagram would really help ☺).
When looking for similar issues online, we find several instances of people getting confused on how to incorporate SVG documents within HTML in a typed way (e.g. https://fa.caml.narkive.com/0g4ImGGn/caml-list-js-of-ocaml-and-svg ), and I think that it would be really useful to clarify how to do it properly with a small example.
The text was updated successfully, but these errors were encountered: