1

I'd like to emit some html (generated from my F# code) into a FsLab journal but cannot seem to find the correct incantation to make it happen.

If I have a function in my code that returns an html snippet is there a way to get this directly into the page without being surrounded by a <pre> tag?


I have tried, for example:

let f () = 
    """Some <b>bold</b> sample"""
let htmlContent = f ()

then

(*** include-value:htmlContent ***)

but the output is just the html code itself formatted like output.

I took a dive into the F# formatting GH pages and found the (*** raw ***) command so I also tried:

(*** include-value:htmlContent, raw ***)

but the output still gets surrounded by the <pre> & <code> tags.

Is it possible to simply emit raw html in this way without the <pre> tag?

Stewart_R
  • 13,764
  • 11
  • 60
  • 106

1 Answers1

3

If you are using the latest version, then you can add custom HTML printers using fsi.AddHtmlPrinter. We need to improve FsLab docs, but this is also used by F# Interactive Service in Atom.

To emit raw HTML, you can include something like this in your script:

(*** hide ***)
type Html = Html of string
#if HAS_FSI_ADDHTMLPRINTER
fsi.AddHtmlPrinter(fun (Html h) ->
  seq [], h)
#endif

Then, you should be able to create HTML nodes with:

let b = Html("""Some <b>bold</b> sample""")
(*** include-value:b ***)
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Thanks Tomas, this seems like just what I was looking for but I don't seem to have fsi.AddHtmlPrinter (I get: "the field, constructor or member is not defined") Do I need a newer version of fsi itself? – Stewart_R Oct 14 '16 at 01:02
  • Ahhh - Ive got it now, this extn: https://github.com/ionide/FsInteractiveService/blob/1868abbdb53ce3db9fd8a3714cc93501c1b5dda4/src/FsInteractiveService/Main.fs yeah? - Thanks Tomas – Stewart_R Oct 14 '16 at 01:22
  • 2
    @Stewart_R Sadly VS does not know about this method - which is why I use `#if` to hide it - but FsLab journal defines the `HAS_FSI_ADDHTMLPRINTER` symbol and adds the member to the `fsi` object, so it will run (even if VS thinks it will not!) – Tomas Petricek Oct 14 '16 at 22:22