diff --git a/src/Weave.jl b/src/Weave.jl index 7cd60ea..d0d4fd1 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -179,24 +179,25 @@ function weave( open(io->write(io,formatted), outname, "w") # Special for that need external programs - if doc.doctype == "pandoc2html" + doctype = doc.doctype + if doctype == "pandoc2html" mdname = outname outname = get_outname(out_path, doc, ext = "html") pandoc2html(formatted, doc, outname, pandoc_options) rm(mdname) - elseif doc.doctype == "pandoc2pdf" + elseif doctype == "pandoc2pdf" mdname = outname outname = get_outname(out_path, doc, ext = "pdf") pandoc2pdf(formatted, doc, outname, pandoc_options) rm(mdname) - elseif doc.doctype == "md2pdf" + elseif doctype == "md2pdf" success = run_latex(doc, outname, latex_cmd) success || return outname = get_outname(out_path, doc, ext = "pdf") end doc.cwd == pwd() && (outname = basename(outname)) - @info("Report weaved to $outname") + @info "Report weaved to $outname" return abspath(outname) end @@ -245,7 +246,7 @@ function notebook( end @info "Running nbconvert" - return out = read( + return read( `$jupyter_path nbconvert --ExecutePreprocessor.timeout=$timeout --to notebook --execute $outfile $nbconvert_options --output $outfile`, String, ) @@ -272,11 +273,12 @@ function include_weave( "\n", ) include_string(m, code) - catch e - throw(e) + catch err + throw(err) finally cd(old_path) end + return nothing end include_weave(source, informat = nothing) = include_weave(Main, source, informat) diff --git a/src/display_methods.jl b/src/display_methods.jl index b13d5d3..4d0da9e 100644 --- a/src/display_methods.jl +++ b/src/display_methods.jl @@ -1,5 +1,4 @@ -using Markdown -import .WeaveMarkdown +using Markdown, .WeaveMarkdown # Contains report global properties mutable struct Report <: AbstractDisplay @@ -78,17 +77,11 @@ function Base.display(report::Report, data) end end -function Base.display(report::Report, m::MIME"image/png", data) - figname = add_figure(report, data, m, ".png") -end +Base.display(report::Report, m::MIME"image/png", data) = add_figure(report, data, m, ".png") -function Base.display(report::Report, m::MIME"image/svg+xml", data) - figname = add_figure(report, data, m, ".svg") -end +Base.display(report::Report, m::MIME"image/svg+xml", data) = add_figure(report, data, m, ".svg") -function Base.display(report::Report, m::MIME"application/pdf", data) - figname = add_figure(report, data, m, ".pdf") -end +Base.display(report::Report, m::MIME"application/pdf", data) = add_figure(report, data, m, ".pdf") #Text is written to stdout, called from "term" mode chunks function Base.display(report::Report, m::MIME"text/plain", data) diff --git a/src/format.jl b/src/format.jl index 5cdf35b..edafbd2 100644 --- a/src/format.jl +++ b/src/format.jl @@ -1,11 +1,9 @@ -import Mustache, Highlights -import .WeaveMarkdown -using Dates -using Markdown +using Mustache, Highlights +using .WeaveMarkdown, Markdown, Dates using REPL.REPLCompletions: latex_symbols function format(doc::WeaveDoc) - formatted = AbstractString[] + formatted = String[] docformat = doc.format # Complete format dictionaries with defaults @@ -29,19 +27,12 @@ function format(doc::WeaveDoc) formatted = join(formatted, "\n") # Render using a template if needed - rendered = render_doc(formatted, doc, doc.format) - - return rendered + return render_doc(formatted, doc) end -""" - render_doc(formatted::AbstractString, format) +render_doc(formatted, doc) = render_doc(formatted, doc, doc.format) -Render formatted document to a template -""" -function render_doc(formatted, doc::WeaveDoc, format) - return formatted -end +render_doc(formatted, doc, format) = formatted function highlight( mime::MIME, @@ -56,7 +47,7 @@ function stylesheet(m::MIME, theme) return sprint((io, x) -> Highlights.stylesheet(io, m, x), theme) end -function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML) +function render_doc(formatted, doc, format::JMarkdown2HTML) css = stylesheet(MIME("text/html"), doc.highlight_theme) path, wsource = splitdir(abspath(doc.source)) # wversion = string(Pkg.installed("Weave")) @@ -94,7 +85,7 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML) ) end -function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex) +function render_doc(formatted, doc, format::JMarkdown2tex) highlight = stylesheet(MIME("text/latex"), doc.highlight_theme) path, wsource = splitdir(abspath(doc.source)) # wversion = string(Pkg.installed("Weave")) diff --git a/src/run.jl b/src/run.jl index f1a9dd2..a275df1 100644 --- a/src/run.jl +++ b/src/run.jl @@ -43,21 +43,18 @@ function run_doc( ) # cache :all, :user, :off, :refresh - doc.cwd = get_cwd(doc, out_path) # doctype detection is unnecessary here, but existing unit test requires this. isnothing(doctype) && (doctype = detect_doctype(doc.source)) doc.doctype = doctype doc.format = formats[doctype] - if (haskey(doc.format.formatdict, :keep_unicode)) + if haskey(doc.format.formatdict, :keep_unicode) doc.format.formatdict[:keep_unicode] = latex_keep_unicode end + doc.cwd = get_cwd(doc, out_path) isdir(doc.cwd) || mkpath(doc.cwd) - - if occursin("2pdf", doctype) && cache == :off - fig_path = mktempdir(abspath(doc.cwd)) - elseif occursin("2html", doctype) + if (occursin("2pdf", doctype) && cache == :off) || occursin("2html", doctype) fig_path = mktempdir(abspath(doc.cwd)) end @@ -70,14 +67,10 @@ function run_doc( set_rc_params(doc, fig_path, fig_ext) # New sandbox for each document with args exposed - isnothing(mod) && (mod::Module = sandbox::Module = Core.eval(Main, :(module $(gensym(:WeaveSandBox)) end))) + isnothing(mod) && (mod = sandbox = Core.eval(Main, :(module $(gensym(:WeaveSandBox)) end))::Module) @eval mod WEAVE_ARGS = $args - if haskey(doc.format.formatdict, :mimetypes) - mimetypes = doc.format.formatdict[:mimetypes] - else - mimetypes = default_mime_types - end + mimetypes = get(doc.format.formatdict, :mimetypes, default_mime_types) report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes, throw_errors) pushdisplay(report) @@ -112,7 +105,7 @@ function run_doc( cache !== :off && write_cache(doc, cache_path) - @isdefined(sandbox) && clear_module!(sandbox::Module) + @isdefined(sandbox) && clear_module!(sandbox) catch err rethrow(err) finally @@ -139,26 +132,23 @@ function detect_doctype(pathname::AbstractString) end function run_chunk(chunk::CodeChunk, doc::WeaveDoc, report::Report, SandBox::Module) - @info("Weaving chunk $(chunk.number) from line $(chunk.start_line)") - result_chunks = eval_chunk(chunk, report, SandBox) - occursin("2html", report.formatdict[:doctype]) && - (result_chunks = embed_figures(result_chunks, report.cwd)) - return result_chunks + # TODO: integrate with Juno's progress metre + @info "Weaving chunk $(chunk.number) from line $(chunk.start_line)" + result = eval_chunk(chunk, report, SandBox) + occursin("2html", report.formatdict[:doctype]) && (embed_figures!(result, report.cwd)) + return result end -function embed_figures(chunk::CodeChunk, cwd) - chunk.figures = [img2base64(fig, cwd) for fig in chunk.figures] - return chunk -end - -function embed_figures(result_chunks, cwd) - for i = 1:length(result_chunks) - figs = result_chunks[i].figures - if !isempty(figs) - result_chunks[i].figures = [img2base64(fig, cwd) for fig in figs] - end +function embed_figures!(chunk::CodeChunk, cwd) + for (i, fig) in enumerate(chunk.figures) + chunk.figures[i] = img2base64(fig, cwd) + end +end + +function embed_figures!(chunks::Vector{CodeChunk}, cwd) + for chunk in chunks + embed_figures!(chunk, cwd) end - return result_chunks end function img2base64(fig, cwd) @@ -193,8 +183,7 @@ function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox:: merge!(chunk.options, options) chunks = eval_chunk(chunk, report, SandBox) - occursin("2html", report.formatdict[:doctype]) && - (chunks = embed_figures(chunks, report.cwd)) + occursin("2html", report.formatdict[:doctype]) && (embed_figures!(chunks, report.cwd)) output = chunks[1].output endswith(output, "\n") && (output = output[1:end-1]) @@ -270,17 +259,16 @@ function capture_output(expr, SandBox::Module, term, disp, lastline, throw_error end # Parse chunk input to array of expressions -function parse_input(input::AbstractString) - parsed = Tuple{AbstractString,Any}[] - input = lstrip(input) - n = sizeof(input) +function parse_input(s) + res = [] + s = lstrip(s) + n = sizeof(s) pos = 1 # The first character is extra line end - while pos ≤ n - oldpos = pos - code, pos = Meta.parse(input, pos) - push!(parsed, (input[oldpos:pos-1], code)) + while (oldpos = pos) ≤ n + ex, pos = Meta.parse(s, pos) + push!(res, (s[oldpos:pos-1], ex)) end - parsed + return res end function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) @@ -321,13 +309,9 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) # chunk.options[:fig] && (chunk.figures = copy(report.figures)) # end - chunks + return chunks end -# function eval_chunk(chunk::DocChunk, report::Report, SandBox) -# chunk -# end - """ clear_module!(mod::Module) diff --git a/test/formatter_test.jl b/test/formatter_test.jl index 1ee8bee..06c5e84 100644 --- a/test/formatter_test.jl +++ b/test/formatter_test.jl @@ -31,7 +31,7 @@ o = Weave.format_output(doc.chunks[4].content, doc.format) @test o_check == o doc.template = "templates/mini.tpl" -rendered = Weave.render_doc("Hello", doc, doc.format) +rendered = Weave.render_doc("Hello", doc) @test rendered == "\nHello\n" # Tex format @@ -48,7 +48,7 @@ o = Weave.format_output(doc.chunks[3].content, doc.format) @test o_check == o doc.template = "templates/mini.tpl" -rendered = Weave.render_doc("Hello", doc, doc.format) +rendered = Weave.render_doc("Hello", doc) @test rendered == "\nHello\n" # Test header parsing and stripping