mirror of https://github.com/mpastell/Weave.jl
commit
36bce9cf1f
|
@ -21,12 +21,7 @@ function format(doc::WeaveDoc)
|
||||||
docformat.formatdict[:cwd] = doc.cwd #pass wd to figure formatters
|
docformat.formatdict[:cwd] = doc.cwd #pass wd to figure formatters
|
||||||
docformat.formatdict[:theme] = doc.highlight_theme
|
docformat.formatdict[:theme] = doc.highlight_theme
|
||||||
|
|
||||||
#strip header
|
strip_header!(doc)
|
||||||
if isa(doc.chunks[1], DocChunk)
|
|
||||||
if !occursin("pandoc", doc.doctype)
|
|
||||||
doc.chunks[1] = strip_header(doc.chunks[1])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for chunk in copy(doc.chunks)
|
for chunk in copy(doc.chunks)
|
||||||
result = format_chunk(chunk, formatdict, docformat)
|
result = format_chunk(chunk, formatdict, docformat)
|
||||||
|
@ -106,12 +101,27 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2tex)
|
||||||
[Pair(Symbol(k), v) for (k,v) in doc.header]...)
|
[Pair(Symbol(k), v) for (k,v) in doc.header]...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function strip_header(chunk::DocChunk)
|
strip_header!(doc::WeaveDoc) = strip_header!(doc.chunks[1], doc.doctype)
|
||||||
if occursin(r"^---$(?<header>.+)^---$"ms, chunk.content[1].content)
|
function strip_header!(docchunk::DocChunk, doctype)
|
||||||
chunk.content[1].content = lstrip(replace(chunk.content[1].content, r"^---$(?<header>.+)^---$"ms => ""))
|
doctype == "pandoc" && return
|
||||||
end
|
content = docchunk.content[1].content
|
||||||
return chunk
|
if (m = match(HEADER_REGEX, content)) !== nothing
|
||||||
|
# TODO: is there other format where we want to keep headers ?
|
||||||
|
docchunk.content[1].content = if doctype != "github"
|
||||||
|
lstrip(replace(content, HEADER_REGEX => ""))
|
||||||
|
else
|
||||||
|
# only strips Weave headers
|
||||||
|
header = YAML.load(m[:header])
|
||||||
|
delete!(header, "options")
|
||||||
|
if isempty(header)
|
||||||
|
lstrip(replace(content, HEADER_REGEX => ""))
|
||||||
|
else
|
||||||
|
lstrip(replace(content, HEADER_REGEX => "---\n$(YAML.write(header))---"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
strip_header!(codechunk::CodeChunk, doctype) = nothing
|
||||||
|
|
||||||
function format_chunk(chunk::DocChunk, formatdict, docformat)
|
function format_chunk(chunk::DocChunk, formatdict, docformat)
|
||||||
return join([format_inline(c) for c in chunk.content], "")
|
return join([format_inline(c) for c in chunk.content], "")
|
||||||
|
|
|
@ -64,8 +64,10 @@ function parse_header(chunk::CodeChunk)
|
||||||
return Dict()
|
return Dict()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
const HEADER_REGEX = r"^---$(?<header>((?!---).)+)^---$"ms
|
||||||
|
|
||||||
function parse_header(chunk::DocChunk)
|
function parse_header(chunk::DocChunk)
|
||||||
m = match(r"^---$(?<header>.+)^---$"ms, chunk.content[1].content)
|
m = match(HEADER_REGEX, chunk.content[1].content)
|
||||||
if m !== nothing
|
if m !== nothing
|
||||||
header = YAML.load(string(m[:header]))
|
header = YAML.load(string(m[:header]))
|
||||||
else
|
else
|
||||||
|
|
|
@ -83,7 +83,7 @@ function convert_doc(doc::WeaveDoc, format::NotebookOutput)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isa(doc.chunks[1], DocChunk)
|
if isa(doc.chunks[1], DocChunk)
|
||||||
doc.chunks[1] = strip_header(doc.chunks[1])
|
strip_header!(doc)
|
||||||
doc.chunks[1].content[1].content = Mustache.render(head_tpl;
|
doc.chunks[1].content[1].content = Mustache.render(head_tpl;
|
||||||
[Pair(Symbol(k), v) for (k,v) in doc.header]...) * doc.chunks[1].content[1].content
|
[Pair(Symbol(k), v) for (k,v) in doc.header]...) * doc.chunks[1].content[1].content
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,14 +72,14 @@ h = Weave.parse_header(dchunk)
|
||||||
h_ref = Dict("author" => "Matti Pastell", "title" => "Test block")
|
h_ref = Dict("author" => "Matti Pastell", "title" => "Test block")
|
||||||
@test h_ref == h
|
@test h_ref == h
|
||||||
|
|
||||||
htext = Weave.strip_header(dchunk)
|
Weave.strip_header!(dchunk, "md2html")
|
||||||
h_ref = """
|
h_ref = """
|
||||||
# Actual header
|
# Actual header
|
||||||
|
|
||||||
and some text
|
and some text
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@test htext.content[1].content == h_ref
|
@test dchunk.content[1].content == h_ref
|
||||||
|
|
||||||
# Test wrapping
|
# Test wrapping
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue