From 42faaae5def5db0c02ee9bae827a265ccdc44e38 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Fri, 22 Apr 2016 15:39:25 +0300 Subject: [PATCH] Detect informat based on file extension --- bin/weave.jl | 7 ++++++- src/Weave.jl | 6 +++--- src/readers.jl | 13 ++++++++++++- src/run.jl | 2 +- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bin/weave.jl b/bin/weave.jl index 564cb69..e16c73b 100755 --- a/bin/weave.jl +++ b/bin/weave.jl @@ -23,7 +23,7 @@ ap = ArgParseSettings("Weave Julia documents using Weave.jl", help = "output format" "--informat" arg_type = String - default = "noweb" + default = ":auto" help = "output format" "--out_path" arg_type = String @@ -44,6 +44,11 @@ delete!(args, "source") args_col = [] #Check for special values of out_path + +if args["informat"] == ":auto" + args["informat"] = :auto +end + if args["out_path"] == ":doc" args["out_path"] = :doc elseif args["out_path"] == ":pwd" diff --git a/src/Weave.jl b/src/Weave.jl index 5dc6cc3..7664ee7 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -55,7 +55,7 @@ Tangle source code from input document to .jl file. * `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, `"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"` """ -function tangle(source ; out_path=:doc, informat="noweb") +function tangle(source ; out_path=:doc, informat=:auto) doc = read_doc(source, informat) cwd = get_cwd(doc, out_path) @@ -94,12 +94,12 @@ Weave an input document to output file. **Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. """ function weave(source ; doctype = "pandoc", plotlib="Gadfly", - informat="noweb", out_path=:doc, fig_path = "figures", fig_ext = nothing, + 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, - informat = informat, out_path=out_path, + out_path=out_path, fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache) formatted = format(doc) diff --git a/src/readers.jl b/src/readers.jl index 0f77bb7..f313789 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -25,9 +25,20 @@ const input_formats = Dict{AbstractString, Any}( r"(^#\+)|(^#%%\+)|(^# %%\+)") ) +"""Detect the input format based on file extension""" +function detect_informat(source::AbstractString) + ext = lowercase(splitext(source)[2]) + + ext == ".jl" && return "script" + ext == ".jmd" && return "markdown" + return "noweb" +end + + """Read and parse input document""" -function read_doc(source::AbstractString, format="noweb"::AbstractString) +function read_doc(source::AbstractString, format=:auto) + format == :auto && (format = detect_informat(source)) document = @compat readstring(source) parsed = parse_doc(document, format) doc = WeaveDoc(source, parsed) diff --git a/src/run.jl b/src/run.jl index 911bcb8..8846616 100644 --- a/src/run.jl +++ b/src/run.jl @@ -18,7 +18,7 @@ Run code chunks and capture output from parsed document. **Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. """ -function Base.run(doc::WeaveDoc; doctype = "pandoc", plotlib="Gadfly", informat="noweb", +function Base.run(doc::WeaveDoc; doctype = "pandoc", plotlib="Gadfly", out_path=:doc, fig_path = "figures", fig_ext = nothing, cache_path = "cache", cache = :off) #cache :all, :user, :off, :refresh