mirror of https://github.com/mpastell/Weave.jl
Parse the chunk options as a set of Julia expressions and store options in a Dict{Symbol,Any}
parent
f0ac1189d7
commit
5daa95287d
|
@ -15,7 +15,7 @@ weave("examples/julia_sample.mdw")
|
|||
|
||||
## Terminal chunk
|
||||
|
||||
<<"term"=>true>>=
|
||||
<<term=true>>=
|
||||
x = 1:10
|
||||
d = {"juliareport" => "testing"}
|
||||
y = [2, 4 ,8]
|
||||
|
@ -35,6 +35,6 @@ ylabel("sinc(x)")
|
|||
|
||||
You can also include a plot with caption and hide the code:
|
||||
|
||||
<<"echo"=>false, "caption"=>"Random walk.", "label"=>"random">>=
|
||||
<<echo=false; caption="Random walk."; label="random">>=
|
||||
plot(cumsum(randn(1000, 1)))
|
||||
@
|
||||
|
|
|
@ -15,7 +15,7 @@ weave(Pkg.dir("JuliaReport","examples","winston_sample.mdw"), plotlib="Winston")
|
|||
|
||||
## Terminal chunk
|
||||
|
||||
<<"term"=>true>>=
|
||||
<<term=true>>=
|
||||
x = 1:10
|
||||
d = {"juliareport" => "testing"}
|
||||
y = [2, 4 ,8]
|
||||
|
@ -36,7 +36,7 @@ ylabel("sinc(x)")
|
|||
|
||||
You can also include a plot with caption and hide the code:
|
||||
|
||||
<<"echo"=>false, "caption"=>"Random walk.", "label"=>"random">>=
|
||||
<<echo=false; caption="Random walk."; label="random">>=
|
||||
figure()
|
||||
plot(cumsum(randn(1000, 1)))
|
||||
@
|
||||
|
|
|
@ -17,7 +17,6 @@ type Report
|
|||
figdir::String
|
||||
end
|
||||
|
||||
|
||||
const report = Report("", false, "", "", Any[], "", "")
|
||||
|
||||
function listformats()
|
||||
|
@ -47,11 +46,11 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb",
|
|||
l_plotlib = lowercase(plotlib)
|
||||
if l_plotlib == "winston"
|
||||
eval(Expr(:using, :Winston))
|
||||
rcParams["plotlib"] = "Winston"
|
||||
rcParams[:plotlib] = "Winston"
|
||||
elseif l_plotlib == "pyplot"
|
||||
#eval(parse("import PyPlot.plt"))
|
||||
eval(Expr(:using, :PyPlot))
|
||||
rcParams["plotlib"] = "PyPlot"
|
||||
rcParams[:plotlib] = "PyPlot"
|
||||
end
|
||||
|
||||
|
||||
|
@ -107,42 +106,40 @@ end
|
|||
|
||||
|
||||
function run(parsed)
|
||||
i = 1
|
||||
for chunk = copy(parsed)
|
||||
if chunk["type"] == "code"
|
||||
#print(chunk["content"])
|
||||
info("""Weaving chunk $(chunk["number"]) from line $(chunk["start_line"])""")
|
||||
defaults = copy(rcParams["chunk"]["defaultoptions"])
|
||||
options = copy(chunk["options"])
|
||||
try
|
||||
options = merge(rcParams["chunk"]["defaultoptions"], options)
|
||||
catch
|
||||
options = rcParams["chunk"]["defaultoptions"]
|
||||
warn(string("Invalid format for chunk options line: ", chunk["start_line"]))
|
||||
end
|
||||
i = 1
|
||||
for chunk in copy(parsed)
|
||||
if chunk[:type] == "code"
|
||||
#print(chunk["content"])
|
||||
info("Weaving chunk $(chunk[:number]) from line $(chunk[:start_line])")
|
||||
defaults = copy(rcParams[:chunk][:defaultoptions])
|
||||
options = copy(chunk[:options])
|
||||
try
|
||||
options = merge(rcParams[:chunk][:defaultoptions], options)
|
||||
catch
|
||||
options = rcParams[:chunk][:defaultoptions]
|
||||
warn("Invalid format for chunk options line: $(chunk[:start_line])")
|
||||
end
|
||||
|
||||
merge!(chunk, options)
|
||||
delete!(chunk, "options")
|
||||
merge!(chunk, options)
|
||||
delete!(chunk, :options)
|
||||
|
||||
chunk[:evaluate] || (chunk[:result] = ""; continue) #Do nothing if eval is false
|
||||
if chunk[:term]
|
||||
chunk[:result] = run_term(chunk[:content])
|
||||
else
|
||||
chunk[:result] = run_block(chunk[:content])
|
||||
end
|
||||
|
||||
chunk["evaluate"] || (chunk["result"] = ""; continue) #Do nothing if eval is false
|
||||
if chunk["term"]
|
||||
chunk["result"] = run_term(chunk["content"])
|
||||
else
|
||||
chunk["result"] = run_block(chunk["content"])
|
||||
end
|
||||
|
||||
chunk["fig"] && (chunk["figure"] = savefigs(chunk))
|
||||
|
||||
end
|
||||
parsed[i] = copy(chunk)
|
||||
i += 1
|
||||
chunk[:fig] && (chunk[:figure] = savefigs(chunk))
|
||||
end
|
||||
return parsed
|
||||
parsed[i] = copy(chunk)
|
||||
i += 1
|
||||
end
|
||||
parsed
|
||||
end
|
||||
|
||||
function savefigs(chunk)
|
||||
l_plotlib = lowercase(rcParams["plotlib"])
|
||||
l_plotlib = lowercase(rcParams[:plotlib])
|
||||
if l_plotlib == "pyplot"
|
||||
return savefigs_pyplot(chunk)
|
||||
elseif l_plotlib == "winston"
|
||||
|
@ -156,7 +153,7 @@ function savefigs_pyplot(chunk)
|
|||
figpath = joinpath(report.cwd, report.figdir)
|
||||
isdir(figpath) || mkdir(figpath)
|
||||
|
||||
chunkid = ((chunk["name"] == nothing) ? chunk["number"] : chunk["name"])
|
||||
chunkid = get(chunk,:name,chunk[:number]) #((chunk[:name] == nothing) ? chunk[:number] : chunk[:name])
|
||||
#Iterate over all open figures, save them and store names
|
||||
for fig = plt.get_fignums()
|
||||
full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext")
|
||||
|
@ -175,7 +172,7 @@ function savefigs_winston(chunk)
|
|||
figpath = joinpath(report.cwd, report.figdir)
|
||||
isdir(figpath) || mkdir(figpath)
|
||||
|
||||
chunkid = ((chunk["name"] == nothing) ? chunk["number"] : chunk["name"])
|
||||
chunkid = get(chunk,:name,chunk[:number])#((chunk[:name] == nothing) ? chunk[:number] : chunk[:name])
|
||||
#Iterate over all open figures, save them and store names
|
||||
#println(Winston._display.figs)
|
||||
#println(Winston._display.fig_order)
|
||||
|
@ -197,12 +194,10 @@ end
|
|||
#Saving Winston figures
|
||||
#savefig(Winston._display.figs[1].plot, "test.png")
|
||||
|
||||
|
||||
|
||||
export weave
|
||||
|
||||
typealias StrD Dict{ASCIIString,Any}
|
||||
|
||||
include("config.jl")
|
||||
include("readers.jl")
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
const rcParams =
|
||||
@compat Dict{ASCIIString,Any}("figdir"=> "figures",
|
||||
"plotlib" => "PyPlot",
|
||||
"storeresults"=> false,
|
||||
"cachedir"=> "cache",
|
||||
"chunk"=>
|
||||
Dict{ASCIIString,Any}("defaultoptions"=>
|
||||
Dict{ASCIIString,Any}("echo"=> true,
|
||||
"results"=> "verbatim",
|
||||
"fig"=> true,
|
||||
"include"=> true,
|
||||
"evaluate"=> true,
|
||||
"caption"=> false,
|
||||
"term"=> false,
|
||||
"name"=> nothing,
|
||||
"wrap"=> true,
|
||||
"f_pos"=> "htpb",
|
||||
"f_size"=> (8, 6),
|
||||
"f_env"=> nothing,
|
||||
"f_spines"=> true,
|
||||
"complete"=> true,
|
||||
"engine"=> "julia",
|
||||
"option_string"=> "")
|
||||
)
|
||||
)
|
||||
@compat Dict{Symbol,Any}(:figdir=> "figures",
|
||||
:plotlib => "PyPlot",
|
||||
:storeresults=> false,
|
||||
:cachedir=> "cache",
|
||||
:chunk=>
|
||||
Dict{Symbol,Any}(:defaultoptions=>
|
||||
Dict{Symbol,Any}(:echo=> true,
|
||||
:results=> "verbatim",
|
||||
:fig=> true,
|
||||
:include=> true,
|
||||
:evaluate=> true,
|
||||
:caption=> false,
|
||||
:term=> false,
|
||||
:name=> nothing,
|
||||
:wrap=> true,
|
||||
:f_pos=> "htpb",
|
||||
:f_size=> (8, 6),
|
||||
:f_env=> nothing,
|
||||
:f_spines=> true,
|
||||
:complete=> true,
|
||||
:engine=> "julia",
|
||||
:option_string=> "")
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
pushopt(options::Dict,expr::Expr) = Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2])
|
||||
|
||||
function read_noweb(document)
|
||||
#doctext = readall(open(document))
|
||||
lines = split(bytestring(open(document) do io
|
||||
|
@ -11,7 +13,6 @@ function read_noweb(document)
|
|||
docno = 1
|
||||
codeno = 1
|
||||
content = ""
|
||||
lineno = 0
|
||||
start_line = 0
|
||||
|
||||
options = Dict()
|
||||
|
@ -19,26 +20,21 @@ function read_noweb(document)
|
|||
parsed = Dict[]
|
||||
for lineno in 1:length(lines)
|
||||
line = lines[lineno]
|
||||
if ismatch(codestart, line) && state=="doc"
|
||||
if (m = match(codestart, line)) != nothing && state=="doc"
|
||||
state = "code"
|
||||
m = match(codestart, line)
|
||||
optionstring=m.captures[1]
|
||||
#println(m.captures[1])
|
||||
if strip(optionstring)==""
|
||||
options = StrD()
|
||||
else
|
||||
try
|
||||
options = eval(parse("{" * optionstring * "}"))
|
||||
catch
|
||||
options = StrD()
|
||||
warn(string("Invalid format for chunk options line: ", lineno))
|
||||
end
|
||||
optionstring=strip(m.captures[1])
|
||||
@show optionstring
|
||||
options = Dict{Symbol,Any}()
|
||||
if length(optionstring) > 0
|
||||
expr = parse(optionstring)
|
||||
Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2])
|
||||
Base.Meta.isexpr(expr,:toplevel) && map(pushopt,fill(options,length(expr.args)),expr.args)
|
||||
end
|
||||
haskey(options, "label") && (options["name"] = options["label"])
|
||||
haskey(options, "name") || (options["name"] = nothing)
|
||||
|
||||
chunk = @compat Dict{ASCIIString,Any}("type" => "doc", "content"=> content,
|
||||
"number" => docno, "start_line"=>start_line)
|
||||
haskey(options, :label) && (options[:name] = options[:label])
|
||||
haskey(options, :name) || (options[:name] = nothing)
|
||||
@show options
|
||||
chunk = @compat Dict{Symbol,Any}(:type => "doc", :content => content,
|
||||
:number => docno,:start_line => start_line)
|
||||
docno += 1
|
||||
start_line = lineno
|
||||
push!(parsed, chunk)
|
||||
|
@ -46,10 +42,10 @@ function read_noweb(document)
|
|||
continue
|
||||
end
|
||||
if ismatch(codeend, line) && state=="code"
|
||||
chunk = @compat Dict{ASCIIString,Any}("type" => "code", "content" => content,
|
||||
"number" => codeno, "options" => options,
|
||||
"optionstring" => optionstring,
|
||||
"start_line" => start_line)
|
||||
chunk = @compat Dict{Symbol,Any}(:type => "code", :content => content,
|
||||
:number => codeno, :options => options,
|
||||
:optionstring => optionstring,
|
||||
:start_line => start_line)
|
||||
codeno+=1
|
||||
start_line = lineno
|
||||
content = ""
|
||||
|
@ -63,8 +59,8 @@ function read_noweb(document)
|
|||
|
||||
#Remember the last chunk
|
||||
if content != ""
|
||||
chunk = @compat Dict{ASCIIString,Any}("type" => "doc", "content" => content,
|
||||
"number" => docno, "start_line" => lineno)
|
||||
chunk = @compat Dict{Symbol,Any}(:type => "doc", :content => content,
|
||||
:number => docno, :start_line => start_line)
|
||||
push!(parsed, chunk)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue