mirror of https://github.com/mpastell/Weave.jl
a bit refactor:
- don't match slide `---` separator as header start/end - recognize `doctype` in constructionavi/dynamic
parent
492d1f330b
commit
17c2151d86
15
src/Weave.jl
15
src/Weave.jl
|
@ -114,8 +114,7 @@ function weave(
|
|||
latex_cmd::AbstractString = "xelatex",
|
||||
latex_keep_unicode::Bool = false,
|
||||
)
|
||||
doc = WeaveDoc(source, informat)
|
||||
isnothing(doctype) && (doctype = detect_doctype(doc.source))
|
||||
doc = WeaveDoc(source, informat, doctype)
|
||||
|
||||
# overwrites given options with header options, which have more precedence
|
||||
# NOTE:
|
||||
|
@ -124,7 +123,7 @@ function weave(
|
|||
weave_options = get(doc.header, WEAVE_OPTION_NAME, Dict())
|
||||
if !isempty(weave_options)
|
||||
doctype = get(weave_options, "doctype", doctype)
|
||||
weave_options = combine_args(weave_options, doctype)
|
||||
specific_options!(weave_options, doctype)
|
||||
if haskey(weave_options, "out_path")
|
||||
out_path = let
|
||||
out_path = weave_options["out_path"]
|
||||
|
@ -201,6 +200,16 @@ function weave(
|
|||
return abspath(outname)
|
||||
end
|
||||
|
||||
function specific_options!(weave_options, doctype)
|
||||
fmts = keys(formats)
|
||||
for (k,v) in weave_options
|
||||
if k in fmts
|
||||
k == doctype && merge!(weave_options, v)
|
||||
delete!(weave_options, k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
weave(doc::AbstractString, doctype::Union{Symbol,AbstractString}) =
|
||||
weave(doc; doctype = doctype)
|
||||
|
||||
|
|
|
@ -62,18 +62,3 @@ get_chunk_defaults() = rcParams[:chunk_defaults]
|
|||
Restore Weave.jl default chunk options.
|
||||
"""
|
||||
restore_chunk_defaults!() = rcParams[:chunk_defaults] = defaultParams[:chunk_defaults]
|
||||
|
||||
"""Combine format specific and common options from document header"""
|
||||
function combine_args(args, doctype)
|
||||
common = Dict()
|
||||
specific = Dict()
|
||||
for key in keys(args)
|
||||
if key in keys(Weave.formats)
|
||||
specific[key] = args[key]
|
||||
else
|
||||
common[key] = args[key]
|
||||
end
|
||||
end
|
||||
haskey(specific, doctype) && merge!(common, specific[doctype])
|
||||
common
|
||||
end
|
||||
|
|
|
@ -112,10 +112,8 @@ function render_doc(formatted, doc, format::JMarkdown2tex)
|
|||
end
|
||||
|
||||
function restore_header!(doc)
|
||||
doctype = doc.doctype
|
||||
|
||||
# TODO: is there any other format where we want to restore headers ?
|
||||
doctype ≠ "github" && return
|
||||
doc.doctype ≠ "github" && return
|
||||
|
||||
# only strips Weave headers
|
||||
delete!(doc.header, WEAVE_OPTION_NAME)
|
||||
|
|
|
@ -509,7 +509,7 @@ function formatfigures(chunk, docformat::AsciiDoc)
|
|||
end
|
||||
|
||||
# Add new supported formats here
|
||||
const formats = Dict{AbstractString,Any}(
|
||||
const formats = Dict(
|
||||
"tex" => tex,
|
||||
"texminted" => texminted,
|
||||
"pandoc" => pandoc,
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
using JSON, YAML
|
||||
|
||||
|
||||
function WeaveDoc(source, format::Union{Nothing,AbstractString} = nothing)
|
||||
function WeaveDoc(source, informat = nothing, doctype = nothing)
|
||||
path, fname = splitdir(abspath(source))
|
||||
basename = splitext(fname)[1]
|
||||
|
||||
isnothing(format) && (format = detect_informat(source))
|
||||
header, chunks = parse_doc(read(source, String), format)
|
||||
isnothing(informat) && (informat = detect_informat(source))
|
||||
header, chunks = parse_doc(read(source, String), informat)
|
||||
|
||||
isnothing(doctype) && (doctype = detect_doctype(source))
|
||||
|
||||
# update default chunk options from header
|
||||
chunk_defaults = deepcopy(get_chunk_defaults())
|
||||
|
@ -25,7 +27,7 @@ function WeaveDoc(source, format::Union{Nothing,AbstractString} = nothing)
|
|||
chunks,
|
||||
"",
|
||||
nothing,
|
||||
"",
|
||||
doctype,
|
||||
"",
|
||||
header,
|
||||
"",
|
||||
|
@ -37,12 +39,12 @@ function WeaveDoc(source, format::Union{Nothing,AbstractString} = nothing)
|
|||
end
|
||||
|
||||
"""
|
||||
detect_informat(source::AbstractString)
|
||||
detect_informat(path)
|
||||
|
||||
Detect Weave input format based on file extension of `source`.
|
||||
Detect Weave input format based on file extension of `path`.
|
||||
"""
|
||||
function detect_informat(source::AbstractString)
|
||||
ext = lowercase(last(splitext(source)))
|
||||
function detect_informat(path)
|
||||
ext = lowercase(last(splitext(path)))
|
||||
|
||||
ext == ".jl" && return "script"
|
||||
ext == ".jmd" && return "markdown"
|
||||
|
@ -50,17 +52,17 @@ function detect_informat(source::AbstractString)
|
|||
return "noweb"
|
||||
end
|
||||
|
||||
function parse_doc(document, format)
|
||||
function parse_doc(document, informat)
|
||||
document = replace(document, "\r\n" => "\n") # normalize line ending
|
||||
|
||||
header_text, document = separete_header_text(document)
|
||||
header_text, document = separate_header_text(document)
|
||||
|
||||
return parse_header(header_text),
|
||||
format == "markdown" ? parse_markdown(document) :
|
||||
format == "noweb" ? parse_markdown(document, true) :
|
||||
format == "script" ? parse_script(document) :
|
||||
format == "notebook" ? parse_notebook(document) :
|
||||
error("unsupported format given: $(format)")
|
||||
informat == "markdown" ? parse_markdown(document) :
|
||||
informat == "noweb" ? parse_markdown(document, true) :
|
||||
informat == "script" ? parse_script(document) :
|
||||
informat == "notebook" ? parse_notebook(document) :
|
||||
error("unsupported input format given: $informat")
|
||||
end
|
||||
|
||||
function pushopt(options::Dict, expr::Expr)
|
||||
|
@ -69,6 +71,22 @@ function pushopt(options::Dict, expr::Expr)
|
|||
end
|
||||
end
|
||||
|
||||
"""
|
||||
detect_doctype(path)
|
||||
|
||||
Detect the output format based on file extension.
|
||||
"""
|
||||
function detect_doctype(path)
|
||||
_, ext = lowercase.(splitext(path))
|
||||
|
||||
match(r"^\.(jl|.?md|ipynb)", ext) !== nothing && return "md2html"
|
||||
ext == ".rst" && return "rst"
|
||||
ext == ".tex" && return "texminted"
|
||||
ext == ".txt" && return "asciidoc"
|
||||
|
||||
return "pandoc"
|
||||
end
|
||||
|
||||
# inline
|
||||
# ------
|
||||
|
||||
|
@ -117,10 +135,10 @@ const HEADER_REGEX = r"^---$(?<header>((?!---).)+)^---$"ms
|
|||
|
||||
# TODO: non-Weave headers should keep live in a doc
|
||||
# separates header section from `text`
|
||||
function separete_header_text(text)
|
||||
function separate_header_text(text)
|
||||
m = match(HEADER_REGEX, text)
|
||||
isnothing(m) && return "", text
|
||||
return m[:header], replace(text, HEADER_REGEX => "")
|
||||
return m[:header], replace(text, HEADER_REGEX => ""; count = 1)
|
||||
end
|
||||
|
||||
# HACK:
|
||||
|
|
|
@ -174,9 +174,7 @@ function run_chunk(chunk::DocChunk, doc::WeaveDoc, report::Report, SandBox::Modu
|
|||
return chunk
|
||||
end
|
||||
|
||||
function run_inline(inline::InlineText, doc::WeaveDoc, report::Report, SandBox::Module)
|
||||
return inline
|
||||
end
|
||||
run_inline(inline::InlineText, doc::WeaveDoc, report::Report, SandBox::Module) = inline
|
||||
|
||||
function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox::Module)
|
||||
# Make a temporary CodeChunk for running code. Collect results and don't wrap
|
||||
|
|
|
@ -9,7 +9,7 @@ mutable struct WeaveDoc
|
|||
chunks::Vector{WeaveChunk}
|
||||
cwd::AbstractString
|
||||
format::Any
|
||||
doctype::AbstractString
|
||||
doctype::String
|
||||
header_script::String
|
||||
header::Dict
|
||||
template::Union{AbstractString,Mustache.MustacheTokens}
|
||||
|
|
Loading…
Reference in New Issue