Skip to content

Commit fe5be21

Browse files
Register the notebook LaTeX formatters by reflection, and drop polyfill.io (#2111)
AngouriMath's extension.dib, the C# script NuGet loads with the package, began `using AngouriMath;`. An F# notebook loads it into the C# kernel, which has no reference to AngouriMath, so the first output of every F# notebook was CS0246. It now finds ILatexizeable, EDecimal, EInteger and ERational among the loaded assemblies and registers its formatters on those types, which works from either kernel. Measured in the binder/Dockerfile image with the packages packed locally: from F#, the script loads and says so, and derivative "x" "x^3" is 3 x^2 in LaTeX; from C#, an entity and an ERational render as before. And both this script and AngouriMath.Interactive's F# extension put <script src='https://polyfill.io/...'> into every HTML output, a domain taken over in 2024 and used to serve malicious code. MathJax 3 needs no polyfill in a current browser; the line is gone, from the tests' expected HTML too. Fixes #2109. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9893cf4 commit fe5be21

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

‎Sources/AngouriMath/extension.dib‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#!csharp
22

3-
using AngouriMath;
4-
using AngouriMath.Extensions;
5-
using Microsoft.DotNet.Interactive;
3+
// The LaTeX formatters, registered by reflection rather than against AngouriMath's types: an F# notebook loads
4+
// this script into the C# kernel, which has no reference to AngouriMath, and `using AngouriMath;` there was a
5+
// compile error at the top of every F# notebook. https://github.com/asc-community/AngouriMath/issues/2109
6+
using System;
7+
using System.Linq;
68
using Microsoft.DotNet.Interactive.Formatting;
79

8-
static void RegisterLatex<T>(Func<T, string> latexer)
9-
{
10-
Formatter.Register<T>(
11-
t => $"$${latexer(t)}$$",
12-
"text/latex");
10+
static Type FindType(string assembly, string name)
11+
=> AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assembly)?.GetType(name);
1312

14-
Formatter.Register<T>(
15-
t => $@"
16-
<script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
13+
static void RegisterLatex(Type type, Func<object, string> latexer)
14+
{
15+
Formatter.Register(type, (o, writer) => writer.Write($"$${latexer(o)}$$"), "text/latex");
16+
Formatter.Register(type, (o, writer) => writer.Write($@"
1717
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
18-
\[{latexer(t)}\]",
19-
"text/html"
20-
);
21-
22-
Formatter.SetPreferredMimeTypesFor(typeof(T), "text/latex");
18+
\[{latexer(o)}\]"), "text/html");
19+
Formatter.SetPreferredMimeTypesFor(type, "text/latex");
2320
}
2421

25-
Formatter.SetPreferredMimeTypesFor(typeof(PeterO.Numbers.EDecimal), "text/plain");
26-
Formatter.Register<PeterO.Numbers.EDecimal>(o => o.ToString(), "text/plain");
22+
foreach (var name in new[] { "PeterO.Numbers.EDecimal", "PeterO.Numbers.EInteger" })
23+
if (FindType("Numbers", name) is { } plain)
24+
{
25+
Formatter.SetPreferredMimeTypesFor(plain, "text/plain");
26+
Formatter.Register(plain, (o, writer) => writer.Write(o.ToString()), "text/plain");
27+
}
2728

28-
Formatter.SetPreferredMimeTypesFor(typeof(PeterO.Numbers.EInteger), "text/plain");
29-
Formatter.Register<PeterO.Numbers.EInteger>(o => o.ToString(), "text/plain");
29+
if (FindType("AngouriMath", "AngouriMath.Core.ILatexizeable") is { } latexizeable
30+
&& latexizeable.GetMethod("Latexize", Type.EmptyTypes) is { } latexize)
31+
RegisterLatex(latexizeable, o => (string)latexize.Invoke(o, null));
3032

31-
RegisterLatex<AngouriMath.Core.ILatexizeable>(o => o.Latexize());
32-
33-
RegisterLatex<PeterO.Numbers.ERational>(o => $@"\frac{{{o.Numerator}}}{{{o.Denominator}}}");
33+
if (FindType("Numbers", "PeterO.Numbers.ERational") is { } rational)
34+
RegisterLatex(rational, o => $@"\frac{{{rational.GetProperty("Numerator").GetValue(o)}}}{{{rational.GetProperty("Denominator").GetValue(o)}}}");
3435

3536
"Extensions for LaTeX loaded!"
36-

‎Sources/Tests/InteractiveWrapperUnitTests/HtmlOutput.fs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ let ``Latex with magic ILatexizeable html`` () =
1818
let entity = parsed "x / 2"
1919
let html = entity.ToDisplayString("text/html")
2020
Assert.Equal(@"
21-
<script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
2221
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
2322
\[\frac{x}{2}\]", html)
2423

@@ -29,7 +28,6 @@ let ``Latex with magic ERational for html`` () =
2928
let entity = (("2 / 3" |> asNumber) :?> Entity.Number.Rational).ERational
3029
let html = entity.ToDisplayString("text/html")
3130
Assert.Equal(@"
32-
<script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
3331
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
3432
\[\frac{2}{3}\]", html)
3533

@@ -40,7 +38,6 @@ let ``Latex with magic ERational multi-char for html`` () =
4038
let entity = (("23 / 36" |> asNumber) :?> Entity.Number.Rational).ERational
4139
let html = entity.ToDisplayString("text/html")
4240
Assert.Equal(@"
43-
<script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
4441
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
4542
\[\frac{23}{36}\]", html)
4643

‎Sources/Wrappers/AngouriMath.Interactive/KernelExtension.fs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type KernelExtension() =
2020

2121
// register text/html
2222
(fun o -> $@"
23-
<script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
2423
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
2524
\[{latexizer o}\]")
2625
|> (fun f -> new Func<'a, string>(f))

0 commit comments

Comments
 (0)