Allow out_path to be file or directory

pull/37/head v0.1.1
= 2016-04-24 15:02:03 +03:00
parent b5ac812ff4
commit 13670b1620
5 changed files with 67 additions and 22 deletions

View File

@ -14,6 +14,7 @@
* New output format: MultiMarkdown
* Added support for figure width in Pandoc
* Autodetect input and output formats based on filename
* Allow `out_path` be a file or directory.
### v0.1.0

View File

@ -52,15 +52,16 @@ end
Tangle source code from input document to .jl file.
* `informat`: `"noweb"` of `"markdown"`
* `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"`
* `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)
cwd = get_cwd(doc, out_path)
doc.cwd = get_cwd(doc, out_path)
outname = "$(cwd)/$(doc.basename).jl"
open(outname, "w") do io
outname = get_outname(out_path, doc, ext = "jl")
open(outname, "w") do io
for chunk in doc.chunks
if typeof(chunk) == CodeChunk
options = merge(rcParams[:chunk_defaults], chunk.options)
@ -70,10 +71,11 @@ function tangle(source ; out_path=:doc, informat=:auto)
end
end
end
info("Writing to file $(doc.basename).jl")
doc.cwd == pwd() && (outname = basename(outname))
info("Writing to file $outname")
end
"""
`function weave(source ; doctype = :auto, plotlib="Gadfly",
informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing,
@ -85,8 +87,7 @@ Weave an input document to output file.
See `list_out_formats()`
* `plotlib`: `"PyPlot"`, `"Gadfly"` or `nothing`
* `informat`: :auto = set based on file extension or set to `"noweb"`, `"markdown"` or `script`
* `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"`
* `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.
* `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.
@ -107,8 +108,7 @@ function weave(source ; doctype = :auto, plotlib="Gadfly",
formatted = join(formatted, "\n")
outname = "$(doc.cwd)/$(doc.basename).$(doc.format.formatdict[:extension])"
ext = doc.format.formatdict[:extension]
outname = get_outname(out_path, doc)
open(outname, "w") do io
write(io, formatted)
@ -116,17 +116,21 @@ function weave(source ; doctype = :auto, plotlib="Gadfly",
#Convert using pandoc
if doc.doctype == "md2html"
pandoc2html(formatted, doc)
ext = "html"
outname = get_outname(out_path, doc, ext = "html")
pandoc2html(formatted, doc, outname)
elseif doc.doctype == "md2pdf"
pandoc2pdf(formatted, doc)
ext = "pdf"
outname = get_outname(out_path, doc, ext = "pdf")
pandoc2pdf(formatted, doc, outname)
end
info("Report weaved to $(doc.basename).$ext")
doc.cwd == pwd() && (outname = basename(outname))
info("Report weaved to $outname")
end
function Base.display(report::Report, m::MIME"text/plain", data)
s = reprmime(m, data)
print("\n" * s)

View File

@ -5,7 +5,7 @@
Convert output from pandoc markdown to html using Weave.jl template
"""
function pandoc2html(formatted::AbstractString, doc::WeaveDoc)
function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString)
html_template = joinpath(Pkg.dir("Weave"), "templates/pandoc_skeleton.html")
css_template = joinpath(Pkg.dir("Weave"), "templates/pandoc_skeleton.css")
@ -13,8 +13,6 @@ function pandoc2html(formatted::AbstractString, doc::WeaveDoc)
wversion = string(Pkg.installed("Weave"))
wtime = Date(now())
outname = "$(doc.basename).html"
#Change path for pandoc
old_wd = pwd()
cd(doc.cwd)
@ -40,7 +38,7 @@ end
Convert output from pandoc markdown to pdf using Weave.jl template
"""
function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc)
function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString)
header_template = joinpath(Pkg.dir("Weave"), "templates/pandoc_header.txt")
@ -48,7 +46,6 @@ function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc)
wversion = string(Pkg.installed("Weave"))
wtime = Date(now())
outname = "$(doc.basename).pdf"
#Change path for pandoc
old_wd = pwd()

View File

@ -260,11 +260,37 @@ function get_cwd(doc::WeaveDoc, out_path)
elseif out_path == :pwd
cwd = pwd()
else
cwd = expanduser(out_path)
#If there is no extension, use as path
splitted = splitext(out_path)
if splitted[2] == ""
cwd = expanduser(out_path)
else
cwd = splitdir(expanduser(out_path))[1]
end
end
return cwd
end
"""Get output file name based on out_path"""
function get_outname(out_path::Symbol, doc::WeaveDoc; ext = nothing)
ext == nothing && (ext = doc.format.formatdict[:extension])
outname = "$(doc.cwd)/$(doc.basename).$ext"
end
"""Get output file name based on out_path"""
function get_outname(out_path::AbstractString, doc::WeaveDoc; ext = nothing)
ext == nothing && (ext = doc.format.formatdict[:extension])
splitted = splitext(out_path)
if (splitted[2]) == ""
outname = "$(doc.cwd)/$(doc.basename).$ext"
else
outname = expanduser(out_path)
end
end
function set_rc_params(formatdict, fig_path, fig_ext)
if fig_ext == nothing
rcParams[:chunk_defaults][:fig_ext] = formatdict[:fig_ext]

View File

@ -28,12 +28,29 @@ ref = @compat readstring(open("documents/chunk_options_ref.rst"))
@test result == ref
cleanup && rm("documents/chunk_options.rst")
#Test out_path
weave("documents/chunk_options.noweb", doctype="rst",
out_path="documents/outpath_options.rst" , plotlib=nothing)
result = @compat readstring(open("documents/outpath_options.rst"))
ref = @compat readstring(open("documents/chunk_options_ref.rst"))
@test result == ref
cleanup && rm("documents/outpath_options.rst")
#Test tangle
tangle("documents/chunk_options.noweb")
result = @compat readstring(open("documents/chunk_options.jl"))
ref = @compat readstring(open("documents/chunk_options_ref.jl"))
@test result == ref
cleanup && rm("documents/chunk_options.jl")
tangle("documents/chunk_options.noweb", out_path = "documents/outoptions.jl")
result = @compat readstring(open("documents/outoptions.jl"))
ref = @compat readstring(open("documents/chunk_options_ref.jl"))
@test result == ref
cleanup && rm("documents/outoptions.jl")
#Test functions and sandbox clearing
weave("documents/chunk_func.noweb", plotlib=nothing)
result = @compat readstring(open("documents/chunk_func.md"))