mirror of https://github.com/mpastell/Weave.jl
Made report object local, closes #3
parent
3c4cc04e14
commit
e76e823a3d
23
src/Weave.jl
23
src/Weave.jl
|
@ -25,7 +25,7 @@ type Report <: Display
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
const report = Report()
|
#const report = Report()
|
||||||
|
|
||||||
const supported_mime_types =
|
const supported_mime_types =
|
||||||
[MIME"image/png",
|
[MIME"image/png",
|
||||||
|
@ -78,7 +78,7 @@ function tangle(source ; out_path=:doc, informat="noweb")
|
||||||
|
|
||||||
outname = "$(cwd)/$(basename).jl"
|
outname = "$(cwd)/$(basename).jl"
|
||||||
open(outname, "w") do io
|
open(outname, "w") do io
|
||||||
for chunk in read(source, informat)
|
for chunk in read_doc(source, informat)
|
||||||
if typeof(chunk) == CodeChunk
|
if typeof(chunk) == CodeChunk
|
||||||
write(io, chunk.content*"\n")
|
write(io, chunk.content*"\n")
|
||||||
end
|
end
|
||||||
|
@ -109,6 +109,7 @@ weave(source ; doctype = "pandoc", plotlib="Gadfly",
|
||||||
""" ->
|
""" ->
|
||||||
function weave(source ; doctype = "pandoc", plotlib="Gadfly", informat="noweb", out_path=:doc, fig_path = "figures", fig_ext = nothing)
|
function weave(source ; doctype = "pandoc", plotlib="Gadfly", informat="noweb", out_path=:doc, fig_path = "figures", fig_ext = nothing)
|
||||||
|
|
||||||
|
report = Report()
|
||||||
cwd, fname = splitdir(abspath(source))
|
cwd, fname = splitdir(abspath(source))
|
||||||
basename = splitext(fname)[1]
|
basename = splitext(fname)[1]
|
||||||
formatdict = formats[doctype].formatdict
|
formatdict = formats[doctype].formatdict
|
||||||
|
@ -153,8 +154,8 @@ function weave(source ; doctype = "pandoc", plotlib="Gadfly", informat="noweb",
|
||||||
end
|
end
|
||||||
|
|
||||||
pushdisplay(report)
|
pushdisplay(report)
|
||||||
parsed = read(source, informat)
|
parsed = read_doc(source, informat)
|
||||||
executed = run(parsed)
|
executed = run(parsed, report)
|
||||||
popdisplay(report)
|
popdisplay(report)
|
||||||
formatted = format(executed, doctype)
|
formatted = format(executed, doctype)
|
||||||
outname = "$(report.cwd)/$(report.basename).$(formatdict[:extension])"
|
outname = "$(report.cwd)/$(report.basename).$(formatdict[:extension])"
|
||||||
|
@ -170,7 +171,7 @@ end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function run_block(code_str)
|
function run_block(code_str, report::Report)
|
||||||
oldSTDOUT = STDOUT
|
oldSTDOUT = STDOUT
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
|
@ -200,7 +201,7 @@ function run_block(code_str)
|
||||||
return string("\n", result)
|
return string("\n", result)
|
||||||
end
|
end
|
||||||
|
|
||||||
function run_term(code_str)
|
function run_term(code_str, report::Report)
|
||||||
prompt = "\njulia> "
|
prompt = "\njulia> "
|
||||||
codestart = "\n\n"*report.formatdict[:codestart]
|
codestart = "\n\n"*report.formatdict[:codestart]
|
||||||
|
|
||||||
|
@ -227,27 +228,27 @@ function run_term(code_str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function run(parsed)
|
function run(parsed, report::Report)
|
||||||
#Clear sandbox for each document
|
#Clear sandbox for each document
|
||||||
#Raises a warning, couldn't find a "cleaner"
|
#Raises a warning, couldn't find a "cleaner"
|
||||||
#way to do it.
|
#way to do it.
|
||||||
eval(parse("module ReportSandBox\nend"))
|
eval(parse("module ReportSandBox\nend"))
|
||||||
executed = Any[]
|
executed = Any[]
|
||||||
for chunk in copy(parsed)
|
for chunk in copy(parsed)
|
||||||
result_chunk = eval_chunk(chunk)
|
result_chunk = eval_chunk(chunk, report::Report)
|
||||||
push!(executed, result_chunk)
|
push!(executed, result_chunk)
|
||||||
end
|
end
|
||||||
executed
|
executed
|
||||||
end
|
end
|
||||||
|
|
||||||
function savefigs(chunk)
|
function savefigs(chunk, report::Report)
|
||||||
l_plotlib = lowercase(rcParams[:plotlib])
|
l_plotlib = lowercase(rcParams[:plotlib])
|
||||||
if l_plotlib == "pyplot"
|
if l_plotlib == "pyplot"
|
||||||
return savefigs_pyplot(chunk)
|
return savefigs_pyplot(chunk, report::Report)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function savefigs_pyplot(chunk)
|
function savefigs_pyplot(chunk, report::Report)
|
||||||
fignames = String[]
|
fignames = String[]
|
||||||
ext = report.formatdict[:fig_ext]
|
ext = report.formatdict[:fig_ext]
|
||||||
figpath = joinpath(report.cwd, chunk.options[:fig_path])
|
figpath = joinpath(report.cwd, chunk.options[:fig_path])
|
||||||
|
|
|
@ -14,15 +14,15 @@ const input_formats = @compat Dict{String, Any}(
|
||||||
|
|
||||||
|
|
||||||
@doc "Read and parse input document" ->
|
@doc "Read and parse input document" ->
|
||||||
function Base.read(document, format="noweb")
|
function read_doc(document::String, format="noweb"::String)
|
||||||
document = bytestring(open(document) do io
|
document = bytestring(open(document) do io
|
||||||
mmap_array(Uint8,(filesize(document),),io)
|
mmap_array(Uint8,(filesize(document),),io)
|
||||||
end)
|
end)
|
||||||
return parse(document, format)
|
return parse_doc(document, format)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "Parse document from string" ->
|
@doc "Parse document from string" ->
|
||||||
function Base.parse(document, format="noweb")
|
function parse_doc(document::String, format="noweb"::String)
|
||||||
#doctext = readall(open(document))
|
#doctext = readall(open(document))
|
||||||
lines = split(document, "\n")
|
lines = split(document, "\n")
|
||||||
|
|
||||||
|
|
10
src/run.jl
10
src/run.jl
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
function eval_chunk(chunk::CodeChunk)
|
function eval_chunk(chunk::CodeChunk, report::Report)
|
||||||
info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")
|
info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")
|
||||||
defaults = copy(rcParams[:chunk_defaults])
|
defaults = copy(rcParams[:chunk_defaults])
|
||||||
options = copy(chunk.options)
|
options = copy(chunk.options)
|
||||||
|
@ -30,20 +30,20 @@ function eval_chunk(chunk::CodeChunk)
|
||||||
end
|
end
|
||||||
|
|
||||||
if chunk.options[:term]
|
if chunk.options[:term]
|
||||||
chunk.output = run_term(chunk.content)
|
chunk.output = run_term(chunk.content, report::Report)
|
||||||
chunk.options[:term_state] = report.term_state
|
chunk.options[:term_state] = report.term_state
|
||||||
else
|
else
|
||||||
chunk.output = run_block(chunk.content)
|
chunk.output = run_block(chunk.content, report::Report)
|
||||||
end
|
end
|
||||||
|
|
||||||
if rcParams[:plotlib] == "PyPlot"
|
if rcParams[:plotlib] == "PyPlot"
|
||||||
chunk.options[:fig] && (chunk.figures = savefigs(chunk))
|
chunk.options[:fig] && (chunk.figures = savefigs(chunk, report::Report))
|
||||||
else
|
else
|
||||||
chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
||||||
end
|
end
|
||||||
chunk
|
chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
function eval_chunk(chunk::DocChunk)
|
function eval_chunk(chunk::DocChunk, report::Report)
|
||||||
chunk
|
chunk
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue