Index: lib/hikidoc.rb =================================================================== --- lib/hikidoc.rb (リビジョン 46) +++ lib/hikidoc.rb (作業コピー) @@ -60,7 +60,7 @@ @output.reset escape_plugin_blocks(src) {|escaped| compile_blocks escaped - @output.string + @output.output } end @@ -97,10 +97,7 @@ end end buf << s.rest - buf = yield(buf) - buf.gsub(/\0(\d+)\0/) { - @output.inline_plugin(plugin_block($1.to_i)) - } + yield(buf) end def restore_plugin_block(str) @@ -109,6 +106,18 @@ } end + def evaluate_plugin_block(str) + result = @output.container + str.split(/(\0\d+\0)/).each do |s| + if s[0, 1] == "\0" and s[-1, 1] == "\0" + result << @output.inline_plugin(plugin_block(s[1..-2].to_i)) + else + yield(s, result) + end + end + result + end + def plugin_block(id) @plugin_blocks[id] or raise "must not happen: #{id.inspect}" end @@ -341,12 +350,12 @@ def compile_paragraph(f) str = f.break(PARAGRAPH_END_RE)\ .reject {|line| COMMENT_RE =~ line }\ - .map {|line| compile_inline(strip(line)) }\ + .map {|line| compile_inline(strip(line), false) }\ .join("\n") if /\A\0(\d+)\0\z/ =~ str @output.block_plugin plugin_block($1.to_i) else - @output.paragraph str + @output.paragraph(evaluate_plugin_block(str) {|s, buf| buf << s}) end end @@ -357,12 +366,25 @@ BRACKET_LINK_RE = /\[\[.+\]\]/ URI_RE = /(?:https?|ftp|file|mailto):[A-Za-z0-9;\/?:@&=+$,\-_.!~*\'()#%]+/ - def compile_inline(str) + def compile_inline(str, eval_plugin=true) + s = "" + if eval_plugin + buf = evaluate_plugin_block(str) do |s, buf| + s = compile_inline_content(s, buf) + end + else + buf = @output.container + s = compile_inline_content(str, buf) + end + buf << @output.text(s) + buf + end + + def compile_inline_content(str, buf) re = / (#{BRACKET_LINK_RE}) | (#{URI_RE}) | (#{MODIFIER_RE}) /xo - buf = "" while m = re.match(str) buf << @output.text(m.pre_match) case @@ -377,8 +399,7 @@ end str = m.post_match end - buf << @output.text(str) - buf + str end def compile_bracket_link(link) @@ -430,7 +451,7 @@ } def compile_modifier(str) - buf = "" + buf = @output.container while m = / (#{MODIFIER_RE}) /xo.match(str) buf << @output.text(m.pre_match) @@ -484,10 +505,18 @@ @f = StringIO.new end + def output + string + end + def string @f.string end + def container + "" + end + # # Procedures #