Skip to content

Commit 34814bf

Browse files
authored
Add include-it-raw literate command (#624)
1 parent 638b793 commit 34814bf

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

docs/literate.fsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The F# script files is processed as follows:
6464
| `(*** include-fsi-output ***)` | The F# Interactive output of the preceeding snippet |
6565
| `(*** include-fsi-merged-output ***)` | The merge of console output and F# Interactive output of the preceeding snippet |
6666
| `(*** include-it ***)` | The formatted result of the preceeding snippet |
67+
| `(*** include-it-raw ***)` | The unformatted result of the preceeding snippet |
6768
| `(*** include-value: value-name ***)` | The formatted value |
6869
| `(*** raw ***)` | The subsequent code is treated as raw text |
6970

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

8789
#### Hiding code snippets

src/FSharp.Formatting.Literate/Document.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ type LiterateParagraph =
6767
/// (*** include-it:foo ***) - Include "it" value from a named snippet
6868
| ItValueReference of string * LiterateParagraphOptions
6969

70+
/// (*** include-it-raw ***) - Include "it" value from the subsequent snippet here as raw text (Not formatted as fsi)
71+
/// (*** include-it-raw:foo ***) - Include "it" value from a named snippet as raw text (Not formatted as fsi)
72+
| ItRawReference of string * LiterateParagraphOptions
73+
7074
/// (*** include-value:foo ***) - Include the formatting of a specified value here
7175
| ValueReference of string * LiterateParagraphOptions
7276

@@ -86,6 +90,7 @@ type LiterateParagraph =
8690
| FsiOutputReference(_,popts) -> popts
8791
| OutputReference(_,popts) -> popts
8892
| ItValueReference(_,popts) -> popts
93+
| ItRawReference(_,popts) -> popts
8994
| ValueReference(_,popts) -> popts
9095
| LiterateCode(_,_,popts) -> popts
9196
| LanguageTaggedCode(_,_,popts) -> popts

src/FSharp.Formatting.Literate/Evaluator.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type FsiEmbedKind =
2020
| ConsoleOutput
2121
/// The 'it' value
2222
| ItValue
23+
/// The 'it' value as raw text
24+
| ItRaw
2325
/// A specific value
2426
| Value
2527

@@ -376,6 +378,22 @@ module __FsiSettings =
376378
let outputText = defaultArg result.FsiMergedOutput "No output has been produced."
377379
let output = outputText.Trim()
378380
[ OutputBlock (output, "text/plain", Some executionCount) ]
381+
| { ItValue = Some (obj, ty) }, FsiEmbedKind.ItRaw ->
382+
match valueTransformations |> Seq.pick (fun f -> lock lockObj (fun () -> f (obj, ty, executionCount))) with
383+
| [] ->
384+
[ OutputBlock("No value returned by any evaluator", "text/plain", Some executionCount) ]
385+
| blocks ->
386+
blocks
387+
|> List.map (function
388+
| OutputBlock(output,_,Some executionCount) ->
389+
let output =
390+
if ty.FullName = (typeof<string>).FullName then
391+
let l = output.Length
392+
output.Substring(1,l-2)
393+
else
394+
output
395+
OutputBlock(output,"text/html",Some executionCount)
396+
| _ -> OutputBlock("Value could not be returned raw", "text/plain", Some executionCount))
379397
| { ItValue = Some (obj, ty) }, FsiEmbedKind.ItValue
380398
| { Result = Some (obj, ty) }, FsiEmbedKind.Value ->
381399
match valueTransformations |> Seq.pick (fun f -> lock lockObj (fun () -> f (obj, ty, executionCount))) with

src/FSharp.Formatting.Literate/ParseScript.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ type internal ParseScript(parseOptions, ctx:CompilerContext) =
194194
let p = EmbedParagraphs(ItValueReference(ref, popts), None)
195195
transformBlocks None count noEval (p::acc) defs blocks
196196

197+
// Include unformatted 'it' of previous block
198+
| BlockCommand((Command "include-it-raw" "") as cmds)::blocks when prevCodeId.IsSome ->
199+
let popts = getParaOptions cmds
200+
let p1 = EmbedParagraphs(ItRawReference(prevCodeId.Value, popts), None)
201+
transformBlocks prevCodeId count noEval (p1::acc) defs blocks
202+
203+
// Include unformatted 'it' of a named block
204+
| BlockCommand(Command "include-it-raw" ref as cmds)::blocks ->
205+
let popts = getParaOptions cmds
206+
let p = EmbedParagraphs(ItRawReference(ref, popts), None)
207+
transformBlocks None count noEval (p::acc) defs blocks
208+
197209
// Include formatted named value
198210
| BlockCommand(Command "include-value" ref as cmds)::blocks ->
199211
let popts = getParaOptions cmds

src/FSharp.Formatting.Literate/Transformations.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ module internal Transformations =
295295
| FsiOutputReference (ref, _popts)
296296
| OutputReference (ref, _popts)
297297
| ItValueReference (ref, _popts)
298+
| ItRawReference (ref, _popts)
298299
| ValueReference (ref, _popts) ->
299300
let key = (match special with ValueReference _ -> ValueRef ref | _ -> OutputRef ref)
300301
match results.TryFind(key) with
@@ -305,6 +306,7 @@ module internal Transformations =
305306
| FsiOutputReference _ -> FsiEmbedKind.FsiOutput
306307
| OutputReference _ -> FsiEmbedKind.ConsoleOutput
307308
| ItValueReference _ -> FsiEmbedKind.ItValue
309+
| ItRawReference _ -> FsiEmbedKind.ItRaw
308310
| ValueReference _ -> FsiEmbedKind.Value
309311
| _ -> failwith "unreachable"
310312
ctx.Evaluator.Value.Format(result, kind, executionCount)
@@ -385,6 +387,7 @@ module internal Transformations =
385387
| FsiOutputReference _
386388
| OutputReference _
387389
| ItValueReference _
390+
| ItRawReference _
388391
| ValueReference _ ->
389392
let msg = "Warning: Output, it-value and value references require --eval"
390393
printfn "%s" msg

tests/FSharp.Literate.Tests/EvalTests.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ let ``Can hide and include-it`` () =
348348
html1 |> shouldContainText "2000"
349349
html1 |> shouldNotContainText "1000"
350350

351+
[<Test>]
352+
let ``Can hide and include-it-raw`` () =
353+
let content = """
354+
1000+1000
355+
|> string
356+
(*** include-it-raw ***)
357+
"""
358+
let fsie = getFsiEvaluator()
359+
let doc1 = Literate.ParseScriptString(content, "." </> "A.fsx", formatAgent=getFormatAgent(), fsiEvaluator = fsie)
360+
let html1 = Literate.ToHtml(doc1)
361+
html1 |> shouldContainText "2000"
362+
html1 |> shouldNotContainText "\"2000\""
363+
html1 |> shouldNotContainText "<code>2000</code>"
364+
351365
[<Test>]
352366
let ``Can include-output`` () =
353367
let content = """

0 commit comments

Comments
 (0)