mirror of https://github.com/mpastell/Weave.jl
Added rst format, closes #14
parent
99d7a5ebb7
commit
4a3cad3118
|
@ -128,6 +128,12 @@ function run_block(code_str)
|
|||
end
|
||||
|
||||
function run_term(code_str)
|
||||
prompt = "\njulia> "
|
||||
codestart = "\n\n"*report.formatdict[:codestart]
|
||||
|
||||
if haskey(report.formatdict, :indent)
|
||||
prompt = indent(prompt, report.formatdict[:indent])
|
||||
end
|
||||
|
||||
#Emulate terminal
|
||||
n = length(code_str)
|
||||
|
@ -135,9 +141,11 @@ function run_term(code_str)
|
|||
while pos < n
|
||||
oldpos = pos
|
||||
code, pos = parse(code_str, pos)
|
||||
report.term_state == :fig && (report.cur_result*="\n\n"*report.formatdict[:codestart])
|
||||
prompts = string("\njulia> ", rstrip(code_str[oldpos:(pos-1)]), "\n")
|
||||
report.cur_result *= prompts
|
||||
|
||||
|
||||
report.term_state == :fig && (report.cur_result*= codestart)
|
||||
prompts = string(prompt, rstrip(code_str[oldpos:(pos-1)]), "\n")
|
||||
report.cur_result *= prompts
|
||||
report.term_state = :text
|
||||
s = eval(ReportSandBox, code)
|
||||
s != nothing && display(s)
|
||||
|
@ -172,6 +180,9 @@ function run(parsed)
|
|||
report.figures = String[]
|
||||
report.cur_chunk = chunk
|
||||
report.term_state = :text
|
||||
if haskey(report.formatdict, :out_width) && chunk[:out_width] == nothing
|
||||
chunk[:out_width] = report.formatdict[:out_width]
|
||||
end
|
||||
|
||||
if chunk[:term]
|
||||
chunk[:result] = run_term(chunk[:content])
|
||||
|
@ -222,6 +233,8 @@ function Base.display(report::Report, m::MIME"text/plain", data)
|
|||
end
|
||||
|
||||
s = reprmime(m, data)
|
||||
haskey(report.formatdict, :indent) && (s = indent(s, report.formatdict[:indent]))
|
||||
|
||||
report.cur_result *= s * "\n"
|
||||
|
||||
if report.term_state == :fig #Catch Winston plot command output
|
||||
|
|
|
@ -8,7 +8,7 @@ const rcParams =
|
|||
:fig=> true,
|
||||
:include=> true,
|
||||
:eval => true,
|
||||
:fig_cap=> false,
|
||||
:fig_cap=> nothing,
|
||||
#Size in inches
|
||||
:fig_width => 6,
|
||||
:fig_height => 4,
|
||||
|
|
|
@ -46,6 +46,10 @@ end
|
|||
function format_codechunk(chunk, formatdict)
|
||||
|
||||
|
||||
if haskey(formatdict, :indent)
|
||||
chunk[:content] = indent(chunk[:content], formatdict[:indent])
|
||||
end
|
||||
|
||||
if !chunk[:eval]
|
||||
if chunk[:echo]
|
||||
result = "$(formatdict[:codestart])$(chunk[:content])$(formatdict[:codeend])"
|
||||
|
@ -72,9 +76,11 @@ function format_codechunk(chunk, formatdict)
|
|||
if (strip(chunk[:result])!= "") && (chunk[:results] != "hidden")
|
||||
#@show chunk
|
||||
if chunk[:results] != "markup"
|
||||
haskey(formatdict, :indent) && (chunk[:result] = indent(chunk[:result]))
|
||||
result *= "$(chunk[:result])"
|
||||
elseif chunk[:results] == "markup"
|
||||
if haskey(formatdict, :indent)
|
||||
chunk[:result] = indent(chunk[:result], formatdict[:indent])
|
||||
end
|
||||
result *= "$(formatdict[:outputstart])$(chunk[:result])\n$(formatdict[:outputend])\n"
|
||||
end
|
||||
end
|
||||
|
@ -89,8 +95,6 @@ end
|
|||
|
||||
function format_termchunk(chunk, formatdict)
|
||||
if chunk[:echo] && chunk[:results] != "hidden"
|
||||
haskey(formatdict, :termindent) && (chunk[:result] = indent(chunk[:result]))
|
||||
|
||||
result = "$(formatdict[:termstart])$(chunk[:result])\n"
|
||||
@show chunk[:term_state]
|
||||
chunk[:term_state] == :text && (result*= "$(formatdict[:termend])\n")
|
||||
|
@ -101,7 +105,8 @@ return result
|
|||
end
|
||||
|
||||
function indent(text, nindent)
|
||||
return text
|
||||
return join(map(x->
|
||||
string(repeat(" ", nindent), x), split(text, "\n")), "\n")
|
||||
end
|
||||
|
||||
|
||||
|
@ -140,17 +145,19 @@ type Markdown
|
|||
formatdict::Dict{Symbol,Any}
|
||||
end
|
||||
|
||||
const pandoc = Markdown(@compat Dict{Symbol,Any}(:codestart => "~~~~{.julia}",
|
||||
:codeend=>"~~~~~~~~~~~~~\n\n",
|
||||
:outputstart=>"~~~~{.julia}",
|
||||
:outputend=>"~~~~~~~~~~~~~\n\n",
|
||||
:fig_ext=>".png",
|
||||
:extension=>"md",
|
||||
:doctype=>"pandoc"
|
||||
const pandoc = Markdown(@compat Dict{Symbol,Any}(
|
||||
:codestart => "~~~~{.julia}",
|
||||
:codeend=>"~~~~~~~~~~~~~\n\n",
|
||||
:outputstart=>"~~~~{.julia}",
|
||||
:outputend=>"~~~~~~~~~~~~~\n\n",
|
||||
:fig_ext=>".png",
|
||||
:extension=>"md",
|
||||
:doctype=>"pandoc"
|
||||
))
|
||||
|
||||
|
||||
const github = Markdown(@compat Dict{Symbol,Any}(:codestart => "````julia",
|
||||
const github = Markdown(@compat Dict{Symbol,Any}(
|
||||
:codestart => "````julia",
|
||||
:codeend=> "````\n\n",
|
||||
:outputstart=> "````julia",
|
||||
:outputend=> "````\n\n",
|
||||
|
@ -160,6 +167,23 @@ const github = Markdown(@compat Dict{Symbol,Any}(:codestart => "````julia",
|
|||
))
|
||||
|
||||
|
||||
type Rest
|
||||
formatdict::Dict{Symbol,Any}
|
||||
end
|
||||
|
||||
const rst = Rest(@compat Dict{Symbol,Any}(
|
||||
:codestart => ".. code-block:: julia\n",
|
||||
:codeend => "\n\n",
|
||||
:outputstart => "::\n",
|
||||
:outputend => "\n\n",
|
||||
:indent=> 4,
|
||||
:fig_ext => ".png",
|
||||
:extension => "rst",
|
||||
:out_width => "15 cm",
|
||||
:doctype => "rst"
|
||||
))
|
||||
|
||||
|
||||
|
||||
function formatfigures(chunk, docformat::Tex)
|
||||
fignames = chunk[:figure]
|
||||
|
@ -178,7 +202,7 @@ function formatfigures(chunk, docformat::Tex)
|
|||
|
||||
|
||||
for fig = fignames
|
||||
figstring *= "\\includegraphics[width= $width]{$fig}\n"
|
||||
figstring *= "\\includegraphics[width=$width]{$fig}\n"
|
||||
end
|
||||
|
||||
# Figure environment
|
||||
|
@ -211,7 +235,7 @@ function formatfigures(chunk, docformat::Markdown)
|
|||
|
||||
length(fignames) > 0 || (return "")
|
||||
|
||||
if caption != false
|
||||
if caption != nothing
|
||||
result *= "![$caption]($(fignames[1]))\n"
|
||||
for fig = fignames[2:end]
|
||||
result *= "![]($fig)\n"
|
||||
|
@ -226,9 +250,32 @@ function formatfigures(chunk, docformat::Markdown)
|
|||
end
|
||||
|
||||
|
||||
function formatfigures(chunk, docformat::Rest)
|
||||
fignames = chunk[:figure]
|
||||
caption = chunk[:fig_cap]
|
||||
width = chunk[:out_width]
|
||||
result = ""
|
||||
figstring = ""
|
||||
|
||||
for fig=fignames
|
||||
figstring *= @sprintf(".. image:: %s\n :width: %s\n\n", fig, width)
|
||||
end
|
||||
|
||||
if caption != nothing
|
||||
result *= string(".. figure:: $(fignames[1])\n",
|
||||
" :width: $width\n\n",
|
||||
" $caption\n\n")
|
||||
else
|
||||
result *= figstring
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#Add new supported formats here
|
||||
const formats = @compat Dict{String, Any}("tex" => tex,
|
||||
"texminted" => texminted,
|
||||
"pandoc" => pandoc,
|
||||
"github" => github
|
||||
"github" => github,
|
||||
"rst" => rst
|
||||
)
|
||||
|
|
|
@ -22,6 +22,7 @@ function Base.display(report::Report, m::MIME"image/png", p::Plot)
|
|||
if report.term_state == :text
|
||||
report.cur_result *= "\n" * report.formatdict[:codeend]
|
||||
end
|
||||
|
||||
|
||||
report.cur_result *= formatfigures(chunk, docformat)
|
||||
report.term_state = :fig
|
||||
|
|
Loading…
Reference in New Issue