Add parsing of YAML header #63

pull/66/head
Matti Pastell 2016-12-14 21:50:29 +02:00
parent 9664acbe85
commit d5bbf4f678
4 changed files with 32 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -281,7 +281,7 @@ function init_plotting(plotlib)
else
l_plotlib = lowercase(plotlib)
rcParams[:chunk_defaults][:fig] = true
if l_plotlib == "winston"
if l_plotlib == "winston"
eval(parse("""include("$srcdir/winston.jl")"""))
rcParams[:plotlib] = "Winston"
elseif l_plotlib == "pyplot"