Implemented formatting in Julia, removed PyCall depency

pull/12/head
Matti Pastell 2014-12-02 16:57:00 +02:00
parent f993537ba5
commit 706ab8f72f
5 changed files with 206 additions and 36 deletions

View File

@ -1,2 +1 @@
PyCall
Compat

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,5 @@
module JuliaReport
using Compat
using PyCall
#using PyPlot
@pyimport pweave #Output formatting uses Pweave
#Contains report global properties
#Similar to pweave.PwebProcessor
@ -24,17 +20,11 @@ function listformats()
end
function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", figdir = "figures", figformat = nothing)
pweave.rcParams["chunk"]["defaultoptions"]["engine"] = "julia"
doc = pweave.Pweb(source, doctype, shell="julia")
#doc[:setreader](informat)
#doc[:parse]()
cwd, fname = splitdir(abspath(source))
basename = splitext(fname)[1]
formatdict = doc[:formatter][:getformatdict]()
figformat == nothing || (formatdict["figfmt"] = figformat)
#println(formatdict["figfmt"])
formatdict = formats[doctype].formatdict
figformat == nothing || (formatdict[:figfmt] = figformat)
#report = Report(source, false, cwd, basename, formatdict, "", figdir)
report.source = source
@ -43,25 +33,29 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb",
report.figdir = figdir
report.formatdict = formatdict
l_plotlib = lowercase(plotlib)
if l_plotlib == "winston"
eval(Expr(:using, :Winston))
rcParams[:plotlib] = "Winston"
elseif l_plotlib == "pyplot"
#eval(parse("import PyPlot.plt"))
eval(Expr(:using, :PyPlot))
rcParams[:plotlib] = "PyPlot"
if plotlib == nothing
rcParams[:chunk][:defaultoptions][:fig] = false
else
l_plotlib = lowercase(plotlib)
if l_plotlib == "winston"
eval(Expr(:using, :Winston))
rcParams[:plotlib] = "Winston"
elseif l_plotlib == "pyplot"
eval(Expr(:using, :PyPlot))
rcParams[:plotlib] = "PyPlot"
end
end
parsed = read_noweb(source)
doc[:executed] = run(parsed)
executed = run(parsed)
formatted = format(executed, doctype)
outname = "$(report.cwd)/$(report.basename).$(formatdict[:extension])"
@show outname
outfile = open(outname, "w")
write(outfile, join(formatted, "\n"))
info("Report weaved to $(report.basename).$(formatdict[:extension])")
#Formatting with pweave
#doc[:executed] = run(PyVector(doc["parsed"]))
doc[:isexecuted] = true
doc[:format]()
doc[:write]()
end
@ -149,11 +143,10 @@ end
function savefigs_pyplot(chunk)
fignames = String[]
ext = report.formatdict["figfmt"]
ext = report.formatdict[:figfmt]
figpath = joinpath(report.cwd, report.figdir)
isdir(figpath) || mkdir(figpath)
chunkid = get(chunk,:name,chunk[:number]) #((chunk[:name] == nothing) ? chunk[:number] : chunk[:name])
chunkid = (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")
@ -167,15 +160,15 @@ function savefigs_pyplot(chunk)
end
#This currently only works if there is only one figure/chunk
#Doesn't work with FramedPlots
#Doesn't work with Julia 0.4
#Doesn"t work with FramedPlots
#Doesn"t work with Julia 0.4
function savefigs_winston(chunk)
fignames = String[]
ext = report.formatdict["figfmt"]
ext = report.formatdict[:figfmt]
figpath = joinpath(report.cwd, report.figdir)
isdir(figpath) || mkdir(figpath)
chunkid = get(chunk,:name,chunk[:number])#((chunk[:name] == nothing) ? chunk[:number] : chunk[:name])
chunkid = chunk[:name] == nothing ? chunk[:number] : chunk[:name]
#println(Winston._display.figs)
#println(Winston._display.fig_order)
@ -184,7 +177,7 @@ function savefigs_winston(chunk)
for fig = copy(Winston._display.fig_order)
full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext")
rel_name = "$(report.figdir)/$(report.basename)_$(chunkid)_$fig$ext" #Relative path is used in output
@show rel_name
#@show rel_name
#figure(fig) #Calling figure clears the canvas!
#savefig(Winston._display.figs[gcf()].plot, full_name) #Produces empty figures
savefig(full_name)
@ -199,6 +192,7 @@ export weave
include("config.jl")
include("readers.jl")
include("formatters.jl")
end

177
src/formatters.jl Normal file
View File

@ -0,0 +1,177 @@
#Format the executed document
function format(executed, doctype)
formatted = String[]
docformat = formats[doctype]
@show docformat
formatdict = docformat.formatdict
get!(formatdict, :termstart, formatdict[:codestart])
get!(formatdict, :termend, formatdict[:codeend])
for chunk in copy(executed)
if chunk[:type] == "doc"
push!(formatted, chunk[:content])
else
#Format code
result = format_codechunk(chunk, formatdict)
#Handle figures
if chunk[:fig] && length(chunk[:figure]) > 0
if chunk[:include]
result *= formatfigures(chunk, docformat)
end
end
push!(formatted, result)
end
end
return formatted
end
function format_codechunk(chunk, formatdict)
if !chunk[:evaluate]
if chunk[:echo]
result = "$(formatdict[:codestart])$(chunk[:content])$(formatdict[:codeend])"
return result
else
r = ""
return r
end
end
if chunk[:term]
result = format_termchunk(chunk, formatdict)
else
if chunk[:echo]
result = "$(formatdict[:codestart])$(chunk[:content])\n$(formatdict[:codeend])\n"
else
result = ""
end
if (strip(chunk[:result])!= "") && (chunk[:results] != "hidden")
#@show chunk
if chunk[:results] != "verbatim"
haskey(formatdict, :indent) && (chunk[:result] = indent(chunk[:result]))
result *= "$(chunk[:result])"
elseif chunk[:results] == "verbatim"
result *= "$(formatdict[:outputstart])$(chunk[:result])\n$(formatdict[:outputend])\n"
end
end
end
return result
end
function format_termchunk(chunk, formatdict)
if chunk[:echo] && chunk[:results] != "hidden"
haskey(formatdict, :termindent) && (chunk[:result] = indent(chunk[:result]))
result = "$(formatdict[:termstart])$(chunk[:result])\n$(formatdict[:termend])\n"
else
result = ""
end
return result
end
function indent(text, nindent)
return text
end
type Tex
formatdict::Dict{Symbol,Any}
end
const tex = Tex(@compat Dict{Symbol,Any}(:codestart => "\\begin{juliacode}",
:codeend => "\\end{juliacode}",
:outputstart => "\\begin{juliaout}",
:outputend => "\\end{juliaout}",
:figfmt => ".pdf",
:extension =>"tex",
:width => "\\linewidth",
:doctype => "tex"
))
type Pandoc
formatdict::Dict{Symbol,Any}
end
const pandoc = Pandoc(@compat Dict{Symbol,Any}(:codestart => "~~~~{.julia}",
:codeend=>"~~~~~~~~~~~~~\n\n",
:outputstart=>"~~~~{.julia}",
:outputend=>"~~~~~~~~~~~~~\n\n",
:figfmt=>".png",
:extension=>"md",
:width=>"15 cm",
:doctype=>"pandoc"
))
function formatfigures(chunk, docformat::Tex)
fignames = chunk[:figure]
caption = chunk[:caption]
width = get!(chunk, :width, docformat.formatdict[:width])
f_pos = chunk[:f_pos]
f_env = chunk[:f_env]
result = ""
figstring = ""
if f_env != nothing
result *= """\\begin{$f_env}\n"""
end
for fig = fignames
figstring *= "\\includegraphics[width= $width]{$fig}\n"
end
# Figure environment
if caption != nothing
result *= string("\\begin{figure}[$f_pos]\n",
"\\center\n",
"$figstring",
"\\caption{$caption}\n")
if chunk[:name] != nothing
label = chunk[:name]
result *= "\label{fig:$label}\n"
result *= "\\end{figure}\n"
end
else
result *= figstring
end
if f_env != nothing
result += "\\end{$f_env}\n"
end
return result
end
function formatfigures(chunk, docformat::Pandoc)
fignames = chunk[:figure]
caption = chunk[:caption]
width = get!(chunk, :width, docformat.formatdict[:width])
result = ""
figstring = ""
length(fignames) > 0 || (return "")
if caption != false
result *= "![$caption]($(fignames[1]))\n"
for fig = fignames[2:end]
result *= "![]($fig)\n"
println("Warning, only the first figure gets a caption\n")
end
else
for fig in fignames
result *= "![]($fig)\n"
end
end
return result
end
#Add new supported formats here
const formats = @compat Dict{String, Any}("tex" => tex,
"pandoc" => pandoc)