From 0e44f9683cd30bc7a38d63057be8778e82e1dc11 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 11 Apr 2016 18:40:18 +0300 Subject: [PATCH] Make string AbstractStrings --- src/Weave.jl | 20 ++++++++++---------- src/chunks.jl | 32 ++++++++++++++++---------------- src/config.jl | 2 +- src/formatters.jl | 46 +++++++++++++++++++++++----------------------- src/gadfly.jl | 2 +- src/pyplot.jl | 2 +- src/readers.jl | 24 ++++++++++++------------ src/run.jl | 26 +++++++++++++------------- 8 files changed, 77 insertions(+), 77 deletions(-) diff --git a/src/Weave.jl b/src/Weave.jl index 5ba6839..6f37e24 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -6,19 +6,19 @@ using Docile #Contains report global properties type Report <: Display - cwd::String - basename::String + cwd::AbstractString + basename::AbstractString formatdict::Dict{Symbol,Any} - pending_code::String - cur_result::String + pending_code::AbstractString + cur_result::AbstractString fignum::Int - figures::Array{String} + figures::Array{AbstractString} term_state::Symbol cur_chunk end function Report(cwd, basename, formatdict) - Report(cwd, basename, formatdict, "", "", 1, String[], :text, nothing) + Report(cwd, basename, formatdict, "", "", 1, AbstractString[], :text, nothing) end @@ -40,7 +40,7 @@ end @doc "List supported output formats" -> function list_out_formats() for format = keys(formats) - println(string(format,": ", formats[format].description)) + println(AbstractString(format,": ", formats[format].description)) end end @@ -55,7 +55,7 @@ tangle(source ; out_path=:doc, informat="noweb") * `informat`: `"noweb"` of `"markdown"` * `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, -`"somepath"`: Path as a string e.g `"/home/mpastell/weaveout"` +`"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"` """-> function tangle(source ; out_path=:doc, informat="noweb") doc = read_doc(source, informat) @@ -87,7 +87,7 @@ weave(source ; doctype = "pandoc", plotlib="Gadfly", * `plotlib`: `"PyPlot"`, `"Gadfly"`, or `"Winston"` * `informat`: `"noweb"` of `"markdown"` * `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, - `"somepath"`: Path as a string e.g `"/home/mpastell/weaveout"` + `"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"` * `fig_path`: where figures will be generated, relative to out_path * `fig_ext`: Extension for saved figures e.g. `".pdf"`, `".png"`. Default setting depends on `doctype`. * `cache_path`: where of cached output will be saved. @@ -121,7 +121,7 @@ function Base.display(report::Report, m::MIME"text/plain", data) #report.cur_result *= "\n" * s end -function weave(doc::String, doctype::String) +function weave(doc::AbstractString, doctype::AbstractString) weave(doc, doctype=doctype) end diff --git a/src/chunks.jl b/src/chunks.jl index bcd7224..3c58f43 100644 --- a/src/chunks.jl +++ b/src/chunks.jl @@ -1,12 +1,12 @@ type WeaveDoc - source::String - basename::String - path::String + source::AbstractString + basename::AbstractString + path::AbstractString chunks::Array - cwd::String + cwd::AbstractString format - doctype::String + doctype::AbstractString function WeaveDoc(source, chunks) path, fname = splitdir(abspath(source)) basename = splitext(fname)[1] @@ -15,29 +15,29 @@ type WeaveDoc end immutable ChunkOutput - code::String - stdout::String - displayed::String - figures::Array{String} + code::AbstractString + stdout::AbstractString + displayed::AbstractString + figures::Array{AbstractString} end type CodeChunk - content::String + content::AbstractString number::Int result_no::Int start_line::Int - option_string::String + option_AbstractString::AbstractString options::Dict{Symbol, Any} - output::String - figures::Array{String} + output::AbstractString + figures::Array{AbstractString} result::Array{ChunkOutput} - function CodeChunk(content, number, start_line, option_string, options) - new(content, number, 0, start_line, option_string, options, "", String[], ChunkOutput[]) + function CodeChunk(content, number, start_line, option_AbstractString, options) + new(content, number, 0, start_line, option_AbstractString, options, "", AbstractString[], ChunkOutput[]) end end type DocChunk - content::String + content::AbstractString number::Int start_line::Int end diff --git a/src/config.jl b/src/config.jl index f09d15f..560fe2f 100644 --- a/src/config.jl +++ b/src/config.jl @@ -22,7 +22,7 @@ const rcParams = :wrap=> true, :line_width => 75, :engine=> "julia", - #:option_string=> "", + #:option_AbstractString=> "", #Defined in formats :fig_ext => nothing, :fig_pos=> nothing, diff --git a/src/formatters.jl b/src/formatters.jl index 18876cb..025fba3 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -1,6 +1,6 @@ function format(doc::WeaveDoc) - formatted = String[] + formatted = AbstractString[] docformat = doc.format #@show docformat @@ -105,12 +105,12 @@ end function indent(text, nindent) return join(map(x-> - string(repeat(" ", nindent), x), split(text, "\n")), "\n") + AbstractString(repeat(" ", nindent), x), split(text, "\n")), "\n") end function wraplines(text, line_width=75) - result = String[] + result = AbstractString[] lines = split(text, "\n") for line in lines if length(line) > line_width @@ -135,7 +135,7 @@ end type Tex - description::String + description::AbstractString formatdict::Dict{Symbol,Any} end @@ -171,7 +171,7 @@ const texminted = Tex("Latex using minted for highlighting", )) type Markdown - description::String + description::AbstractString formatdict::Dict{Symbol,Any} end @@ -200,7 +200,7 @@ const github = Markdown("Github markdown", type Rest - description::String + description::AbstractString formatdict::Dict{Symbol,Any} end @@ -218,7 +218,7 @@ const rst = Rest("reStructuredText and Sphinx", )) type AsciiDoc - description::String + description::AbstractString formatdict::Dict{Symbol,Any} end @@ -243,7 +243,7 @@ function formatfigures(chunk, docformat::Tex) f_pos = chunk.options[:fig_pos] f_env = chunk.options[:fig_env] result = "" - figstring = "" + figAbstractString = "" @@ -254,19 +254,19 @@ function formatfigures(chunk, docformat::Tex) for fig = fignames if splitext(fig)[2] == ".tex" #Tikz figures - figstring *= "\\resizebox{$width}{!}{\\input{$fig}}\n" + figAbstractString *= "\\resizebox{$width}{!}{\\input{$fig}}\n" else - figstring *= "\\includegraphics[width=$width]{$fig}\n" + figAbstractString *= "\\includegraphics[width=$width]{$fig}\n" end end # Figure environment if caption != nothing - result *= string("\\center\n", - "$figstring", + result *= AbstractString("\\center\n", + "$figAbstractString", "\\caption{$caption}\n") else - result *= figstring + result *= figAbstractString end if chunk.options[:name] != nothing && f_env !=nothing @@ -286,7 +286,7 @@ function formatfigures(chunk, docformat::Markdown) fignames = chunk.figures caption = chunk.options[:fig_cap] result = "" - figstring = "" + figAbstractString = "" length(fignames) > 0 || (return "") @@ -310,18 +310,18 @@ function formatfigures(chunk, docformat::Rest) caption = chunk.options[:fig_cap] width = chunk.options[:out_width] result = "" - figstring = "" + figAbstractString = "" for fig=fignames - figstring *= @sprintf(".. image:: %s\n :width: %s\n\n", fig, width) + figAbstractString *= @sprintf(".. image:: %s\n :width: %s\n\n", fig, width) end if caption != nothing - result *= string(".. figure:: $(fignames[1])\n", + result *= AbstractString(".. figure:: $(fignames[1])\n", " :width: $width\n\n", " $caption\n\n") else - result *= figstring + result *= figAbstractString return result end end @@ -332,27 +332,27 @@ function formatfigures(chunk, docformat::AsciiDoc) caption = chunk.options[:fig_cap] width = chunk.options[:out_width] result = "" - figstring = "" + figAbstractString = "" for fig=fignames - figstring *= @sprintf("image::%s[width=%s]\n", fig, width) + figAbstractString *= @sprintf("image::%s[width=%s]\n", fig, width) end if caption != nothing - result *= string("image::$(fignames[1])", + result *= AbstractString("image::$(fignames[1])", "[width=$width,", "title=\"$caption\"]") else - result *= figstring + result *= figAbstractString return result end end #Add new supported formats here -const formats = @compat Dict{String, Any}("tex" => tex, +const formats = @compat Dict{AbstractString, Any}("tex" => tex, "texminted" => texminted, "pandoc" => pandoc, "github" => github, diff --git a/src/gadfly.jl b/src/gadfly.jl index 3131022..ab84fba 100644 --- a/src/gadfly.jl +++ b/src/gadfly.jl @@ -20,7 +20,7 @@ function Base.display(report::Report, m::MIME"image/png", p::Plot) #report.cur_result *= formatfigures(chunk, docformat) #report.term_state = :fig - # chunk.figures = String[] + # chunk.figures = AbstractString[] #else push!(report.figures, rel_name) #end diff --git a/src/pyplot.jl b/src/pyplot.jl index 1aea609..de36e98 100644 --- a/src/pyplot.jl +++ b/src/pyplot.jl @@ -1,7 +1,7 @@ using PyPlot function savefigs_pyplot(chunk, report::Report) - fignames = String[] + fignames = AbstractString[] ext = report.formatdict[:fig_ext] figpath = joinpath(report.cwd, chunk.options[:fig_path]) isdir(figpath) || mkdir(figpath) diff --git a/src/readers.jl b/src/readers.jl index efd555c..bf10ac6 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -1,7 +1,7 @@ pushopt(options::Dict,expr::Expr) = Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2]) -const input_formats = @compat Dict{String, Any}( +const input_formats = @compat Dict{AbstractString, Any}( "noweb" => Dict{Symbol, Any}( :codestart => r"^<<(.*?)>>=\s*$", :codeend => r"^@\s*$" @@ -14,7 +14,7 @@ const input_formats = @compat Dict{String, Any}( @doc "Read and parse input document" -> -function read_doc(source::String, format="noweb"::String) +function read_doc(source::AbstractString, format="noweb"::AbstractString) document = bytestring(open(source) do io mmap_array(Uint8,(filesize(source),),io) end) @@ -22,8 +22,8 @@ function read_doc(source::String, format="noweb"::String) doc = WeaveDoc(source, parsed) end -@doc "Parse chunks from string" -> -function parse_doc(document::String, format="noweb"::String) +@doc "Parse chunks from AbstractString" -> +function parse_doc(document::AbstractString, format="noweb"::AbstractString) #doctext = readall(open(document)) lines = split(document, "\n") @@ -37,21 +37,21 @@ function parse_doc(document::String, format="noweb"::String) start_line = 0 options = Dict() - optionstring = "" + optionAbstractString = "" parsed = Any[] for lineno in 1:length(lines) line = lines[lineno] if (m = match(codestart, line)) != nothing && state=="doc" state = "code" if m.captures[1] == nothing - optionstring = "" + optionAbstractString = "" else - optionstring=strip(m.captures[1]) + optionAbstractString=strip(m.captures[1]) end - #@show optionstring + #@show optionAbstractString options = Dict{Symbol,Any}() - if length(optionstring) > 0 - expr = parse(optionstring) + if length(optionAbstractString) > 0 + expr = parse(optionAbstractString) Base.Meta.isexpr(expr,:(=)) && (options[expr.args[1]] = expr.args[2]) Base.Meta.isexpr(expr,:toplevel) && map(pushopt,fill(options,length(expr.args)),expr.args) end @@ -70,10 +70,10 @@ function parse_doc(document::String, format="noweb"::String) end if ismatch(codeend, line) && state=="code" - chunk = CodeChunk(content, codeno, start_line, optionstring, options) + chunk = CodeChunk(content, codeno, start_line, optionAbstractString, options) #chunk = @compat Dict{Symbol,Any}(:type => "code", :content => content, # :number => codeno, :options => options, -# :optionstring => optionstring, +# :optionAbstractString => optionAbstractString, # :start_line => start_line) codeno+=1 diff --git a/src/run.jl b/src/run.jl index 150c3a7..b3aaf79 100644 --- a/src/run.jl +++ b/src/run.jl @@ -11,7 +11,7 @@ function run(doc::WeaveDoc; doctype = "pandoc", plotlib="Gadfly", informat="nowe * `plotlib`: `"PyPlot"`, `"Gadfly"`, or `"Winston"` * `informat`: `"noweb"` of `"markdown"` * `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory, -`"somepath"`: Path as a string e.g `"/home/mpastell/weaveout"` +`"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"` * `fig_path`: where figures will be generated, relative to out_path * `fig_ext`: Extension for saved figures e.g. `".pdf"`, `".png"`. Default setting depends on `doctype`. * `cache_path`: where of cached output will be saved. @@ -89,7 +89,7 @@ end function reset_report(report::Report) report.cur_result = "" - report.figures = String[] + report.figures = AbstractString[] report.term_state = :text end @@ -131,7 +131,7 @@ function run_block(chunk::CodeChunk, report::Report, SandBox::Module) else @show input content = "\n" * input * str_expr - rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) input = "" rchunk.result_no = result_no result_no *=1 @@ -162,7 +162,7 @@ function run_term(chunk::CodeChunk, report::Report, SandBox::Module) output *= prompt * str_expr else content = prompt * output * str_expr - rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) rchunk.output = content * out * displayed @show rchunk.output output = "" @@ -193,14 +193,14 @@ function run_term2(code_str, report::Report, SandBox::Module) code, pos = parse(code_str, pos) report.term_state == :fig && (report.cur_result*= codestart) - prompts = string(prompt, rstrip(code_str[oldpos:(pos-1)]), "\n") + prompts = AbstractString(prompt, rstrip(code_str[oldpos:(pos-1)]), "\n") report.cur_result *= prompts report.term_state = :text s = eval(SandBox, code) s != nothing && display(s) end - return string(report.cur_result) + return AbstractString(report.cur_result) end function capture_output(expr::Expr, SandBox::Module, term, plotlib) @@ -226,8 +226,8 @@ end #Parse chunk input to array of expressions -function parse_input(input::String) - parsed = (String, Expr)[] +function parse_input(input::AbstractString) + parsed = (AbstractString, Expr)[] n = length(input) pos = 2 #The first character is extra line end while pos < n @@ -281,7 +281,7 @@ end function clear_sandbox(SandBox::Module) for name = names(SandBox, true) if name != :eval && name != names(SandBox)[1] - try eval(SandBox, parse(string(string(name), "=nothing"))) end + try eval(SandBox, parse(AbstractString(AbstractString(name), "=nothing"))) end end end end @@ -354,7 +354,7 @@ function collect_results(chunk::CodeChunk, fmt::ScriptResult) content *= r.code else content = "\n" * content * r.code - rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) content = "" rchunk.result_no = result_no result_no *=1 @@ -364,7 +364,7 @@ function collect_results(chunk::CodeChunk, fmt::ScriptResult) end end if content != "" - rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) push!(result_chunks, rchunk) end @@ -380,7 +380,7 @@ function collect_results(chunk::CodeChunk, fmt::TermResult) output *= prompt * r.code output *= r.displayed * r.stdout if !isempty(r.figures) - rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) rchunk.output = output output = "" rchunk.figures = r.figures @@ -388,7 +388,7 @@ function collect_results(chunk::CodeChunk, fmt::TermResult) end end if output != "" - rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_string, copy(chunk.options)) + rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options)) rchunk.output = output push!(result_chunks, rchunk) end