diff --git a/README.md b/README.md index a8bba5f..83dad13 100644 --- a/README.md +++ b/README.md @@ -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. @@ -10,7 +11,7 @@ and Sweave. * Noweb syntax for documents. * Execute code as terminal or "script" chunks. -* Capture PyPlot or Winston figures. +* Capture PyPlot, Gadfly or Winston figures. * Supports latex and pandoc markdown output **Not implemented** @@ -33,11 +34,15 @@ 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): +Using Winston for plots (Julia 0.3 only): weave(Pkg.dir("JuliaReport","examples","winston_sample.mdw"), plotlib="Winston", doctype="pandoc") +Using Gadfly (Julia 0.3 only): + + weave(Pkg.dir("JuliaReport","examples","gadfly_sample.mdw"), plotlib="Gadfly") + diff --git a/examples/winston_sample.mdw b/examples/winston_sample.mdw index 13cb2cb..fa98a28 100644 --- a/examples/winston_sample.mdw +++ b/examples/winston_sample.mdw @@ -25,18 +25,19 @@ y = [2, 4 ,8] The figures and code can be included in the output. -<<>>= +<>= using Winston t = linspace(0, 2*pi, 100) -plot(t, sinc(t)) -xlabel("x") -ylabel("sinc(x)") +p = plot(t, sinc(t)) @ You can also include a plot with caption and hide the code: <>= -plot(cumsum(randn(1000, 1))) +p = plot(cumsum(randn(1000, 1))) +xlabel("x") +ylabel("sinc(x)") +display(p) @ <<>>= @@ -44,7 +45,6 @@ x = linspace(0, 3pi, 100) c = cos(x) s = sin(x) -figure() p = FramedPlot( title="title!", xlabel="\\Sigma x^2_i", @@ -53,4 +53,5 @@ p = FramedPlot( add(p, FillBetween(x, c, x, s)) add(p, Curve(x, c, color="red")) add(p, Curve(x, s, color="blue")) +display(p) @ diff --git a/examples/winston_sample.pdf b/examples/winston_sample.pdf index 35f4495..7dac669 100644 Binary files a/examples/winston_sample.pdf and b/examples/winston_sample.pdf differ diff --git a/src/JuliaReport.jl b/src/JuliaReport.jl index 6429e0b..cf99534 100644 --- a/src/JuliaReport.jl +++ b/src/JuliaReport.jl @@ -61,7 +61,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 @@ -74,13 +74,12 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", 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 + pushdisplay(report) parsed = read_noweb(source) executed = run(parsed) popdisplay(report) @@ -98,24 +97,32 @@ end function run_block(code_str) oldSTDOUT = STDOUT + result = "" + + rw, wr = redirect_stdout() #If there is nothing to read code will hang println() - rw, wr = redirect_stdout() - #include_string(code_str) - n = length(code_str) - pos = 2 #The first character is extra line end - while pos < n - oldpos = pos - code, pos = parse(code_str, pos) - s = eval(ReportSandBox, code) - s != nothing && display(s) + + try + n = length(code_str) + pos = 2 #The first character is extra line end + while pos < n + oldpos = pos + code, pos = parse(code_str, pos) + s = eval(ReportSandBox, code) + if rcParams[:plotlib] == "Gadfly" + s != nothing && display(s) + end + end + finally + + redirect_stdout(oldSTDOUT) + close(wr) + result = readall(rw) + close(rw) end - redirect_stdout(oldSTDOUT) - close(wr) - result = readall(rw) - close(rw) return string("\n", result) end diff --git a/src/winston.jl b/src/winston.jl index 8e4e91f..327b585 100644 --- a/src/winston.jl +++ b/src/winston.jl @@ -12,7 +12,7 @@ function display(report::Report, m::MIME"image/png", data) 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] + report.cur_result *= "\n\n" * report.formatdict[:codestart] * "\n" chunk[:figure] = String[] else push!(report.figures, rel_name)