Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Format & breaking

0 views
Skip to first unread message

Jonathan Roewen

unread,
Jul 8, 2006, 3:40:40 PM7/8/06
to OCaml
Hi,

I have a small question about format module, and pretty-printing XHTML.

Basically, I want to print something like:

<div>
<p>....</p>
<p>....</p> // when content fits on a single line..
<p> // when content doesn't fit on a single line...
....
</p>
</div>

but I can't get it to generate that. I was reading about the
structural hovboxes, but the behaviour it describes doesn't seem to
work -- at least with xhtml tags.

For example, the above would be printed as:
<div>
<p>....</p>
<p>....</p> // when content fits on a single line..
<p> // when content doesn't fit on a single line...
....</p></div>

which is frustrating. Or else I'm just misunderstanding the semantics
of the Format module, which wouldn't surprise me ;-)

Jonathan

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Nicolas Pouillard

unread,
Jul 9, 2006, 3:12:51 AM7/9/06
to Jonathan Roewen, OCaml
On 7/8/06, Jonathan Roewen <jonatha...@gmail.com> wrote:
> Hi,
>
> I have a small question about format module, and pretty-printing XHTML.
>
> Basically, I want to print something like:
>
> <div>
> <p>....</p>
> <p>....</p> // when content fits on a single line..
> <p> // when content doesn't fit on a single line...
> ....
> </p>
> </div>
>
> but I can't get it to generate that. I was reading about the
> structural hovboxes, but the behaviour it describes doesn't seem to
> work -- at least with xhtml tags.
>
> For example, the above would be printed as:
> <div>
> <p>....</p>
> <p>....</p> // when content fits on a single line..
> <p> // when content doesn't fit on a single line...
> ....</p></div>
>
> which is frustrating. Or else I'm just misunderstanding the semantics
> of the Format module, which wouldn't surprise me ;-)
>

To do that you need two boxes:

"@[<hv0>@[<hv2><p>@,...@]@,</p>@]"

Here a more complete example:

type xml = Elt of string * xml list | Pcdata of string

let pp = Format.fprintf

let rec print_elt f =
function
| Elt (tag, contents) ->
pp f "@[<hv0>@[<hv2><%s>@,%a@]@,</%s>@]"
tag print_list_elts contents tag
| Pcdata s ->
Format.pp_print_string f s

and print_list_elts f =
let rec loop =
function
| [] -> ()
| x::xs -> (pp f "@,"; print_elt f x; loop xs) in
function
| [] -> ()
| [x] -> print_elt f x
| x::xs -> (print_elt f x; loop xs)

let tree =
Elt ("div", [
Elt ("p", [Pcdata "a short text"]);
Elt ("p", [Pcdata "a
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
text"])
])

let () = Format.printf "%a@." print_elt tree

Regards,

--
Nicolas Pouillard

0 new messages