(** *) #r "nuget: FSharp.Formatting,1.0.0" (** [![Binder](img/badge-binder.svg)](https://mybinder.org/v2/gh/fsprojects/fsharp.formatting/gh-pages?filepath=evaluation.ipynb) [![Script](img/badge-script.svg)](https://fsprojects.github.io/FSharp.Formatting//evaluation.fsx) [![Notebook](img/badge-notebook.svg)](https://fsprojects.github.io/FSharp.Formatting//evaluation.ipynb) # Embedding script output For literate F# scripts, you may embed the result of running the script as part of the literate output. This is a feature of the functions discussed in [literate programming](literate.html) and it is implemented using the [F# Compiler service](http://fsharp.github.io/FSharp.Compiler.Service/). ## Including Console Output To include the Console output use `include-output`: let test = 40 + 2 printf "A result is: %d" test (*** include-output ***) The script defines a variable `test` and then prints it. The console output is included in the output. To include the a formatted value use `include-it`: [ 0 .. 99 ] (*** include-it ***) To include the meta output of F# Interactive processing such as type signatures use `(*** include-fsi-output ***)`: let test = 40 + 3 (*** include-fsi-output ***) To include both console otuput and F# Interactive output blended use `(*** include-fsi-merged-output ***)`. let test = 40 + 4 (*** include-fsi-merged-output ***) You can use the same commands with a named snippet: (*** include-it: test ***) (*** include-fsi-output: test ***) (*** include-output: test ***) You can use the `include-value` command to format a specific value: let value1 = [ 0 .. 50 ] let value2 = [ 51 .. 100 ] (*** include-value: value1 ***) ## Using AddPrinter and AddHtmlPrinter You can use `fsi.AddPrinter`, `fsi.AddPrintTransformer` and `fsi.AddHtmlPrinter` to extend the formatting of objects. ## Emitting Raw Text To emit raw text in F# literate scripts use the following: (** (*** raw ***) Some raw text. *) which would emit
Some raw text.directly into the document. ## F# Formatting as a Library: Specifying the Evaluator and Formatting If using F# Formatting as a library the embedding of F# output requires specifying an additional parameter to the parsing functions discussed in [literate programming documentation](literate.html). Assuming you have all the references in place, you can now create an instance of [FsiEvaluator](https://fsprojects.github.io/FSharp.Formatting/reference/fsharp-formatting-literate-evaluation-fsievaluator.html) that represents a wrapper for F# interactive and pass it to all the functions that parse script files or process script files: *) open FSharp.Formatting.Literate open FSharp.Formatting.Literate.Evaluation open FSharp.Formatting.Markdown // Sample literate content let content = """ let a = 10 (*** include-value:a ***)""" // Create evaluator and parse script let fsi = FsiEvaluator() let doc = Literate.ParseScriptString(content, fsiEvaluator = fsi) Literate.ToHtml(doc) (** When the `fsiEvaluator` parameter is specified, the script is evaluated and so you can use additional commands such as `include-value`. When the evaluator is **not** specified, it is not created automatically and so the functionality is not available (this way, you won't accidentally run unexpected code!) If you specify the `fsiEvaluator` parameter, but don't want a specific snippet to be evaluated (because it might throw an exception, for example), you can use the `(*** do-not-eval ***)` command. The constructor of [FsiEvaluator](https://fsprojects.github.io/FSharp.Formatting/reference/fsharp-formatting-literate-evaluation-fsievaluator.html) takes command line parameters for `fsi.exe` that can be used to specify, for example, defined symbols and other attributes for F# Interactive. You can also subscribe to the `EvaluationFailed` event which is fired whenever the evaluation of an expression fails. You can use that to do tests that verify that all off the code in your documentation executes without errors. ## F# Formatting as a Library: Custom formatting functions As mentioned earlier, values are formatted using a simple `"%A"` formatter by default. However, you can specify a formatting function that provides a nicer formatting for values of certain types. For example, let's say that we would want to format F# lists such as `[1; 2; 3]` as HTML ordered lists `
*)Formatting demo
1:
lettest = ["one";"two";"three"]
one
two
three