-
Notifications
You must be signed in to change notification settings - Fork 0
/
DibRunner.cs
84 lines (71 loc) · 2.42 KB
/
DibRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.Mermaid;
using Microsoft.DotNet.Interactive.PowerShell;
using System.Reactive.Linq;
namespace Dib2Html;
public static class DibRunner
{
static CompositeKernel MakeKernel()
=> new CompositeKernel
{
new CSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseWho()
.UseValueSharing()
.UseImportMagicCommand(),
new FSharpKernel()
.UseNugetDirective()
.UseKernelHelpers()
.UseWho()
.UseValueSharing()
.UseImportMagicCommand(),
new HtmlKernel(),
new JavaScriptKernel()
.UseWho()
.UseValueSharing()
.UseImportMagicCommand(),
new MermaidKernel()
.UseWho(),
new PowerShellKernel()
.UseImportMagicCommand()
.UseWho()
.UseProfiles(),
}
.UseLogMagicCommand()
.UseImportMagicCommand()
.UseNuGetExtensions();
public static void RunAndConvert(FileInfo filename, FileInfo? outfile, bool logInstall)
{
var kernel = MakeKernel();
var diagnostics = new DiagnosticsLogger(kernel);
var outputs = new OutputLogger(kernel, logInstall);
kernel.LoadAndRunInteractiveDocument(filename).Wait();
string name = System.IO.Path.GetFileNameWithoutExtension(filename.Name);
string htmlResult =
$"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{name}</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body>
<div style="max-width: 1000px; margin: auto;">
{outputs.HtmlOutputs.Aggregate((e, s) => e + s)}
<div>
</body>
</html>
""";
File.WriteAllText(outfile?.FullName ?? $"{name}.html", htmlResult);
if (diagnostics.Diagnostics.Any())
{
foreach (var diag in diagnostics.Diagnostics)
Console.WriteLine(diag);
}
}
}