Initial Gadfly support

pull/17/head
Matti Pastell 2014-12-03 23:34:29 +02:00
parent 6c540a4cec
commit 3830ef9e54
6 changed files with 65 additions and 27 deletions

View File

@ -1,6 +1,7 @@
# JuliaReport
[![Build Status](https://travis-ci.org/mpastell/JuliaReport.jl.svg?branch=master)](https://travis-ci.org/mpastell/JuliaReport.jl)
JuliaReport is a scientific report generator/literate programming tool
for Julia. It resembles [Pweave](http://mpastell.com/pweave) and, Knitr
and Sweave.
@ -33,7 +34,7 @@ using ";" and need to be valid Julia expressions. e.g.
Run from julia:
using JuliaReport
weave(Pkg.dir("JuliaReport","examples","julia_sample.mdw"))
weave(Pkg.dir("JuliaReport","examples","julia_sample.mdw")
Or using Winston for plots (Julia 0.3 only):

View File

@ -1 +1,2 @@
Compat
Compat
TextWrap

View File

@ -12,11 +12,13 @@ type Report <: Display
pending_code::String
figdir::String
executed::Array
cur_result::String
fignum::Int
figures::Array
cur_chunk::Dict
function Report()
new("", false, "", "", Any[], "", "", {}, 1, {})
new("", false, "", "", Any[], "", "", {}, "", 1, {}, @compat Dict{Symbol, Any}())
end
end
@ -75,7 +77,7 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb",
elseif l_plotlib == "gadfly"
println("GadFly")
eval(parse("""include(Pkg.dir("JuliaReport","src","gadfly.jl"))"""))
rcParams[:plotlib] = "gadfly"
rcParams[:plotlib] = "Gadfly"
#pushdisplay(doc)
end
end
@ -119,11 +121,6 @@ function run_block(code_str)
end
function run_term(code_str)
oldSTDOUT = STDOUT
#If there is nothing to read code will hang
println()
rw, wr = redirect_stdout()
#Emulate terminal
n = length(code_str)
@ -131,20 +128,15 @@ function run_term(code_str)
while pos < n
oldpos = pos
code, pos = parse(code_str, pos)
println(string("\njulia> ", rstrip(code_str[oldpos:(pos-1)])))
display(string("\njulia> ", rstrip(code_str[oldpos:(pos-1)])))
prompts = string("\n\njulia> ", rstrip(code_str[oldpos:(pos-1)]), "\n")
report.cur_result *= prompts
s = eval(ReportSandBox, code)
s != nothing && display(s)
smime = nothing
s == nothing || (smime = reprmime(MIME("text/plain"), s))
smime == nothing || println(smime)
end
redirect_stdout(oldSTDOUT)
close(wr)
result = readall(rw)
close(rw)
return string(result)
return string(report.cur_result)
end
@ -167,13 +159,22 @@ function run(parsed)
delete!(chunk, :options)
chunk[:evaluate] || (chunk[:result] = ""; continue) #Do nothing if eval is false
report.fignum = 1
report.cur_result = ""
report.figures = String[]
report.cur_chunk = chunk
if chunk[:term]
chunk[:result] = run_term(chunk[:content])
else
chunk[:result] = run_block(chunk[:content])
end
chunk[:fig] && (chunk[:figure] = savefigs(chunk))
if rcParams[:plotlib] == "Gadfly"
chunk[:fig] && (chunk[:figure] = copy(report.figures))
else
chunk[:fig] && (chunk[:figure] = savefigs(chunk))
end
end
parsed[i] = copy(chunk)
i += 1
@ -242,6 +243,7 @@ end
function display(report::Report, m::MIME"text/plain", data)
s = reprmime(m, data)
report.cur_result *= s
push!(report.executed, s)
end

View File

@ -1,3 +1,4 @@
using TextWrap
#Format the executed document
function format(executed, doctype)
@ -9,9 +10,11 @@ function format(executed, doctype)
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
@ -29,6 +32,8 @@ end
function format_codechunk(chunk, formatdict)
if !chunk[:evaluate]
if chunk[:echo]
result = "$(formatdict[:codestart])$(chunk[:content])$(formatdict[:codeend])"
@ -42,6 +47,10 @@ function format_codechunk(chunk, formatdict)
if chunk[:term]
result = format_termchunk(chunk, formatdict)
else
if chunk[:wrap]
chunk[:result] = "\n" * wrap(chunk[:result])
end
if chunk[:echo]
result = "$(formatdict[:codestart])$(chunk[:content])\n$(formatdict[:codeend])\n"
else
@ -178,7 +187,7 @@ function formatfigures(chunk, docformat::Pandoc)
end
else
for fig in fignames
result *= "![]($fig)\n"
result *= "![]($fig)\\\n"
end
end
return result

View File

@ -8,12 +8,37 @@ Gadfly.set_default_plot_format(:png)
function display(doc::Report, m::MIME"image/png", data)
filename = @sprintf("%s_figure%d.png", doc.basename, doc.fignum)
figpath = joinpath(report.cwd, report.figdir)
isdir(figpath) || mkdir(figpath)
ext = report.formatdict[:figfmt]
fig = doc.fignum
chunk = report.cur_chunk
chunkid = (chunk[:name] == nothing) ? chunk[:number] : chunk[:name]
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
docformat = formats[report.formatdict[:doctype]]
if chunk[:term]
chunk[:figure] = [rel_name]
report.cur_result *= "\n" * report.formatdict[:codeend]
report.cur_result *= formatfigures(chunk, docformat)
report.cur_result *= "\n\n" * report.formatdict[:codestart]
chunk[:figure] = String[]
else
push!(doc.figures, rel_name)
end
doc.fignum += 1
out = open(filename, "w")
out = open(full_name, "w")
writemime(out, m, data)
close(out)
push!(doc.figures, filename)
push!(doc.executed, filename)
#push!(doc.executed, filename)
end

View File

@ -5,7 +5,7 @@ function read_noweb(document)
lines = split(bytestring(open(document) do io
mmap_array(Uint8,(filesize(document),),io)
end), "\n")
#doctext = document #Replace with file...
codestart = r"^<<(.*?)>>="
codeend = r"^@(\s*)$"
state = "doc"