Skip to content

Commit

Permalink
Add include-it-raw literate command (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil authored Dec 21, 2020
1 parent 638b793 commit 34814bf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/literate.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The F# script files is processed as follows:
| `(*** include-fsi-output ***)` | The F# Interactive output of the preceeding snippet |
| `(*** include-fsi-merged-output ***)` | The merge of console output and F# Interactive output of the preceeding snippet |
| `(*** include-it ***)` | The formatted result of the preceeding snippet |
| `(*** include-it-raw ***)` | The unformatted result of the preceeding snippet |
| `(*** include-value: value-name ***)` | The formatted value |
| `(*** raw ***)` | The subsequent code is treated as raw text |

Expand All @@ -82,6 +83,7 @@ F# language.
| `(*** include-fsi-output: snippet-name ***)` | The F# Interactive output of the named snippet |
| `(*** include-fsi-merged-output: snippet-name ***)` | The merge of console output and F# Interactive output of the named snippet |
| `(*** include-it: snippet-name ***)` | The formatted result of the named snippet |
| `(*** include-it-raw: snippet-name ***)` | The unformatted result of the named snippet |
| `(*** include: snippet-name ***)` | Include the code of the named snippet |

#### Hiding code snippets
Expand Down
5 changes: 5 additions & 0 deletions src/FSharp.Formatting.Literate/Document.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type LiterateParagraph =
/// (*** include-it:foo ***) - Include "it" value from a named snippet
| ItValueReference of string * LiterateParagraphOptions

/// (*** include-it-raw ***) - Include "it" value from the subsequent snippet here as raw text (Not formatted as fsi)
/// (*** include-it-raw:foo ***) - Include "it" value from a named snippet as raw text (Not formatted as fsi)
| ItRawReference of string * LiterateParagraphOptions

/// (*** include-value:foo ***) - Include the formatting of a specified value here
| ValueReference of string * LiterateParagraphOptions

Expand All @@ -86,6 +90,7 @@ type LiterateParagraph =
| FsiOutputReference(_,popts) -> popts
| OutputReference(_,popts) -> popts
| ItValueReference(_,popts) -> popts
| ItRawReference(_,popts) -> popts
| ValueReference(_,popts) -> popts
| LiterateCode(_,_,popts) -> popts
| LanguageTaggedCode(_,_,popts) -> popts
Expand Down
18 changes: 18 additions & 0 deletions src/FSharp.Formatting.Literate/Evaluator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type FsiEmbedKind =
| ConsoleOutput
/// The 'it' value
| ItValue
/// The 'it' value as raw text
| ItRaw
/// A specific value
| Value

Expand Down Expand Up @@ -376,6 +378,22 @@ module __FsiSettings =
let outputText = defaultArg result.FsiMergedOutput "No output has been produced."
let output = outputText.Trim()
[ OutputBlock (output, "text/plain", Some executionCount) ]
| { ItValue = Some (obj, ty) }, FsiEmbedKind.ItRaw ->
match valueTransformations |> Seq.pick (fun f -> lock lockObj (fun () -> f (obj, ty, executionCount))) with
| [] ->
[ OutputBlock("No value returned by any evaluator", "text/plain", Some executionCount) ]
| blocks ->
blocks
|> List.map (function
| OutputBlock(output,_,Some executionCount) ->
let output =
if ty.FullName = (typeof<string>).FullName then
let l = output.Length
output.Substring(1,l-2)
else
output
OutputBlock(output,"text/html",Some executionCount)
| _ -> OutputBlock("Value could not be returned raw", "text/plain", Some executionCount))
| { ItValue = Some (obj, ty) }, FsiEmbedKind.ItValue
| { Result = Some (obj, ty) }, FsiEmbedKind.Value ->
match valueTransformations |> Seq.pick (fun f -> lock lockObj (fun () -> f (obj, ty, executionCount))) with
Expand Down
12 changes: 12 additions & 0 deletions src/FSharp.Formatting.Literate/ParseScript.fs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ type internal ParseScript(parseOptions, ctx:CompilerContext) =
let p = EmbedParagraphs(ItValueReference(ref, popts), None)
transformBlocks None count noEval (p::acc) defs blocks

// Include unformatted 'it' of previous block
| BlockCommand((Command "include-it-raw" "") as cmds)::blocks when prevCodeId.IsSome ->
let popts = getParaOptions cmds
let p1 = EmbedParagraphs(ItRawReference(prevCodeId.Value, popts), None)
transformBlocks prevCodeId count noEval (p1::acc) defs blocks

// Include unformatted 'it' of a named block
| BlockCommand(Command "include-it-raw" ref as cmds)::blocks ->
let popts = getParaOptions cmds
let p = EmbedParagraphs(ItRawReference(ref, popts), None)
transformBlocks None count noEval (p::acc) defs blocks

// Include formatted named value
| BlockCommand(Command "include-value" ref as cmds)::blocks ->
let popts = getParaOptions cmds
Expand Down
3 changes: 3 additions & 0 deletions src/FSharp.Formatting.Literate/Transformations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ module internal Transformations =
| FsiOutputReference (ref, _popts)
| OutputReference (ref, _popts)
| ItValueReference (ref, _popts)
| ItRawReference (ref, _popts)
| ValueReference (ref, _popts) ->
let key = (match special with ValueReference _ -> ValueRef ref | _ -> OutputRef ref)
match results.TryFind(key) with
Expand All @@ -305,6 +306,7 @@ module internal Transformations =
| FsiOutputReference _ -> FsiEmbedKind.FsiOutput
| OutputReference _ -> FsiEmbedKind.ConsoleOutput
| ItValueReference _ -> FsiEmbedKind.ItValue
| ItRawReference _ -> FsiEmbedKind.ItRaw
| ValueReference _ -> FsiEmbedKind.Value
| _ -> failwith "unreachable"
ctx.Evaluator.Value.Format(result, kind, executionCount)
Expand Down Expand Up @@ -385,6 +387,7 @@ module internal Transformations =
| FsiOutputReference _
| OutputReference _
| ItValueReference _
| ItRawReference _
| ValueReference _ ->
let msg = "Warning: Output, it-value and value references require --eval"
printfn "%s" msg
Expand Down
14 changes: 14 additions & 0 deletions tests/FSharp.Literate.Tests/EvalTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ let ``Can hide and include-it`` () =
html1 |> shouldContainText "2000"
html1 |> shouldNotContainText "1000"

[<Test>]
let ``Can hide and include-it-raw`` () =
let content = """
1000+1000
|> string
(*** include-it-raw ***)
"""
let fsie = getFsiEvaluator()
let doc1 = Literate.ParseScriptString(content, "." </> "A.fsx", formatAgent=getFormatAgent(), fsiEvaluator = fsie)
let html1 = Literate.ToHtml(doc1)
html1 |> shouldContainText "2000"
html1 |> shouldNotContainText "\"2000\""
html1 |> shouldNotContainText "<code>2000</code>"

[<Test>]
let ``Can include-output`` () =
let content = """
Expand Down

0 comments on commit 34814bf

Please sign in to comment.