Search code examples
smlsmlnjmlton

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

  • According to the SML/NJ bug tracker, the following function can be used to convert HTML4.html to a string:

    fun toString html =
      let
        val buf = CharBuffer.new 1024
      in
        HTML4Print.prHTML {
          putc = fn c => CharBuffer.add1 (buf, c),
          puts = fn s => CharBuffer.addVec (buf, s)
        } html;
        CharBuffer.contents buf
      end
    

    To be able to use HTML4Print.prHTML in the SML/NJ REPL, the REPL should be started using sml '$/html4-lib.cm'. Alternatively, enter CM.make "$/html4-lib.cm"; after starting the REPL.

    The function has signature val toString = fn : HTML4.html -> CharBuffer.vector. CharBuffer is an extension to the Basis Library (reference: 2018 001 Addition of monomorphic buffers). CharBuffer.vector is the same type as CharVector.vector, which is the same type as String.string, which is the same type as string.