From 92a5a372c4cf2c26128cbd9d694e6e68476efd40 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Wed, 20 Apr 2016 21:31:55 +0300 Subject: [PATCH] Updates to gadfly sample --- examples/gadfly_sample.mdw | 43 +++++++++++++++++++++++++++++++------- src/readers.jl | 12 +++++------ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/examples/gadfly_sample.mdw b/examples/gadfly_sample.mdw index 64a447a..f3d94c9 100644 --- a/examples/gadfly_sample.mdw +++ b/examples/gadfly_sample.mdw @@ -2,23 +2,52 @@ % Matti Pastell % 20th April 2016 +# Introduction +This a sample [Julia](http://julialang.org/) noweb document that can +be executed using [Weave.jl](https://github.com/mpastell/Weave.jl). +The code is delimited from docs using `<<>>=` and `@` markup which can be seen +looking at the source document [gadfly_sample.mdw] in the examples directory +of the package. The source document can be executed and the results with Gadfly +plots are captured in the resulting file. + +You can create markdown output or pdf and HTML directly (with Pandoc) using +the weave command as follows: ```julia using Weave -weave(Pkg.dir("Weave","examples","gadfly_sample.mdw"), out_path = :pwd) +#Markdown +weave(Pkg.dir("Weave","examples","gadfly_sample.mdw"), + out_path = :pwd, doctype = "pandoc") +#HTML +weave(Pkg.dir("Weave","examples","gadfly_sample.mdw"), + out_path = :pwd, doctype = "md2html") +#pdf +weave(Pkg.dir("Weave","examples","gadfly_sample.mdw"), + out_path = :pwd, doctype = "md2pdf") ``` +*The documents will be written to the Julia working directory when you +use the `out_path = :pwd`.* + +# Capturing code + +The basic code chunk will be run with default options and the code and +output will be captured. + +<<>>= +Using Gadfly +x = linspace(0, 2*pi) +println(x) +plot(x = x, y = sin(x)) +@ + + + <>= -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/readers.jl b/src/readers.jl index 9b6c2f0..5b91ac2 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -34,21 +34,21 @@ function parse_doc(document::AbstractString, format="noweb"::AbstractString) start_line = 0 options = Dict() - optionAbstractString = "" + optionString = "" parsed = Any[] for lineno in 1:length(lines) line = lines[lineno] if (m = match(codestart, line)) != nothing && state=="doc" state = "code" if m.captures[1] == nothing - optionAbstractString = "" + optionString = "" else - optionAbstractString=strip(m.captures[1]) + optionString=strip(m.captures[1]) end #@show optionAbstractString options = Dict{Symbol,Any}() - if length(optionAbstractString) > 0 - expr = parse(optionAbstractString) + 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 @@ -67,7 +67,7 @@ function parse_doc(document::AbstractString, format="noweb"::AbstractString) end if ismatch(codeend, line) && state=="code" - chunk = CodeChunk(content, codeno, start_line, optionAbstractString, options) + chunk = CodeChunk(content, codeno, start_line, optionString, options) #chunk = @compat Dict{Symbol,Any}(:type => "code", :content => content, # :number => codeno, :options => options, # :optionAbstractString => optionAbstractString,