Skip to content

Commit 28a09ae

Browse files
committed
test pynb parsing
1 parent 6f15a86 commit 28a09ae

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/FSharp.Formatting.Literate/ParsePynb.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ module internal ParsePynb =
138138
| "code" -> getCode cell
139139
| _ -> failwith $"unknown cell type {cell_type}"
140140

141-
let ipynbToMarkdown ipynbFile =
142-
let json = JsonDocument.Parse(File.ReadAllText(ipynbFile))
141+
let pynbStringToMarkdown (ipynb: string) =
142+
let json = JsonDocument.Parse(ipynb)
143143

144144
json.RootElement.GetProperty("cells").EnumerateArray()
145145
|> Seq.map (parseCell >> (fun x -> x.ToMarkdown()))
146-
|> String.concat "\n\n"
146+
|> String.concat ""
147+
148+
let pynbToMarkdown ipynbFile = ipynbFile |> File.ReadAllText |> pynbStringToMarkdown

tests/FSharp.Literate.Tests/LiterateTests.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,34 @@ let goodbye = 2
14331433

14341434
pynb2 |> shouldEqual expected2
14351435

1436+
[<Test>]
1437+
let ``md --> pynb --> md comes back the same`` () =
1438+
let mdIn = """Heading
1439+
=======
1440+
1441+
| Col1 | Col2 |
1442+
|:----:|------|
1443+
| Table with heading cell A1 | Table with heading cell B1 |
1444+
| Table with heading cell A2 | Table with heading cell B2 |
1445+
1446+
```fsharp
1447+
let add a b = a + b
1448+
```
1449+
1450+
```csharp
1451+
```
1452+
1453+
```python
1454+
```
1455+
"""
1456+
let mdOut =
1457+
Literate.ParseMarkdownString(mdIn,
1458+
parseOptions =
1459+
(MarkdownParseOptions.ParseCodeAsOther
1460+
||| MarkdownParseOptions.ParseNonCodeAsOther))
1461+
|> Literate.ToPynb
1462+
|> ParsePynb.pynbStringToMarkdown
1463+
mdOut |> shouldEqual mdIn
14361464

14371465
[<Test>]
14381466
let ``Script output is exactly right`` () =

0 commit comments

Comments
 (0)