Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improvements over Pluto conversions #10

Merged
merged 5 commits into from
Jan 15, 2024
Merged
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
52 changes: 49 additions & 3 deletions bin/pluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -44,6 +46,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
---
""",
)
Expand All @@ -55,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)
Expand Down Expand Up @@ -87,13 +98,48 @@ 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` 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 = 2)
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 = 2)
return JuliaFormatter.format_text(String(stripped); indent = 4)
end
end