Weave.jl/src/Weave.jl

125 lines
4.2 KiB
Julia
Raw Normal View History

module Weave
2014-11-25 00:10:48 +01:00
"""
`list_out_formats()`
List supported output formats
"""
2014-12-05 22:33:54 +01:00
function list_out_formats()
for format = keys(formats)
println(string(format,": ", formats[format].description))
2014-12-05 22:33:54 +01:00
end
end
2014-11-25 00:10:48 +01:00
"""
`tangle(source ; out_path=:doc, informat="noweb")`
Tangle source code from input document to .jl file.
2015-01-01 22:36:58 +01:00
* `informat`: `"noweb"` of `"markdown"`
2016-04-24 14:02:03 +02:00
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, `"somepath"`, directory name as a string e.g `"/home/mpastell/weaveout"`
or filename as string e.g. ~/outpath/outfile.jl.
"""
function tangle(source ; out_path=:doc, informat=:auto)
doc = read_doc(source, informat)
2016-04-24 14:02:03 +02:00
doc.cwd = get_cwd(doc, out_path)
2015-01-01 22:36:58 +01:00
2016-04-24 14:02:03 +02:00
outname = get_outname(out_path, doc, ext = "jl")
2016-04-24 14:02:03 +02:00
open(outname, "w") do io
for chunk in doc.chunks
if typeof(chunk) == CodeChunk
options = merge(rcParams[:chunk_defaults], chunk.options)
if options[:tangle]
write(io, chunk.content*"\n")
end
end
end
end
2016-04-24 14:02:03 +02:00
doc.cwd == pwd() && (outname = basename(outname))
info("Writing to file $outname")
end
2016-04-24 14:02:03 +02:00
"""
2016-04-22 15:16:12 +02:00
`function weave(source ; doctype = :auto, plotlib="Gadfly",
informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache=:off)`
Weave an input document to output file.
2015-01-01 22:32:15 +01:00
2016-04-22 15:16:12 +02:00
* `doctype`: :auto = set based on file extension or specify one of the supported formats.
See `list_out_formats()`
2016-04-19 15:38:03 +02:00
* `plotlib`: `"PyPlot"`, `"Gadfly"` or `nothing`
2016-04-22 15:16:12 +02:00
* `informat`: :auto = set based on file extension or set to `"noweb"`, `"markdown"` or `script`
2016-04-24 14:02:03 +02:00
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, `"somepath"`: output directory as a String e.g `"/home/mpastell/weaveout"` or filename as string e.g. ~/outpath/outfile.tex.
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`.
* `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.
"""
function weave(source ; doctype = :auto, plotlib=:auto,
informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache=:off)
doc = read_doc(source, informat)
doc = run(doc, doctype = doctype, plotlib=plotlib,
out_path=out_path,
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache)
formatted = format(doc)
2014-12-01 20:23:35 +01:00
2016-04-20 17:34:24 +02:00
formatted = join(formatted, "\n")
2016-04-24 14:02:03 +02:00
outname = get_outname(out_path, doc)
2016-04-20 17:34:24 +02:00
open(outname, "w") do io
write(io, formatted)
end
2014-12-03 14:41:53 +01:00
2016-04-20 17:34:24 +02:00
#Convert using pandoc
if doc.doctype == "md2html"
2016-04-24 14:02:03 +02:00
outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname)
2016-04-20 17:34:24 +02:00
elseif doc.doctype == "md2pdf"
2016-04-24 14:02:03 +02:00
outname = get_outname(out_path, doc, ext = "pdf")
pandoc2pdf(formatted, doc, outname)
2016-04-20 17:34:24 +02:00
end
2016-04-24 14:02:03 +02:00
doc.cwd == pwd() && (outname = basename(outname))
info("Report weaved to $outname")
2014-11-25 00:10:48 +01:00
end
2014-12-03 14:41:53 +01:00
2016-04-11 17:40:18 +02:00
function weave(doc::AbstractString, doctype::AbstractString)
weave(doc, doctype=doctype)
end
2014-12-03 14:41:53 +01:00
#Hooks to run before and after chunks, this is form IJulia,
#but note that Weave hooks take the chunk as input
const preexecute_hooks = Function[]
push_preexecute_hook(f::Function) = push!(preexecute_hooks, f)
pop_preexecute_hook(f::Function) = splice!(preexecute_hooks, findfirst(pretexecute_hooks, f))
const postexecute_hooks = Function[]
push_postexecute_hook(f::Function) = push!(postexecute_hooks, f)
pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexecute_hooks, f))
include("config.jl")
include("chunks.jl")
include("display_methods.jl")
include("readers.jl")
include("run.jl")
include("cache.jl")
include("formatters.jl")
include("pandoc.jl")
include("writers.jl")
2016-12-12 13:05:26 +01:00
export weave, list_out_formats, tangle, convert_doc,
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults
2014-11-25 00:10:48 +01:00
end