small refactors (mostly about code styles)

pull/330/head
Shuhei Kadowaki 2020-05-16 00:32:09 +09:00
parent 589e1ce3d1
commit 7c2eb23bcd
5 changed files with 53 additions and 83 deletions

View File

@ -179,24 +179,25 @@ function weave(
open(io->write(io,formatted), outname, "w") open(io->write(io,formatted), outname, "w")
# Special for that need external programs # Special for that need external programs
if doc.doctype == "pandoc2html" doctype = doc.doctype
if doctype == "pandoc2html"
mdname = outname mdname = outname
outname = get_outname(out_path, doc, ext = "html") outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname, pandoc_options) pandoc2html(formatted, doc, outname, pandoc_options)
rm(mdname) rm(mdname)
elseif doc.doctype == "pandoc2pdf" elseif doctype == "pandoc2pdf"
mdname = outname mdname = outname
outname = get_outname(out_path, doc, ext = "pdf") outname = get_outname(out_path, doc, ext = "pdf")
pandoc2pdf(formatted, doc, outname, pandoc_options) pandoc2pdf(formatted, doc, outname, pandoc_options)
rm(mdname) rm(mdname)
elseif doc.doctype == "md2pdf" elseif doctype == "md2pdf"
success = run_latex(doc, outname, latex_cmd) success = run_latex(doc, outname, latex_cmd)
success || return success || return
outname = get_outname(out_path, doc, ext = "pdf") outname = get_outname(out_path, doc, ext = "pdf")
end end
doc.cwd == pwd() && (outname = basename(outname)) doc.cwd == pwd() && (outname = basename(outname))
@info("Report weaved to $outname") @info "Report weaved to $outname"
return abspath(outname) return abspath(outname)
end end
@ -245,7 +246,7 @@ function notebook(
end end
@info "Running nbconvert" @info "Running nbconvert"
return out = read( return read(
`$jupyter_path nbconvert --ExecutePreprocessor.timeout=$timeout --to notebook --execute $outfile $nbconvert_options --output $outfile`, `$jupyter_path nbconvert --ExecutePreprocessor.timeout=$timeout --to notebook --execute $outfile $nbconvert_options --output $outfile`,
String, String,
) )
@ -272,11 +273,12 @@ function include_weave(
"\n", "\n",
) )
include_string(m, code) include_string(m, code)
catch e catch err
throw(e) throw(err)
finally finally
cd(old_path) cd(old_path)
end end
return nothing
end end
include_weave(source, informat = nothing) = include_weave(Main, source, informat) include_weave(source, informat = nothing) = include_weave(Main, source, informat)

View File

@ -1,5 +1,4 @@
using Markdown using Markdown, .WeaveMarkdown
import .WeaveMarkdown
# Contains report global properties # Contains report global properties
mutable struct Report <: AbstractDisplay mutable struct Report <: AbstractDisplay
@ -78,17 +77,11 @@ function Base.display(report::Report, data)
end end
end end
function Base.display(report::Report, m::MIME"image/png", data) Base.display(report::Report, m::MIME"image/png", data) = add_figure(report, data, m, ".png")
figname = add_figure(report, data, m, ".png")
end
function Base.display(report::Report, m::MIME"image/svg+xml", data) Base.display(report::Report, m::MIME"image/svg+xml", data) = add_figure(report, data, m, ".svg")
figname = add_figure(report, data, m, ".svg")
end
function Base.display(report::Report, m::MIME"application/pdf", data) Base.display(report::Report, m::MIME"application/pdf", data) = add_figure(report, data, m, ".pdf")
figname = add_figure(report, data, m, ".pdf")
end
#Text is written to stdout, called from "term" mode chunks #Text is written to stdout, called from "term" mode chunks
function Base.display(report::Report, m::MIME"text/plain", data) function Base.display(report::Report, m::MIME"text/plain", data)

View File

