From 8a35f75fd134a56baf29997d65e2222e1da979ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Storopoli?= Date: Mon, 15 Jan 2024 19:27:48 +0000 Subject: [PATCH 1/5] fix: add more frontmatter stuff --- bin/pluto.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bin/pluto.jl b/bin/pluto.jl index 64fe7c8..df1652a 100644 --- a/bin/pluto.jl +++ b/bin/pluto.jl @@ -44,6 +44,14 @@ function convert_notebook(path) """ --- title: "$(title)" + format: + html: + embed-resources: true + self-contained-math: true + toc: true + fig-format: svg + fig-width: 8 + fig-height: 6 --- """, ) From d9c27f3976f855d10f102b00ffcbcaded8c299fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Storopoli?= Date: Mon, 15 Jan 2024 19:28:39 +0000 Subject: [PATCH 2/5] fix(format): indent = 4 --- bin/pluto.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/pluto.jl b/bin/pluto.jl index df1652a..baa7668 100644 --- a/bin/pluto.jl +++ b/bin/pluto.jl @@ -100,8 +100,8 @@ function format_code(code) stripped = strip(code) if startswith(stripped, "begin") && endswith(stripped, "end") stripped = String(strip(stripped[7:end-3])) - return JuliaFormatter.format_text(stripped; indent = 2) + return JuliaFormatter.format_text(stripped; indent = 4) else - return JuliaFormatter.format_text(String(stripped); indent = 2) + return JuliaFormatter.format_text(String(stripped); indent = 4) end end From ee4a924d8aab40abc4ba73dc89ca18fae7eb4422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Storopoli?= Date: Mon, 15 Jan 2024 19:28:57 +0000 Subject: [PATCH 3/5] fix: add `let ... end` blocks --- bin/pluto.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/pluto.jl b/bin/pluto.jl index baa7668..b54d977 100644 --- a/bin/pluto.jl +++ b/bin/pluto.jl @@ -96,11 +96,14 @@ function print_code(buffer, code) end function format_code(code) - # Drop `begin` and `end` if they start and end the code block. + # Drop `begin`/`let` and `end` if they start and end the code block. stripped = strip(code) if startswith(stripped, "begin") && endswith(stripped, "end") stripped = String(strip(stripped[7:end-3])) return JuliaFormatter.format_text(stripped; indent = 4) + elseif startswith(stripped, "let") && endswith(stripped, "end") + stripped = String(strip(stripped[5:end-3])) + return JuliaFormatter.format_text(stripped; indent = 4) else return JuliaFormatter.format_text(String(stripped); indent = 4) end From f93cf9dd8e5a43c2f8741acdee630db81502be4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Storopoli?= Date: Mon, 15 Jan 2024 19:29:09 +0000 Subject: [PATCH 4/5] feat: convert admonitions --- bin/pluto.jl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/bin/pluto.jl b/bin/pluto.jl index b54d977..84040c2 100644 --- a/bin/pluto.jl +++ b/bin/pluto.jl @@ -3,6 +3,8 @@ import Logging import MacroTools import Pluto +const ADMONITION_REGEX = r"!!! (note|warning|tip)" + function convert_notebooks(dir) dir = abspath(dir) for (root, dirs, files) in walkdir(dir) @@ -63,6 +65,7 @@ function convert_notebook(path) if Meta.isexpr(ex, :toplevel) if !isempty(ex.args) && MacroTools.@capture ex.args[end] @md_str(str_) source = ex.args[end].args[end] + source = contains(source, ADMONITION_REGEX) ? format_markdown(source) : source println(buffer, source) else print_code(buffer, cell.code) @@ -95,6 +98,38 @@ function print_code(buffer, code) println(buffer) end +function format_markdown(text) + # Convert Pluto admonitions to Quarto callouts + replacements = Dict("note" => "callout-note", "warning" => "callout-caution", "tip" => "callout-tip") + lines = split(text, '\n') + new_text = "" + in_block = false + + for line in lines + begin_block = match(ADMONITION_REGEX, line) + if begin_block !== nothing + if in_block + new_text = new_text[1:end-1] * ":::\n\n" + end + new_text *= ":::{.$(replacements[begin_block.captures[1]])}\n" + in_block = true + elseif in_block && !startswith(line, r"^[^\s]") + new_text *= replace(replace(String(line), r"^ {4,8}" => ""), r"^\t+" => "") * "\n" + elseif in_block + new_text = new_text[1:end-1] * ":::\n\n" * line * "\n" + in_block = false + else + new_text *= line * "\n" + end + end + + if in_block + new_text = new_text[1:end-1] * ":::\n\n" + end + + return new_text +end + function format_code(code) # Drop `begin`/`let` and `end` if they start and end the code block. stripped = strip(code) From 7ff8e2e51ec5110025ce175d20c063e93d156860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Storopoli?= Date: Mon, 15 Jan 2024 19:41:04 +0000 Subject: [PATCH 5/5] fix: frontmatter --- bin/pluto.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pluto.jl b/bin/pluto.jl index 84040c2..96b14d4 100644 --- a/bin/pluto.jl +++ b/bin/pluto.jl @@ -47,7 +47,7 @@ function convert_notebook(path) --- title: "$(title)" format: - html: + html: embed-resources: true self-contained-math: true toc: true