From 8fd732fec5d12f16780d402f44a87774f3839289 Mon Sep 17 00:00:00 2001 From: Rik Huijzer Date: Sat, 19 Mar 2022 12:08:31 +0100 Subject: [PATCH] Support `with_terminal` and update Pluto to 0.18.4 (#88) --- Project.toml | 4 +- docs/make.jl | 5 ++- docs/src/with_terminal.md | 87 +++++++++++++++++++++++++++++++++++++++ src/PlutoStaticHTML.jl | 1 + src/html.jl | 9 +++- src/with_terminal.jl | 21 ++++++++++ test/html.jl | 19 ++++++++- test/runtests.jl | 4 ++ test/with_terminal.jl | 48 +++++++++++++++++++++ 9 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 docs/src/with_terminal.md create mode 100644 src/with_terminal.jl create mode 100644 test/with_terminal.jl diff --git a/Project.toml b/Project.toml index 75eec92c..3f74eb5d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PlutoStaticHTML" uuid = "359b1769-a58e-495b-9770-312e911026ad" authors = ["Rik Huijzer "] -version = "4.0.2" +version = "4.0.3" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -11,5 +11,5 @@ SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" [compat] -Pluto = "=0.18.2" +Pluto = "=0.18.4" julia = "1.6" diff --git a/docs/make.jl b/docs/make.jl index 7133e6c2..53acf188 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -23,14 +23,15 @@ function build() return nothing end -if !("DISABLE_NOTEBOOK_BUILD" in keys(ENV)) +if !("DISABLE_NOTEBOOKS_BUILD" in keys(ENV)) build() end sitename = "PlutoStaticHTML.jl" pages = [ "PlutoStaticHTML" => "index.md", - "Example notebook" => "notebooks/example.md" + "Example notebook" => "notebooks/example.md", + "`with_terminal`" => "with_terminal.md" ] # Using MathJax3 since Pluto uses that engine too. diff --git a/docs/src/with_terminal.md b/docs/src/with_terminal.md new file mode 100644 index 00000000..b6e15332 --- /dev/null +++ b/docs/src/with_terminal.md @@ -0,0 +1,87 @@ +# `with_terminal` + +`PlutoUI` has a well-known function `with_terminal` to show terminal output with a black background and colored text. +For example, when having loaded `PlutoUI` via `using PlutoUI`, the following code will show the text "Some terminal output" in a mini terminal window inside `Pluto`: + +```julia +with_terminal() do + println("Some terminal output") +end +``` + +This functionality is supported by `PlutoStaticHTML` too. +To make it work, `PlutoStaticHTML` takes the output from `Pluto`, which looks roughly as follows: + +```html +
+ +
+``` + +and changes it to: + +```html +
+Some terminal output
+
+``` + +This output is now much simpler to style to your liking. +Below, there is an example style that you can apply which will style the terminal output just like it would in `Pluto`. + +In terminals, the colors are enabled via so called ANSI escape codes. +These ANSI colors can be shown correctly by adding the following Javascript to the footer of your website. +This code will loop through all the HTML elements with `id="plutouiterminal"` and apply the `ansi_to_html` function to the body of those elements: + +```html + +``` + +Next, the output can be made more to look like an embedded terminal by adding the following to your CSS: + +```css +#plutouiterminal { + max-height: 300px; + overflow: auto; + white-space: pre; + color: white; + background-color: black; + border-radius: 3px; + margin-top: 8px; + margin-bottom: 8px; + padding: 15px; + display: block; + font-size: 14px; +} +``` + +!!! note + Note that the Javascript code above downloads the `ansi_up.js` file from a content delivery network (CDN). + This is not advised because CDNs are bad for privacy, may go offline and are bad for performance. + They are bad for performance because browsers do not cache CDN downloaded content between different domains for security reasons. + Therefore, CDN content will cause at least an extra DNS lookup in most cases. diff --git a/src/PlutoStaticHTML.jl b/src/PlutoStaticHTML.jl index 1cbfc00c..b0acd212 100644 --- a/src/PlutoStaticHTML.jl +++ b/src/PlutoStaticHTML.jl @@ -36,6 +36,7 @@ include("module_doc.jl") include("context.jl") include("cache.jl") include("mimeoverride.jl") +include("with_terminal.jl") include("html.jl") include("build.jl") diff --git a/src/html.jl b/src/html.jl index cb45f4b0..1aad983e 100644 --- a/src/html.jl +++ b/src/html.jl @@ -242,12 +242,17 @@ end function _output2html(cell::Cell, ::MIME"text/html", hopts) body = cell.output.body + + if contains(body, """ + + """ + +txt = strip(PlutoStaticHTML._extract_txt(body)) +@test startswith(txt, "MethodInstance") +@test endswith(txt, "return %2") +@test contains(txt, '\n') + +patched = PlutoStaticHTML._patch_with_terminal(body) +@test contains(patched, """
""")