From d5bbf4f678437c38996b261e60138643a068b32d Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Wed, 14 Dec 2016 21:50:29 +0200 Subject: [PATCH] Add parsing of YAML header #63 --- src/chunks.jl | 5 +++-- src/format.jl | 11 ++++++++++- src/readers.jl | 20 ++++++++++++++++++-- src/run.jl | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/chunks.jl b/src/chunks.jl index 1a5aced..d24c80f 100644 --- a/src/chunks.jl +++ b/src/chunks.jl @@ -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 diff --git a/src/format.jl b/src/format.jl index 02aeefb..4e10aca 100644 --- a/src/format.jl +++ b/src/format.jl @@ -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"^---$(?
.+)^---$"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 diff --git a/src/readers.jl b/src/readers.jl index b623a62..898a0f1 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -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"^---$(?
.+)^---$"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) diff --git a/src/run.jl b/src/run.jl index e45bfa9..6bb4284 100644 --- a/src/run.jl +++ b/src/run.jl @@ -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"