Weave.jl/src/Weave.jl

242 lines
8.7 KiB
Julia
Raw Normal View History

module Weave
import Highlights
using Compat
using Requires
function __init__()
@require Plots="91a5bcdd-55d7-5caf-9e0b-520d859cae80" Base.include(Main, "plots.jl")
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" Base.include(Main, "gadfly.jl")
end
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))
2018-07-23 19:29:07 +02:00
@info("Writing to file $outname")
end
2016-04-24 14:02:03 +02:00
"""
weave(source ; doctype = :auto,
2016-12-26 12:26:36 +01:00
informat=:auto, out_path=:doc, args = Dict(),
2018-07-25 22:24:25 +02:00
mod::Union{Module, Symbol} = Main,
2016-12-26 12:26:36 +01:00
fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache=:off,
template = nothing, highlight_theme = nothing, css = nothing,
pandoc_options = "",
latex_cmd = "xelatex")
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()`
* `informat`: :auto = set based on file extension or set to `"noweb"`, `"markdown"` or `script`
2017-03-09 21:09:36 +01: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
2016-12-26 12:26:36 +01:00
string e.g. ~/outpath/outfile.tex.
2018-07-25 22:24:25 +02:00
* `args`: dictionary of arguments to pass to document. Available as WEAVE_ARGS
* `mod`: Module where Weave `eval`s code. Defaults to `:sandbox`
to create new sandbox module, you can also pass a module e.g. `Main`.
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.
2018-02-27 14:08:08 +01:00
* `throw_errors` if `false` errors are included in output document and the whole document is
executed. if `true` errors are thrown when they occur.
* `template` : Template (file path) for md2html or md2tex formats.
* `highlight_theme` : Theme (Highlights.AbstractTheme) for used syntax highlighting
* `css` : CSS (file path) used for md2html format
* `pandoc_options` = String array of options to pass to pandoc for `pandoc2html` and
`pandoc2pdf` formats e.g. ["--toc", "-N"]
* `latex_cmd` the command used to make pdf from .tex
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,
2016-12-26 12:26:36 +01:00
informat=:auto, out_path=:doc, args = Dict(),
mod::Union{Module, Symbol} = :sandbox,
2016-12-26 12:26:36 +01:00
fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache=:off,
throw_errors = false,
template = nothing, highlight_theme = nothing, css = nothing,
pandoc_options = String[]::Array{String},
latex_cmd = "xelatex")
doc = read_doc(source, informat)
# Read args from document header, overrides command line args
if haskey(doc.header, "options")
(doctype, informat, out_path, args, mod, fig_path, fig_ext,
cache_path, cache, throw_errors, template, highlight_theme, css,
pandoc_options, latex_cmd) = parse_header_options(doc)
end
2016-12-23 07:34:54 +01:00
highlight_theme != nothing && (doc.highlight_theme = highlight_theme)
#theme != nothing && (doc.theme = theme) #Reserved for themes
css != nothing && (doc.css = css)
template != nothing && (doc.template = template)
2016-12-27 21:22:32 +01:00
try
doc = run(doc, doctype = doctype,
2018-07-25 22:24:25 +02:00
mod = mod,
2016-12-27 21:22:32 +01:00
out_path=out_path, args = args,
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache,
throw_errors = throw_errors)
2016-12-27 21:22:32 +01:00
formatted = format(doc)
2016-12-23 07:34:54 +01:00
2016-12-27 21:22:32 +01:00
outname = get_outname(out_path, doc)
2016-12-27 21:22:32 +01:00
open(outname, "w") do io
write(io, formatted)
end
2016-04-20 17:34:24 +02:00
2016-12-27 21:22:32 +01:00
#Special for that need external programs
if doc.doctype == "pandoc2html"
mdname = outname
outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname, pandoc_options)
2016-12-27 21:22:32 +01:00
rm(mdname)
elseif doc.doctype == "pandoc2pdf"
mdname = outname
outname = get_outname(out_path, doc, ext = "pdf")
pandoc2pdf(formatted, doc, outname, pandoc_options)
2016-12-27 21:22:32 +01:00
rm(mdname)
elseif doc.doctype == "md2pdf"
success = run_latex(doc, outname, latex_cmd)
success && rm(doc.fig_path, force = true, recursive = true)
2016-12-27 21:22:32 +01:00
success || return
outname = get_outname(out_path, doc, ext = "pdf")
end
2014-12-03 14:41:53 +01:00
2016-12-27 21:22:32 +01:00
doc.cwd == pwd() && (outname = basename(outname))
2018-07-23 19:29:07 +02:00
@info("Report weaved to $outname")
return abspath(outname)
catch err
@warn("Something went wrong during weaving")
@error(sprint(showerror, err))
return nothing
2016-12-27 21:22:32 +01:00
finally
2018-07-26 10:35:17 +02:00
doctype == :auto && (doctype = detect_doctype(doc.source))
if occursin("pandoc2pdf", doctype) && cache == :off
rm(doc.fig_path, force = true, recursive = true)
elseif occursin("2html", doctype)
2018-07-26 10:35:17 +02:00
rm(doc.fig_path, force = true, recursive = true)
end
end
2014-11-25 00:10:48 +01:00
end
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
"""
notebook(source::String, out_path=:pwd, timeout=-1, nbconvert_options="")
2018-02-27 14:08:08 +01:00
Convert Weave document `source` to Jupyter notebook and execute the code
using nbconvert. Requires IJulia. **Ignores** all chunk options
2018-02-27 14:08:08 +01:00
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document,
`:pwd`: Julia working directory, `"somepath"`: Path as a
String e.g `"/home/mpastell/weaveout"`
* `timeout`: nbconvert cell timeout in seconds. Defaults to -1 (no timeout)
* `nbconvert_options`: string of additional options to pass to nbconvert, such as `--allow-errors`
"""
2019-02-25 18:46:25 +01:00
function notebook(source::String, out_path=:pwd, timeout=-1, nbconvert_options=[])
doc = read_doc(source)
converted = convert_doc(doc, NotebookOutput())
doc.cwd = get_cwd(doc, out_path)
outfile = get_outname(out_path, doc, ext="ipynb")
open(outfile, "w") do f
write(f, converted)
end
@info("Running nbconvert")
2019-02-25 18:46:25 +01:00
Base.eval(Main, Meta.parse("import IJulia"))
out = read(`$(Main.IJulia.JUPYTER) nbconvert --ExecutePreprocessor.timeout=$timeout --to notebook --execute $outfile $nbconvert_options --output $outfile`, String)
end
"""
2016-12-23 07:36:07 +01:00
include_weave(doc, informat=:auto)
Include code from Weave document calling `include_string` on
all code from doc. Code is run in the path of the include document.
"""
2016-12-23 07:36:07 +01:00
function include_weave(source, informat=:auto)
old_path = pwd()
doc = read_doc(source, informat)
cd(doc.path)
try
code = join([x.content for x in
filter(x -> isa(x,Weave.CodeChunk), doc.chunks)], "\n")
include_string(code)
catch e
cd(old_path)
throw(e)
end
end
#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(preexecute_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("chunks.jl")
include("config.jl")
include("WeaveMarkdown/markdown.jl")
include("display_methods.jl")
include("readers.jl")
include("run.jl")
include("cache.jl")
include("formatters.jl")
include("format.jl")
include("pandoc.jl")
include("writers.jl")
2016-12-12 13:05:26 +01:00
export weave, list_out_formats, tangle, convert_doc, notebook,
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults,
2016-12-26 12:45:33 +01:00
include_weave
2014-11-25 00:10:48 +01:00
end