|
| 1 | +namespace FSharp.Formatting.Literate |
| 2 | + |
| 3 | +open System.IO |
| 4 | +open System.Text.Json |
| 5 | + |
| 6 | +module internal ParsePynb = |
| 7 | + |
| 8 | + type ParsedCell = |
| 9 | + | Code of |
| 10 | + {| lang: string |
| 11 | + source: string |
| 12 | + outputs: string[] option |} |
| 13 | + | Markdown of source: string |
| 14 | + |
| 15 | + member this.ToMarkdown() = |
| 16 | + match this with |
| 17 | + | Markdown source -> source |
| 18 | + | Code code -> |
| 19 | + let codeBlock = sprintf $"```{code.lang}\n{code.source}\n```" |
| 20 | + |
| 21 | + match code.outputs with |
| 22 | + | None -> codeBlock |
| 23 | + | Some outputs -> |
| 24 | + let outputsString = outputs |> String.concat "\n\n" |
| 25 | + sprintf $"{codeBlock}\n\n{outputsString}\n\n" |
| 26 | + |
| 27 | + module Output = |
| 28 | + let (|TextHtml|_|) (x: JsonElement) = |
| 29 | + match x.TryGetProperty("text/html") with |
| 30 | + | true, html -> |
| 31 | + html.EnumerateArray() |
| 32 | + |> Seq.map (fun x -> x.GetString()) |
| 33 | + |> String.concat "" |
| 34 | + |> Some |
| 35 | + | _ -> None |
| 36 | + |
| 37 | + let (|TextPlain|_|) (x: JsonElement) = |
| 38 | + match x.TryGetProperty("text/plain") with |
| 39 | + | true, text -> |
| 40 | + let text = text.EnumerateArray() |> Seq.map (fun x -> x.GetString()) |> String.concat "" |
| 41 | + |
| 42 | + Some( |
| 43 | + """<table class="pre"><tr><td><pre><code>""" |
| 44 | + + text |
| 45 | + + """</code></pre></td></tr></table>""" |
| 46 | + ) |
| 47 | + | _ -> None |
| 48 | + |
| 49 | + let (|DisplayData|_|) (x: JsonElement) = |
| 50 | + if x.GetProperty("output_type").GetString() = "display_data" then |
| 51 | + match x.GetProperty("data") with |
| 52 | + | TextHtml html -> html |
| 53 | + | TextPlain text -> text |
| 54 | + | s -> failwith $"unknown ouptut {s}" |
| 55 | + |> Some |
| 56 | + else |
| 57 | + None |
| 58 | + |
| 59 | + let (|Stream|_|) (x: JsonElement) = |
| 60 | + if x.GetProperty("output_type").GetString() = "stream" then |
| 61 | + let text = |
| 62 | + x.GetProperty("text").EnumerateArray() |
| 63 | + |> Seq.map (fun x -> x.GetString()) |
| 64 | + |> String.concat "" |
| 65 | + |
| 66 | + Some( |
| 67 | + """<table class="pre"><tr><td><pre><code>""" |
| 68 | + + text |
| 69 | + + """</code></pre></td></tr></table>""" |
| 70 | + ) |
| 71 | + else |
| 72 | + None |
| 73 | + |
| 74 | + let parse (output: JsonElement) = |
| 75 | + match output with |
| 76 | + | Stream stream -> stream |
| 77 | + | DisplayData displayData -> displayData |
| 78 | + | s -> failwith $"""unknown output {s.GetProperty("output_type").GetString()}""" |
| 79 | + |
| 80 | + let getSource (cell: JsonElement) = |
| 81 | + let source = |
| 82 | + match cell.TryGetProperty("source") with |
| 83 | + | true, xs -> xs.EnumerateArray() |
| 84 | + | _ -> failwith "no source" |
| 85 | + |
| 86 | + source |> Seq.map (fun x -> x.GetString()) |> String.concat "" |
| 87 | + |
| 88 | + let collectOutputs (cell: JsonElement) = |
| 89 | + match cell.TryGetProperty("outputs") with |
| 90 | + | true, outputs -> |
| 91 | + let xs = outputs.EnumerateArray() |
| 92 | + |
| 93 | + if Seq.isEmpty xs then |
| 94 | + None |
| 95 | + else |
| 96 | + xs |> Seq.map Output.parse |> Seq.toArray |> Some |
| 97 | + | _ -> None |
| 98 | + |
| 99 | + let getCode (cell: JsonElement) = |
| 100 | + let lang = |
| 101 | + let metadata (elem: JsonElement) = |
| 102 | + match elem.TryGetProperty("metadata") with |
| 103 | + | false, _ -> failwith "Code cell does not have metadata" |
| 104 | + | true, metadata -> metadata |
| 105 | + |
| 106 | + let languageInfo (metadata: JsonElement) = |
| 107 | + match metadata.TryGetProperty("polyglot_notebook") with |
| 108 | + | false, _ -> failwith "code cell does not have metadata.polyglot_notebook" |
| 109 | + | true, language_info -> language_info |
| 110 | + |
| 111 | + let kernelName (languageInfo: JsonElement) = |
| 112 | + match languageInfo.TryGetProperty("kernelName") with |
| 113 | + | false, _ -> failwith "code cell does not have metadata.polyglot_notebook.kernelName" |
| 114 | + | true, name -> name.GetString() |
| 115 | + |
| 116 | + cell |> metadata |> languageInfo |> kernelName |
| 117 | + |
| 118 | + let source = getSource cell |
| 119 | + let outputs = collectOutputs cell |
| 120 | + |
| 121 | + Code |
| 122 | + {| lang = lang |
| 123 | + source = source |
| 124 | + outputs = outputs |} |
| 125 | + |
| 126 | + |
| 127 | + let parseCell (cell: JsonElement) = |
| 128 | + let cell_type = |
| 129 | + match cell.TryGetProperty("cell_type") with |
| 130 | + | true, cellType -> cellType.GetString() |
| 131 | + | _ -> failwith "no cell type" |
| 132 | + |
| 133 | + match cell_type with |
| 134 | + | "markdown" -> |
| 135 | + match getSource cell, collectOutputs cell with |
| 136 | + | _, Some _ -> failwith $"Markdown should not have outputs" |
| 137 | + | source, None -> Markdown source |
| 138 | + | "code" -> getCode cell |
| 139 | + | _ -> failwith $"unknown cell type {cell_type}" |
| 140 | + |
| 141 | + let ipynbToMarkdown ipynbFile = |
| 142 | + let json = JsonDocument.Parse(File.ReadAllText(ipynbFile)) |
| 143 | + |
| 144 | + json.RootElement.GetProperty("cells").EnumerateArray() |
| 145 | + |> Seq.map (parseCell >> (fun x -> x.ToMarkdown())) |
| 146 | + |> String.concat "\n\n" |
0 commit comments