Browse Source

Formatted

pull/200/head
J S 3 years ago
parent
commit
fa050c7863
  1. 21
      docs/make.jl
  2. 3
      src/IJulia.jl
  3. 183
      src/Literate.jl
  4. 569
      test/runtests.jl

21
docs/make.jl

@ -5,7 +5,11 @@ if haskey(ENV, "GITHUB_ACTIONS") @@ -5,7 +5,11 @@ if haskey(ENV, "GITHUB_ACTIONS")
end
deployconfig = Documenter.auto_detect_deploy_system()
Documenter.post_status(deployconfig; type="pending", repo="github.com/fredrikekre/Literate.jl.git")
Documenter.post_status(
deployconfig;
type = "pending",
repo = "github.com/fredrikekre/Literate.jl.git",
)
using Literate
using Plots # to not capture precompilation output
@ -23,7 +27,12 @@ Literate.notebook(EXAMPLE, OUTPUT, preprocess = preprocess) @@ -23,7 +27,12 @@ Literate.notebook(EXAMPLE, OUTPUT, preprocess = preprocess)
Literate.script(EXAMPLE, OUTPUT, preprocess = preprocess)
# generate the example notebook for the documentation, keep in sync with outputformats.md
Literate.markdown(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; credit = false, name = "name")
Literate.markdown(
joinpath(@__DIR__, "src/outputformats.jl"),
OUTPUT;
credit = false,
name = "name",
)
Literate.notebook(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; name = "notebook")
Literate.script(joinpath(@__DIR__, "src/outputformats.jl"), OUTPUT; credit = false)
@ -41,7 +50,10 @@ if haskey(ENV, "GITHUB_ACTIONS") @@ -41,7 +50,10 @@ if haskey(ENV, "GITHUB_ACTIONS")
end
url = "https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/$(folder)/"
str = read(joinpath(@__DIR__, "src/outputformats.md"), String)
str = replace(str, "[notebook.ipynb](generated/notebook.ipynb)." => "[notebook.ipynb]($(url)generated/notebook.ipynb).")
str = replace(
str,
"[notebook.ipynb](generated/notebook.ipynb)." => "[notebook.ipynb]($(url)generated/notebook.ipynb).",
)
write(joinpath(@__DIR__, "src/outputformats.md"), str)
end
@ -61,7 +73,8 @@ makedocs( @@ -61,7 +73,8 @@ makedocs(
"customprocessing.md",
"documenter.md",
"tips.md",
"generated/example.md"]
"generated/example.md",
],
)
deploydocs(

3
src/IJulia.jl

@ -19,7 +19,8 @@ const application_vnd_vegalite_v2 = MIME("application/vnd.vegalite.v2+json") @@ -19,7 +19,8 @@ const application_vnd_vegalite_v2 = MIME("application/vnd.vegalite.v2+json")
function display_dict(x)
data = Dict{String,Any}("text/plain" => limitstringmime(text_plain, x))
if showable(application_vnd_vegalite_v2, x)
data[string(application_vnd_vegalite_v2)] = JSON.parse(limitstringmime(application_vnd_vegalite_v2, x))
data[string(application_vnd_vegalite_v2)] =
JSON.parse(limitstringmime(application_vnd_vegalite_v2, x))
end
if showable(image_svg, x)
data[string(image_svg)] = limitstringmime(image_svg, x)

183
src/Literate.jl

@ -44,7 +44,9 @@ mutable struct CodeChunk <: Chunk @@ -44,7 +44,9 @@ mutable struct CodeChunk <: Chunk
end
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)
lines = collect(eachline(IOBuffer(content)))
@ -78,7 +80,10 @@ function parse(content; allow_continued = true) @@ -78,7 +80,10 @@ function parse(content; allow_continued = true)
end
# remove "## " and "##\n", strip the leading "#" from "## xyz" and "##| xyz"
# Note: accepts only standard space character (not no-break space U+00A0)
line = replace(replace(line, r"^(\h*)#(#(:? |\|).*)$" => s"\1\2"), r"^(\h*#)#$" => s"\1")
line = replace(
replace(line, r"^(\h*)#(#(:? |\|).*)$" => s"\1\2"),
r"^(\h*#)#$" => s"\1",
)
push!(chunks[end].lines, line)
end
end
@ -108,7 +113,10 @@ function parse(content; allow_continued = true) @@ -108,7 +113,10 @@ function parse(content; allow_continued = true)
append!(merged_chunks[end].lines, chunk.lines)
else # need to put back "#"
for line in chunk.lines
push!(merged_chunks[end].lines, rstrip(line.first * "# " * line.second))
push!(
merged_chunks[end].lines,
rstrip(line.first * "# " * line.second),
)
end
end
else
@ -124,11 +132,7 @@ function parse(content; allow_continued = true) @@ -124,11 +132,7 @@ function parse(content; allow_continued = true)
return chunks
end
function replace_default(content, sym;
config::Dict,
branch = "gh-pages",
commit = "master"
)
function replace_default(content, sym; config::Dict, branch = "gh-pages", commit = "master")
repls = Pair{Any,Any}[]
# add some shameless advertisement
@ -199,12 +203,16 @@ function replace_default(content, sym; @@ -199,12 +203,16 @@ function replace_default(content, sym;
# fix links
if get(ENV, "DOCUMENTATIONGENERATOR", "") == "true"
## DocumentationGenerator.jl
base_url = get(ENV, "DOCUMENTATIONGENERATOR_BASE_URL", "DOCUMENTATIONGENERATOR_BASE_URL")
base_url =
get(ENV, "DOCUMENTATIONGENERATOR_BASE_URL", "DOCUMENTATIONGENERATOR_BASE_URL")
nbviewer_root_url = "https://nbviewer.jupyter.org/urls/$(base_url)"
push!(repls, "@__NBVIEWER_ROOT_URL__" => nbviewer_root_url)
else
push!(repls, "@__REPO_ROOT_URL__" => get(config, "repo_root_url", "<unknown>"))
push!(repls, "@__NBVIEWER_ROOT_URL__" => get(config, "nbviewer_root_url", "<unknown>"))
push!(
repls,
"@__NBVIEWER_ROOT_URL__" => get(config, "nbviewer_root_url", "<unknown>"),
)
push!(repls, "@__BINDER_ROOT_URL__" => get(config, "binder_root_url", "<unknown>"))
end
@ -227,8 +235,11 @@ end @@ -227,8 +235,11 @@ end
filename(str) = first(splitext(last(splitdir(str))))
isdocumenter(cfg) = cfg["flavor"]::AbstractFlavor isa DocumenterFlavor
_DEFAULT_IMAGE_FORMATS = [(MIME("image/svg+xml"), ".svg"), (MIME("image/png"), ".png"),
(MIME("image/jpeg"), ".jpeg")]
_DEFAULT_IMAGE_FORMATS = [
(MIME("image/svg+xml"), ".svg"),
(MIME("image/png"), ".png"),
(MIME("image/jpeg"), ".jpeg"),
]
# Cache of inputfile => head branch
const HEAD_BRANCH_CACHE = Dict{String,String}()
@ -247,7 +258,7 @@ function edit_commit(inputfile, user_config) @@ -247,7 +258,7 @@ function edit_commit(inputfile, user_config)
pipeline(
setenv(`$(git) rev-parse --show-toplevel`; dir = dirname(inputfile));
stderr = devnull,
)
),
)
catch
end
@ -283,13 +294,17 @@ function edit_commit(inputfile, user_config) @@ -283,13 +294,17 @@ function edit_commit(inputfile, user_config)
end
# Default to DefaultFlavor() setting
pick_codefence(flavor::AbstractFlavor,execute::Bool,name::AbstractString)=pick_codefence(DefaultFlavor(),execute,name)
pick_codefence(flavor::DefaultFlavor,execute::Bool,name::AbstractString)=("````julia" => "````")
pick_codefence(flavor::DocumenterFlavor,execute::Bool,name::AbstractString)=(execute ?
pick_codefence(DefaultFlavor(),execute,name) : ("````@example $(name)" => "````")
pick_codefence(flavor::AbstractFlavor, execute::Bool, name::AbstractString) =
pick_codefence(DefaultFlavor(), execute, name)
pick_codefence(flavor::DefaultFlavor, execute::Bool, name::AbstractString) =
("````julia" => "````")
pick_codefence(flavor::DocumenterFlavor, execute::Bool, name::AbstractString) = (
execute ? pick_codefence(DefaultFlavor(), execute, name) :
("````@example $(name)" => "````")
)
pick_codefence(flavor::QuartoFlavor,execute::Bool,name::AbstractString)=(execute ?
error("QuartoFlavor does not support argument execute=true!") : ("```{julia}" => "```")
pick_codefence(flavor::QuartoFlavor, execute::Bool, name::AbstractString) = (
execute ? error("QuartoFlavor does not support argument execute=true!") :
("```{julia}" => "```")
)
function create_configuration(inputfile; user_config, user_kwargs, type = nothing)
@ -301,16 +316,25 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -301,16 +316,25 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
# deprecation of documenter kwarg
if (d = get(user_config, "documenter", nothing); d !== nothing)
if type === :md
Base.depwarn("The documenter=$(d) keyword to Literate.markdown is deprecated." *
Base.depwarn(
"The documenter=$(d) keyword to Literate.markdown is deprecated." *
" Pass `flavor = Literate.$(d ? "DocumenterFlavor" : "CommonMarkFlavor")()`" *
" instead.", Symbol("Literate.markdown"))
" instead.",
Symbol("Literate.markdown"),
)
user_config["flavor"] = d ? DocumenterFlavor() : CommonMarkFlavor()
elseif type === :nb
Base.depwarn("The documenter=$(d) keyword to Literate.notebook is deprecated." *
" It is not used anymore for notebook output.", Symbol("Literate.notebook"))
Base.depwarn(
"The documenter=$(d) keyword to Literate.notebook is deprecated." *
" It is not used anymore for notebook output.",
Symbol("Literate.notebook"),
)
elseif type === :jl
Base.depwarn("The documenter=$(d) keyword to Literate.script is deprecated." *
" It is not used anymore for script output.", Symbol("Literate.script"))
Base.depwarn(
"The documenter=$(d) keyword to Literate.script is deprecated." *
" It is not used anymore for script output.",
Symbol("Literate.script"),
)
end
end
@ -327,7 +351,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -327,7 +351,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
cfg["codefence"] = pick_codefence(
get(user_config, "flavor", cfg["flavor"]),
get(user_config, "execute", cfg["execute"]),
get(user_config, "name", replace(cfg["name"], r"\s" => "_"))
get(user_config, "name", replace(cfg["name"], r"\s" => "_")),
)
cfg["image_formats"] = _DEFAULT_IMAGE_FORMATS
cfg["edit_commit"] = edit_commit(inputfile, user_config)
@ -367,7 +391,8 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) @@ -367,7 +391,8 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
else
get(user_config, "devurl", "dev")
end
elseif (m = match(r"refs\/pull\/(\d+)\/merge", get(ENV, "GITHUB_REF", ""))) !== nothing
elseif (m = match(r"refs\/pull\/(\d+)\/merge", get(ENV, "GITHUB_REF", ""))) !==
nothing
"previews/PR$(m.captures[1])"
else
"dev"
@ -447,8 +472,12 @@ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation @@ -447,8 +472,12 @@ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation
function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
# Create configuration by merging default and userdefined
config = create_configuration(inputfile; user_config=user_config,
user_kwargs=user_kwargs, type=type)
config = create_configuration(
inputfile;
user_config = user_config,
user_kwargs = user_kwargs,
type = type,
)
# normalize paths
inputfile = normpath(inputfile)
@ -457,15 +486,21 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) @@ -457,15 +486,21 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
isdir(outputdir) || error("not a directory: $(outputdir)")
ext = type === (:nb) ? ".ipynb" : (type === (:md) && config["flavor"] isa QuartoFlavor) ? ".qmd" : ".$(type)"
ext =
type === (:nb) ? ".ipynb" :
(type === (:md) && config["flavor"] isa QuartoFlavor) ? ".qmd" : ".$(type)"
outputfile = joinpath(outputdir, config["name"]::String * ext)
if inputfile == outputfile
throw(ArgumentError("outputfile (`$outputfile`) is identical to inputfile (`$inputfile`)"))
throw(
ArgumentError(
"outputfile (`$outputfile`) is identical to inputfile (`$inputfile`)",
),
)
end
output_thing = type === (:md) ? "markdown page" :
type === (:nb) ? "notebook" :
type === (:jl) ? "plain script file" : error("nope")
output_thing =
type === (:md) ? "markdown page" :
type === (:nb) ? "notebook" : type === (:jl) ? "plain script file" : error("nope")
@info "generating $(output_thing) from `$(Base.contractuser(inputfile))`"
# Add some information for passing around Literate methods
@ -521,8 +556,13 @@ of possible configuration with `config` and other keyword arguments. @@ -521,8 +556,13 @@ of possible configuration with `config` and other keyword arguments.
"""
function script(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), kwargs...)
# preprocessing and parsing
chunks, config =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:jl)
chunks, config = preprocessor(
inputfile,
outputdir;
user_config = config,
user_kwargs = kwargs,
type = :jl,
)
# create the script file
ioscript = IOBuffer()
@ -560,8 +600,13 @@ of possible configuration with `config` and other keyword arguments. @@ -560,8 +600,13 @@ of possible configuration with `config` and other keyword arguments.
"""
function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), kwargs...)
# preprocessing and parsing
chunks, config =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md)
chunks, config = preprocessor(
inputfile,
outputdir;
user_config = config,
user_kwargs = kwargs,
type = :md,
)
# create the markdown file
sb = sandbox()
@ -578,7 +623,9 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg @@ -578,7 +623,9 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
write(iocode, codefence.first)
# make sure the code block is finalized if we are printing to ```@example
# (or ````@example, any number of backticks >= 3 works)
if chunk.continued && occursin(r"^`{3,}@example", codefence.first) && isdocumenter(config)
if chunk.continued &&
occursin(r"^`{3,}@example", codefence.first) &&
isdocumenter(config)
write(iocode, "; continued = true")
end
write(iocode, '\n')
@ -596,11 +643,16 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg @@ -596,11 +643,16 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
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;
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"])
image_formats = config["image_formats"],
)
end
end
end
@ -615,9 +667,16 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg @@ -615,9 +667,16 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
return outputfile
end
function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
inputfile::String, fake_source::String,
flavor::AbstractFlavor, image_formats::Vector)
function execute_markdown!(
io::IO,
sb::Module,
block::String,
outputdir;
inputfile::String,
fake_source::String,
flavor::AbstractFlavor,
image_formats::Vector,
)
# TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block; inputfile = inputfile, fake_source = fake_source)
# issue #101: consecutive codefenced blocks need newline
@ -626,7 +685,8 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir; @@ -626,7 +685,8 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
if r !== nothing && !REPL.ends_with_semicolon(block)
if (flavor isa FranklinFlavor || flavor isa DocumenterFlavor) &&
Base.invokelatest(showable, MIME("text/html"), r)
htmlfence = flavor isa FranklinFlavor ? ("~~~" => "~~~") : ("```@raw html" => "```")
htmlfence =
flavor isa FranklinFlavor ? ("~~~" => "~~~") : ("```@raw html" => "```")
write(io, "\n", htmlfence.first, "\n")
Base.invokelatest(show, io, MIME("text/html"), r)
write(io, "\n", htmlfence.second, "\n")
@ -685,8 +745,13 @@ of possible configuration with `config` and other keyword arguments. @@ -685,8 +745,13 @@ of possible configuration with `config` and other keyword arguments.
"""
function notebook(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), kwargs...)
# preprocessing and parsing
chunks, config =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb)
chunks, config = preprocessor(
inputfile,
outputdir;
user_config = config,
user_kwargs = kwargs,
type = :nb,
)
# create the notebook
nb = jupyter_notebook(chunks, config)
@ -708,13 +773,15 @@ function jupyter_notebook(chunks, config) @@ -708,13 +773,15 @@ function jupyter_notebook(chunks, config)
chunktype = isa(chunk, MDChunk) ? "markdown" : "code"
if !isempty(chunk.lines) && line_is_nbmeta(chunk.lines[1])
metatype, metadata = parse_nbmeta(chunk.lines[1])
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)
else
metadata = Dict{String,Any}()
end
lines = isa(chunk, MDChunk) ?
String[x.second for x in chunk.lines] : # skip indent
lines =
isa(chunk, MDChunk) ? String[x.second for x in chunk.lines] : # skip indent
chunk.lines
@views map!(x -> x * '\n', lines[1:end-1], lines[1:end-1])
cell["cell_type"] = chunktype
@ -753,8 +820,11 @@ function jupyter_notebook(chunks, config) @@ -753,8 +820,11 @@ function jupyter_notebook(chunks, config)
@info "executing notebook `$(config["name"] * ".ipynb")`"
try
cd(config["literate_outputdir"]) do
nb = execute_notebook(nb; inputfile=config["literate_inputfile"],
fake_source=config["literate_outputfile"])
nb = execute_notebook(
nb;
inputfile = config["literate_inputfile"],
fake_source = config["literate_outputfile"],
)
end
catch err
@error "error when executing notebook based on input file: " *
@ -773,7 +843,8 @@ function execute_notebook(nb; inputfile::String, fake_source::String) @@ -773,7 +843,8 @@ function execute_notebook(nb; inputfile::String, fake_source::String)
execution_count += 1
cell["execution_count"] = execution_count
block = join(cell["source"])
r, str, display_dicts = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source)
r, str, display_dicts =
execute_block(sb, block; inputfile = inputfile, fake_source = fake_source)
# str should go into stream
if !isempty(str)
@ -875,14 +946,16 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source @@ -875,14 +946,16 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source
end
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here
if c.error
error("""
error(
"""
$(sprint(showerror, c.value))
when executing the following code block from inputfile `$(Base.contractuser(inputfile))`
```julia
$block
```
""")
""",
)
end
return c.value, c.output, disp.data
end

569
test/runtests.jl

@ -293,7 +293,8 @@ const GITLAB_ENV = Dict( @@ -293,7 +293,8 @@ const GITLAB_ENV = Dict(
(k => nothing for k in keys(TRAVIS_ENV))...,
(k => nothing for k in keys(ACTIONS_ENV))...,
)
@testset "Literate.script" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
@testset "Literate.script" begin
Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
# write content to inputfile
@ -353,69 +354,123 @@ const GITLAB_ENV = Dict( @@ -353,69 +354,123 @@ const GITLAB_ENV = Dict(
@test script == expected_script
# Travis with with PR preview build
withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "",
"TRAVIS_PULL_REQUEST" => "42") do
withenv(TRAVIS_ENV..., "TRAVIS_TAG" => "", "TRAVIS_PULL_REQUEST" => "42") do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", script)
@test occursin(
"# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
@test occursin(
"# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl",
script,
)
@test occursin(
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl",
script,
)
# Travis with no tag -> dev directory
withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "") do
withenv(TRAVIS_ENV..., "TRAVIS_TAG" => "") do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", script)
@test occursin(
"# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
@test occursin(
"# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl",
script,
)
@test occursin(
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl",
script,
)
# GitHub Actions with a tag
withenv(ACTIONS_ENV...) do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl", script)
@test occursin(
"# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
@test occursin(
"# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl",
script,
)
@test occursin(
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl",
script,
)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
withenv(
ACTIONS_ENV...,
"GITHUB_EVENT_NAME" => "pull_request",
"GITHUB_REF" => "refs/pull/42/merge") do
"GITHUB_REF" => "refs/pull/42/merge",
) do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", script)
@test occursin(
"# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
@test occursin(
"# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl",
script,
)
@test occursin(
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl",
script,
)
# GitHub Actions without a tag -> dev directory
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/heads/master") do
withenv(ACTIONS_ENV..., "GITHUB_REF" => "refs/heads/master") do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin("# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", script)
@test occursin("# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", script)
@test occursin(
"# Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
@test occursin(
"# Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl",
script,
)
@test occursin(
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl",
script,
)
# building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
withenv(
"DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0",
) do
Literate.script(inputfile, outdir)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", script)
@test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", script)
@test occursin(
"jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl",
script,
)
@test_broken occursin(
"https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
script,
)
# pre- and post-processing
Literate.script(inputfile, outdir,
Literate.script(
inputfile,
outdir,
preprocess = x -> replace(x, "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = x -> replace(x, "PLACEHOLDER4" => "4REDLOHECALP"))
postprocess = x -> replace(x, "PLACEHOLDER4" => "4REDLOHECALP"),
)
script = read(joinpath(outdir, "inputfile.jl"), String)
@test !occursin("PLACEHOLDER1", script)
@test !occursin("PLACEHOLDER2", script)
@ -442,7 +497,9 @@ const GITLAB_ENV = Dict( @@ -442,7 +497,9 @@ const GITLAB_ENV = Dict(
# mdstrings
mdstrings_file = "inputfile_mdstrings.jl"
write(mdstrings_file, """
write(
mdstrings_file,
"""
md\"\"\"
# Markdown header
@ -457,9 +514,14 @@ const GITLAB_ENV = Dict( @@ -457,9 +514,14 @@ const GITLAB_ENV = Dict(
comment
===#
2 + 2
""")
Literate.script(mdstrings_file, outdir,
keep_comments = true, credit=false)
""",
)
Literate.script(
mdstrings_file,
outdir,
keep_comments = true,
credit = false,
)
script = read(joinpath(outdir, mdstrings_file), String)
@test strip(script) == """
md\"\"\"
@ -476,8 +538,13 @@ const GITLAB_ENV = Dict( @@ -476,8 +538,13 @@ const GITLAB_ENV = Dict(
# comment
2 + 2"""
Literate.script(mdstrings_file, outdir,
keep_comments = true, mdstrings = true, credit=false)
Literate.script(
mdstrings_file,
outdir,
keep_comments = true,
mdstrings = true,
credit = false,
)
script = read(joinpath(outdir, mdstrings_file), String)
@test strip(script) == """
# # Markdown header
@ -501,9 +568,11 @@ const GITLAB_ENV = Dict( @@ -501,9 +568,11 @@ const GITLAB_ENV = Dict(
@test_throws ArgumentError Literate.script(inputfile)
end
end
end end
end
end
@testset "Literate.markdown" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
@testset "Literate.markdown" begin
Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
# write content to inputfile
@ -639,84 +708,171 @@ end end @@ -639,84 +708,171 @@ end end
@test markdown == expected_markdown
# Travis with PR preview build
withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "",
"TRAVIS_PULL_REQUEST" => "42") do
withenv(TRAVIS_ENV..., "TRAVIS_TAG" => "", "TRAVIS_PULL_REQUEST" => "42") do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl",
markdown,
)
@test occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# Travis with no tag -> dev directory
withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "") do
withenv(TRAVIS_ENV..., "TRAVIS_TAG" => "") do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl",
markdown,
)
@test occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# GitHub Actions with a tag
withenv(ACTIONS_ENV...) do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/file.jl",
markdown,
)
@test occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
withenv(
ACTIONS_ENV...,
"GITHUB_REF" => "refs/pull/42/merge",
"GITHUB_EVENT_NAME" => "pull_request") do
"GITHUB_EVENT_NAME" => "pull_request",
) do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/file.jl",
markdown,
)
@test occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=previews/PR42/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# GitHub Actions without a tag -> dev directory
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/heads/master") do
withenv(ACTIONS_ENV..., "GITHUB_REF" => "refs/heads/master") do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl", markdown)
@test occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/github/fredrikekre/Literate.jl/blob/gh-pages/dev/file.jl",
markdown,
)
@test occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://github.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# GitLab CI with GitLab Pages
withenv(GITLAB_ENV...) do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("Link to repo root: https://gitlab.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin("Link to nbviewer: https://nbviewer.jupyter.org/urls/fredrikekre.gitlab.io/Literate.jl/file.jl", markdown)
@test_broken occursin("Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl", markdown)
@test occursin("EditURL = \"https://gitlab.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"", markdown)
@test occursin(
"Link to repo root: https://gitlab.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
@test occursin(
"Link to nbviewer: https://nbviewer.jupyter.org/urls/fredrikekre.gitlab.io/Literate.jl/file.jl",
markdown,
)
@test_broken occursin(
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=dev/file.jl",
markdown,
)
@test occursin(
"EditURL = \"https://gitlab.com/fredrikekre/Literate.jl/blob/master/test/$(basename(sandbox))/inputfile.jl\"",
markdown,
)
# building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
withenv(
"DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0",
) do
Literate.markdown(inputfile, outdir)
end
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", markdown)
@test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", markdown)
@test occursin(
"jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl",
markdown,
)
@test_broken occursin(
"https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
markdown,
)
# pre- and post-processing
Literate.markdown(inputfile, outdir,
preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = x -> replace(replace(x, "PLACEHOLDER2" => "2REDLOHECALP"), "PLACEHOLDER4" => "4REDLOHECALP"))
Literate.markdown(
inputfile,
outdir,
preprocess = x -> replace(
replace(x, "PLACEHOLDER1" => "1REDLOHECALP"),
"PLACEHOLDER3" => "3REDLOHECALP",
),
postprocess = x -> replace(
replace(x, "PLACEHOLDER2" => "2REDLOHECALP"),
"PLACEHOLDER4" => "4REDLOHECALP",
),
)
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test !occursin("PLACEHOLDER1", markdown)
@test !occursin("PLACEHOLDER2", markdown)
@ -738,10 +894,21 @@ end end @@ -738,10 +894,21 @@ end end
# flavor = QuartoFlavor()
# execution of Quarto markdown is not allowed
let expected_error = ErrorException("QuartoFlavor does not support argument execute=true!")
@test_throws expected_error Literate.markdown("quarto.jl",flavor=Literate.QuartoFlavor(),execute=true)
let expected_error = ErrorException(
"QuartoFlavor does not support argument execute=true!",
)
@test_throws expected_error Literate.markdown(
"quarto.jl",
flavor = Literate.QuartoFlavor(),
execute = true,
)
end
Literate.markdown(inputfile, outdir, flavor = Literate.QuartoFlavor(),execute=false)
Literate.markdown(
inputfile,
outdir,
flavor = Literate.QuartoFlavor(),
execute = false,
)
markdown = read(joinpath(outdir, "inputfile.qmd"), String)
@test occursin("```{julia}", markdown)
@test !occursin(r"`{3,}@example", markdown)
@ -790,7 +957,9 @@ end end @@ -790,7 +957,9 @@ end end
@test !occursin("blob/master/", markdown)
# execute
write(inputfile, """
write(
inputfile,
"""
using DisplayAs
#-
1+1
@ -845,7 +1014,8 @@ end end @@ -845,7 +1014,8 @@ end end
(@__DIR__) == pwd() ? "cwd correct" : "cwd incorrect"
#-
basename(@__FILE__)
""")
""",
)
Literate.markdown(inputfile, outdir; execute = true)
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("```\n2\n```", markdown) # text/plain
@ -871,14 +1041,25 @@ end end @@ -871,14 +1041,25 @@ end end
@test occursin("```\n\"inputfile.md\"\n```", markdown) # Correct source file (@__FILE__)
# FranklinFlavor
Literate.markdown(inputfile, outdir; execute=true, flavor=Literate.FranklinFlavor())
Literate.markdown(
inputfile,
outdir;
execute = true,
flavor = Literate.FranklinFlavor(),
)
markdown = read(joinpath(outdir, "inputfile.md"), String)
@test occursin("# MD", markdown) # text/markdown
@test occursin("~~~\n<h1>MD</h1>\n~~~", markdown) # text/html
# QuartoFlavor file extension
write(inputfile, "#=\r\nhello world\n=#\r\n")
_, config = Literate.preprocessor(inputfile, outdir; user_kwargs=(), user_config=Dict("flavor"=>Literate.QuartoFlavor()), type=:md)
_, config = Literate.preprocessor(
inputfile,
outdir;
user_kwargs = (),
user_config = Dict("flavor" => Literate.QuartoFlavor()),
type = :md,
)
@test config["literate_ext"] == ".qmd"
# verify that inputfile exists
@ -891,23 +1072,39 @@ end end @@ -891,23 +1072,39 @@ end end
# fredrikekre/Literate.jl#165: \r\n line endings with multiline comments/mdstrings
write(inputfile, "#=\r\nhello world\r\nhej världen\r\n=#\r\n")
chunks, _ = Literate.preprocessor(inputfile, outdir; user_kwargs=(), user_config=(), type=:md)
chunks, _ = Literate.preprocessor(
inputfile,
outdir;
user_kwargs = (),
user_config = (),
type = :md,
)
@test chunks[2].lines == ["" => "hello world", "" => "hej världen"]
write(inputfile, "md\"\"\"\r\nhello world\r\nhej världen\r\n\"\"\"\r\n")
chunks, _ = Literate.preprocessor(inputfile, outdir; user_kwargs=pairs((; mdstrings=true)),
user_config=(), type=:md)
chunks, _ = Literate.preprocessor(
inputfile,
outdir;
user_kwargs = pairs((; mdstrings = true)),
user_config = (),
type = :md,
)
@test chunks[2].lines == ["" => "hello world", "" => "hej världen"]
# fredrikekre/Literate.jl#168
f = "file with space.jl"
write(f, "1 + 1")
Literate.markdown(f, outdir)
@test occursin("file_with_space", read(joinpath(outdir, "file with space.md"), String))
@test occursin(
"file_with_space",
read(joinpath(outdir, "file with space.md"), String),
)
end
end
end
end
end end
@testset "Literate.notebook" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
@testset "Literate.notebook" begin
Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
# write content to inputfile
@ -919,59 +1116,52 @@ end end @@ -919,59 +1116,52 @@ end end
withenv(TRAVIS_ENV...) do
Literate.notebook(inputfile, outdir, execute = false)
end
expected_cells = rstrip.((
expected_cells =
rstrip.((
"""
"cells": [
""",
"""
"source": [
"# Example\\n",
"foo, bar"
]
""",
"""
"source": [
"x = 1"
]
""",
"""
"source": [
"Not markdown\\n",
"Not markdown"
],
""",
"""
"source": [
"x * 1\\n",
"x * 1"
],
""",
"""
"source": [
"Only notebook\\n",
"Only notebook"
]
""",
"""
"source": [
"x + 2\\n",
"x + 2"
]
""",
"""
"source": [
"Not script\\n",
"Not script"
],
""",
"""
"source": [
"x * 3\\n",
@ -981,7 +1171,6 @@ end end @@ -981,7 +1171,6 @@ end end
"#| echo: false Quarto parameters"
],
""",
"""
"source": [
"for i in 1:10\\n",
@ -990,7 +1179,6 @@ end end @@ -990,7 +1179,6 @@ end end
"end"
]
""",
"""
"source": [
"name: inputfile\\n",
@ -999,7 +1187,6 @@ end end @@ -999,7 +1187,6 @@ end end
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl"
]
""",
"""
"source": [
"# name: inputfile\\n",
@ -1008,21 +1195,18 @@ end end @@ -1008,21 +1195,18 @@ end end
"# Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl"
]
""",
"""
"source": [
"PLACEHOLDER1\\n",
"PLACEHOLDER2"
]
""",
"""
"source": [
"# PLACEHOLDER3\\n",
"# PLACEHOLDER4"
]
""",
"""
"source": [
"Some inline math: \$\\\\frac{df}{dx}\$, some multiline inline math: \$y =\\n",
@ -1032,13 +1216,11 @@ end end @@ -1032,13 +1216,11 @@ end end
"\$\$"
]
""",
"""
"source": [
"Indented markdown"
]
""",
"""
"source": [
"for i in 1:10\\n",
@ -1047,27 +1229,25 @@ end end @@ -1047,27 +1229,25 @@ end end
"end"
]
""",
"""
"metadata": {
"meta": "data"
}
""",
"""
"source": [
"First multiline\\n",
"comment"
]
""",
"""
"source": [
"---\\n",
"\\n",
"*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*"
]
"""))
""",
))
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@ -1078,16 +1258,25 @@ end end @@ -1078,16 +1258,25 @@ end end
lastidx = nextind(notebook, last(idx))
end
# test some of the required metadata
for metadata in (" \"nbformat\": ", " \"nbformat_minor\": ", " \"metadata\": {", " \"language_info\": {",
" \"file_extension\": \".jl\"", " \"mimetype\": \"application/julia\"",
" \"name\": \"julia\"", " \"version\": ", " \"kernelspec\": {",
" \"name\": \"julia-", " \"display_name\": \"Julia ", " \"language\": \"julia\"")
for metadata in (
" \"nbformat\": ",
" \"nbformat_minor\": ",
" \"metadata\": {",
" \"language_info\": {",
" \"file_extension\": \".jl\"",
" \"mimetype\": \"application/julia\"",
" \"name\": \"julia\"",
" \"version\": ",
" \"kernelspec\": {",
" \"name\": \"julia-",
" \"display_name\": \"Julia ",
" \"language\": \"julia\"",
)
@test occursin(metadata, notebook)
end
# no tag -> latest directory
withenv(TRAVIS_ENV...,
"TRAVIS_TAG" => "") do
withenv(TRAVIS_ENV..., "TRAVIS_TAG" => "") do
Literate.notebook(inputfile, outdir, execute = false)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@ -1101,44 +1290,65 @@ end end @@ -1101,44 +1290,65 @@ end end
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/v1.2.0/", notebook)
# GitHub Actions with PR preview build
withenv(ACTIONS_ENV...,
withenv(
ACTIONS_ENV...,
"GITHUB_REF" => "refs/pull/42/merge",
"GITHUB_EVENT_NAME" => "pull_request") do
"GITHUB_EVENT_NAME" => "pull_request",
) do
Literate.notebook(inputfile, outdir, execute = false)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/", notebook)
@test occursin(
"fredrikekre/Literate.jl/blob/gh-pages/previews/PR42/",
notebook,
)
# GitHub Actions without a tag
withenv(ACTIONS_ENV...,
"GITHUB_REF" => "refs/heads/master") do
withenv(ACTIONS_ENV..., "GITHUB_REF" => "refs/heads/master") do
Literate.notebook(inputfile, outdir, execute = false)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("fredrikekre/Literate.jl/blob/gh-pages/dev/", notebook)
# building under DocumentationGenerator.jl
withenv("DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0") do
withenv(
"DOCUMENTATIONGENERATOR" => "true",
"DOCUMENTATIONGENERATOR_BASE_URL" => "pkg.julialang.org/docs/Literate/XPnWG/1.2.0",
) do
Literate.notebook(inputfile, outdir, execute = false)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl", notebook)
@test_broken occursin("https://github.com/fredrikekre/Literate.jl/blob/master/file.jl", notebook)
@test occursin(
"jupyter.org/urls/pkg.julialang.org/docs/Literate/XPnWG/1.2.0/file.jl",
notebook,
)
@test_broken occursin(
"https://github.com/fredrikekre/Literate.jl/blob/master/file.jl",
notebook,
)
# pre- and post-processing
function post(nb)
for cell in nb["cells"]
for i in eachindex(cell["source"])
cell["source"][i] = replace(cell["source"][i], "PLACEHOLDER2" => "2REDLOHECALP")
cell["source"][i] = replace(cell["source"][i], "PLACEHOLDER4" => "4REDLOHECALP")
cell["source"][i] =
replace(cell["source"][i], "PLACEHOLDER2" => "2REDLOHECALP")
cell["source"][i] =
replace(cell["source"][i], "PLACEHOLDER4" => "4REDLOHECALP")
end
end
return nb
end
Literate.notebook(inputfile, outdir, execute = false,
preprocess = x -> replace(replace(x, "PLACEHOLDER1" => "1REDLOHECALP"), "PLACEHOLDER3" => "3REDLOHECALP"),
postprocess = post)
Literate.notebook(
inputfile,
outdir,
execute = false,
preprocess = x -> replace(
replace(x, "PLACEHOLDER1" => "1REDLOHECALP"),
"PLACEHOLDER3" => "3REDLOHECALP",
),
postprocess = post,
)
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test !occursin("PLACEHOLDER1", notebook)
@test !occursin("PLACEHOLDER2", notebook)
@ -1151,7 +1361,12 @@ end end @@ -1151,7 +1361,12 @@ end end
# documenter = false (deprecated)
@test_deprecated r"The documenter=false keyword to Literate.notebook is deprecated." begin
Literate.notebook(inputfile, outdir, documenter = false, execute = false)
Literate.notebook(
inputfile,
outdir,
documenter = false,
execute = false,
)
end
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test !occursin("# [Example](@id example-id", notebook)
@ -1166,22 +1381,22 @@ end end @@ -1166,22 +1381,22 @@ end end
# execute = true
Literate.notebook(inputfile, outdir)
expected_outputs = rstrip.((
expected_outputs =
rstrip.((
"""
"cells": [
""",
"""
"data": {
"text/plain": "3"
},
""",
"""
"text": [
"12345678910"
]
"""))
""",
))
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@ -1197,18 +1412,27 @@ end end @@ -1197,18 +1412,27 @@ end end
write(joinpath(outdir, "issue31.jl"), "10 + 21")
Literate.notebook(inputfile, outdir)
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("\"data\": {\n \"text/plain\": \"31\"\n }", notebook)
@test occursin(
"\"data\": {\n \"text/plain\": \"31\"\n }",
notebook,
)
# test error when executing notebook
write(inputfile, "for i in 1:10\n println(i)")
r = @test_logs((:error, r"error when executing notebook based on input file: "), match_mode=:any,
r = @test_logs(
(:error, r"error when executing notebook based on input file: "),
match_mode = :any,
try
Literate.notebook(inputfile, outdir)
catch err
err
end)
end
)
@test isa(r, ErrorException)
@test occursin("when executing the following code block from inputfile ", r.msg)
@test occursin(
"when executing the following code block from inputfile ",
r.msg,
)
@test occursin(inputfile, r.msg)
# verify that inputfile exists
@ -1220,7 +1444,9 @@ end end @@ -1220,7 +1444,9 @@ end end
@test isfile("inputfile.ipynb")
# world time problem with `IJulia.display_dict`
write(inputfile, """
write(
inputfile,
"""
struct VegaLiteRenderable end
Base.show(io::IO, ::MIME"application/vnd.vegalite.v2+json", ::VegaLiteRenderable) =
write(io, \"\"\"
@ -1228,7 +1454,8 @@ end end @@ -1228,7 +1454,8 @@ end end
\"\"\")
Base.Multimedia.istextmime(::MIME{Symbol("application/vnd.vegalite.v2+json")}) = true
VegaLiteRenderable()
""")
""",
)
Literate.notebook(inputfile, outdir)
notebook = read(joinpath(outdir, "inputfile.ipynb"), String)
@test occursin("\"application/vnd.vegalite.v2+json\":", notebook)
@ -1269,7 +1496,7 @@ end end @@ -1269,7 +1496,7 @@ end end
push!(xs, x)
end
"""
for i in 1:9
for i = 1:9
script *= "\n#-\nxs[$i]"
end
write(inputfile, script)
@ -1295,9 +1522,10 @@ end end @@ -1295,9 +1522,10 @@ end end
# Cell 2 has 3 outputs: 2 display and one execute result
cellout = cells[2]["outputs"]
@test length(cellout) == 3
for i in 1:3
for i = 1:3
exe_res = i == 3
@test cellout[i]["output_type"] == (exe_res ? "execute_result" : "display_data")
@test cellout[i]["output_type"] ==
(exe_res ? "execute_result" : "display_data")
@test keys(cellout[i]["data"]) == Set(("text/plain", "text/html"))
@test cellout[i]["data"]["text/plain"] == "DF($i) as text/plain"
@test cellout[i]["data"]["text/html"] == Any["DF($i) as text/html"]
@ -1312,9 +1540,11 @@ end end @@ -1312,9 +1540,11 @@ end end
@test !haskey(cellout[1], "execution_count")
end
end
end end
end
end
@testset "Configuration" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
@testset "Configuration" begin
Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
# write content to inputfile
@ -1329,10 +1559,12 @@ end end @@ -1329,10 +1559,12 @@ end end
)
# Overwriting of URLs
withenv("TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl",
withenv(
"TRAVIS_REPO_SLUG" => "fredrikekre/Literate.jl",
"TRAVIS_TAG" => "",
"TRAVIS_PULL_REQUEST" => "false",
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true") do
"HAS_JOSH_K_SEAL_OF_APPROVAL" => "true",
) do
Literate.script(inputfile, outdir; config = config)
end
script = read(joinpath(outdir, "inputfile.jl"), String)
@ -1341,19 +1573,35 @@ end end @@ -1341,19 +1573,35 @@ end end
@test occursin("Link to binder: www.example3.com/file.jl", script)
# Test pick_codefence function
default_codefence=pick_codefence(Literate.DefaultFlavor(),true,"testname")
default_codefence =
pick_codefence(Literate.DefaultFlavor(), true, "testname")
@test default_codefence == ("````julia" => "````")
@test default_codefence == pick_codefence(Literate.FranklinFlavor(),true,"testname")
@test default_codefence == pick_codefence(Literate.DocumenterFlavor(),true,"testname")
@test default_codefence ==
pick_codefence(Literate.FranklinFlavor(), true, "testname")
@test default_codefence ==
pick_codefence(Literate.DocumenterFlavor(), true, "testname")
documenter_codefence = ("````@example testname" => "````")
@test documenter_codefence == pick_codefence(Literate.DocumenterFlavor(),false,"testname")
let expected_exception=ErrorException("QuartoFlavor does not support argument execute=true!")
@test_throws expected_exception pick_codefence(Literate.QuartoFlavor(),true,"testname")
@test documenter_codefence ==
pick_codefence(Literate.DocumenterFlavor(), false, "testname")
let expected_exception = ErrorException(
"QuartoFlavor does not support argument execute=true!",
)
@test_throws expected_exception pick_codefence(
Literate.QuartoFlavor(),
true,
"testname",
)
end
@test ("```{julia}" => "```") == pick_codefence(Literate.QuartoFlavor(),false,"testname")
@test ("```{julia}" => "```") ==
pick_codefence(Literate.QuartoFlavor(), false, "testname")
# Misc default configs
create(; type, kw...) = Literate.create_configuration(inputfile; user_config=Dict(), user_kwargs=kw, type=type)
create(; type, kw...) = Literate.create_configuration(
inputfile;
user_config = Dict(),
user_kwargs = kw,
type = type,
)
cfg = create(; type = :md, execute = true)
@test cfg["execute"]
@test cfg["codefence"] == ("````julia" => "````")
@ -1362,4 +1610,5 @@ end end @@ -1362,4 +1610,5 @@ end end
@test cfg["codefence"] == ("````@example inputfile" => "````")
end
end
end end
end
end

Loading…
Cancel
Save