@ -1,11 +1,9 @@
import Mustache, Highlights using Mustache, Highlights
import .WeaveMarkdown using .WeaveMarkdown, Markdown, Dates
using Dates
using Markdown
using REPL.REPLCompletions: latex_symbols using REPL.REPLCompletions: latex_symbols
function format(doc::WeaveDoc) function format(doc::WeaveDoc)
formatted = AbstractString[] formatted = String[]
docformat = doc.format docformat = doc.format
# Complete format dictionaries with defaults # Complete format dictionaries with defaults
@ -29,19 +27,12 @@ function format(doc::WeaveDoc)
formatted = join(formatted, "\n") formatted = join(formatted, "\n")
# Render using a template if needed # Render using a template if needed
rendered = render_doc(formatted, doc, doc.format) return render_doc(formatted, doc)
return rendered
end end
""" render_doc(formatted, doc) = render_doc(formatted, doc, doc.format)
render_doc(formatted::AbstractString, format)
Render formatted document to a template render_doc(formatted, doc, format) = formatted
"""
function render_doc(formatted, doc::WeaveDoc, format)
return formatted
end
function highlight( function highlight(
mime::MIME, mime::MIME,
@ -56,7 +47,7 @@ function stylesheet(m::MIME, theme)
return sprint((io, x) -> Highlights.stylesheet(io, m, x), theme) return sprint((io, x) -> Highlights.stylesheet(io, m, x), theme)
end end
function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML) function render_doc(formatted, doc, format::JMarkdown2HTML)
css = stylesheet(MIME("text/html"), doc.highlight_theme) css = stylesheet(MIME("text/html"), doc.highlight_theme)
path, wsource = splitdir(abspath(doc.source)) path, wsource = splitdir(abspath(doc.source))
# wversion = string(Pkg.installed("Weave")) # wversion = string(Pkg.installed("Weave"))
@ -94,7 +85,7 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML)
) )
end end
function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex) function render_doc(formatted, doc, format::JMarkdown2tex)
highlight = stylesheet(MIME("text/latex"), doc.highlight_theme) highlight = stylesheet(MIME("text/latex"), doc.highlight_theme)
path, wsource = splitdir(abspath(doc.source)) path, wsource = splitdir(abspath(doc.source))
# wversion = string(Pkg.installed("Weave")) # wversion = string(Pkg.installed("Weave"))

View File

