Skip to content

Commit

Permalink
folding cells
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Sep 3, 2020
1 parent d32cd46 commit 90dec28
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ See the manual section about [Configuration](@ref) for more information.
"""
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
config = create_configuration(inputfile; user_config=user_config,
user_kwargs=user_kwargs, type=type)
Expand All @@ -318,7 +318,7 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type)
# Add some information for passing around Literate methods
config["literate_inputfile"] = inputfile
config["literate_outputdir"] = outputdir
config["literate_ext"] = type === (:nb) ? ".ipynb" : ".$(type)"
config["literate_ext"] = type === (:nb) ? (flavor === :pluto ? ".jl" : ".ipynb") : ".$(type)"

# read content
content = read(inputfile, String)
Expand Down Expand Up @@ -526,7 +526,7 @@ of possible configuration with `config` and other keyword arguments.
function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), flavor=:jupyter, kwargs...)
# preprocessing and parsing
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
nb = flavor == :jupyter ? jupyter_notebook(chunks, config) : pluto_notebook(chunks, config)
Expand Down Expand Up @@ -662,8 +662,22 @@ function pluto_notebook(chunks, config)

# Print cells
uuids = Base.UUID[]
folds = Bool[]
default_fold = Dict{String,Bool}("markdown"=>true, "code"=>false) # toggleable ???
for (i, chunk) in enumerate(chunks)
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 length(chunk.lines) == 1
line = escape_string(chunk.lines[1].second, '"')
Expand Down Expand Up @@ -698,13 +712,14 @@ function pluto_notebook(chunks, config)
end
uuid = uuid4(content, i)
push!(uuids, uuid)
push!(folds, fold)
print(ionb, "# ╔═╡ ", uuid, '\n')
write(ionb, content, '\n')
end

# Print cell order
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
nb = config["postprocess"](String(take!(ionb)))
Expand Down

0 comments on commit 90dec28

Please sign in to comment.