Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ let help () =
System.Diagnostics.Process.Start psi
$""Sending you to {url}""

/// The page `render` opens: the expression's LaTeX -- what `latex` gives -- typeset by MathJax,
/// with the LaTeX itself beneath it, written to the temporary directory. Joined with
/// String.concat, since `+` builds an expression here.
/// https://github.com/asc-community/AngouriMath/issues/549
let renderedPage (x : obj) =
let source = System.Net.WebUtility.HtmlEncode (latex x)
let page = System.IO.Path.Combine (System.IO.Path.GetTempPath (), $""angourimath-{System.Guid.NewGuid ()}.html"")
let html =
String.concat """" [
""<!DOCTYPE html><html><head><meta charset='utf-8'><title>AngouriMath</title>""
""<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>""
""</head><body><p>$$""; source; ""$$</p><pre>""; source; ""</pre></body></html>"" ]
System.IO.File.WriteAllText (page, html)
page

/// The expression typeset in the browser, as Plot's charts are shown; `latex` gives its LaTeX
/// as text. https://github.com/asc-community/AngouriMath/issues/549
let render (x : obj) =
let page = renderedPage x
let psi = System.Diagnostics.ProcessStartInfo page
psi.UseShellExecute <- true
try
System.Diagnostics.Process.Start psi |> ignore
printfn $""Showing {page} in the browser""
with _ ->
printfn $""Wrote {page}, and no browser could be started to show it""

/// Arithmetic here builds expressions, so `3 / 2` is a rational rather than 1 and `x + 1` is
/// an expression rather than an error. The cost is that ordinary F# whose arithmetic must stay
/// primitive does not typecheck: `let rec fib n = if n < 2 then n else fib (n-1) + fib (n-2)`
Expand Down
13 changes: 13 additions & 0 deletions Sources/Terminal/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ them oldest first, and `back n` counts from the end.

A cell with no value — a `let` — adds nothing, and a question about the history is itself a cell
with a value, so it is remembered too.

## LaTeX

`latex` gives an expression's LaTeX, and `render` shows it typeset in the browser, the way a plot
is shown: a page in the temporary directory, rendered by MathJax, with the LaTeX beneath it.

```
[...] latex (x / 2)
\frac{x}{2}

[...] render (x / 2)
Showing /tmp/angourimath-….html in the browser
```
17 changes: 17 additions & 0 deletions Sources/Tests/TerminalUnitTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ let ``The values of earlier cells are kept in run`` () =
Assert.Equal(LatexSuccess ("11", "11"), execute kernel "back 5")
Assert.Equal(PlainTextSuccess "6", execute kernel "run.Count")

// `render` opens a page with the expression typeset; the page is the LaTeX `latex` gives, and
// escaped for HTML where the LaTeX has a character HTML reads.
// https://github.com/asc-community/AngouriMath/issues/549
[<Fact>]
let ``The rendered page holds the expression's LaTeX`` () =
let kernel = ``Create kernel or fail`` ()
enableAngouriMath kernel |> ignore
let page code =
match execute kernel $"System.IO.File.ReadAllText (renderedPage ({code}))" with
| PlainTextSuccess page -> page
| other -> failwith $"expected the page, and the prompt answered {other}"
Assert.Equal(PlainTextSuccess @"\frac{x}{2}", execute kernel "latex (x / 2)")
let fraction = page "x / 2"
Assert.Contains(@"$$\frac{x}{2}$$", fraction)
Assert.Contains("mathjax@3", fraction)
Assert.Contains("y &lt; 3", page "\"y < 3\"")

[<Fact>]
let ``Test 1`` () =
executeNewWithAm "1 + 1"
Expand Down
Loading