Skip to content

Commit

Permalink
Merge 9dc99ba into 3da8865
Browse files Browse the repository at this point in the history
  • Loading branch information
mpastell committed Dec 26, 2016
2 parents 3da8865 + 9dc99ba commit 7ebd0b4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
33 changes: 29 additions & 4 deletions src/chunks.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

abstract WeaveChunk
abstract Inline

type WeaveDoc
source::AbstractString
basename::AbstractString
path::AbstractString
chunks::Array
chunks::Array{WeaveChunk}
cwd::AbstractString
format
doctype::AbstractString
Expand All @@ -29,7 +32,7 @@ immutable ChunkOutput
figures::Array{AbstractString}
end

type CodeChunk
type CodeChunk <: WeaveChunk
content::AbstractString
number::Int
result_no::Int
Expand All @@ -45,10 +48,32 @@ type CodeChunk
end
end

type DocChunk
content::AbstractString
type DocChunk <: WeaveChunk
content::Array{Inline}
number::Int
start_line::Int
function DocChunk(text::AbstractString, number::Int, start_line::Int, inline_regex = nothing)
chunks = parse_inline(text, inline_regex)
new(chunks, number, start_line)
end
end

type InlineText <: Inline
content::AbstractString
si::Int64
ei::Int64
end

type InlineCode <: Inline
content::AbstractString
si::Int64
ei::Int64
output::AbstractString
rich_output::AbstractString
figures::Array{AbstractString}
function InlineCode(content, si, ei)
new(content, si, ei, "", "", AbstractString[])
end
end

type TermResult
Expand Down
23 changes: 17 additions & 6 deletions src/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,32 @@ function get_titleblock(doc::WeaveDoc)
end

function strip_header(chunk::DocChunk)
if ismatch(r"^---$(?<header>.+)^---$"ms, chunk.content)
chunk.content = lstrip(replace(chunk.content, r"^---$(?<header>.+)^---$"ms, ""))
if ismatch(r"^---$(?<header>.+)^---$"ms, chunk.content[1].content)
chunk.content[1].content = lstrip(replace(chunk.content[1].content, r"^---$(?<header>.+)^---$"ms, ""))
end
return chunk
end

function format_chunk(chunk::DocChunk, formatdict, docformat)
return chunk.content
return join([format_inline(c) for c in chunk.content], "")
end

function format_inline(inline::InlineText)
return inline.content
end

function format_inline(inline::InlineCode)
isempty(inline.rich_output) || return inline.rich_output
isempty(inline.figures) || return inline.figures[end]
isempty(inline.output) || return inline.output
end

function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2HTML)
m = Base.Markdown.parse(chunk.content)
text = format_chunk(chunk, formatdict, nothing)
m = Base.Markdown.parse(text)
return string(Documenter.Writers.HTMLWriter.mdconvert(m))
end


#Fixes to Base latex writer
function Base.Markdown.latex(io::IO, md::Base.Markdown.Paragraph)
println(io)
Expand All @@ -144,7 +154,8 @@ end


function format_chunk(chunk::DocChunk, formatdict, docformat::JMarkdown2tex)
m = Base.Markdown.parse(chunk.content)
text = format_chunk(chunk, formatdict, nothing)
m = Base.Markdown.parse(text)
return Base.Markdown.latex(m)
end

Expand Down
50 changes: 41 additions & 9 deletions src/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@ pushopt(options::Dict,expr::Expr) = Base.Meta.isexpr(expr,:(=)) && (options[expr
type MarkupInput
codestart::Regex
codeend::Regex
inline::Regex
end

type ScriptInput
doc_line::Regex
doc_start::Regex
opt_line::Regex
opt_start::Regex
inline::Regex
end

type NotebookInput
inline
end

const input_formats = Dict{AbstractString, Any}(
"noweb" => MarkupInput(r"^<<(.*?)>>=\s*$",
r"^@\s*$"),
r"^@\s*$",
r"`j\s+(.*?)`"s
),
"markdown" => MarkupInput(
r"^[`~]{3,}(?:\{|\{\.|)julia(?:;|)\s*(.*?)(\}|\s*)$",
r"^[`~]{3,}\s*$"),
r"^[`~]{3,}\s*$",
r"`j\s+(.*?)`"s),
"script" => ScriptInput(
r"(^#'.*)|(^#%%.*)|(^# %%.*)",
r"(^#')|(^#%%)|(^# %%)",
r"(^#\+.*$)|(^#%%\+.*$)|(^# %%\+.*$)",
r"(^#\+)|(^#%%\+)|(^# %%\+)"),
"notebook" => NotebookInput()
r"(^#\+)|(^#%%\+)|(^# %%\+)",
r"`j\s+(.*?)`"s),
"notebook" => NotebookInput(nothing) #Don't parse inline code from notebooks
)

"""Detect the input format based on file extension"""
Expand Down Expand Up @@ -59,7 +66,7 @@ function parse_header(chunk::CodeChunk)
end

function parse_header(chunk::DocChunk)
m = match(r"^---$(?<header>.+)^---$"ms, chunk.content)
m = match(r"^---$(?<header>.+)^---$"ms, chunk.content[1].content)
if m !== nothing
header = YAML.load(string(m[:header]))
else
Expand Down Expand Up @@ -109,7 +116,7 @@ function parse_doc(document::AbstractString, format::MarkupInput)
haskey(options, :name) || (options[:name] = nothing)

