|
|
|
@ -46,7 +46,7 @@ CodeChunk() = CodeChunk(String[], false) |
|
|
|
|
|
|
|
|
|
|
|
ismdline(line) = (occursin(r"^\h*#$", line) || occursin(r"^\h*# .*$", line)) && !occursin(r"^\h*##", line) |
|
|
|
ismdline(line) = (occursin(r"^\h*#$", line) || occursin(r"^\h*# .*$", line)) && !occursin(r"^\h*##", line) |
|
|
|
|
|
|
|
|
|
|
|
function parse(content; allow_continued = true) |
|
|
|
function parse(content; allow_continued=true) |
|
|
|
lines = collect(eachline(IOBuffer(content))) |
|
|
|
lines = collect(eachline(IOBuffer(content))) |
|
|
|
|
|
|
|
|
|
|
|
chunks = Chunk[] |
|
|
|
chunks = Chunk[] |
|
|
|
@ -125,9 +125,9 @@ end |
|
|
|
|
|
|
|
|
|
|
|
function replace_default(content, sym; |
|
|
|
function replace_default(content, sym; |
|
|
|
config::Dict, |
|
|
|
config::Dict, |
|
|
|
branch = "gh-pages", |
|
|
|
branch="gh-pages", |
|
|
|
commit = "master" |
|
|
|
commit="master" |
|
|
|
) |
|
|
|
) |
|
|
|
repls = Pair{Any,Any}[] |
|
|
|
repls = Pair{Any,Any}[] |
|
|
|
|
|
|
|
|
|
|
|
# add some shameless advertisement |
|
|
|
# add some shameless advertisement |
|
|
|
@ -245,7 +245,7 @@ function edit_commit(inputfile, user_config) |
|
|
|
readchomp( |
|
|
|
readchomp( |
|
|
|
pipeline( |
|
|
|
pipeline( |
|
|
|
setenv(`$(git) rev-parse --show-toplevel`; dir=dirname(inputfile)); |
|
|
|
setenv(`$(git) rev-parse --show-toplevel`; dir=dirname(inputfile)); |
|
|
|
stderr=devnull, |
|
|
|
stderr=devnull |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
catch |
|
|
|
catch |
|
|
|
@ -431,7 +431,7 @@ Available options: |
|
|
|
`$(_DEFAULT_IMAGE_FORMATS)`. Results which are `showable` with a MIME type are saved with |
|
|
|
`$(_DEFAULT_IMAGE_FORMATS)`. Results which are `showable` with a MIME type are saved with |
|
|
|
the first match, with the corresponding extension. |
|
|
|
the first match, with the corresponding extension. |
|
|
|
""" |
|
|
|
""" |
|
|
|
const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation |
|
|
|
const DEFAULT_CONFIGURATION = nothing # Dummy const for documentation |
|
|
|
|
|
|
|
|
|
|
|
function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) |
|
|
|
function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) |
|
|
|
# Create configuration by merging default and userdefined |
|
|
|
# Create configuration by merging default and userdefined |
|
|
|
@ -485,7 +485,7 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) |
|
|
|
content = replace_default(content, type; config=config) |
|
|
|
content = replace_default(content, type; config=config) |
|
|
|
|
|
|
|
|
|
|
|
# parse the content into chunks |
|
|
|
# parse the content into chunks |
|
|
|
chunks = parse(content; allow_continued = type !== :nb) |
|
|
|
chunks = parse(content; allow_continued=type !== :nb) |
|
|
|
|
|
|
|
|
|
|
|
return chunks, config |
|
|
|
return chunks, config |
|
|
|
end |
|
|
|
end |
|
|
|
@ -676,72 +676,38 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg |
|
|
|
# preprocessing and parsing |
|
|
|
# preprocessing and parsing |
|
|
|
chunks, config = |
|
|
|
chunks, config = |
|
|
|
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md) |
|
|
|
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md) |
|
|
|
flavor=config["flavor"] |
|
|
|
|
|
|
|
# create the markdown file |
|
|
|
# create the markdown file |
|
|
|
sb = sandbox() |
|
|
|
|
|
|
|
iomd = IOBuffer() |
|
|
|
iomd = IOBuffer() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if flavor isa CarpentriesFlavor |
|
|
|
write_md_chunks!(iomd, chunks, outputdir, config) |
|
|
|
for chunk in chunks |
|
|
|
|
|
|
|
if isa(chunk, MDChunk) |
|
|
|
|
|
|
|
if containsAdmonition(chunk) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
str = chunkToMD(chunk) |
|
|
|
|
|
|
|
mdContent = str.content |
|
|
|
|
|
|
|
writeContent(mdContent, iomd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else |
|
|
|
# custom post-processing from user |
|
|
|
# Handle chunks without admonitions |
|
|
|
content = config["postprocess"](String(take!(iomd))) |
|
|
|
for line in chunk.lines |
|
|
|
|
|
|
|
write(iomd, line.second, '\n') # Skip indent |
|
|
|
# write to file |
|
|
|
end |
|
|
|
outputfile = write_result(content, config) |
|
|
|
end |
|
|
|
return outputfile |
|
|
|
## `else` is copied from vanilla: |
|
|
|
|
|
|
|
else # isa(chunk, CodeChunk) |
|
|
|
end |
|
|
|
iocode = IOBuffer() |
|
|
|
|
|
|
|
codefence = config["codefence"]::Pair |
|
|
|
function write_md_chunks!(iomd, chunks, outputdir, config) |
|
|
|
write(iocode, codefence.first) |
|
|
|
flavor = config["flavor"] |
|
|
|
# make sure the code block is finalized if we are printing to ```@example |
|
|
|
sb = sandbox() |
|
|
|
# (or ````@example, any number of backticks >= 3 works) |
|
|
|
|
|
|
|
if chunk.continued && occursin(r"^`{3,}@example", codefence.first) && isdocumenter(config) |
|
|
|
|
|
|
|
write(iocode, "; continued = true") |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
write(iocode, '\n') |
|
|
|
|
|
|
|
# filter out trailing #hide unless code is executed by Documenter |
|
|
|
|
|
|
|
execute = config["execute"]::Bool |
|
|
|
|
|
|
|
write_hide = isdocumenter(config) && !execute |
|
|
|
|
|
|
|
write_line(line) = write_hide || !endswith(line, "#hide") |
|
|
|
|
|
|
|
for line in chunk.lines |
|
|
|
|
|
|
|
write_line(line) && write(iocode, line, '\n') |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
if write_hide && REPL.ends_with_semicolon(chunk.lines[end]) |
|
|
|
|
|
|
|
write(iocode, "nothing #hide\n") |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
write(iocode, codefence.second, '\n') |
|
|
|
|
|
|
|
any(write_line, chunk.lines) && write(iomd, seekstart(iocode)) |
|
|
|
|
|
|
|
if execute |
|
|
|
|
|
|
|
cd(config["literate_outputdir"]) do |
|
|
|
|
|
|
|
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; |
|
|
|
|
|
|
|
inputfile=config["literate_inputfile"], |
|
|
|
|
|
|
|
fake_source=config["literate_outputfile"], |
|
|
|
|
|
|
|
flavor=config["flavor"], |
|
|
|
|
|
|
|
image_formats=config["image_formats"], |
|
|
|
|
|
|
|
file_prefix="$(config["name"])-$(chunknum)", |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
# kinda Vanilla Function |
|
|
|
|
|
|
|
for (chunknum, chunk) in enumerate(chunks) |
|
|
|
for (chunknum, chunk) in enumerate(chunks) |
|
|
|
if isa(chunk, MDChunk) |
|
|
|
if isa(chunk, MDChunk) |
|
|
|
|
|
|
|
|
|
|
|
#______________________________________________________________________________________________________________ |
|
|
|
#______________________________________________________________________________________________________________ |
|
|
|
|
|
|
|
if flavor isa CarpentriesFlavor |
|
|
|
|
|
|
|
if containsAdmonition(chunk) |
|
|
|
|
|
|
|
writeContent(chunk.lines, iomd) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
else |
|
|
|
if containsYAML(chunk) # This part is the only change. It (should) delete the YAML Admo for non Carpentries MD. |
|
|
|
if containsYAML(chunk) # This part is the only change. It (should) delete the YAML Admo for non Carpentries MD. |
|
|
|
continue |
|
|
|
continue |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
#______________________________________________________________________________________________________________ |
|
|
|
#______________________________________________________________________________________________________________ |
|
|
|
|
|
|
|
|
|
|
|
for line in chunk.lines |
|
|
|
for line in chunk.lines |
|
|
|
@ -776,23 +742,13 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg |
|
|
|
fake_source=config["literate_outputfile"], |
|
|
|
fake_source=config["literate_outputfile"], |
|
|
|
flavor=config["flavor"], |
|
|
|
flavor=config["flavor"], |
|
|
|
image_formats=config["image_formats"], |
|
|
|
image_formats=config["image_formats"], |
|
|
|
file_prefix="$(config["name"])-$(chunknum)", |
|
|
|
file_prefix="$(config["name"])-$(chunknum)" |
|
|
|
) |
|
|
|
) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
write(iomd, '\n') # add a newline between each chunk |
|
|
|
write(iomd, '\n') # add a newline between each chunk |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# custom post-processing from user |
|
|
|
|
|
|
|
content = config["postprocess"](String(take!(iomd))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# write to file |
|
|
|
|
|
|
|
outputfile = write_result(content, config) |
|
|
|
|
|
|
|
return outputfile |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -851,8 +807,8 @@ function parse_nbmeta(line) |
|
|
|
# Cf. https://jupytext.readthedocs.io/en/latest/formats.html#the-percent-format |
|
|
|
# Cf. https://jupytext.readthedocs.io/en/latest/formats.html#the-percent-format |
|
|
|
m = match(r"^%% ([^[{]+)?\s*(?:\[(\w+)\])?\s*(\{.*)?$", line) |
|
|
|
m = match(r"^%% ([^[{]+)?\s*(?:\[(\w+)\])?\s*(\{.*)?$", line) |
|
|
|
typ = m.captures[2] |
|
|
|
typ = m.captures[2] |
|
|
|
name = m.captures[1] === nothing ? Dict{String, String}() : Dict("name" => m.captures[1]) |
|
|
|
name = m.captures[1] === nothing ? Dict{String,String}() : Dict("name" => m.captures[1]) |
|
|
|
meta = m.captures[3] === nothing ? Dict{String, Any}() : JSON.parse(m.captures[3]) |
|
|
|
meta = m.captures[3] === nothing ? Dict{String,Any}() : JSON.parse(m.captures[3]) |
|
|
|
return typ, merge(name, meta) |
|
|
|
return typ, merge(name, meta) |
|
|
|
end |
|
|
|
end |
|
|
|
line_is_nbmeta(line::Pair) = line_is_nbmeta(line.second) |
|
|
|
line_is_nbmeta(line::Pair) = line_is_nbmeta(line.second) |
|
|
|
@ -875,7 +831,7 @@ function notebook(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg |
|
|
|
nb = jupyter_notebook(chunks, config) |
|
|
|
nb = jupyter_notebook(chunks, config) |
|
|
|
|
|
|
|
|
|
|
|
# write to file |
|
|
|
# write to file |
|
|
|
outputfile = write_result(nb, config; print = (io, c)->JSON.print(io, c, 1)) |
|
|
|
outputfile = write_result(nb, config; print=(io, c) -> JSON.print(io, c, 1)) |
|
|
|
return outputfile |
|
|
|
return outputfile |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@ -894,7 +850,7 @@ function jupyter_notebook(chunks, config) |
|
|
|
metatype !== nothing && metatype != chunktype && error("specifying a different cell type is not supported") |
|
|
|
metatype !== nothing && metatype != chunktype && error("specifying a different cell type is not supported") |
|
|
|
popfirst!(chunk.lines) |
|
|
|
popfirst!(chunk.lines) |
|
|
|
else |
|
|
|
else |
|
|
|
metadata = Dict{String, Any}() |
|
|
|
metadata = Dict{String,Any}() |
|
|
|
end |
|
|
|
end |
|
|
|
lines = isa(chunk, MDChunk) ? |
|
|
|
lines = isa(chunk, MDChunk) ? |
|
|
|
String[x.second for x in chunk.lines] : # skip indent |
|
|
|
String[x.second for x in chunk.lines] : # skip indent |
|
|
|
@ -923,7 +879,7 @@ function jupyter_notebook(chunks, config) |
|
|
|
language_info = Dict() |
|
|
|
language_info = Dict() |
|
|
|
language_info["file_extension"] = ".jl" |
|
|
|
language_info["file_extension"] = ".jl" |
|
|
|
language_info["mimetype"] = "application/julia" |
|
|
|
language_info["mimetype"] = "application/julia" |
|
|
|
language_info["name"]= "julia" |
|
|
|
language_info["name"] = "julia" |
|
|
|
language_info["version"] = string(VERSION) |
|
|
|
language_info["version"] = string(VERSION) |
|
|
|
metadata["language_info"] = language_info |
|
|
|
metadata["language_info"] = language_info |
|
|
|
|
|
|
|
|
|
|
|
@ -963,7 +919,7 @@ function execute_notebook(nb; inputfile::String, fake_source::String) |
|
|
|
stream = Dict{String,Any}() |
|
|
|
stream = Dict{String,Any}() |
|
|
|
stream["output_type"] = "stream" |
|
|
|
stream["output_type"] = "stream" |
|
|
|
stream["name"] = "stdout" |
|
|
|
stream["name"] = "stdout" |
|
|
|
stream["text"] = collect(Any, eachline(IOBuffer(String(str)), keep = true)) |
|
|
|
stream["text"] = collect(Any, eachline(IOBuffer(String(str)), keep=true)) |
|
|
|
push!(cell["outputs"], stream) |
|
|
|
push!(cell["outputs"], stream) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@ -973,7 +929,7 @@ function execute_notebook(nb; inputfile::String, fake_source::String) |
|
|
|
function split_mime(dict) |
|
|
|
function split_mime(dict) |
|
|
|
for mime in ("image/svg+xml", "text/html") |
|
|
|
for mime in ("image/svg+xml", "text/html") |
|
|
|
if haskey(dict, mime) |
|
|
|
if haskey(dict, mime) |
|
|
|
dict[mime] = collect(Any, eachline(IOBuffer(dict[mime]), keep = true)) |
|
|
|
dict[mime] = collect(Any, eachline(IOBuffer(dict[mime]), keep=true)) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
return dict |
|
|
|
return dict |
|
|
|
@ -1053,7 +1009,7 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source |
|
|
|
# - c.output: combined stdout and stderr |
|
|
|
# - c.output: combined stdout and stderr |
|
|
|
# `rethrow = Union{}` means that we try-catch all the exceptions thrown in the do-block |
|
|
|
# `rethrow = Union{}` means that we try-catch all the exceptions thrown in the do-block |
|
|
|
# and return them via the return value (they get handled below). |
|
|
|
# and return them via the return value (they get handled below). |
|
|
|
c = IOCapture.capture(rethrow = Union{}) do |
|
|
|
c = IOCapture.capture(rethrow=Union{}) do |
|
|
|
include_string(sb, block, fake_source) |
|
|
|
include_string(sb, block, fake_source) |
|
|
|
end |
|
|
|
end |
|
|
|
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here |
|
|
|
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here |
|
|
|
|