Browse Source

Revert "Formatted"

This reverts commit fa050c7863.
pull/200/head
J S 3 years ago
parent
commit
76183c0476
  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,11 +5,7 @@ if haskey(ENV, "GITHUB_ACTIONS") @@ -5,11 +5,7 @@ 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
@ -27,12 +23,7 @@ Literate.notebook(EXAMPLE, OUTPUT, preprocess = preprocess) @@ -27,12 +23,7 @@ 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)
@ -50,10 +41,7 @@ if haskey(ENV, "GITHUB_ACTIONS") @@ -50,10 +41,7 @@ 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
@ -73,8 +61,7 @@ makedocs( @@ -73,8 +61,7 @@ makedocs(
"customprocessing.md",
"documenter.md",
"tips.md",
"generated/example.md",
],
"generated/example.md"]
)
deploydocs(

3
src/IJulia.jl

@ -19,8 +19,7 @@ const application_vnd_vegalite_v2 = MIME("application/vnd.vegalite.v2+json") @@ -19,8 +19,7 @@ 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,9 +44,7 @@ mutable struct CodeChunk <: Chunk @@ -44,9 +44,7 @@ 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)))
@ -80,10 +78,7 @@ function parse(content; allow_continued = true) @@ -80,10 +78,7 @@ 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
@ -113,10 +108,7 @@ function parse(content; allow_continued = true) @@ -113,10 +108,7 @@ 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
@ -132,7 +124,11 @@ function parse(content; allow_continued = true) @@ -132,7 +124,11 @@ 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
@ -203,16 +199,12 @@ function replace_default(content, sym; config::Dict, branch = "gh-pages", commit @@ -203,16 +199,12 @@ function replace_default(content, sym; config::Dict, branch = "gh-pages", commit
# 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
@ -235,11 +227,8 @@ end @@ -235,11 +227,8 @@ 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}()
@ -258,7 +247,7 @@ function edit_commit(inputfile, user_config) @@ -258,7 +247,7 @@ function edit_commit(inputfile, user_config)
pipeline(
setenv(`$(git) rev-parse --show-toplevel`; dir=dirname(inputfile));
stderr=devnull,
),
)
)
catch
end
@ -294,17 +283,13 @@ function edit_commit(inputfile, user_config) @@ -294,17 +283,13 @@ 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)
@ -316,25 +301,16 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin @@ -316,25 +301,16 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin
# 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
@ -351,7 +327,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin @@ -351,7 +327,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin
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)
@ -391,8 +367,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin @@ -391,8 +367,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type = nothin
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"
@ -472,12 +447,8 @@ const DEFAULT_CONFIGURATION = nothing # Dummy const for documentation @@ -472,12 +447,8 @@ 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)
@ -486,21 +457,15 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) @@ -486,21 +457,15 @@ 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
@ -556,13 +521,8 @@ of possible configuration with `config` and other keyword arguments. @@ -556,13 +521,8 @@ 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()
@ -600,13 +560,8 @@ of possible configuration with `config` and other keyword arguments. @@ -600,13 +560,8 @@ 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()
@ -623,9 +578,7 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k @@ -623,9 +578,7 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k
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')
@ -643,16 +596,11 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k @@ -643,16 +596,11 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k
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
@ -667,16 +615,9 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k @@ -667,16 +615,9 @@ function markdown(inputfile, outputdir = pwd(); config::AbstractDict = Dict(), k
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
@ -685,8 +626,7 @@ function execute_markdown!( @@ -685,8 +626,7 @@ function execute_markdown!(
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")
@ -745,13 +685,8 @@ of possible configuration with `config` and other keyword arguments. @@ -745,13 +685,8 @@ 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)
@ -773,15 +708,13 @@ function jupyter_notebook(chunks, config) @@ -773,15 +708,13 @@ 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
@ -820,11 +753,8 @@ function jupyter_notebook(chunks, config) @@ -820,11 +753,8 @@ 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: " *
@ -843,8 +773,7 @@ function execute_notebook(nb; inputfile::String, fake_source::String) @@ -843,8 +773,7 @@ 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)
@ -946,16 +875,14 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source @@ -946,16 +875,14 @@ 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,8 +293,7 @@ const GITLAB_ENV = Dict( @@ -293,8 +293,7 @@ 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
@ -354,123 +353,69 @@ const GITLAB_ENV = Dict( @@ -354,123 +353,69 @@ 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)
@ -497,9 +442,7 @@ const GITLAB_ENV = Dict( @@ -497,9 +442,7 @@ const GITLAB_ENV = Dict(
# mdstrings
mdstrings_file = "inputfile_mdstrings.jl"
write(
mdstrings_file,
"""
write(mdstrings_file, """
md\"\"\"
# Markdown header
@ -514,14 +457,9 @@ Content of the multiline @@ -514,14 +457,9 @@ Content of the multiline
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\"\"\"
@ -538,13 +476,8 @@ comment @@ -538,13 +476,8 @@ comment
# 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
@ -568,11 +501,9 @@ comment @@ -568,11 +501,9 @@ comment
@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
@ -708,171 +639,84 @@ end @@ -708,171 +639,84 @@ 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)
@ -894,21 +738,10 @@ end @@ -894,21 +738,10 @@ 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)
@ -957,9 +790,7 @@ end @@ -957,9 +790,7 @@ end
@test !occursin("blob/master/", markdown)
# execute
write(
inputfile,
"""
write(inputfile, """
using DisplayAs
#-
1+1
@ -1014,8 +845,7 @@ end @@ -1014,8 +845,7 @@ 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
@ -1041,25 +871,14 @@ end @@ -1041,25 +871,14 @@ 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
@ -1072,39 +891,23 @@ end @@ -1072,39 +891,23 @@ 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),
)
end
end
@test occursin("file_with_space", read(joinpath(outdir, "file with space.md"), String))
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
@ -1116,52 +919,59 @@ end @@ -1116,52 +919,59 @@ 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",
@ -1171,6 +981,7 @@ end @@ -1171,6 +981,7 @@ end
"#| echo: false Quarto parameters"
],
""",
"""
"source": [
"for i in 1:10\\n",
@ -1179,6 +990,7 @@ end @@ -1179,6 +990,7 @@ end
"end"
]
""",
"""
"source": [
"name: inputfile\\n",
@ -1187,6 +999,7 @@ end @@ -1187,6 +999,7 @@ end
"Link to binder: https://mybinder.org/v2/gh/fredrikekre/Literate.jl/gh-pages?filepath=v1.2.0/file.jl"
]
""",
"""
"source": [
"# name: inputfile\\n",
@ -1195,18 +1008,21 @@ end @@ -1195,18 +1008,21 @@ 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",
@ -1216,11 +1032,13 @@ end @@ -1216,11 +1032,13 @@ end
"\$\$"
]
""",
"""
"source": [
"Indented markdown"
]
""",
"""
"source": [
"for i in 1:10\\n",
@ -1229,25 +1047,27 @@ end @@ -1229,25 +1047,27 @@ 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)
@ -1258,25 +1078,16 @@ end @@ -1258,25 +1078,16 @@ 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)
@ -1290,65 +1101,44 @@ end @@ -1290,65 +1101,44 @@ 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)
@ -1361,12 +1151,7 @@ end @@ -1361,12 +1151,7 @@ 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)
@ -1381,22 +1166,22 @@ end @@ -1381,22 +1166,22 @@ 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)
@ -1412,27 +1197,18 @@ end @@ -1412,27 +1197,18 @@ 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
@ -1444,9 +1220,7 @@ end @@ -1444,9 +1220,7 @@ 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, \"\"\"
@ -1454,8 +1228,7 @@ end @@ -1454,8 +1228,7 @@ 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)
@ -1496,7 +1269,7 @@ end @@ -1496,7 +1269,7 @@ end
push!(xs, x)
end
"""
for i = 1:9
for i in 1:9
script *= "\n#-\nxs[$i]"
end
write(inputfile, script)
@ -1522,10 +1295,9 @@ end @@ -1522,10 +1295,9 @@ end
# Cell 2 has 3 outputs: 2 display and one execute result
cellout = cells[2]["outputs"]
@test length(cellout) == 3
for i = 1:3
for i in 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"]
@ -1540,11 +1312,9 @@ end @@ -1540,11 +1312,9 @@ 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
@ -1559,12 +1329,10 @@ end @@ -1559,12 +1329,10 @@ 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)
@ -1573,35 +1341,19 @@ end @@ -1573,35 +1341,19 @@ 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" => "````")
@ -1610,5 +1362,4 @@ end @@ -1610,5 +1362,4 @@ end
@test cfg["codefence"] == ("````@example inputfile" => "````")
end
end
end
end
end end

Loading…
Cancel
Save