'How to convert SML/NJ HTML4 representation to a string

When using SML/NJ library's HTML4 library, how do I convert the Standard ML representation of HTML4 into a string?

For example, if I have the HTML representation below, what function can I use to get a string similar to <html><head><title>Example</title></head><body><h1>Hello!</h1></body></html>?

(* CM.make "$/html4-lib.cm"; *)
open HTML4;
val myHTML = HTML {
  version=NONE,
  head=[Head_TITLE ([], [PCDATA "Example"])],
  content=BodyOrFrameset_BODY (BODY ([], [
    BlockOrScript_BLOCK (H1 ([], [CDATA [PCDATA "Hello!"]]))]))
};

(SML/NJ version: 110.99.2)



Solution 1:[1]

It seems you could use the HTML4Print structure (which appears in the export list in the CM file):

$ sml '$/html4-lib.cm'
Standard ML of New Jersey (64-bit) v110.99.2 [built: Thu Sep 23 13:44:44 2021]
[library $/html4-lib.cm is stable]
- open HTML4Print;
[autoloading]
[library $SMLNJ-LIB/Util/smlnj-lib.cm is stable]
[autoloading done]
opening HTML4Print
  val prHTML : {putc:char -> unit, puts:string -> unit} -> HTML4.html -> unit
  val prBODY : {putc:char -> unit, puts:string -> unit} -> HTML4.body -> unit

So, with your value, it produces:

- HTML4Print.prHTML { putc = print o String.str, puts = print } myHTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>
Example
</TITLE>
</HEAD>
<BODY>
<H1>Hello!</H1>
</BODY>
</HTML>
val it = () : unit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1