if !isempty(strip(content))
chunk = DocChunk(content, docno, start_line)
chunk = DocChunk(content, docno, start_line, format.inline)
docno += 1
push!(parsed, chunk)
end
Expand Down Expand Up @@ -143,7 +150,7 @@ function parse_doc(document::AbstractString, format::MarkupInput)

#Remember the last chunk
if strip(content) != ""
chunk = DocChunk(content, docno, start_line)
chunk = DocChunk(content, docno, start_line, format.inline)
#chunk = Dict{Symbol,Any}(:type => "doc", :content => content,
# :number => docno, :start_line => start_line)
push!(parsed, chunk)
Expand Down Expand Up @@ -223,7 +230,7 @@ function parse_doc(document::AbstractString, format::ScriptInput)
elseif state == "doc" && strip(line) != "" && strip(read) != ""
state = "code"
(docno > 1) && (read = "\n" * read) # Add whitespace to doc chunk. Needed for markdown output
chunk = DocChunk(read, docno, start_line)
chunk = DocChunk(read, docno, start_line, format.inline)
push!(parsed, chunk)
options = Dict{Symbol,Any}()
start_line = lineno
Expand All @@ -244,7 +251,7 @@ function parse_doc(document::AbstractString, format::ScriptInput)
chunk = CodeChunk("\n" * strip(read), codeno, start_line, optionString, options)
push!(parsed, chunk)
else
chunk = DocChunk(read, docno, start_line)
chunk = DocChunk(read, docno, start_line, format.inline)
push!(parsed, chunk)
end

Expand Down Expand Up @@ -277,3 +284,28 @@ function parse_doc(document::String, format::NotebookInput)

return parsed
end

#Use this if regex is undefined
function parse_inline(text, noex)
return Inline[InlineText(text, 1, length(text))]
end

function parse_inline(text::AbstractString, inline_ex::Regex)
ismatch(inline_ex, text) || return Inline[InlineText(text, 1, length(text))]

inline_chunks = eachmatch(inline_ex, text)
s = 1
e = 1
res = Inline[]

for ic in inline_chunks
s = ic.offset
doc = InlineText(text[e:(s-1)], e, s-1)
push!(res, doc)
e = s + length(ic.match)
push!(res, InlineCode(ic.captures[1], s, e))
end
push!(res, InlineText(text[e:end], e, length(text)))

return res
end
32 changes: 28 additions & 4 deletions src/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
for i = 1:n
chunk = doc.chunks[i]

if typeof(chunk) == CodeChunk
if isa(chunk, CodeChunk)
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
end
Expand All @@ -84,7 +84,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
result_chunks = restore_chunk(chunk, cached)
else

result_chunks = run_chunk(chunk, report, SandBox)
result_chunks = run_chunk(chunk, report, SandBox)
end

executed = [executed; result_chunks]
Expand Down Expand Up @@ -119,6 +119,7 @@ end


function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")
result_chunks = eval_chunk(chunk, report, SandBox)
contains(report.formatdict[:doctype], "2html") && (result_chunks = embed_figures(result_chunks, report.cwd))
return result_chunks
Expand Down Expand Up @@ -155,9 +156,31 @@ function img2base64(fig, cwd)
end

function run_chunk(chunk::DocChunk, report::Report, SandBox::Module)
chunk.content = [run_inline(c, report, SandBox) for c in chunk.content]
return chunk
end

function run_inline(inline::InlineText, report::Report, SandBox::Module)
return inline
end

function run_inline(inline::InlineCode, report::Report, SandBox::Module)
#Make a temporary CodeChunk for running code. Collect results and don't wrap
chunk = CodeChunk(inline.content, 0, 0, "", Dict(:hold => true, :wrap => false))
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)

chunks = eval_chunk(chunk, report, SandBox)
contains(report.formatdict[:doctype], "2html") && (chunks = embed_figures(chunks, report.cwd))

output = chunks[1].output
startswith(output, "\n") && (output = replace(output, "\n", "", 1))
inline.output = output
inline.rich_output = chunks[1].rich_output
inline.figures = chunks[1].figures
return inline
end

function reset_report(report::Report)
report.cur_result = ""
report.figures = AbstractString[]
Expand Down Expand Up @@ -231,8 +254,9 @@ end
#Parse chunk input to array of expressions
function parse_input(input::AbstractString)
parsed = Tuple{AbstractString, Any}[]
input = lstrip(input)
n = length(input)
pos = 2 #The first character is extra line end
pos = 1 #The first character is extra line end
while pos n
oldpos = pos
code, pos = parse(input, pos)
Expand All @@ -243,7 +267,7 @@ end


function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")


if !chunk.options[:eval]
chunk.output = ""
Expand Down
2 changes: 1 addition & 1 deletion test/formatter_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ h_ref = """
and some text
"""
@test htext.content == h_ref
@test htext.content[1].content == h_ref
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ using Base.Test
info("Test: Chunk options")
include("chunk_options.jl")

info("Test: Converting")
include("convert_test.jl")
#info("Test: Converting")
#include("convert_test.jl")

info("Test: Caching")
include("cache_test.jl")
#info("Test: Caching")
#include("cache_test.jl")

info("Testing rich output")
include("rich_output.jl")
Expand Down

0 comments on commit 7ebd0b4

Please sign in to comment.