diff --git a/README.md b/README.md index f8f4d5a..a8bba5f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # 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. @@ -34,7 +33,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): diff --git a/examples/gadfly_sample.mdw b/examples/gadfly_sample.mdw new file mode 100644 index 0000000..b66b368 --- /dev/null +++ b/examples/gadfly_sample.mdw @@ -0,0 +1,14 @@ +# Gadfly + +<>= +using Gadfly +x = linspace(0, 2π, 200) +plot(x=x, y = sin(x), Geom.line) +y = 20 +plot(x=x, y = cos(x), Geom.line) +@ + +<<>>= +x = linspace(0, 200); +println(x) +@ diff --git a/src/JuliaReport.jl b/src/JuliaReport.jl index 756faec..0dbb68a 100644 --- a/src/JuliaReport.jl +++ b/src/JuliaReport.jl @@ -1,9 +1,9 @@ module JuliaReport using Compat +import Base: display #Contains report global properties -#Similar to pweave.PwebProcessor -type Report +type Report <: Display source::String documentationmode::Bool cwd::String @@ -11,9 +11,32 @@ type Report formatdict pending_code::String figdir::String + executed::Array + fignum::Int + figures::Array + + function Report() + new("", false, "", "", Any[], "", "", {}, 1, {}) + end + +end + +const report = Report() + +const supported_mime_types = + { + MIME"image/png", + MIME"text/plain" } + +function display(doc::Report, data) + for m in supported_mime_types + if mimewritable(m(), data) + display(doc, m(), data) + brea + end + end end -const report = Report("", false, "", "", Any[], "", "") #function listformats() #pweave.listformats() TODO: implement @@ -37,6 +60,7 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", report.basename = basename report.figdir = figdir report.formatdict = formatdict + pushdisplay(report) if plotlib == nothing rcParams[:chunk][:defaultoptions][:fig] = false @@ -48,17 +72,24 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", elseif l_plotlib == "pyplot" eval(Expr(:using, :PyPlot)) rcParams[:plotlib] = "PyPlot" + elseif l_plotlib == "gadfly" + println("GadFly") + eval(parse("""include(Pkg.dir("JuliaReport","src","gadfly.jl"))""")) + rcParams[:plotlib] = "gadfly" + #pushdisplay(doc) end end parsed = read_noweb(source) executed = run(parsed) + popdisplay(report) formatted = format(executed, doctype) outname = "$(report.cwd)/$(report.basename).$(formatdict[:extension])" @show outname open(outname, "w") do io write(io, join(formatted, "\n")) end + info("Report weaved to $(report.basename).$(formatdict[:extension])") end @@ -76,7 +107,8 @@ function run_block(code_str) while pos < n oldpos = pos code, pos = parse(code_str, pos) - eval(ReportSandBox, code) + s = eval(ReportSandBox, code) + s != nothing && display(s) end redirect_stdout(oldSTDOUT) @@ -100,9 +132,12 @@ function run_term(code_str) 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)]))) s = eval(ReportSandBox, code) - s == nothing || (smime = reprmime(MIME("text/plain"), s)) #display(s) - println(smime) + s != nothing && display(s) + smime = nothing + s == nothing || (smime = reprmime(MIME("text/plain"), s)) + smime == nothing || println(smime) end redirect_stdout(oldSTDOUT) @@ -152,6 +187,8 @@ function savefigs(chunk) return savefigs_pyplot(chunk) elseif l_plotlib == "winston" return savefigs_winston(chunk) + elseif l_plotlib == "gadfly" + return String[] end end @@ -202,6 +239,14 @@ function savefigs_winston(chunk) end + +function display(report::Report, m::MIME"text/plain", data) + s = reprmime(m, data) + push!(report.executed, s) +end + + + export weave include("config.jl") diff --git a/src/gadfly.jl b/src/gadfly.jl new file mode 100644 index 0000000..e1203ae --- /dev/null +++ b/src/gadfly.jl @@ -0,0 +1,19 @@ +using Gadfly + +import Base: start, next, done, display, writemime + + +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) + doc.fignum += 1 + out = open(filename, "w") + writemime(out, m, data) + close(out) + + push!(doc.figures, filename) + push!(doc.executed, filename) +end