@ -43,21 +43,18 @@ function run_doc(
) )
# cache :all, :user, :off, :refresh # cache :all, :user, :off, :refresh
doc.cwd = get_cwd(doc, out_path)
# doctype detection is unnecessary here, but existing unit test requires this. # doctype detection is unnecessary here, but existing unit test requires this.
isnothing(doctype) && (doctype = detect_doctype(doc.source)) isnothing(doctype) && (doctype = detect_doctype(doc.source))
doc.doctype = doctype doc.doctype = doctype
doc.format = formats[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 doc.format.formatdict[:keep_unicode] = latex_keep_unicode
end end
doc.cwd = get_cwd(doc, out_path)
isdir(doc.cwd) || mkpath(doc.cwd) isdir(doc.cwd) || mkpath(doc.cwd)
if (occursin("2pdf", doctype) && cache == :off) || occursin("2html", doctype)
if occursin("2pdf", doctype) && cache == :off
fig_path = mktempdir(abspath(doc.cwd))
elseif occursin("2html", doctype)
fig_path = mktempdir(abspath(doc.cwd)) fig_path = mktempdir(abspath(doc.cwd))
end end
@ -70,14 +67,10 @@ function run_doc(
set_rc_params(doc, fig_path, fig_ext) set_rc_params(doc, fig_path, fig_ext)
# New sandbox for each document with args exposed # 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 @eval mod WEAVE_ARGS = $args
if haskey(doc.format.formatdict, :mimetypes) mimetypes = get(doc.format.formatdict, :mimetypes, default_mime_types)
mimetypes = doc.format.formatdict[:mimetypes]
else
mimetypes = default_mime_types
end
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes, throw_errors) report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes, throw_errors)
pushdisplay(report) pushdisplay(report)
@ -112,7 +105,7 @@ function run_doc(
cache !== :off && write_cache(doc, cache_path) cache !== :off && write_cache(doc, cache_path)
@isdefined(sandbox) && clear_module!(sandbox::Module) @isdefined(sandbox) && clear_module!(sandbox)
catch err catch err
rethrow(err) rethrow(err)
finally finally
@ -139,26 +132,23 @@ function detect_doctype(pathname::AbstractString)
end end
function run_chunk(chunk::CodeChunk, doc::WeaveDoc, report::Report, SandBox::Module) function run_chunk(chunk::CodeChunk, doc::WeaveDoc, report::Report, SandBox::Module)
@info("Weaving chunk $(chunk.number) from line $(chunk.start_line)") # TODO: integrate with Juno's progress metre
result_chunks = eval_chunk(chunk, report, SandBox) @info "Weaving chunk $(chunk.number) from line $(chunk.start_line)"
occursin("2html", report.formatdict[:doctype]) && result = eval_chunk(chunk, report, SandBox)
(result_chunks = embed_figures(result_chunks, report.cwd)) occursin("2html", report.formatdict[:doctype]) && (embed_figures!(result, report.cwd))
return result_chunks return result
end end
function embed_figures(chunk::CodeChunk, cwd) function embed_figures!(chunk::CodeChunk, cwd)
chunk.figures = [img2base64(fig, cwd) for fig in chunk.figures] for (i, fig) in enumerate(chunk.figures)
return chunk chunk.figures[i] = img2base64(fig, cwd)
end end
end
function embed_figures(result_chunks, cwd)
for i = 1:length(result_chunks) function embed_figures!(chunks::Vector{CodeChunk}, cwd)
figs = result_chunks[i].figures for chunk in chunks
if !isempty(figs) embed_figures!(chunk, cwd)
result_chunks[i].figures = [img2base64(fig, cwd) for fig in figs]
end
end end
return result_chunks
end end
function img2base64(fig, cwd) function img2base64(fig, cwd)
@ -193,8 +183,7 @@ function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox::
merge!(chunk.options, options) merge!(chunk.options, options)
chunks = eval_chunk(chunk, report, SandBox) chunks = eval_chunk(chunk, report, SandBox)
occursin("2html", report.formatdict[:doctype]) && occursin("2html", report.formatdict[:doctype]) && (embed_figures!(chunks, report.cwd))
(chunks = embed_figures(chunks, report.cwd))
output = chunks[1].output output = chunks[1].output
endswith(output, "\n") && (output = output[1:end-1]) endswith(output, "\n") && (output = output[1:end-1])
@ -270,17 +259,16 @@ function capture_output(expr, SandBox::Module, term, disp, lastline, throw_error
end end
# Parse chunk input to array of expressions # Parse chunk input to array of expressions
function parse_input(input::AbstractString) function parse_input(s)
parsed = Tuple{AbstractString,Any}[] res = []
input = lstrip(input) s = lstrip(s)
n = sizeof(input) n = sizeof(s)
pos = 1 # The first character is extra line end pos = 1 # The first character is extra line end
while pos n while (oldpos = pos) n
oldpos = pos ex, pos = Meta.parse(s, pos)
code, pos = Meta.parse(input, pos) push!(res, (s[oldpos:pos-1], ex))
push!(parsed, (input[oldpos:pos-1], code))
end end
parsed return res
end end
function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) 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)) # chunk.options[:fig] && (chunk.figures = copy(report.figures))
# end # end
chunks return chunks
end end
# function eval_chunk(chunk::DocChunk, report::Report, SandBox)
# chunk
# end
""" """
clear_module!(mod::Module) clear_module!(mod::Module)

View File

@ -31,7 +31,7 @@ o = Weave.format_output(doc.chunks[4].content, doc.format)
@test o_check == o @test o_check == o
doc.template = "templates/mini.tpl" doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc, doc.format) rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n" @test rendered == "\nHello\n"
# Tex format # Tex format
@ -48,7 +48,7 @@ o = Weave.format_output(doc.chunks[3].content, doc.format)
@test o_check == o @test o_check == o
doc.template = "templates/mini.tpl" doc.template = "templates/mini.tpl"
rendered = Weave.render_doc("Hello", doc, doc.format) rendered = Weave.render_doc("Hello", doc)
@test rendered == "\nHello\n" @test rendered == "\nHello\n"
# Test header parsing and stripping # Test header parsing and stripping