Browse Source

Factor out some common pre-processing code.

pull/122/head
Fredrik Ekre 5 years ago
parent
commit
85da8c56c5
  1. 121
      src/Literate.jl

121
src/Literate.jl

@ -298,20 +298,10 @@ See the manual section about [Configuration](@ref) for more information.
""" """
const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation
""" function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
Literate.script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
Generate a plain script file from `inputfile` and write the result to `outputdir`.
!!! compat "Literate 2.5"
Default output directory `pwd` requires at least Literate version 2.5.
See the manual section on [Configuration](@ref) for documentation
of possible configuration with `config` and other keyword arguments.
"""
function script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
# Create configuration by merging default and userdefined # Create configuration by merging default and userdefined
config = create_configuration(inputfile; user_config=config, user_kwargs=kwargs) config = create_configuration(inputfile; user_config=user_config,
user_kwargs=user_kwargs, type=type)
# normalize paths # normalize paths
inputfile = normpath(inputfile) inputfile = normpath(inputfile)
@ -320,18 +310,56 @@ function script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
mkpath(outputdir) mkpath(outputdir)
outputdir = realpath(abspath(outputdir)) outputdir = realpath(abspath(outputdir))
@info "generating plain script file from `$(Base.contractuser(inputfile))`" output_thing = type === (:md) ? "markdown page" :
type === (:nb) ? "notebook" :
type === (:jl) ? "plain script file" : error("nope")
@info "generating $(output_thing) from `$(Base.contractuser(inputfile))`"
# read content # read content
content = read(inputfile, String) content = read(inputfile, String)
# run custom pre-processing from user # run custom pre-processing from user
content = config["preprocess"](content) content = config["preprocess"](content)
# run some Documenter specific things for markdown output
if type === :md && config["documenter"]::Bool
# change the Edit on GitHub link
path = relpath(inputfile, get(config, "repo_root_path", pwd())::String)
path = replace(path, "\\" => "/")
content = """
# ```@meta
# EditURL = "@__REPO_ROOT_URL__/$(path)"
# ```
""" * content
end
# default replacements # default replacements
content = replace_default(content, :jl; config=config) content = replace_default(content, type; config=config)
# parse the content into chunks
chunks = parse(content; allow_continued = type !== :nb)
return inputfile, outputdir, config, chunks
end
"""
Literate.script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
Generate a plain script file from `inputfile` and write the result to `outputdir`.
!!! compat "Literate 2.5"
Default output directory `pwd` requires at least Literate version 2.5.
See the manual section on [Configuration](@ref) for documentation
of possible configuration with `config` and other keyword arguments.
"""
function script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
# preprocessing and parsing
inputfile, outputdir, config, chunks =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:jl)
# create the script file # create the script file
chunks = parse(content)
ioscript = IOBuffer() ioscript = IOBuffer()
for chunk in chunks for chunk in chunks
if isa(chunk, CodeChunk) if isa(chunk, CodeChunk)
@ -377,42 +405,12 @@ See the manual section on [Configuration](@ref) for documentation
of possible configuration with `config` and other keyword arguments. of possible configuration with `config` and other keyword arguments.
""" """
function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
# Create configuration by merging default and userdefined # preprocessing and parsing
config = create_configuration(inputfile; user_config=config, user_kwargs=kwargs, type=:md) inputfile, outputdir, config, chunks =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:md)
# normalize paths
inputfile = normpath(inputfile)
isfile(inputfile) || throw(ArgumentError("cannot find inputfile `$(inputfile)`"))
inputfile = realpath(abspath(inputfile))
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
@info "generating markdown page from `$(Base.contractuser(inputfile))`"
# read content
content = read(inputfile, String)
# run custom pre-processing from user
content = config["preprocess"](content)
# run some Documenter specific things
if config["documenter"]::Bool
# change the Edit on GitHub link
path = relpath(inputfile, get(config, "repo_root_path", pwd())::String)
path = replace(path, "\\" => "/")
content = """
# ```@meta
# EditURL = "@__REPO_ROOT_URL__/$(path)"
# ```
""" * content
end
# default replacements
content = replace_default(content, :md; config=config)
# create the markdown file # create the markdown file
sb = sandbox() sb = sandbox()
chunks = parse(content)
iomd = IOBuffer() iomd = IOBuffer()
continued = false continued = false
for chunk in chunks for chunk in chunks
@ -520,28 +518,9 @@ See the manual section on [Configuration](@ref) for documentation
of possible configuration with `config` and other keyword arguments. of possible configuration with `config` and other keyword arguments.
""" """
function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...) function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
# Create configuration by merging default and userdefined # preprocessing and parsing
config = create_configuration(inputfile; user_config=config, user_kwargs=kwargs) inputfile, outputdir, config, chunks =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb)
# normalize paths
inputfile = normpath(inputfile)
isfile(inputfile) || throw(ArgumentError("cannot find inputfile `$(inputfile)`"))
inputfile = realpath(abspath(inputfile))
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
@info "generating notebook from `$(Base.contractuser(inputfile))`"
# read content
content = read(inputfile, String)
# run custom pre-processing from user
content = config["preprocess"](content)
# default replacements
content = replace_default(content, :nb; config=config)
# parse
chunks = parse(content; allow_continued = false)
# create the notebook # create the notebook
nb = Dict() nb = Dict()

Loading…
Cancel
Save