2014-12-08 09:25:57 +01:00
|
|
|
module Weave
|
2014-12-01 19:31:44 +01:00
|
|
|
using Compat
|
2014-11-24 23:08:25 +01:00
|
|
|
|
2014-11-25 00:10:48 +01:00
|
|
|
#Contains report global properties
|
2014-12-03 14:41:53 +01:00
|
|
|
type Report <: Display
|
2016-04-11 17:40:18 +02:00
|
|
|
cwd::AbstractString
|
|
|
|
basename::AbstractString
|
2015-01-06 23:01:25 +01:00
|
|
|
formatdict::Dict{Symbol,Any}
|
2016-04-11 17:40:18 +02:00
|
|
|
pending_code::AbstractString
|
|
|
|
cur_result::AbstractString
|
2014-12-03 14:41:53 +01:00
|
|
|
fignum::Int
|
2016-04-11 17:40:18 +02:00
|
|
|
figures::Array{AbstractString}
|
2014-12-05 10:28:55 +01:00
|
|
|
term_state::Symbol
|
2015-01-04 14:45:41 +01:00
|
|
|
cur_chunk
|
2015-01-08 18:35:47 +01:00
|
|
|
end
|
2014-12-05 10:28:55 +01:00
|
|
|
|
2015-01-08 18:35:47 +01:00
|
|
|
function Report(cwd, basename, formatdict)
|
2016-04-11 17:40:18 +02:00
|
|
|
Report(cwd, basename, formatdict, "", "", 1, AbstractString[], :text, nothing)
|
2014-12-03 14:41:53 +01:00
|
|
|
end
|
|
|
|
|
2015-01-08 18:35:47 +01:00
|
|
|
|
2015-01-05 23:55:57 +01:00
|
|
|
#const report = Report()
|
2014-12-03 14:41:53 +01:00
|
|
|
|
|
|
|
const supported_mime_types =
|
2014-12-03 23:01:38 +01:00
|
|
|
[MIME"image/png",
|
|
|
|
MIME"text/plain"]
|
2014-12-03 14:41:53 +01:00
|
|
|
|
2014-12-04 20:54:56 +01:00
|
|
|
function Base.display(doc::Report, data)
|
2014-12-03 14:41:53 +01:00
|
|
|
for m in supported_mime_types
|
|
|
|
if mimewritable(m(), data)
|
|
|
|
display(doc, m(), data)
|
|
|
|
brea
|
|
|
|
end
|
|
|
|
end
|
2014-11-25 00:10:48 +01:00
|
|
|
end
|
|
|
|
|
2016-04-19 14:23:48 +02:00
|
|
|
"""
|
2016-04-20 10:16:50 +02:00
|
|
|
`list_out_formats()`
|
2016-04-19 14:23:48 +02:00
|
|
|
|
|
|
|
List supported output formats
|
|
|
|
"""
|
2014-12-05 22:33:54 +01:00
|
|
|
function list_out_formats()
|
|
|
|
for format = keys(formats)
|
2016-04-12 16:35:12 +02:00
|
|
|
println(string(format,": ", formats[format].description))
|
2014-12-05 22:33:54 +01:00
|
|
|
end
|
|
|
|
end
|
2014-12-03 08:21:56 +01:00
|
|
|
|
2014-11-25 00:10:48 +01:00
|
|
|
|
2016-04-19 14:23:48 +02:00
|
|
|
"""
|
2016-04-20 10:16:50 +02:00
|
|
|
`tangle(source ; out_path=:doc, informat="noweb")`
|
2016-04-19 14:23:48 +02:00
|
|
|
|
|
|
|
Tangle source code from input document to .jl file.
|
2015-01-01 22:36:58 +01:00
|
|
|
|
|
|
|
* `informat`: `"noweb"` of `"markdown"`
|
|
|
|
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory,
|
2016-04-11 17:40:18 +02:00
|
|
|
`"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"`
|
2016-04-19 14:23:48 +02:00
|
|
|
"""
|
2014-12-21 08:25:45 +01:00
|
|
|
function tangle(source ; out_path=:doc, informat="noweb")
|
2015-01-06 23:01:25 +01:00
|
|
|
doc = read_doc(source, informat)
|
|
|
|
cwd = get_cwd(doc, out_path)
|
2015-01-01 22:36:58 +01:00
|
|
|
|
2015-01-07 11:20:48 +01:00
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
outname = "$(cwd)/$(doc.basename).jl"
|
2014-12-21 08:25:45 +01:00
|
|
|
open(outname, "w") do io
|
2015-01-06 23:01:25 +01:00
|
|
|
for chunk in doc.chunks
|
2015-01-05 15:35:24 +01:00
|
|
|
if typeof(chunk) == CodeChunk
|
|
|
|
write(io, chunk.content*"\n")
|
2014-12-21 08:25:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-07 11:20:48 +01:00
|
|
|
info("Writing to file $(doc.basename).jl")
|
2014-12-21 08:25:45 +01:00
|
|
|
end
|
2014-12-03 08:21:56 +01:00
|
|
|
|
2016-04-19 14:23:48 +02:00
|
|
|
"""
|
2016-04-20 10:16:50 +02:00
|
|
|
`weave(source ; doctype = "pandoc", plotlib="Gadfly",
|
|
|
|
informat="noweb", out_path=:doc, fig_path = "figures", fig_ext = nothing)`
|
2016-04-19 14:23:48 +02:00
|
|
|
|
|
|
|
Weave an input document to output file.
|
2015-01-01 22:32:15 +01:00
|
|
|
|
|
|
|
* `doctype`: see `list_out_formats()`
|
2016-04-19 15:38:03 +02:00
|
|
|
* `plotlib`: `"PyPlot"`, `"Gadfly"` or `nothing`
|
2015-01-01 22:32:15 +01:00
|
|
|
* `informat`: `"noweb"` of `"markdown"`
|
2015-01-01 22:36:58 +01:00
|
|
|
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory,
|
2016-04-19 15:47:53 +02:00
|
|
|
`"somepath"`: Path as a String e.g `"/home/mpastell/weaveout"`
|
2015-01-01 22:32:15 +01:00
|
|
|
* `fig_path`: where figures will be generated, relative to out_path
|
|
|
|
* `fig_ext`: Extension for saved figures e.g. `".pdf"`, `".png"`. Default setting depends on `doctype`.
|
2015-01-08 17:52:42 +01:00
|
|
|
* `cache_path`: where of cached output will be saved.
|
|
|
|
* `cache`: controls caching of code: `:off` = no caching, `:all` = cache everything,
|
|
|
|
`:user` = cache based on chunk options, `:refresh`, run all code chunks and save new cache.
|
2015-01-01 22:32:15 +01:00
|
|
|
|
|
|
|
**Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
2016-04-19 14:23:48 +02:00
|
|
|
"""
|
2015-01-06 23:01:25 +01:00
|
|
|
function weave(source ; doctype = "pandoc", plotlib="Gadfly",
|
2015-01-07 11:20:48 +01:00
|
|
|
informat="noweb", out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
2015-01-08 17:52:42 +01:00
|
|
|
cache_path = "cache", cache=:off)
|
2014-11-26 22:29:25 +01:00
|
|
|
|
2016-04-20 10:16:50 +02:00
|
|
|
doc = read_doc(source, informat)
|
2015-01-06 23:01:25 +01:00
|
|
|
doc = run(doc, doctype = doctype, plotlib=plotlib,
|
2015-01-07 11:20:48 +01:00
|
|
|
informat = informat, out_path=out_path,
|
2015-01-08 17:52:42 +01:00
|
|
|
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache)
|
2015-01-06 23:01:25 +01:00
|
|
|
formatted = format(doc)
|
2014-12-01 20:23:35 +01:00
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
outname = "$(doc.cwd)/$(doc.basename).$(doc.format.formatdict[:extension])"
|
2014-12-02 19:42:12 +01:00
|
|
|
open(outname, "w") do io
|
|
|
|
write(io, join(formatted, "\n"))
|
|
|
|
end
|
2014-12-03 14:41:53 +01:00
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
info("Report weaved to $(doc.basename).$(doc.format.formatdict[:extension])")
|
2014-11-25 00:10:48 +01:00
|
|
|
end
|
|
|
|
|
2014-12-05 22:13:44 +01:00
|
|
|
|
2014-12-04 20:54:56 +01:00
|
|
|
function Base.display(report::Report, m::MIME"text/plain", data)
|
2015-01-10 19:10:40 +01:00
|
|
|
s = reprmime(m, data)
|
2015-01-11 22:38:58 +01:00
|
|
|
print("\n" * s)
|
|
|
|
#report.cur_result *= "\n" * s
|
2014-12-03 14:41:53 +01:00
|
|
|
end
|
|
|
|
|
2016-04-11 17:40:18 +02:00
|
|
|
function weave(doc::AbstractString, doctype::AbstractString)
|
2015-01-08 23:05:46 +01:00
|
|
|
weave(doc, doctype=doctype)
|
|
|
|
end
|
2014-12-03 14:41:53 +01:00
|
|
|
|
2016-04-20 10:16:50 +02:00
|
|
|
export weave, list_out_formats, tangle,
|
|
|
|
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults
|
2015-01-04 14:45:41 +01:00
|
|
|
|
2014-11-26 22:29:25 +01:00
|
|
|
include("config.jl")
|
2015-01-07 15:37:02 +01:00
|
|
|
include("chunks.jl")
|
2014-11-26 22:29:25 +01:00
|
|
|
include("readers.jl")
|
2015-01-07 15:37:02 +01:00
|
|
|
include("run.jl")
|
|
|
|
include("cache.jl")
|
2014-12-02 15:57:00 +01:00
|
|
|
include("formatters.jl")
|
2014-11-25 00:10:48 +01:00
|
|
|
end
|