mirror of https://github.com/mpastell/Weave.jl
Add parsing of YAML header #63
parent
9664acbe85
commit
d5bbf4f678
|
@ -8,10 +8,11 @@ type WeaveDoc
|
|||
format
|
||||
doctype::AbstractString
|
||||
header_script::String
|
||||
function WeaveDoc(source, chunks)
|
||||
header
|
||||
function WeaveDoc(source, chunks, header)
|
||||
path, fname = splitdir(abspath(source))
|
||||
basename = splitext(fname)[1]
|
||||
new(source, basename, path, chunks, "", nothing, "", "")
|
||||
new(source, basename, path, chunks, "", nothing, "", "", header)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -15,6 +15,11 @@ function format(doc::WeaveDoc)
|
|||
|
||||
docformat.formatdict[:cwd] = doc.cwd #pass wd to figure formatters
|
||||
|
||||
#strip header
|
||||
if isa(doc.chunks[1], DocChunk)
|
||||
doc.chunks[1] = strip_header(doc.chunks[1])
|
||||
end
|
||||
|
||||
for chunk in copy(doc.chunks)
|
||||
result = format_chunk(chunk, formatdict, docformat)
|
||||
push!(formatted, result)
|
||||
|
@ -92,6 +97,11 @@ function get_title(doc::WeaveDoc)
|
|||
return title
|
||||
end
|
||||
|
||||
function strip_header(chunk::DocChunk)
|
||||
chunk.content = lstrip(replace(chunk.content, r"^---$(?<header>.+)^---$"ms, ""))
|
||||
return chunk
|
||||
end
|
||||
|
||||
function format_chunk(chunk::DocChunk, formatdict, docformat)
|
||||
return chunk.content
|
||||
end
|
||||
|
@ -113,7 +123,6 @@ end
|
|||
|
||||
function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
|
||||
m = Base.Markdown.parse(chunk.content)
|
||||
#TODO add space between paragraphs
|
||||
return Base.Markdown.latex(m)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import JSON
|
||||
import JSON, YAML
|
||||
|
||||
pushopt(options::Dict,expr::Expr) = Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2])
|
||||
|
||||
|
@ -48,7 +48,23 @@ function read_doc(source::AbstractString, format=:auto)
|
|||
format == :auto && (format = detect_informat(source))
|
||||
document = readstring(source)
|
||||
parsed = parse_doc(document, format)
|
||||
doc = WeaveDoc(source, parsed)
|
||||
header = parse_header(parsed[1])
|
||||
doc = WeaveDoc(source, parsed, header)
|
||||
return doc
|
||||
end
|
||||
|
||||
function parse_header(chunk::CodeChunk)
|
||||
return nothing
|
||||
end
|
||||
|
||||
function parse_header(chunk::DocChunk)
|
||||
m = match(r"^---$(?<header>.+)^---$"ms, chunk.content)
|
||||
if m !== nothing
|
||||
header = YAML.load(string(m[:header]))
|
||||
else
|
||||
header = nothing
|
||||
end
|
||||
return header
|
||||
end
|
||||
|
||||
function parse_doc(document::AbstractString, format="noweb"::AbstractString)
|
||||
|
|
Loading…
Reference in New Issue