detect doctype when running a doc

pull/311/head
Shuhei Kadowaki 2020-05-16 23:10:44 +09:00
parent a740ba3556
commit 58bc741b7f
3 changed files with 13 additions and 30 deletions

View File

@ -116,7 +116,7 @@ function weave(
latex_cmd::AbstractString = "xelatex",
latex_keep_unicode::Bool = false,
)
doc = WeaveDoc(source, informat, doctype)
doc = WeaveDoc(source, informat)
# overwrites given options with header options, which have more precedence
# NOTE:
@ -202,6 +202,9 @@ function weave(
return abspath(outname)
end
weave(doc::AbstractString, doctype::Union{Symbol,AbstractString}; kwargs...) =
weave(doc; doctype = doctype, kwargs...)
function specific_options!(weave_options, doctype)
fmts = keys(formats)
for (k,v) in weave_options
@ -212,9 +215,6 @@ function specific_options!(weave_options, doctype)
end
end
weave(doc::AbstractString, doctype::Union{Symbol,AbstractString}) =
weave(doc; doctype = doctype)
"""
notebook(source::AbstractString; kwargs...)

View File

@ -1,15 +1,13 @@
using JSON, YAML
function WeaveDoc(source, informat = nothing, doctype = nothing)
function WeaveDoc(source, informat = nothing)
path, fname = splitdir(abspath(source))
basename = splitext(fname)[1]
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())
if haskey(header, WEAVE_OPTION_NAME)
@ -27,7 +25,7 @@ function WeaveDoc(source, informat = nothing, doctype = nothing)
chunks,
"",
nothing,
doctype,
"",
"",
header,
"",
@ -68,22 +66,6 @@ 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
# ------

View File

@ -46,9 +46,7 @@ function run_doc(
)
# cache :all, :user, :off, :refresh
# doctype detection is unnecessary here, but existing unit test requires this.
isnothing(doctype) && (doctype = detect_doctype(doc.source))
doc.doctype = doctype
doc.doctype = isnothing(doctype) ? (doctype = detect_doctype(doc.source)) : doctype
doc.format = formats[doctype]
if haskey(doc.format.formatdict, :keep_unicode)
@ -124,13 +122,16 @@ function run_doc(
return doc
end
run_doc(doc::WeaveDoc, doctype::Union{Nothing,AbstractString}; kwargs...) =
run_doc(doc; doctype = doctype, kwargs...)
"""
detect_doctype(pathname::AbstractString)
detect_doctype(path)
Detect the output format based on file extension.
"""
function detect_doctype(pathname::AbstractString)
_, ext = lowercase.(splitext(pathname))
function detect_doctype(path)
_, ext = lowercase.(splitext(path))
match(r"^\.(jl|.?md|ipynb)", ext) !== nothing && return "md2html"
ext == ".rst" && return "rst"