ocsigen / tyxml

Build valid HTML and SVG documents
https://ocsigen.org/tyxml/
Other
163 stars 57 forks source link

Composability issues with the ppx #106

Open Drup opened 8 years ago

Drup commented 8 years ago

Here is an example of something that doesn't work:

let mytitle = [%html5 "<title>A Fabulous Web Page</title>" ]

let mypage = [%html5
  "<html>
    <head>"mytitle"</head>
    <body></body>"
]

Error message by markup: "head element must have exactly one title child element".

I don't know how solvable it is. It sounds tricky.

aantron commented 8 years ago

Well, this isn't really due to the PPX primarily – it is due to irregularities in the TyXML interface. However, the PPX might have an opportunity to hackily coerce its way through such situations, due to having some reflection available. It does seem a bit complicated, since we would want the coercion to be compile-time for best effect.

Drup commented 8 years ago

I don't see how you would do anything about it in a composable way. Consider:

let mytitle = [%html5 "<title>A Fabulous Web Page</title>" ]

let myhead = [%html5
  "<head>"mytitle"<meta bla/>"mytitle"</head>"
]

You need name resolution and typechecking to know that this is incorrect. You couldn't even generate tyxml code for that, without typechecking. This is precisely why tyxml is irregular there.

It's not a big problem in practice, though, you can just inline the title tag and use the antiquotation on the pcdata.

aantron commented 8 years ago

Yes, I think making this fully composable without inserting run-time coercions isn't going to happen with a PPX.

aantron commented 8 years ago

I think this should be made to work, if possible, since you made the change to how antiquotations are inserted:

let%html to_ocaml = "<li><a href='ocaml.org'>OCaml</a></li>"
let%html to_tyxml = "<li><a href='ocsigen.org/tyxml'>TyXML</a></li>"
let%html links = "<ul>" to_ocaml "<li>foo</li>" to_tyxml "</ul>"

Looking into it.

aantron commented 8 years ago

Ok,

let%html links = "<ul>" [to_ocaml] "<li>foo</li>" [to_tyxml] "</ul>"