Browse Source

folding cells

pull/120/head
Fredrik Ekre 5 years ago
parent
commit
872c00d1e0
  1. 23
      src/Literate.jl

23
src/Literate.jl

@ -373,7 +373,7 @@ Available options:
""" """
const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation
function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) function preprocessor(inputfile, outputdir; user_config, user_kwargs, type, flavor)
# Create configuration by merging default and userdefined # Create configuration by merging default and userdefined
config = create_configuration(inputfile; user_config=user_config, config = create_configuration(inputfile; user_config=user_config,
user_kwargs=user_kwargs, type=type) user_kwargs=user_kwargs, type=type)
@ -393,7 +393,7 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
# Add some information for passing around Literate methods # Add some information for passing around Literate methods
config["literate_inputfile"] = inputfile config["literate_inputfile"] = inputfile
config["literate_outputdir"] = outputdir config["literate_outputdir"] = outputdir
config["literate_ext"] = type === (:nb) ? ".ipynb" : ".$(type)" config["literate_ext"] = type === (:nb) ? (flavor === :pluto ? ".jl" : ".ipynb") : ".$(type)"
# read content # read content
content = read(inputfile, String) content = read(inputfile, String)
@ -607,7 +607,7 @@ of possible configuration with `config` and other keyword arguments.
function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), flavor=:jupyter, kwargs...) function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), flavor=:jupyter, kwargs...)
# preprocessing and parsing # preprocessing and parsing
chunks, config = chunks, config =
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb) preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb, flavor=flavor)
# create the notebook # create the notebook
nb = flavor == :jupyter ? jupyter_notebook(chunks, config) : pluto_notebook(chunks, config) nb = flavor == :jupyter ? jupyter_notebook(chunks, config) : pluto_notebook(chunks, config)
@ -756,8 +756,22 @@ function pluto_notebook(chunks, config)
# Print cells # Print cells
uuids = Base.UUID[] uuids = Base.UUID[]
folds = Bool[]
default_fold = Dict{String,Bool}("markdown"=>true, "code"=>false) # toggleable ???
for (i, chunk) in enumerate(chunks) for (i, chunk) in enumerate(chunks)
io = IOBuffer() io = IOBuffer()
# Jupyter style metadata # TODO: factor out, identical to jupyter notebook
chunktype = isa(chunk, MDChunk) ? "markdown" : "code"
fold = default_fold[chunktype]
if !isempty(chunk.lines) && line_is_nbmeta(chunk.lines[1])
@show chunk.lines
metatype, metadata = parse_nbmeta(chunk.lines[1])
metatype !== nothing && metatype != chunktype && error("specifying a different cell type is not supported")
popfirst!(chunk.lines)
fold = get(metadata, "fold", fold)
end
if isa(chunk, MDChunk) if isa(chunk, MDChunk)
if length(chunk.lines) == 1 if length(chunk.lines) == 1
line = escape_string(chunk.lines[1].second, '"') line = escape_string(chunk.lines[1].second, '"')
@ -792,13 +806,14 @@ function pluto_notebook(chunks, config)
end end
uuid = uuid4(content, i) uuid = uuid4(content, i)
push!(uuids, uuid) push!(uuids, uuid)
push!(folds, fold)
print(ionb, "# ╔═╡ ", uuid, '\n') print(ionb, "# ╔═╡ ", uuid, '\n')
write(ionb, content, '\n') write(ionb, content, '\n')
end end
# Print cell order # Print cell order
print(ionb, "# ╔═╡ Cell order:\n") print(ionb, "# ╔═╡ Cell order:\n")
foreach(x -> print(ionb, "# ╠═", x, '\n'), uuids) foreach(((x, f),) -> print(ionb, "# $(f ? "╟─" : "╠═")", x, '\n'), zip(uuids, folds))
# custom post-processing from user # custom post-processing from user
nb = config["postprocess"](String(take!(ionb))) nb = config["postprocess"](String(take!(ionb)))

Loading…
Cancel
Save