Implemented noweb reader in Julia

Causes changes to chunk option formatting. Updated sample accordingly.
pull/7/head
Matti Pastell 2014-11-26 23:29:25 +02:00
parent 6780641e11
commit e5187af15f
4 changed files with 116 additions and 15 deletions

View File

@ -3,7 +3,7 @@
This a sample [Julia](http://julialang.org/) noweb document that can
be executed using JuliaReport. Output from code chunks and PyPlot
plots will be included in the weaved document. You also need to install Pweave from Github in order to use JuliaReport.
plots will be included in the weaved document. You also need to install Pweave from Github in order to use JuliaReport.
This documented can be turned into Pandoc markdown with captured
result from Julia prompt.
@ -15,7 +15,7 @@ weave("examples/julia_sample.mdw")
## Terminal chunk
<<term=True>>=
<<"term"=>true>>=
x = 1:10
d = {"juliareport" => "testing"}
y = [2, 4 ,8]
@ -35,6 +35,6 @@ ylabel("sinc(x)")
You can also include a plot with caption and hide the code:
<<echo=False, caption="Random walk.", label="random">>=
<<"echo"=>false, "caption"=>"Random walk.", "label"=>"random">>=
plot(cumsum(randn(1000, 1)))
@

View File

@ -1,9 +1,8 @@
module JuliaReport
using PyCall
using PyPlot
@pyimport pweave #Output formatting uses Pweave
#Parsing and formatting uses Pweave
@pyimport pweave
#Contains report global properties
#Similar to pweave.PwebProcessor
@ -18,7 +17,7 @@ type Report
end
global report = Report("", false, "", "", {}, "", "")
global const report = Report("", false, "", "", {}, "", "")
function listformats()
pweave.listformats()
@ -28,19 +27,27 @@ function weave(source ; doctype = "pandoc", informat="noweb", figdir = "figures"
pweave.rcParams["chunk"]["defaultoptions"]["engine"] = "julia"
doc = pweave.Pweb(source, doctype, shell="julia")
doc[:setreader](informat)
doc[:parse]()
#doc[:setreader](informat)
#doc[:parse]()
#Julia version of doc.run()
cwd, fname = splitdir(abspath(source))
basename = splitext(fname)[1]
formatdict = doc[:formatter][:getformatdict]()
figformat == nothing || (formatdict["figfmt"] = figformat)
#println(formatdict["figfmt"])
global report = Report(source, false, cwd, basename, formatdict, "", figdir)
#report = Report(source, false, cwd, basename, formatdict, "", figdir)
report.source = source
report.cwd = cwd
report.basename = basename
report.figdir = figdir
report.formatdict = formatdict
parsed = read_noweb(source)
doc[:executed] = run(parsed)
#Formatting with pweave
doc[:executed] = run(PyVector(doc["parsed"]))
#doc[:executed] = run(PyVector(doc["parsed"]))
doc[:isexecuted] = true
doc[:format]()
doc[:write]()
@ -93,11 +100,19 @@ function run(parsed)
if chunk["type"] == "code"
#print(chunk["content"])
info("""Weaving chunk $(chunk["number"]) from line $(chunk["start_line"])""")
defaults = copy(pweave.rcParams["chunk"]["defaultoptions"])
merge!(defaults, chunk["options"])
merge!(chunk, defaults)
chunk["evaluate"] || (chunk["result"] = ""; continue) #Do nothing if eval is false
defaults = copy(rcParams["chunk"]["defaultoptions"])
options = copy(chunk["options"])
try
options = merge(rcParams["chunk"]["defaultoptions"], options)
catch
options = rcParams["chunk"]["defaultoptions"]
warn(string("Invalid format for chunk options line: ", chunk["start_line"]))
end
merge!(chunk, options)
chunk["evaluate"] || (chunk["result"] = ""; continue) #Do nothing if eval is false
if chunk["term"]
chunk["result"] = run_term(chunk["content"])
else
@ -134,4 +149,6 @@ end
export weave
include("config.jl")
include("readers.jl")
end

24
src/config.jl Normal file
View File

@ -0,0 +1,24 @@
rcParams = {"figdir"=> "figures",
"usematplotlib"=> true,
"storeresults"=> false,
"cachedir"=> "cache",
"chunk"=> {"defaultoptions"=> {
"echo"=> true,
"results"=> "verbatim",
"fig"=> true,
"include"=> true,
"evaluate"=> true,
"caption"=> false,
"term"=> false,
"name"=> nothing,
"wrap"=> true,
"f_pos"=> "htpb",
"f_size"=> (8, 6),
"f_env"=> nothing,
"f_spines"=> true,
"complete"=> true,
"engine"=> "julia",
"option_string"=> ""
}
}
}

60
src/readers.jl Normal file
View File

@ -0,0 +1,60 @@
function read_noweb(document)
doctext = readall(open(document))
#doctext = document #Replace with file...
codestart = r"^<<(.*?)>>="
codeend = r"^@(\s*)$"
state = "doc"
docno = 1
codeno = 1
content = ""
lineno = 0
start_line = 0
options = Dict()
optionstring = ""
parsed = Dict[]
for (lineno, line) in enumerate(split(doctext, "\n"))
if ismatch(codestart, line) && state=="doc"
state = "code"
m = match(codestart, line)
optionstring=m.captures[1]
#println(m.captures[1])
if strip(optionstring)==""
options = Dict()
else
options = eval(parse("{" * optionstring * "}"))
end
haskey(options, "label") && (options["name"] = options["label"])
haskey(options, "name") || (options["name"] = nothing)
chunk = {"type" => "doc", "content"=> content, "number" => docno, "start_line"=>start_line}
docno += 1
start_line = lineno
push!(parsed, chunk)
content = ""
continue
end
if ismatch(codeend, line) && state=="code"
chunk = {"type" => "code", "content" => content, "number" => codeno,
"options"=>options,"optionstring"=>optionstring, "start_line"=>start_line}
codeno+=1
start_line = lineno
content = ""
state = "doc"
push!(parsed, chunk)
continue
end
content *= "\n" * line
end
#Remember the last chunk
if content != ""
chunk = {"type" => "doc", "content"=> content, "number" => docno, "start_line"=>lineno}
push!(parsed, chunk)
end
return parsed
end