From 6684ac810d608f18c994ad0cb544427e2a7bab6b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 Feb 2014 23:18:41 +0900 Subject: [PATCH 001/214] for debug --- bin/review-compile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/review-compile b/bin/review-compile index 8c3713cf0..4b9ffc617 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -22,10 +22,10 @@ require 'fileutils' require 'optparse' def main - Signal.trap(:INT) { exit 1 } - if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/ - Signal.trap(:PIPE, 'IGNORE') - end +# Signal.trap(:INT) { exit 1 } +# if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/ +# Signal.trap(:PIPE, 'IGNORE') +# end _main rescue Errno::EPIPE exit 0 From 75944683f13646703ee84003571480ab536fd01a Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 Feb 2014 23:23:11 +0900 Subject: [PATCH 002/214] add task to compile kpeg --- Rakefile | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Rakefile b/Rakefile index 679d22db8..c35649944 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +require 'fileutils' + begin require 'bundler' Bundler::GemHelper.install_tasks @@ -11,6 +13,15 @@ require 'rake/clean' task :default => [:test] +task :kpeg do + FileUtils.rm_f "lib/review/compiler.rb" + FileUtils.rm_f "lib/review/compiler/literals_1_8.rb" + FileUtils.rm_f "lib/review/compiler/literals_1_9.rb" + sh "kpeg -s lib/review/compiler/literals_1_8.kpeg -o lib/review/compiler/literals_1_8.rb" + sh "kpeg -s lib/review/compiler/literals_1_9.kpeg -o lib/review/compiler/literals_1_9.rb" + sh "kpeg -s lib/review/review.kpeg -o lib/review/compiler.rb" +end + Rake::TestTask.new("test") do |t| t.libs << "test" t.test_files = Dir.glob("test/**/test_*.rb") From 12a6c4aaa080f7103cff88f1333e2745468b9bb3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 Feb 2014 23:23:43 +0900 Subject: [PATCH 003/214] remove unused methods in LineInput --- lib/lineinput.rb | 68 ------------------------------------------------ 1 file changed, 68 deletions(-) diff --git a/lib/lineinput.rb b/lib/lineinput.rb index f24d9c77c..885c59c8e 100644 --- a/lib/lineinput.rb +++ b/lib/lineinput.rb @@ -48,46 +48,6 @@ def ungets(line) line end - def peek - line = gets() - ungets line if line - line - end - - def next? - peek() ? true : false - end - - def skip_blank_lines - n = 0 - while line = gets() - unless line.strip.empty? - ungets line - return n - end - n += 1 - end - n - end - - def gets_if(re) - line = gets() - if not line or not (re =~ line) - ungets line - return nil - end - line - end - - def gets_unless(re) - line = gets() - if not line or re =~ line - ungets line - return nil - end - line - end - def each while line = gets() yield line @@ -105,16 +65,6 @@ def while_match(re) nil end - def getlines_while(re) - buf = [] - while_match(re) do |line| - buf.push line - end - buf - end - - alias span getlines_while # from Haskell - def until_match(re) while line = gets() if re =~ line @@ -126,16 +76,6 @@ def until_match(re) nil end - def getlines_until(re) - buf = [] - until_match(re) do |line| - buf.push line - end - buf - end - - alias break getlines_until # from Haskell - def until_terminator(re) while line = gets() return if re =~ line # discard terminal line @@ -144,12 +84,4 @@ def until_terminator(re) nil end - def getblock(term_re) - buf = [] - until_terminator(term_re) do |line| - buf.push line - end - buf - end - end From 10dac4a299651d6b04a72418d62848ab9f0e6dae Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 1 Mar 2014 12:20:07 +0900 Subject: [PATCH 004/214] use kpeg parser --- lib/review/compiler.rb | 2643 ++++++++++++++++++++++++++++++++++++---- lib/review/location.rb | 24 + lib/review/review.kpeg | 520 ++++++++ 3 files changed, 2933 insertions(+), 254 deletions(-) create mode 100644 lib/review/location.rb create mode 100644 lib/review/review.kpeg diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index c6315e930..5ccef7791 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1,51 +1,381 @@ -# encoding: utf-8 -# -# Copyright (c) 2002-2007 Minero Aoki -# Copyright (c) 2009-2010 Minero Aoki, Kenshi Muto -# -# This program is free software. -# You can distribute or modify this program under the terms of -# the GNU LGPL, Lesser General Public License version 2.1. -# +class ReVIEW::Compiler + # :stopdoc: + + # This is distinct from setup_parser so that a standalone parser + # can redefine #initialize and still have access to the proper + # parser setup code. + def initialize(str, debug=false) + setup_parser(str, debug) + end -require 'review/extentions' -require 'review/preprocessor' -require 'review/exception' -require 'lineinput' -module ReVIEW - class Location - def initialize(filename, f) - @filename = filename - @f = f + # Prepares for parsing +str+. If you define a custom initialize you must + # call this method before #parse + def setup_parser(str, debug=false) + set_string str, 0 + @memoizations = Hash.new { |h,k| h[k] = {} } + @result = nil + @failed_rule = nil + @failing_rule_offset = -1 + + setup_foreign_grammar + end + + attr_reader :string + attr_reader :failing_rule_offset + attr_accessor :result, :pos + + def current_column(target=pos) + if c = string.rindex("\n", target-1) + return target - c - 1 + end + + target + 1 + end + + def current_line(target=pos) + cur_offset = 0 + cur_line = 0 + + string.each_line do |line| + cur_line += 1 + cur_offset += line.size + return cur_line if cur_offset >= target + end + + -1 + end + + def lines + lines = [] + string.each_line { |l| lines << l } + lines + end + + + + def get_text(start) + @string[start..@pos-1] + end + + # Sets the string and current parsing position for the parser. + def set_string string, pos + @string = string + @string_size = string ? string.size : 0 + @pos = pos + end + + def show_pos + width = 10 + if @pos < width + "#{@pos} (\"#{@string[0,@pos]}\" @ \"#{@string[@pos,width]}\")" + else + "#{@pos} (\"... #{@string[@pos - width, width]}\" @ \"#{@string[@pos,width]}\")" + end + end + + def failure_info + l = current_line @failing_rule_offset + c = current_column @failing_rule_offset + + if @failed_rule.kind_of? Symbol + info = self.class::Rules[@failed_rule] + "line #{l}, column #{c}: failed rule '#{info.name}' = '#{info.rendered}'" + else + "line #{l}, column #{c}: failed rule '#{@failed_rule}'" + end + end + + def failure_caret + l = current_line @failing_rule_offset + c = current_column @failing_rule_offset + + line = lines[l-1] + "#{line}\n#{' ' * (c - 1)}^" + end + + def failure_character + l = current_line @failing_rule_offset + c = current_column @failing_rule_offset + lines[l-1][c-1, 1] + end + + def failure_oneline + l = current_line @failing_rule_offset + c = current_column @failing_rule_offset + + char = lines[l-1][c-1, 1] + + if @failed_rule.kind_of? Symbol + info = self.class::Rules[@failed_rule] + "@#{l}:#{c} failed rule '#{info.name}', got '#{char}'" + else + "@#{l}:#{c} failed rule '#{@failed_rule}', got '#{char}'" + end + end + + class ParseError < RuntimeError + end + + def raise_error + raise ParseError, failure_oneline + end + + def show_error(io=STDOUT) + error_pos = @failing_rule_offset + line_no = current_line(error_pos) + col_no = current_column(error_pos) + + io.puts "On line #{line_no}, column #{col_no}:" + + if @failed_rule.kind_of? Symbol + info = self.class::Rules[@failed_rule] + io.puts "Failed to match '#{info.rendered}' (rule '#{info.name}')" + else + io.puts "Failed to match rule '#{@failed_rule}'" + end + + io.puts "Got: #{string[error_pos,1].inspect}" + line = lines[line_no-1] + io.puts "=> #{line}" + io.print(" " * (col_no + 3)) + io.puts "^" + end + + def set_failed_rule(name) + if @pos > @failing_rule_offset + @failed_rule = name + @failing_rule_offset = @pos + end + end + + attr_reader :failed_rule + + def match_string(str) + len = str.size + if @string[pos,len] == str + @pos += len + return str + end + + return nil + end + + def scan(reg) + if m = reg.match(@string[@pos..-1]) + width = m.end(0) + @pos += width + return true + end + + return nil + end + + if "".respond_to? :ord + def get_byte + if @pos >= @string_size + return nil + end + + s = @string[@pos].ord + @pos += 1 + s + end + else + def get_byte + if @pos >= @string_size + return nil + end + + s = @string[@pos] + @pos += 1 + s + end + end + + def parse(rule=nil) + # We invoke the rules indirectly via apply + # instead of by just calling them as methods because + # if the rules use left recursion, apply needs to + # manage that. + + if !rule + apply(:_root) + else + method = rule.gsub("-","_hyphen_") + apply :"_#{method}" + end end - attr_reader :filename + class MemoEntry + def initialize(ans, pos) + @ans = ans + @pos = pos + @result = nil + @set = false + @left_rec = false + end + + attr_reader :ans, :pos, :result, :set + attr_accessor :left_rec - def lineno - @f.lineno + def move!(ans, pos, result) + @ans = ans + @pos = pos + @result = result + @set = true + @left_rec = false + end end - def string + def external_invoke(other, rule, *args) + old_pos = @pos + old_string = @string + + set_string other.string, other.pos + begin - "#{@filename}:#{@f.lineno}" - rescue - "#{@filename}:nil" + if val = __send__(rule, *args) + other.pos = @pos + other.result = @result + else + other.set_failed_rule "#{self.class}##{rule}" + end + val + ensure + set_string old_string, old_pos end end - alias to_s string - end + def apply_with_args(rule, *args) + memo_key = [rule, args] + if m = @memoizations[memo_key][@pos] + @pos = m.pos + if !m.set + m.left_rec = true + return nil + end + + @result = m.result + + return m.ans + else + m = MemoEntry.new(nil, @pos) + @memoizations[memo_key][@pos] = m + start_pos = @pos + + ans = __send__ rule, *args + + lr = m.left_rec + + m.move! ans, @pos, @result + + # Don't bother trying to grow the left recursion + # if it's failing straight away (thus there is no seed) + if ans and lr + return grow_lr(rule, args, start_pos, m) + else + return ans + end + + return ans + end + end + + def apply(rule) + if m = @memoizations[rule][@pos] + @pos = m.pos + if !m.set + m.left_rec = true + return nil + end + + @result = m.result + + return m.ans + else + m = MemoEntry.new(nil, @pos) + @memoizations[rule][@pos] = m + start_pos = @pos + + ans = __send__ rule + + lr = m.left_rec + + m.move! ans, @pos, @result + + # Don't bother trying to grow the left recursion + # if it's failing straight away (thus there is no seed) + if ans and lr + return grow_lr(rule, nil, start_pos, m) + else + return ans + end + + return ans + end + end + + def grow_lr(rule, args, start_pos, m) + while true + @pos = start_pos + @result = m.result + + if args + ans = __send__ rule, *args + else + ans = __send__ rule + end + return nil unless ans + + break if @pos <= m.pos + + m.move! ans, @pos, @result + end + + @result = m.result + @pos = m.pos + return m.ans + end + class RuleInfo + def initialize(name, rendered) + @name = name + @rendered = rendered + end - class Compiler + attr_reader :name, :rendered + end - def initialize(strategy) - @strategy = strategy + def self.rule_info(name, rendered) + RuleInfo.new(name, rendered) end - attr_reader :strategy + + # :startdoc: + + + + class Error; end + +require 'review/location' +require 'review/extentions' +require 'review/preprocessor' +require 'review/exception' + require 'lineinput' + if RUBY_VERSION > '1.9' then + require 'review/compiler/literals_1_9' + else + require 'review/compiler/literals_1_8' + end + + ## redifine Compiler.new + def initialize(strategy) + @strategy = strategy + end + + attr_accessor :strategy def compile(chap) @chapter = chap @@ -53,6 +383,23 @@ def compile(chap) @strategy.result end + def do_compile + f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(@chapter.content))) + @strategy.bind self, @chapter, ReVIEW::Location.new(@chapter.basename, f) + setup_parser(@chapter.content) + parse() + end + + def text(str) + st = @strategy.dup + f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(str))) + st.bind self, @chapter, ReVIEW::Location.new(@chapter.basename, f) + parser = ReVIEW::Compiler.new(st) + parser.setup_parser(str) + parser.parse("InlineElementContents") + st.result + end + class SyntaxElement def initialize(name, type, argc, &block) @name = name @@ -90,15 +437,15 @@ def block_allowed? SYNTAX = {} - def Compiler.defblock(name, argc, optional = false, &block) + def self.defblock(name, argc, optional = false, &block) defsyntax name, (optional ? :optional : :block), argc, &block end - def Compiler.defsingle(name, argc, &block) + def self.defsingle(name, argc, &block) defsyntax name, :line, argc, &block end - def Compiler.defsyntax(name, type, argc, &block) + def self.defsyntax(name, type, argc, &block) SYNTAX[name] = SyntaxElement.new(name, type, argc, &block) end @@ -120,7 +467,7 @@ def initialize(name) INLINE = {} - def Compiler.definline(name) + def self.definline(name) INLINE[name] = InlineSyntaxElement.new(name) end @@ -218,61 +565,62 @@ def inline_defined?(name) definline :comment definline :include - private - def do_compile - f = LineInput.new(Preprocessor::Strip.new(StringIO.new(@chapter.content))) - @strategy.bind self, @chapter, Location.new(@chapter.basename, f) - tagged_section_init - while f.next? - case f.peek - when /\A\#@/ - f.gets # Nothing to do - when /\A=+[\[\s\{]/ - compile_headline f.gets - when %r<\A\s+\*> - compile_ulist f - when %r<\A\s+\d+\.> - compile_olist f - when %r<\A\s*:\s> - compile_dlist f - when %r<\A//\}> - f.gets - error 'block end seen but not opened' - when %r<\A//[a-z]+> - name, args, lines = read_command(f) + def tagged_section_init + @tagged_section = [] + end + + def open_tagged_section(tag, level, label, caption) + mid = "#{tag}_begin" + unless @strategy.respond_to?(mid) + error "strategy does not support tagged section: #{tag}" + return + end + @tagged_section.push [tag, level] + @strategy.__send__ mid, level, label, caption + end + + def close_tagged_section(tag, level) + mid = "#{tag}_end" + if @strategy.respond_to?(mid) + @strategy.__send__ mid, level + else + error "strategy does not support block op: #{mid}" + end + end + + def close_all_tagged_section + until @tagged_section.empty? + close_tagged_section(* @tagged_section.pop) + end + end + + def compile_command(name, args, lines) syntax = syntax_descriptor(name) - unless syntax - error "unknown command: //#{name}" - compile_unknown_command args, lines - next - end - compile_command syntax, args, lines - when %r<\A//> - line = f.gets - warn "`//' seen but is not valid command: #{line.strip.inspect}" - if block_open?(line) - warn "skipping block..." - read_block(f) - end - else - if f.peek.strip.empty? - f.gets - next - end - compile_paragraph f + unless @strategy.respond_to?(syntax.name) + error "strategy does not support command: //#{syntax.name}" + compile_unknown_command args, lines + return + end + begin + syntax.check_args args + rescue CompileError => err + error err.message + args = ['(NoArgument)'] * syntax.min_argc + end + if syntax.block_allowed? + compile_block syntax, args, lines + else + if lines + error "block is not allowed for command //#{syntax.name}; ignore" end + compile_single syntax, args end - close_all_tagged_section end - def compile_headline(line) - @headline_indexs ||= [@chapter.number.to_i - 1] - m = /\A(=+)(?:\[(.+?)\])?(?:\{(.+?)\})?(.*)/.match(line) - level = m[1].size - tag = m[2] - label = m[3] - caption = m[4].strip + def compile_headline(level, tag, label, caption) + @headline_indexs ||= [0] ## XXX + caption.strip! index = level - 1 if tag if tag !~ /\A\// @@ -293,10 +641,6 @@ def compile_headline(line) @headline_indexs[index] = 0 if @headline_indexs[index].nil? @headline_indexs[index] += 1 close_current_tagged_section(level) - if ReVIEW.book.param["hdnumberingmode"] - caption = @chapter.on_CHAPS? ? "#{@headline_indexs.join('.')} #{caption}" : caption - warn "--hdnumberingmode is deprecated. use --level option." - end @strategy.headline level, label, caption end end @@ -307,56 +651,17 @@ def close_current_tagged_section(level) end end - def headline(level, label, caption) - @strategy.headline level, label, caption - end - - def tagged_section_init - @tagged_section = [] - end - - def open_tagged_section(tag, level, label, caption) - mid = "#{tag}_begin" - unless @strategy.respond_to?(mid) - error "strategy does not support tagged section: #{tag}" - headline level, label, caption - return - end - @tagged_section.push [tag, level] - @strategy.__send__ mid, level, label, caption - end - - def close_tagged_section(tag, level) - mid = "#{tag}_end" - if @strategy.respond_to?(mid) - @strategy.__send__ mid, level - else - error "strategy does not support block op: #{mid}" - end - end - - def close_all_tagged_section - until @tagged_section.empty? - close_tagged_section(* @tagged_section.pop) - end + def comment(text) + @strategy.comment(text) end - def compile_ulist(f) + def compile_ulist(elem) level = 0 - f.while_match(/\A\s+\*|\A\#@/) do |line| - next if line =~ /\A\#@/ - - buf = [text(line.sub(/\*+/, '').strip)] - f.while_match(/\A\s+(?!\*)\S/) do |cont| - buf.push text(cont.strip) - end - - line =~ /\A\s+(\*+)/ - current_level = $1.size + elem.each do |current_level, buf| if level == current_level @strategy.ul_item_end # body - @strategy.ul_item_begin buf + @strategy.ul_item_begin [buf] elsif level < current_level # down level_diff = current_level - level level = current_level @@ -365,7 +670,7 @@ def compile_ulist(f) @strategy.ul_item_begin [] end @strategy.ul_begin {level} - @strategy.ul_item_begin buf + @strategy.ul_item_begin [buf] elsif level > current_level # up level_diff = level - current_level level = current_level @@ -375,7 +680,7 @@ def compile_ulist(f) end @strategy.ul_item_end # body - @strategy.ul_item_begin buf + @strategy.ul_item_begin [buf] end end @@ -385,166 +690,1996 @@ def compile_ulist(f) end end - def compile_olist(f) + def compile_olist(elem) @strategy.ol_begin - f.while_match(/\A\s+\d+\.|\A\#@/) do |line| - next if line =~ /\A\#@/ - - num = line.match(/(\d+)\./)[1] - buf = [text(line.sub(/\d+\./, '').strip)] - f.while_match(/\A\s+(?!\d+\.)\S/) do |cont| - buf.push text(cont.strip) - end + elem.each do |num, buf| @strategy.ol_item buf, num end @strategy.ol_end end - def compile_dlist(f) - @strategy.dl_begin - while /\A\s*:/ =~ f.peek - @strategy.dt text(f.gets.sub(/\A\s*:/, '').strip) - @strategy.dd f.break(/\A(\S|\s*:)/).map {|line| text(line.strip) } - f.skip_blank_lines - end - @strategy.dl_end + + def compile_unknown_command(args, lines) + @strategy.unknown_command args, lines end - def compile_paragraph(f) - buf = [] - f.until_match(%r<\A//|\A\#@>) do |line| - break if line.strip.empty? - buf.push text(line.sub(/^(\t+)\s*/) {|m| "" * m.size}.strip.gsub(//, "\t")) - end - @strategy.paragraph buf + def compile_block(syntax, args, lines) + @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) end - def read_command(f) - line = f.gets - name = line.slice(/[a-z]+/).intern - args = parse_args(line.sub(%r<\A//[a-z]+>, '').rstrip.chomp('{'), name) - lines = block_open?(line) ? read_block(f) : nil - return name, args, lines + def default_block(syntax) + if syntax.block_required? + error "block is required for //#{syntax.name}; use empty block" + end + [] end - def block_open?(line) - line.rstrip[-1,1] == '{' + def compile_single(syntax, args) + @strategy.__send__(syntax.name, *args) end - def read_block(f) - head = f.lineno - buf = [] - f.until_match(%r<\A//\}>) do |line| - unless line =~ /\A\#@/ - buf.push text(line.rstrip) - end + + + def compile_inline(op, arg) + unless inline_defined?(op) + raise CompileError, "no such inline op: #{op}" end - unless %r<\A//\}> =~ f.peek - error "unexpected EOF (block begins at: #{head})" - return buf + unless @strategy.respond_to?("inline_#{op}") + raise "strategy does not support inline op: @<#{op}>" end - f.gets # discard terminator - buf + @strategy.__send__("inline_#{op}", arg) + rescue => err + error err.message end - def parse_args(str, name=nil) - return [] if str.empty? - scanner = StringScanner.new(str) - words = [] - while word = scanner.scan(/(\[\]|\[.*?[^\\]\])/) - w2 = word[1..-2].gsub(/\\(.)/){ - ch = $1 - (ch == "]" or ch == "\\") ? ch : "\\" + ch - } - words << w2 - end - if !scanner.eos? - error "argument syntax error: #{scanner.rest} in #{str.inspect}" - return [] - end - return words + def compile_paragraph(buf) + @strategy.paragraph buf end - def compile_command(syntax, args, lines) - unless @strategy.respond_to?(syntax.name) - error "strategy does not support command: //#{syntax.name}" - compile_unknown_command args, lines - return + def warn(msg) + @strategy.warn msg + end + + def error(msg) + @strategy.error msg + end + + + + + # :stopdoc: + def setup_foreign_grammar + @_grammar_literals = ReVIEW::Compiler::Literals.new(nil) + end + + # root = Start + def _root + _tmp = apply(:_Start) + set_failed_rule :_root unless _tmp + return _tmp + end + + # Start = &. { tagged_section_init } Block* { close_all_tagged_section } + def _Start + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = get_byte + self.pos = _save1 + unless _tmp + self.pos = _save + break end - begin - syntax.check_args args - rescue CompileError => err - error err.message - args = ['(NoArgument)'] * syntax.min_argc + @result = begin; tagged_section_init ; end + _tmp = true + unless _tmp + self.pos = _save + break end - if syntax.block_allowed? - compile_block syntax, args, lines + while true + _tmp = apply(:_Block) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + @result = begin; close_all_tagged_section ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Start unless _tmp + return _tmp + end + + # Block = BlankLine* (SinglelineComment:c | Headline:headline | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) + def _Block + + _save = self.pos + while true # sequence + while true + _tmp = apply(:_BlankLine) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + + _save2 = self.pos + while true # choice + _tmp = apply(:_SinglelineComment) + c = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_Headline) + headline = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_BlockElement) + c = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_Ulist) + c = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_Olist) + c = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_Dlist) + c = @result + break if _tmp + self.pos = _save2 + _tmp = apply(:_Paragraph) + c = @result + break if _tmp + self.pos = _save2 + break + end # end choice + + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Block unless _tmp + return _tmp + end + + # BlankLine = Newline + def _BlankLine + _tmp = apply(:_Newline) + set_failed_rule :_BlankLine unless _tmp + return _tmp + end + + # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* { compile_headline(level, cmd, label, caption) } + def _Headline + + _save = self.pos + while true # sequence + _tmp = apply(:_HeadlinePrefix) + level = @result + unless _tmp + self.pos = _save + break + end + _save1 = self.pos + _tmp = apply(:_BracketArg) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save1 + end + cmd = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_BraceArg) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save2 + end + label = @result + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + caption = @result + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Newline) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + @result = begin; compile_headline(level, cmd, label, caption) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Headline unless _tmp + return _tmp + end + + # HeadlinePrefix = < /={1,5}/ > { text.length } + def _HeadlinePrefix + + _save = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:={1,5})/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; text.length ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_HeadlinePrefix unless _tmp + return _tmp + end + + # Paragraph = ParagraphSub+:c { compile_paragraph(c) } + def _Paragraph + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_ParagraphSub) + if _tmp + _ary << @result + while true + _tmp = apply(:_ParagraphSub) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary else - if lines - error "block is not allowed for command //#{syntax.name}; ignore" + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; compile_paragraph(c) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Paragraph unless _tmp + return _tmp + end + + # ParagraphSub = (InlineElement:c { c } | < ContentText > { text })+:d Newline { d } + def _ParagraphSub + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save4 = self.pos + while true # sequence + _text_start = self.pos + _tmp = apply(:_ContentText) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save4 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + break + end # end choice + + if _tmp + _ary << @result + while true + + _save5 = self.pos + while true # choice + + _save6 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save6 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save6 + end + break + end # end sequence + + break if _tmp + self.pos = _save5 + + _save7 = self.pos + while true # sequence + _text_start = self.pos + _tmp = apply(:_ContentText) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save7 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save7 + end + break + end # end sequence + + break if _tmp + self.pos = _save5 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp end - compile_single syntax, args + _tmp = true + @result = _ary + else + self.pos = _save1 end - end + d = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end + @result = begin; d ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - def compile_unknown_command(args, lines) - @strategy.unknown_command args, lines - end + set_failed_rule :_ParagraphSub unless _tmp + return _tmp + end - def compile_block(syntax, args, lines) - @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) - end + # ContentText = NonInlineElement+:c { c } + def _ContentText + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_NonInlineElement) + if _tmp + _ary << @result + while true + _tmp = apply(:_NonInlineElement) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - def default_block(syntax) - if syntax.block_required? - error "block is required for //#{syntax.name}; use empty block" + set_failed_rule :_ContentText unless _tmp + return _tmp + end + + # NonInlineElement = !InlineElement < /[^\r\n]/ > { text } + def _NonInlineElement + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break end - [] - end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - def compile_single(syntax, args) - @strategy.__send__(syntax.name, *args) - end + set_failed_rule :_NonInlineElement unless _tmp + return _tmp + end - def text(str) - return '' if str.empty? - words = str.split(/(@<\w+>\{(?:[^\}\\]|\\.)*?\})/, -1) - words.each do |w| - error "`@' seen but is not valid inline op: #{w}" if w.scan(/@<\w+>/).size > 1 && !/\A@/.match(w) + # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline) + def _BlockElement + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = match_string("//") + unless _tmp + self.pos = _save1 + break + end + _tmp = apply(:_ElementName) + symbol = @result + unless _tmp + self.pos = _save1 + break + end + _ary = [] + while true + _tmp = apply(:_BracketArg) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + args = @result + unless _tmp + self.pos = _save1 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save1 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save1 + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save1 + break + end + _save4 = self.pos + _tmp = apply(:_BlockElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save4 + end + contents = @result + unless _tmp + self.pos = _save1 + break + end + _tmp = match_string("//}") + unless _tmp + self.pos = _save1 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save1 + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save1 + break + end + @result = begin; compile_command(symbol, args, contents) ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save6 = self.pos + while true # sequence + _tmp = match_string("//") + unless _tmp + self.pos = _save6 + break + end + _tmp = apply(:_ElementName) + symbol = @result + unless _tmp + self.pos = _save6 + break + end + _ary = [] + while true + _tmp = apply(:_BracketArg) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + args = @result + unless _tmp + self.pos = _save6 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save6 + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save6 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_BlockElement unless _tmp + return _tmp + end + + # InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" { compile_inline(symbol,contents); } + def _InlineElement + + _save = self.pos + while true # sequence + _tmp = match_string("@<") + unless _tmp + self.pos = _save + break end - result = @strategy.nofunc_text(words.shift) - until words.empty? - result << compile_inline(words.shift.gsub(/\\\}/, '}')) - result << @strategy.nofunc_text(words.shift) + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^>\r\n]+)/) + if _tmp + text = get_text(_text_start) end - result - rescue => err - error err.message - end - public :text # called from strategy + unless _tmp + self.pos = _save + break + end + @result = begin; symbol = text; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = match_string(">") + unless _tmp + self.pos = _save + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _save1 = self.pos + _tmp = apply(:_InlineElementContents) + unless _tmp + _tmp = true + self.pos = _save1 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; contents = text ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save + break + end + @result = begin; compile_inline(symbol,contents); ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - def compile_inline(str) - op, arg = /\A@<(\w+)>\{(.*?)\}\z/.match(str).captures - unless inline_defined?(op) - raise CompileError, "no such inline op: #{op}" + set_failed_rule :_InlineElement unless _tmp + return _tmp + end + + # BracketArg = "[" < /([^\r\n\]\\]|\\[^\r\n])*/ > "]" { text } + def _BracketArg + + _save = self.pos + while true # sequence + _tmp = match_string("[") + unless _tmp + self.pos = _save + break end - unless @strategy.respond_to?("inline_#{op}") - raise "strategy does not support inline op: @<#{op}>" + _text_start = self.pos + _tmp = scan(/\A(?-mix:([^\r\n\]\\]|\\[^\r\n])*)/) + if _tmp + text = get_text(_text_start) end - @strategy.__send__("inline_#{op}", arg) - rescue => err - error err.message - @strategy.nofunc_text(str) - end + unless _tmp + self.pos = _save + break + end + _tmp = match_string("]") + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - def warn(msg) - @strategy.warn msg + set_failed_rule :_BracketArg unless _tmp + return _tmp + end + + # BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } + def _BraceArg + + _save = self.pos + while true # sequence + _tmp = match_string("{") + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:([^\r\n}\\]|\\[^\r\n])*)/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BraceArg unless _tmp + return _tmp + end + + # BlockElementContents = BlockElementContent+:c + def _BlockElementContents + _save = self.pos + _ary = [] + _tmp = apply(:_BlockElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_BlockElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save end + c = @result + set_failed_rule :_BlockElementContents unless _tmp + return _tmp + end - def error(msg) - @strategy.error msg + # BlockElementContent = (SinglelineComment:c | BlockElement:c | BlockElementParagraph:c) + def _BlockElementContent + + _save = self.pos + while true # choice + _tmp = apply(:_SinglelineComment) + c = @result + break if _tmp + self.pos = _save + _tmp = apply(:_BlockElement) + c = @result + break if _tmp + self.pos = _save + _tmp = apply(:_BlockElementParagraph) + c = @result + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_BlockElementContent unless _tmp + return _tmp + end + + # BlockElementParagraph = &. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem } + def _BlockElementParagraph + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = get_byte + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + @result = begin; @blockElem = [] ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _ary = [] + _tmp = apply(:_BlockElementParagraphSub) + if _tmp + _ary << @result + while true + _tmp = apply(:_BlockElementParagraphSub) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; @blockElem ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BlockElementParagraph unless _tmp + return _tmp + end + + # BlockElementParagraphSub = (InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline + def _BlockElementParagraphSub + + _save = self.pos + while true # sequence + _save1 = self.pos + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; @blockElem << c ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save4 = self.pos + while true # sequence + _tmp = apply(:_BlockElementContentText) + c = @result + unless _tmp + self.pos = _save4 + break + end + @result = begin; @blockElem << c ; end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + break + end # end choice + + if _tmp + while true + + _save5 = self.pos + while true # choice + + _save6 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save6 + break + end + @result = begin; @blockElem << c ; end + _tmp = true + unless _tmp + self.pos = _save6 + end + break + end # end sequence + + break if _tmp + self.pos = _save5 + + _save7 = self.pos + while true # sequence + _tmp = apply(:_BlockElementContentText) + c = @result + unless _tmp + self.pos = _save7 + break + end + @result = begin; @blockElem << c ; end + _tmp = true + unless _tmp + self.pos = _save7 + end + break + end # end sequence + + break if _tmp + self.pos = _save5 + break + end # end choice + + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BlockElementParagraphSub unless _tmp + return _tmp + end + + # BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } + def _BlockElementContentText + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("//}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_SinglelineComment) + _tmp = _tmp ? nil : true + self.pos = _save2 + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = apply(:_BlockElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + _save4 = self.pos + _tmp = apply(:_Ulist) + _tmp = _tmp ? nil : true + self.pos = _save4 + unless _tmp + self.pos = _save + break + end + _save5 = self.pos + _tmp = apply(:_Olist) + _tmp = _tmp ? nil : true + self.pos = _save5 + unless _tmp + self.pos = _save + break + end + _save6 = self.pos + _tmp = apply(:_Dlist) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _save7 = self.pos + _tmp = apply(:_NonInlineElement) + if _tmp + while true + _tmp = apply(:_NonInlineElement) + break unless _tmp + end + _tmp = true + else + self.pos = _save7 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BlockElementContentText unless _tmp + return _tmp + end + + # InlineElementContents = !"}" InlineElementContent+:c { c } + def _InlineElementContents + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _ary = [] + _tmp = apply(:_InlineElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_InlineElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementContents unless _tmp + return _tmp + end + + # InlineElementContent = (InlineElement:c | InlineElementContentText:c) + def _InlineElementContent + + _save = self.pos + while true # choice + _tmp = apply(:_InlineElement) + c = @result + break if _tmp + self.pos = _save + _tmp = apply(:_InlineElementContentText) + c = @result + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElementContent unless _tmp + return _tmp + end + + # InlineElementContentText = (!InlineElement /[^\r\n}]/)+ + def _InlineElementContentText + _save = self.pos + + _save1 = self.pos + while true # sequence + _save2 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save2 + unless _tmp + self.pos = _save1 + break + end + _tmp = scan(/\A(?-mix:[^\r\n}])/) + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + if _tmp + while true + + _save3 = self.pos + while true # sequence + _save4 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save4 + unless _tmp + self.pos = _save3 + break + end + _tmp = scan(/\A(?-mix:[^\r\n}])/) + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break unless _tmp + end + _tmp = true + else + self.pos = _save end + set_failed_rule :_InlineElementContentText unless _tmp + return _tmp + end + + # SinglelineContent = ContentInlines:c (Newline | EOF) { c } + def _SinglelineContent + + _save = self.pos + while true # sequence + _tmp = apply(:_ContentInlines) + c = @result + unless _tmp + self.pos = _save + break + end + + _save1 = self.pos + while true # choice + _tmp = apply(:_Newline) + break if _tmp + self.pos = _save1 + _tmp = apply(:_EOF) + break if _tmp + self.pos = _save1 + break + end # end choice + + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_SinglelineContent unless _tmp + return _tmp + end + + # ContentInlines = ContentInline+:c { c.join } + def _ContentInlines + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_ContentInline) + if _tmp + _ary << @result + while true + _tmp = apply(:_ContentInline) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c.join ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_ContentInlines unless _tmp + return _tmp + end + + # ContentInline = (InlineElement:c { c } | !Newline < /[^\r\n]/ > {text }) + def _ContentInline + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_Newline) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save2 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_ContentInline unless _tmp + return _tmp + end + + # Ulist = &. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } + def _Ulist + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = get_byte + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + @result = begin; @ulist_elem=[] ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + + _save3 = self.pos + while true # choice + _tmp = apply(:_UlistElement) + break if _tmp + self.pos = _save3 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save3 + break + end # end choice + + if _tmp + while true + + _save4 = self.pos + while true # choice + _tmp = apply(:_UlistElement) + break if _tmp + self.pos = _save4 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save4 + break + end # end choice + + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + unless _tmp + self.pos = _save + break + end + + _save5 = self.pos + while true # choice + _tmp = apply(:_Newline) + break if _tmp + self.pos = _save5 + _tmp = apply(:_EOF) + break if _tmp + self.pos = _save5 + break + end # end choice + + unless _tmp + self.pos = _save + break + end + @result = begin; compile_ulist(@ulist_elem) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Ulist unless _tmp + return _tmp + end + + # UlistElement = " "+ "*"+:level " "* SinglelineContent:c { @ulist_elem << [level.size, c] } + def _UlistElement + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _ary = [] + _tmp = match_string("*") + if _tmp + _ary << @result + while true + _tmp = match_string("*") + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end + level = @result + unless _tmp + self.pos = _save + break + end + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; @ulist_elem << [level.size, c] ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_UlistElement unless _tmp + return _tmp + end + + # Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) } + def _Olist + + _save = self.pos + while true # sequence + @result = begin; @olist_elem = [] ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _save1 = self.pos + _ary = [] + + _save2 = self.pos + while true # choice + _tmp = apply(:_OlistElement) + break if _tmp + self.pos = _save2 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save2 + break + end # end choice + + if _tmp + _ary << @result + while true + + _save3 = self.pos + while true # choice + _tmp = apply(:_OlistElement) + break if _tmp + self.pos = _save3 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save3 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; compile_olist(@olist_elem) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Olist unless _tmp + return _tmp + end + + # OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, c] } + def _OlistElement + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _save2 = self.pos + _tmp = scan(/\A(?-mix:\d)/) + if _tmp + while true + _tmp = scan(/\A(?-mix:\d)/) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; level=text ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = match_string(".") + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; @olist_elem << [level, c] ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_OlistElement unless _tmp + return _tmp + end + + # Dlist = (DlistElement | SinglelineComment):c Dlist?:cc + def _Dlist + + _save = self.pos + while true # sequence + + _save1 = self.pos + while true # choice + _tmp = apply(:_DlistElement) + break if _tmp + self.pos = _save1 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save1 + break + end # end choice + + c = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Dlist) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save2 + end + cc = @result + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Dlist unless _tmp + return _tmp + end + + # DlistElement = " "* ":" " " Space* SinglelineContent:text DlistElementContent:content + def _DlistElement + + _save = self.pos + while true # sequence + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = match_string(":") + unless _tmp + self.pos = _save + break + end + _tmp = match_string(" ") + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + text = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_DlistElementContent) + content = @result + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_DlistElement unless _tmp + return _tmp + end + + # DlistElementContent = /[ \t]+/ SinglelineContent:c + def _DlistElementContent + + _save = self.pos + while true # sequence + _tmp = scan(/\A(?-mix:[ \t]+)/) + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_DlistElementContent unless _tmp + return _tmp + end + + # SinglelineComment = "#@" < NonNewLine > { comment(text) } Newline + def _SinglelineComment + + _save = self.pos + while true # sequence + _tmp = match_string("\#@") + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _tmp = apply(:_NonNewLine) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; comment(text) ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_SinglelineComment unless _tmp + return _tmp + end + + # NonNewLine = /[^\r\n]+/ + def _NonNewLine + _tmp = scan(/\A(?-mix:[^\r\n]+)/) + set_failed_rule :_NonNewLine unless _tmp + return _tmp + end + + # Digits = Digit+:c { c } + def _Digits + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Digit) + if _tmp + _ary << @result + while true + _tmp = apply(:_Digit) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_Digits unless _tmp + return _tmp + end + + # Space = /[ \t]/ + def _Space + _tmp = scan(/\A(?-mix:[ \t])/) + set_failed_rule :_Space unless _tmp + return _tmp + end + + # EOF = !. + def _EOF + _save = self.pos + _tmp = get_byte + _tmp = _tmp ? nil : true + self.pos = _save + set_failed_rule :_EOF unless _tmp + return _tmp + end + + # ElementName = < LowerAlphabetAscii+ > { text } + def _ElementName + + _save = self.pos + while true # sequence + _text_start = self.pos + _save1 = self.pos + _tmp = apply(:_LowerAlphabetAscii) + if _tmp + while true + _tmp = apply(:_LowerAlphabetAscii) + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_ElementName unless _tmp + return _tmp + end + + # Alphanumeric = %literals.Alphanumeric + def _Alphanumeric + _tmp = @_grammar_literals.external_invoke(self, :_Alphanumeric) + set_failed_rule :_Alphanumeric unless _tmp + return _tmp + end + + # AlphanumericAscii = %literals.AlphanumericAscii + def _AlphanumericAscii + _tmp = @_grammar_literals.external_invoke(self, :_AlphanumericAscii) + set_failed_rule :_AlphanumericAscii unless _tmp + return _tmp + end + + # LowerAlphabetAscii = %literals.LowerAlphabetAscii + def _LowerAlphabetAscii + _tmp = @_grammar_literals.external_invoke(self, :_LowerAlphabetAscii) + set_failed_rule :_LowerAlphabetAscii unless _tmp + return _tmp + end + + # Digit = %literals.Digit + def _Digit + _tmp = @_grammar_literals.external_invoke(self, :_Digit) + set_failed_rule :_Digit unless _tmp + return _tmp + end + + # BOM = %literals.BOM + def _BOM + _tmp = @_grammar_literals.external_invoke(self, :_BOM) + set_failed_rule :_BOM unless _tmp + return _tmp + end + + # Newline = %literals.Newline + def _Newline + _tmp = @_grammar_literals.external_invoke(self, :_Newline) + set_failed_rule :_Newline unless _tmp + return _tmp + end + + # NonAlphanumeric = %literals.NonAlphanumeric + def _NonAlphanumeric + _tmp = @_grammar_literals.external_invoke(self, :_NonAlphanumeric) + set_failed_rule :_NonAlphanumeric unless _tmp + return _tmp + end + # Spacechar = %literals.Spacechar + def _Spacechar + _tmp = @_grammar_literals.external_invoke(self, :_Spacechar) + set_failed_rule :_Spacechar unless _tmp + return _tmp end -end # module ReVIEW + Rules = {} + Rules[:_root] = rule_info("root", "Start") + Rules[:_Start] = rule_info("Start", "&. { tagged_section_init } Block* { close_all_tagged_section }") + Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:headline | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c)") + Rules[:_BlankLine] = rule_info("BlankLine", "Newline") + Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* { compile_headline(level, cmd, label, caption) }") + Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") + Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c { compile_paragraph(c) }") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d Newline { d }") + Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }") + Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { text }") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline)") + Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" < /[^>\\r\\n]+/ > {symbol = text} \">\" \"{\" < InlineElementContents? > { contents = text } \"}\" { compile_inline(symbol,contents); }") + Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" < /([^\\r\\n\\]\\\\]|\\\\[^\\r\\n])*/ > \"]\" { text }") + Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") + Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c") + Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c)") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem }") + Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") + Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }") + Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c }") + Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText:c)") + Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(!InlineElement /[^\\r\\n}]/)+") + Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c (Newline | EOF) { c }") + Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c.join }") + Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > {text })") + Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) }") + Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c { @ulist_elem << [level.size, c] }") + Rules[:_Olist] = rule_info("Olist", "{ @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) }") + Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { level=text } \".\" Space* SinglelineContent:c {@olist_elem << [level, c] }") + Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment):c Dlist?:cc") + Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text DlistElementContent:content") + Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c") + Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine > { comment(text) } Newline") + Rules[:_NonNewLine] = rule_info("NonNewLine", "/[^\\r\\n]+/") + Rules[:_Digits] = rule_info("Digits", "Digit+:c { c }") + Rules[:_Space] = rule_info("Space", "/[ \\t]/") + Rules[:_EOF] = rule_info("EOF", "!.") + Rules[:_ElementName] = rule_info("ElementName", "< LowerAlphabetAscii+ > { text }") + Rules[:_Alphanumeric] = rule_info("Alphanumeric", "%literals.Alphanumeric") + Rules[:_AlphanumericAscii] = rule_info("AlphanumericAscii", "%literals.AlphanumericAscii") + Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "%literals.LowerAlphabetAscii") + Rules[:_Digit] = rule_info("Digit", "%literals.Digit") + Rules[:_BOM] = rule_info("BOM", "%literals.BOM") + Rules[:_Newline] = rule_info("Newline", "%literals.Newline") + Rules[:_NonAlphanumeric] = rule_info("NonAlphanumeric", "%literals.NonAlphanumeric") + Rules[:_Spacechar] = rule_info("Spacechar", "%literals.Spacechar") + # :startdoc: +end diff --git a/lib/review/location.rb b/lib/review/location.rb new file mode 100644 index 000000000..9a823a983 --- /dev/null +++ b/lib/review/location.rb @@ -0,0 +1,24 @@ +module ReVIEW + class Location + def initialize(filename, f) + @filename = filename + @f = f + end + + attr_reader :filename + + def lineno + @f.lineno + end + + def string + begin + "#{@filename}:#{@f.lineno}" + rescue + "#{@filename}:nil" + end + end + + alias to_s string + end +end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg new file mode 100644 index 000000000..cc58ffebe --- /dev/null +++ b/lib/review/review.kpeg @@ -0,0 +1,520 @@ +%% name = ReVIEW::Compiler + +%% { + + class Error; end + +require 'review/location' +require 'review/extentions' +require 'review/preprocessor' +require 'review/exception' + require 'lineinput' + if RUBY_VERSION > '1.9' then + require 'review/compiler/literals_1_9' + else + require 'review/compiler/literals_1_8' + end + + ## redifine Compiler.new + def initialize(strategy) + @strategy = strategy + end + + attr_accessor :strategy + + def compile(chap) + @chapter = chap + do_compile + @strategy.result + end + + def do_compile + f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(@chapter.content))) + @strategy.bind self, @chapter, ReVIEW::Location.new(@chapter.basename, f) + setup_parser(@chapter.content) + parse() + end + + def text(str) + st = @strategy.dup + f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(str))) + st.bind self, @chapter, ReVIEW::Location.new(@chapter.basename, f) + parser = ReVIEW::Compiler.new(st) + parser.setup_parser(str) + parser.parse("InlineElementContents") + st.result + end + + class SyntaxElement + def initialize(name, type, argc, &block) + @name = name + @type = type + @argc_spec = argc + @checker = block + end + + attr_reader :name + + def check_args(args) + unless @argc_spec === args.size + raise CompileError, "wrong # of parameters (block command //#{@name}, expect #{@argc_spec} but #{args.size})" + end + @checker.call(*args) if @checker + end + + def min_argc + case @argc_spec + when Range then @argc_spec.begin + when Integer then @argc_spec + else + raise TypeError, "argc_spec is not Range/Integer: #{inspect()}" + end + end + + def block_required? + @type == :block + end + + def block_allowed? + @type == :block or @type == :optional + end + end + + SYNTAX = {} + + def self.defblock(name, argc, optional = false, &block) + defsyntax name, (optional ? :optional : :block), argc, &block + end + + def self.defsingle(name, argc, &block) + defsyntax name, :line, argc, &block + end + + def self.defsyntax(name, type, argc, &block) + SYNTAX[name] = SyntaxElement.new(name, type, argc, &block) + end + + def syntax_defined?(name) + SYNTAX.key?(name.to_sym) + end + + def syntax_descriptor(name) + SYNTAX[name.to_sym] + end + + class InlineSyntaxElement + def initialize(name) + @name = name + end + + attr_reader :name + end + + INLINE = {} + + def self.definline(name) + INLINE[name] = InlineSyntaxElement.new(name) + end + + def inline_defined?(name) + INLINE.key?(name.to_sym) + end + + defblock :read, 0 + defblock :lead, 0 + defblock :list, 2 + defblock :emlist, 0..1 + defblock :cmd, 0..1 + defblock :table, 0..2 + defblock :quote, 0 + defblock :image, 2..3, true + defblock :source, 0..1 + defblock :listnum, 2 + defblock :emlistnum, 0..1 + defblock :bibpaper, 2..3, true + defblock :doorquote, 1 + defblock :talk, 0 + defblock :texequation, 0 + defblock :graph, 1..3 + + defblock :address, 0 + defblock :blockquote, 0 + defblock :bpo, 0 + defblock :flushright, 0 + defblock :centering, 0 + defblock :note, 0..1 + defblock :box, 0..1 + defblock :comment, 0..1, true + + defsingle :footnote, 2 + defsingle :noindent, 0 + defsingle :linebreak, 0 + defsingle :pagebreak, 0 + defsingle :indepimage, 1..3 + defsingle :numberlessimage, 1..3 + defsingle :hr, 0 + defsingle :parasep, 0 + defsingle :label, 1 + defsingle :raw, 1 + defsingle :tsize, 1 + defsingle :include, 1 + defsingle :olnum, 1 + + definline :chapref + definline :chap + definline :title + definline :img + definline :icon + definline :list + definline :table + definline :fn + definline :kw + definline :ruby + definline :bou + definline :ami + definline :b + definline :dtp + definline :code + definline :bib + definline :hd + definline :href + definline :recipe + + definline :abbr + definline :acronym + definline :cite + definline :dfn + definline :em + definline :kbd + definline :q + definline :samp + definline :strong + definline :var + definline :big + definline :small + definline :del + definline :ins + definline :sup + definline :sub + definline :tt + definline :i + definline :tti + definline :ttb + definline :u + definline :raw + definline :br + definline :m + definline :uchar + definline :idx + definline :hidx + definline :comment + definline :include + + + def tagged_section_init + @tagged_section = [] + end + + def open_tagged_section(tag, level, label, caption) + mid = "#{tag}_begin" + unless @strategy.respond_to?(mid) + error "strategy does not support tagged section: #{tag}" + return + end + @tagged_section.push [tag, level] + @strategy.__send__ mid, level, label, caption + end + + def close_tagged_section(tag, level) + mid = "#{tag}_end" + if @strategy.respond_to?(mid) + @strategy.__send__ mid, level + else + error "strategy does not support block op: #{mid}" + end + end + + def close_all_tagged_section + until @tagged_section.empty? + close_tagged_section(* @tagged_section.pop) + end + end + + def compile_command(name, args, lines) + syntax = syntax_descriptor(name) + unless @strategy.respond_to?(syntax.name) + error "strategy does not support command: //#{syntax.name}" + compile_unknown_command args, lines + return + end + begin + syntax.check_args args + rescue CompileError => err + error err.message + args = ['(NoArgument)'] * syntax.min_argc + end + if syntax.block_allowed? + compile_block syntax, args, lines + else + if lines + error "block is not allowed for command //#{syntax.name}; ignore" + end + compile_single syntax, args + end + end + + def compile_headline(level, tag, label, caption) + @headline_indexs ||= [0] ## XXX + caption.strip! + index = level - 1 + if tag + if tag !~ /\A\// + close_current_tagged_section(level) + open_tagged_section(tag, level, label, caption) + else + open_tag = tag[1..-1] + prev_tag_info = @tagged_section.pop + unless prev_tag_info.first == open_tag + raise CompileError, "#{open_tag} is not opened." + end + close_tagged_section(*prev_tag_info) + end + else + if @headline_indexs.size > (index + 1) + @headline_indexs = @headline_indexs[0..index] + end + @headline_indexs[index] = 0 if @headline_indexs[index].nil? + @headline_indexs[index] += 1 + close_current_tagged_section(level) + @strategy.headline level, label, caption + end + end + + def close_current_tagged_section(level) + while @tagged_section.last and @tagged_section.last[1] >= level + close_tagged_section(* @tagged_section.pop) + end + end + + def comment(text) + @strategy.comment(text) + end + + def compile_ulist(elem) + level = 0 + elem.each do |current_level, buf| + if level == current_level + @strategy.ul_item_end + # body + @strategy.ul_item_begin [buf] + elsif level < current_level # down + level_diff = current_level - level + level = current_level + (1..(level_diff - 1)).to_a.reverse.each do |i| + @strategy.ul_begin {i} + @strategy.ul_item_begin [] + end + @strategy.ul_begin {level} + @strategy.ul_item_begin [buf] + elsif level > current_level # up + level_diff = level - current_level + level = current_level + (1..level_diff).to_a.reverse.each do |i| + @strategy.ul_item_end + @strategy.ul_end {level + i} + end + @strategy.ul_item_end + # body + @strategy.ul_item_begin [buf] + end + end + + (1..level).to_a.reverse.each do |i| + @strategy.ul_item_end + @strategy.ul_end {i} + end + end + + def compile_olist(elem) + @strategy.ol_begin + elem.each do |num, buf| + @strategy.ol_item buf, num + end + @strategy.ol_end + end + + + def compile_unknown_command(args, lines) + @strategy.unknown_command args, lines + end + + def compile_block(syntax, args, lines) + @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) + end + + def default_block(syntax) + if syntax.block_required? + error "block is required for //#{syntax.name}; use empty block" + end + [] + end + + def compile_single(syntax, args) + @strategy.__send__(syntax.name, *args) + end + + + + def compile_inline(op, arg) + unless inline_defined?(op) + raise CompileError, "no such inline op: #{op}" + end + unless @strategy.respond_to?("inline_#{op}") + raise "strategy does not support inline op: @<#{op}>" + end + @strategy.__send__("inline_#{op}", arg) + rescue => err + error err.message + end + + def compile_paragraph(buf) + @strategy.paragraph buf + end + + def warn(msg) + @strategy.warn msg + end + + def error(msg) + @strategy.error msg + end + + +} + +root = Start + +Start = &. { tagged_section_init } Block* { close_all_tagged_section } + + +## TODO InlineElement の後に Ulist / Olist / Dlist が来ると先頭行じゃなくてマッチできてしまうかも +## using &. to detect EOF +Block = BlankLine* + ( SinglelineComment:c + | Headline:headline + | BlockElement:c + | Ulist:c + | Olist:c + | Dlist:c + | Paragraph:c + ) + +BlankLine = Newline + +Headline = ( HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* ) { compile_headline(level, cmd, label, caption) } + +HeadlinePrefix = < /={1,5}/ > { text.length } + +Paragraph = ParagraphSub+:c { compile_paragraph(c) } + +ParagraphSub = ( InlineElement:c { c } + | < ContentText > { text } + )+:d Newline { d } + +## ContentText = !Newline !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } +ContentText = NonInlineElement+:c { c } + +NonInlineElement = !InlineElement < /[^\r\n]/ > { text } + +BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline + { compile_command(symbol, args, contents) } + | "//" ElementName:symbol BracketArg*:args Space* Newline + ) + +InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" + { compile_inline(symbol,contents); } + +BracketArg = "[" < /([^\r\n\]\\]|\\[^\r\n])*/ > "]" { text } + +BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } + +## contents との差は paragraph を切るか切らないか +BlockElementContents = BlockElementContent+:c + +## 各要素は Newline で終わらなければならない +BlockElementContent = ( SinglelineComment:c + | BlockElement:c + | BlockElementParagraph:c + ) + +BlockElementParagraph = &. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem } + +BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } + | BlockElementContentText:c { @blockElem << c } + )+ Newline + +BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } + +InlineElementContents = !"}" InlineElementContent+:c { c } + +InlineElementContent = ( InlineElement:c + | InlineElementContentText:c + ) + +InlineElementContentText = ( !InlineElement /[^\r\n}]/ )+ + +SinglelineContent = ContentInlines:c (Newline | EOF) { c } + +ContentInlines = ContentInline+:c { c.join } + +ContentInline = ( InlineElement:c { c } + | !Newline < /[^\r\n]/ > {text } + ) + +## ContentInlineText = !InlineElement !Newline .:cc { p [:char,cc];cc } + + +## * 箇条書き +## 行頭から… の指定がない + +Ulist = &. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } + +UlistElement = " "+ "*"+:level " "* SinglelineContent:c { @ulist_elem << [level.size, c] } + +## 1. 番号付き箇条書き +## 行頭から… の指定がない + +Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) } + +OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, c] } + +## : 用語リスト + ## 行頭から… の指定がない +Dlist = (DlistElement | SinglelineComment):c Dlist?:cc + +DlistElement = " "* ":" " " Space* SinglelineContent:text DlistElementContent:content + +DlistElementContent = /[ \t]+/ SinglelineContent:c + +SinglelineComment = ("#@" < NonNewLine > { comment(text) } Newline) + +NonNewLine = /[^\r\n]+/ + +Digits = Digit+:c { c } + +## Space = /[  \t]/ +Space = /[ \t]/ + +EOF = !. + +ElementName = < LowerAlphabetAscii+ > { text } + +%literals = ReVIEW::Compiler::Literals +Alphanumeric = %literals.Alphanumeric +AlphanumericAscii = %literals.AlphanumericAscii +LowerAlphabetAscii = %literals.LowerAlphabetAscii +Digit = %literals.Digit +BOM = %literals.BOM +Newline = %literals.Newline +NonAlphanumeric = %literals.NonAlphanumeric +Spacechar = %literals.Spacechar From 43c2e2aac0b12948b7223ee9e26ccecda91e78d3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 1 Mar 2014 13:52:49 +0900 Subject: [PATCH 005/214] fix HTMLBuilder --- lib/review/builder.rb | 22 +-- lib/review/compiler.rb | 103 +++++++------ lib/review/htmlbuilder.rb | 304 ++++++++++++++++++++------------------ lib/review/review.kpeg | 73 +++++---- 4 files changed, 273 insertions(+), 229 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 91592eb60..07f37f9e2 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -21,6 +21,8 @@ class Builder CAPTION_TITLES = %w(note memo tip info planning best important security caution term link notice point shoot reference practice expert) + attr_accessor :output + def pre_paragraph nil end @@ -61,15 +63,17 @@ def result alias :raw_result result def print(*s) - @output.print(*s.map{|i| - convert_outencoding(i, ReVIEW.book.param["outencoding"]) - }) + raise NotImplementedError, "XXX: do not use print" +# @output.print(*s.map{|i| +# convert_outencoding(i, ReVIEW.book.param["outencoding"]) +# }) end def puts(*s) - @output.puts *s.map{|i| - convert_outencoding(i, ReVIEW.book.param["outencoding"]) - } + raise NotImplementedError, "XXX: do not use puts" +# @output.puts *s.map{|i| +# convert_outencoding(i, ReVIEW.book.param["outencoding"]) +# } end def list(lines, id, caption) @@ -167,9 +171,9 @@ def adjust_n_cols(rows) # footnote_end #end - def compile_inline(s) - @compiler.text(s) - end +# def compile_inline(s) +# @compiler.text(s) +# end def inline_chapref(id) @chapter.env.chapter_index.display_string(id) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 5ccef7791..270739b66 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -571,28 +571,34 @@ def tagged_section_init end def open_tagged_section(tag, level, label, caption) + buf = "" mid = "#{tag}_begin" unless @strategy.respond_to?(mid) error "strategy does not support tagged section: #{tag}" return end @tagged_section.push [tag, level] - @strategy.__send__ mid, level, label, caption + buf << @strategy.__send__(mid, level, label, caption) + buf end def close_tagged_section(tag, level) + buf = "" mid = "#{tag}_end" if @strategy.respond_to?(mid) - @strategy.__send__ mid, level + buf << @strategy.__send__(mid, level) else error "strategy does not support block op: #{mid}" end + buf end def close_all_tagged_section + buf = "" until @tagged_section.empty? - close_tagged_section(* @tagged_section.pop) + buf << close_tagged_section(* @tagged_section.pop) end + buf end def compile_command(name, args, lines) @@ -619,20 +625,21 @@ def compile_command(name, args, lines) end def compile_headline(level, tag, label, caption) + buf = "" @headline_indexs ||= [0] ## XXX caption.strip! index = level - 1 if tag if tag !~ /\A\// - close_current_tagged_section(level) - open_tagged_section(tag, level, label, caption) + buf << close_current_tagged_section(level) + buf << open_tagged_section(tag, level, label, caption) else open_tag = tag[1..-1] prev_tag_info = @tagged_section.pop unless prev_tag_info.first == open_tag raise CompileError, "#{open_tag} is not opened." end - close_tagged_section(*prev_tag_info) + buf << close_tagged_section(*prev_tag_info) end else if @headline_indexs.size > (index + 1) @@ -640,15 +647,18 @@ def compile_headline(level, tag, label, caption) end @headline_indexs[index] = 0 if @headline_indexs[index].nil? @headline_indexs[index] += 1 - close_current_tagged_section(level) - @strategy.headline level, label, caption + buf << close_current_tagged_section(level) + buf << @strategy.headline(level, label, caption) end + buf end def close_current_tagged_section(level) + buf = "" while @tagged_section.last and @tagged_section.last[1] >= level - close_tagged_section(* @tagged_section.pop) + buf << close_tagged_section(* @tagged_section.pop) end + buf end def comment(text) @@ -656,51 +666,55 @@ def comment(text) end def compile_ulist(elem) + buf0 = "" level = 0 elem.each do |current_level, buf| if level == current_level - @strategy.ul_item_end + buf0 << @strategy.ul_item_end # body - @strategy.ul_item_begin [buf] + buf0 << @strategy.ul_item_begin([buf]) elsif level < current_level # down level_diff = current_level - level level = current_level (1..(level_diff - 1)).to_a.reverse.each do |i| - @strategy.ul_begin {i} - @strategy.ul_item_begin [] + buf0 << @strategy.ul_begin{i} + buf0 << @strategy.ul_item_begin([]) end - @strategy.ul_begin {level} - @strategy.ul_item_begin [buf] + buf0 << @strategy.ul_begin{level} + buf0 << @strategy.ul_item_begin([buf]) elsif level > current_level # up level_diff = level - current_level level = current_level (1..level_diff).to_a.reverse.each do |i| - @strategy.ul_item_end - @strategy.ul_end {level + i} + buf0 << @strategy.ul_item_end + buf0 << @strategy.ul_end{level + i} end - @strategy.ul_item_end + buf0 << @strategy.ul_item_end # body - @strategy.ul_item_begin [buf] + buf0 <<@strategy.ul_item_begin([buf]) end end (1..level).to_a.reverse.each do |i| - @strategy.ul_item_end - @strategy.ul_end {i} + buf0 << @strategy.ul_item_end + buf0 << @strategy.ul_end{i} end + buf0 end def compile_olist(elem) - @strategy.ol_begin + buf0 = "" + buf0 << @strategy.ol_begin elem.each do |num, buf| - @strategy.ol_item buf, num + buf0 << @strategy.ol_item(buf, num) end - @strategy.ol_end + buf0 << @strategy.ol_end + buf0 end def compile_unknown_command(args, lines) - @strategy.unknown_command args, lines + @strategy.unknown_command(args, lines) end def compile_block(syntax, args, lines) @@ -719,7 +733,6 @@ def compile_single(syntax, args) end - def compile_inline(op, arg) unless inline_defined?(op) raise CompileError, "no such inline op: #{op}" @@ -798,7 +811,7 @@ def _Start return _tmp end - # Block = BlankLine* (SinglelineComment:c | Headline:headline | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) + # Block = BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { @strategy.output << c } def _Block _save = self.pos @@ -820,7 +833,7 @@ def _Block break if _tmp self.pos = _save2 _tmp = apply(:_Headline) - headline = @result + c = @result break if _tmp self.pos = _save2 _tmp = apply(:_BlockElement) @@ -846,6 +859,12 @@ def _Block break end # end choice + unless _tmp + self.pos = _save + break + end + @result = begin; @strategy.output << c ; end + _tmp = true unless _tmp self.pos = _save end @@ -1536,7 +1555,7 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = &. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem } + # BlockElementParagraph = &. { @blockElem = "" } BlockElementParagraphSub:c { @blockElem } def _BlockElementParagraph _save = self.pos @@ -1548,27 +1567,13 @@ def _BlockElementParagraph self.pos = _save break end - @result = begin; @blockElem = [] ; end + @result = begin; @blockElem = "" ; end _tmp = true unless _tmp self.pos = _save break end - _save2 = self.pos - _ary = [] _tmp = apply(:_BlockElementParagraphSub) - if _tmp - _ary << @result - while true - _tmp = apply(:_BlockElementParagraphSub) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save2 - end c = @result unless _tmp self.pos = _save @@ -2255,7 +2260,7 @@ def _Olist return _tmp end - # OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, c] } + # OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, [c]] } def _OlistElement _save = self.pos @@ -2320,7 +2325,7 @@ def _OlistElement self.pos = _save break end - @result = begin; @olist_elem << [level, c] ; end + @result = begin; @olist_elem << [level, [c]] ; end _tmp = true unless _tmp self.pos = _save @@ -2637,7 +2642,7 @@ def _Spacechar Rules = {} Rules[:_root] = rule_info("root", "Start") Rules[:_Start] = rule_info("Start", "&. { tagged_section_init } Block* { close_all_tagged_section }") - Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:headline | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c)") + Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { @strategy.output << c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* { compile_headline(level, cmd, label, caption) }") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") @@ -2651,7 +2656,7 @@ def _Spacechar Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c)") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem }") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c }") @@ -2663,7 +2668,7 @@ def _Spacechar Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) }") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c { @ulist_elem << [level.size, c] }") Rules[:_Olist] = rule_info("Olist", "{ @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) }") - Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { level=text } \".\" Space* SinglelineContent:c {@olist_elem << [level, c] }") + Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { level=text } \".\" Space* SinglelineContent:c {@olist_elem << [level, [c]] }") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment):c Dlist?:cc") Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text DlistElementContent:content") Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c") diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 4947d37e0..b1e38ead3 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -65,7 +65,7 @@ def builder_init_file def result layout_file = File.join(@book.basedir, "layouts", "layout.erb") if File.exist?(layout_file) - title = convert_outencoding(strip_html(compile_inline(@chapter.title)), ReVIEW.book.param["outencoding"]) + title = convert_outencoding(strip_html(@chapter.title), ReVIEW.book.param["outencoding"]) messages() + HTMLLayout.new(@output.string, title, layout_file).result else @@ -99,7 +99,7 @@ def result end header += < - #{convert_outencoding(strip_html(compile_inline(@chapter.title)), ReVIEW.book.param["outencoding"])} + #{convert_outencoding(strip_html(@chapter.title), ReVIEW.book.param["outencoding"])} EOT @@ -170,58 +170,62 @@ def headline_prefix(level) private :headline_prefix def headline(level, label, caption) + buf = "" prefix, anchor = headline_prefix(level) - puts '' if level > 1 + buf << "\n" if level > 1 a_id = "" unless anchor.nil? a_id = %Q[] end if caption.empty? - puts a_id unless label.nil? + buf << a_id+"\n" unless label.nil? else if label.nil? - puts %Q[#{a_id}#{prefix}#{compile_inline(caption)}] + buf << %Q[#{a_id}#{prefix}#{caption}\n] else - puts %Q[#{a_id}#{prefix}#{compile_inline(caption)}] + buf << %Q[#{a_id}#{prefix}#{caption}\n] end end + buf end def nonum_begin(level, label, caption) - puts '' if level > 1 + buf = "" + buf << "\n" if level > 1 unless caption.empty? if label.nil? - puts %Q[#{compile_inline(caption)}] + buf << %Q[#{caption}\n] else - puts %Q[#{compile_inline(caption)}] + buf << %Q[#{caption}\n] end end + buf end def nonum_end(level) end def column_begin(level, label, caption) - puts %Q[
] + buf = %Q[
\n] @column += 1 - puts '' if level > 1 + buf << "\n" if level > 1 a_id = %Q[] if caption.empty? - puts a_id unless label.nil? + buf << a_id + "\n" unless label.nil? else if label.nil? - puts %Q[#{a_id}#{compile_inline(caption)}] + buf << %Q[#{a_id}#{caption}\n] else - puts %Q[#{a_id}#{compile_inline(caption)}] + buf << %Q[#{a_id}#{caption}\n] end end -# headline(level, label, caption) + buf end def column_end(level) - puts '
' + "
\n" end def xcolumn_begin(level, label, caption) @@ -256,17 +260,18 @@ def tsize(str) end def captionblock(type, lines, caption) - puts %Q[
] + buf = %Q[
\n] unless caption.nil? - puts %Q[

#{compile_inline(caption)}

] + buf << %Q[

#{caption}

\n] end if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts blocked_lines.join("\n") + buf << blocked_lines.join("\n") << "\n" else - lines.each {|l| puts "

#{l}

" } + lines.each {|l| buf << "

#{l}

\n" } end - puts '
' + buf << "
\n" + buf end def memo(lines, caption = nil) @@ -315,7 +320,7 @@ def shoot(lines, caption = nil) def box(lines, caption = nil) puts %Q[
] - puts %Q[

#{compile_inline(caption)}

] unless caption.nil? + puts %Q[

#{caption}

] unless caption.nil? print %Q[
]
       lines.each {|line| puts detab(line) }
       puts '
' @@ -327,211 +332,221 @@ def note(lines, caption = nil) end def ul_begin - puts '
    ' + "
      \n" end def ul_item(lines) - puts "
    • #{lines.join}
    • " + "
    • #{lines.join}
    • \n" end def ul_item_begin(lines) - print "
    • #{lines.join}" + "
    • #{lines.join}\n" end def ul_item_end - puts "
    • " + "\n" end def ul_end - puts '
    ' + "
\n" end def ol_begin if @ol_num - puts "
    " ## it's OK in HTML5, but not OK in XHTML1.1 + num = @ol_num @ol_num = nil + "
      \n" ## it's OK in HTML5, but not OK in XHTML1.1 else - puts '
        ' + "
          \n" end end def ol_item(lines, num) - puts "
        1. #{lines.join}
        2. " + "
        3. #{lines.join}
        4. \n" end def ol_end - puts '
        ' + "
      \n" end def dl_begin - puts '
      ' + "
      \n" end def dt(line) - puts "
      #{line}
      " + "
      #{line}
      \n" end def dd(lines) - puts "
      #{lines.join}
      " + "
      #{lines.join}
      \n" end def dl_end - puts '
      ' + "
      \n" end def paragraph(lines) if @noindent.nil? - puts "

      #{lines.join}

      " + "

      #{lines.join}

      \n" else - puts %Q[

      #{lines.join}

      ] @noindent = nil + %Q[

      #{lines.join}

      \n] end end def parasep() - puts '
      ' + "
      \n" end def read(lines) if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts %Q[
      \n#{blocked_lines.join("\n")}\n
      ] + %Q[
      \n#{blocked_lines.join("\n")}\n
      \n] else - puts %Q[

      \n#{lines.join("\n")}\n

      ] + %Q[

      \n#{lines.join("\n")}\n

      \n] end end alias :lead read def list(lines, id, caption) - puts %Q[
      ] + buf = %Q[
      \n] begin - list_header id, caption + buf << list_header(id, caption) rescue KeyError error "no such list: #{id}" end - list_body id, lines - puts '
      ' + buf << list_body(id, lines) + buf << "
      \n" + buf end def list_header(id, caption) if get_chap.nil? - puts %Q[

      #{I18n.t("list")}#{I18n.t("format_number_header_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}

      ] + %Q[

      #{I18n.t("list")}#{I18n.t("format_number_header_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}

      \n] else - puts %Q[

      #{I18n.t("list")}#{I18n.t("format_number_header", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}

      ] + %Q[

      #{I18n.t("list")}#{I18n.t("format_number_header", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}

      \n] end end def list_body(id, lines) id ||= '' - print %Q[
      ]
      +      buf = %Q[
      \n]
             body = lines.inject(''){|i, j| i + detab(j) + "\n"}
             lexer = File.extname(id).gsub(/\./, '')
      -      puts highlight(:body => body, :lexer => lexer, :format => 'html')
      -      puts '
      ' + buf << highlight(:body => body, :lexer => lexer, :format => 'html') + buf << "
      \n" + buf end def source(lines, caption = nil) - puts %Q[
      ] - source_header caption - source_body caption, lines - puts '
      ' + buf = %Q[
      ] + buf << source_header(caption) + buf << source_body(caption, lines) + buf << "
      \n" + buf end def source_header(caption) if caption.present? - puts %Q[

      #{compile_inline(caption)}

      ] + %Q[

      #{caption}

      \n] end end def source_body(id, lines) id ||= '' - print %Q[
      ]
      +      buf = %Q[
      ]
             body = lines.inject(''){|i, j| i + detab(j) + "\n"}
             lexer = File.extname(id).gsub(/\./, '')
      -      puts highlight(:body => body, :lexer => lexer, :format => 'html')
      -      puts '
      ' + buf << highlight(:body => body, :lexer => lexer, :format => 'html') + buf << "
      \n" + buf end def listnum(lines, id, caption) - puts %Q[
      ] + buf = %Q[
      \n] begin - list_header id, caption + buf << list_header(id, caption) rescue KeyError error "no such list: #{id}" end - listnum_body lines - puts '
      ' + buf << listnum_body(lines) + buf << "
      " + buf end def listnum_body(lines) - print %Q[
      ]
      +      buf = %Q[
      \n]
             lines.each_with_index do |line, i|
      -        puts detab((i+1).to_s.rjust(2) + ": " + line)
      +        buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n"
             end
      -      puts '
      ' + buf << "
      \n" + buf end def emlist(lines, caption = nil) - puts %Q[
      ] - puts %Q(

      #{caption}

      ) unless caption.nil? - print %Q[
      ]
      +      buf = %Q[
      \n] + buf << %Q(

      #{caption}

      \n) unless caption.nil? + buf << %Q[
      \n]
             lines.each do |line|
      -        puts detab(line)
      +        buf << detab(line) << "\n"
             end
      -      puts '
      ' - puts '
      ' + buf << "
      \n" + buf << "
      \n" + buf end def emlistnum(lines, caption = nil) - puts %Q[
      ] - puts %Q(

      #{caption}

      ) unless caption.nil? - print %Q[
      ]
      +      buf = %Q[
      \n] + buf << %Q(

      #{caption}

      \n) unless caption.nil? + buf << %Q[
      \n]
             lines.each_with_index do |line, i|
      -        puts detab((i+1).to_s.rjust(2) + ": " + line)
      +        buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n"
             end
             puts '
      ' puts '
      ' end def cmd(lines, caption = nil) - puts %Q[
      ] - puts %Q(

      #{caption}

      ) unless caption.nil? - print %Q[
      ]
      +      buf == %Q[
      \n] + buf << %Q(

      #{caption}

      \n) unless caption.nil? + buf %Q[
      \n]
             lines.each do |line|
      -        puts detab(line)
      +        buf << detab(line) << "\n"
             end
      -      puts '
      ' - puts '
      ' + buf << "
      \n" + buf << "
      \n" + buf end def quotedlist(lines, css_class) - print %Q[
      ]
      +      buf << %Q[
      \n]
             lines.each do |line|
      -        puts detab(line)
      +        buf << detab(line) << "\n"
             end
      -      puts '
      ' + buf << "
      \n" end private :quotedlist def quote(lines) if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts "
      #{blocked_lines.join("\n")}
      " + "
      #{blocked_lines.join("\n")}
      \n" else - puts "
      #{lines.join("\n")}
      " + "
      #{lines.join("\n")}
      \n" end end def doorquote(lines, ref) + buf = "" if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts %Q[
      ] - puts "#{blocked_lines.join("\n")}" - puts %Q[

      #{ref}より

      ] - puts %Q[
      ] + buf << %Q[
      \n] + buf << "#{blocked_lines.join("\n")}\n" + buf << %Q[

      #{ref}より

      \n] + buf << %Q[
      \n] else - puts <<-QUOTE + buf << <<-QUOTE
      #{lines.join("\n")}
       
      @@ -539,6 +554,7 @@ def doorquote(lines, ref)
       
      QUOTE end + buf end def talk(lines) @@ -582,31 +598,34 @@ def result_metric(array) def image_image(id, caption, metric) metrics = parse_metric("html", metric) - puts %Q[
      ] - puts %Q[#{escape_html(compile_inline(caption))}] - image_header id, caption - puts %Q[
      ] + buf = %Q[
      \n] + buf << %Q[#{escape_html(caption)}\n] + buf << image_header(id, caption) + buf << %Q[
      \n] + buf end def image_dummy(id, caption, lines) - puts %Q[
      ] - puts %Q[
      ]
      +      buf = %Q[
      \n] + buf << %Q[
      \n]
             lines.each do |line|
      -        puts detab(line)
      +        buf << detab(line) << "\n"
             end
      -      puts %Q[
      ] - image_header id, caption - puts %Q[
      ] + buf << %Q[
      \n] + buf << image_header(id, caption) + buf << %Q[
      \n] + buf end def image_header(id, caption) - puts %Q[

      ] + buf = %Q[

      \n] if get_chap.nil? - puts %Q[#{I18n.t("image")}#{I18n.t("format_number_header_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("image")}#{I18n.t("format_number_header_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix")}#{caption}\n] else - puts %Q[#{I18n.t("image")}#{I18n.t("format_number_header", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("image")}#{I18n.t("format_number_header", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix")}#{caption}\n] end - puts %Q[

      ] + buf << %Q[

      \n] + buf end def table(lines, id = nil, caption = nil) @@ -623,57 +642,58 @@ def table(lines, id = nil, caption = nil) end rows = adjust_n_cols(rows) - puts %Q[
      ] + buf = %Q[
      \n] begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end - table_begin rows.first.size + buf << table_begin(rows.first.size) return if rows.empty? if sepidx sepidx.times do - tr rows.shift.map {|s| th(s) } + buf << tr(rows.shift.map {|s| th(s) }) end rows.each do |cols| - tr cols.map {|s| td(s) } + buf << tr(cols.map {|s| td(s) }) end else rows.each do |cols| h, *cs = *cols - tr [th(h)] + cs.map {|s| td(s) } + buf << tr([th(h)] + cs.map {|s| td(s) }) end end - table_end - puts %Q[
      ] + buf << table_end + buf << %Q[
      \n] + buf end def table_header(id, caption) if get_chap.nil? - puts %Q[

      #{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}

      ] + %Q[

      #{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{caption}

      \n] else - puts %Q[

      #{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}

      ] + %Q[

      #{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{caption}

      \n] end end def table_begin(ncols) - puts '' + "
      \n" end def tr(rows) - puts "#{rows.join}" + "#{rows.join}\n" end def th(str) - "" + "\n" end def td(str) - "" + "\n" end def table_end - puts '
      #{str}#{str}#{str}#{str}
      ' + "\n" end def comment(lines, comment = nil) @@ -681,63 +701,65 @@ def comment(lines, comment = nil) lines.unshift comment unless comment.blank? if ReVIEW.book.param["draft"] str = lines.map{|line| escape_html(line) }.join("
      ") - puts %Q(
      #{str}
      ) + return %Q(
      #{str}
      \n) else str = lines.join("\n") - puts %Q() + return %Q(\n) end end def footnote(id, str) if ReVIEW.book.param["epubversion"].to_i == 3 - puts %Q(

      [*#{@chapter.footnote(id).number}] #{compile_inline(str)}

      ) + %Q(

      [*#{@chapter.footnote(id).number}] #{str}

      \n) else - puts %Q(

      [*#{@chapter.footnote(id).number}] #{compile_inline(str)}

      ) + %Q(\n) end end def indepimage(id, caption="", metric=nil) metrics = parse_metric("html", metric) caption = "" if caption.nil? - puts %Q[
      ] + buf = %Q[
      \n] begin - puts %Q[#{escape_html(compile_inline(caption))}] + buf << %Q[#{escape_html(caption)}\n] rescue - puts %Q[
      missing image: #{id}
      ] + buf << %Q[
      missing image: #{id}
      \n] end unless caption.empty? - puts %Q[

      ] - puts %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] - puts %Q[

      ] + buf << %Q[

      \n] + buf << %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{caption}\n] + buf << %Q[

      \n] end - puts %Q[
      ] + buf << %Q[
      \n] + buf end alias :numberlessimage indepimage def hr - puts "
      " + "
      \n" end def label(id) - puts %Q() + %Q(\n) end def linebreak - puts "
      " + "
      \n" end def pagebreak - puts %Q(
      ) + %Q(
      \n) end def bpo(lines) - puts "" + buf = "\n" lines.each do |line| - puts detab(line) + buf << detab(line) + "\n" end - puts "" + buf << "\n" + buf end def noindent @@ -774,7 +796,7 @@ def inline_chap(id) def inline_title(id) if ReVIEW.book.param["chapterlink"] - %Q(#{compile_inline(@chapter.env.chapter_index.title(id))}) + %Q(#{@chapter.env.chapter_index.title(id)}) else @chapter.env.chapter_index.title(id) end @@ -890,7 +912,7 @@ def bibpaper_header(id, caption) print %Q() print "[#{@chapter.bibpaper(id).number}]" print %Q() - puts " #{compile_inline(caption)}" + puts " #{caption}" end def bibpaper_bibpaper(id, caption, lines) @@ -904,9 +926,9 @@ def inline_bib(id) def inline_hd_chap(chap, id) n = chap.headline_index.number(id) if chap.number and ReVIEW.book.param["secnolevel"] >= n.split('.').size - str = "「#{n} #{compile_inline(chap.headline(id).caption)}」" + str = "「#{n} #{chap.headline(id).caption}」" else - str = "「#{compile_inline(chap.headline(id).caption)}」" + str = "「#{chap.headline(id).caption}」" end if ReVIEW.book.param["chapterlink"] anchor = "h"+n.gsub(/\./, "-") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index cc58ffebe..a1e71e694 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -216,28 +216,34 @@ require 'review/exception' end def open_tagged_section(tag, level, label, caption) + buf = "" mid = "#{tag}_begin" unless @strategy.respond_to?(mid) error "strategy does not support tagged section: #{tag}" return end @tagged_section.push [tag, level] - @strategy.__send__ mid, level, label, caption + buf << @strategy.__send__(mid, level, label, caption) + buf end def close_tagged_section(tag, level) + buf = "" mid = "#{tag}_end" if @strategy.respond_to?(mid) - @strategy.__send__ mid, level + buf << @strategy.__send__(mid, level) else error "strategy does not support block op: #{mid}" end + buf end def close_all_tagged_section + buf = "" until @tagged_section.empty? - close_tagged_section(* @tagged_section.pop) + buf << close_tagged_section(* @tagged_section.pop) end + buf end def compile_command(name, args, lines) @@ -264,20 +270,21 @@ require 'review/exception' end def compile_headline(level, tag, label, caption) + buf = "" @headline_indexs ||= [0] ## XXX caption.strip! index = level - 1 if tag if tag !~ /\A\// - close_current_tagged_section(level) - open_tagged_section(tag, level, label, caption) + buf << close_current_tagged_section(level) + buf << open_tagged_section(tag, level, label, caption) else open_tag = tag[1..-1] prev_tag_info = @tagged_section.pop unless prev_tag_info.first == open_tag raise CompileError, "#{open_tag} is not opened." end - close_tagged_section(*prev_tag_info) + buf << close_tagged_section(*prev_tag_info) end else if @headline_indexs.size > (index + 1) @@ -285,15 +292,18 @@ require 'review/exception' end @headline_indexs[index] = 0 if @headline_indexs[index].nil? @headline_indexs[index] += 1 - close_current_tagged_section(level) - @strategy.headline level, label, caption + buf << close_current_tagged_section(level) + buf << @strategy.headline(level, label, caption) end + buf end def close_current_tagged_section(level) + buf = "" while @tagged_section.last and @tagged_section.last[1] >= level - close_tagged_section(* @tagged_section.pop) + buf << close_tagged_section(* @tagged_section.pop) end + buf end def comment(text) @@ -301,51 +311,55 @@ require 'review/exception' end def compile_ulist(elem) + buf0 = "" level = 0 elem.each do |current_level, buf| if level == current_level - @strategy.ul_item_end + buf0 << @strategy.ul_item_end # body - @strategy.ul_item_begin [buf] + buf0 << @strategy.ul_item_begin([buf]) elsif level < current_level # down level_diff = current_level - level level = current_level (1..(level_diff - 1)).to_a.reverse.each do |i| - @strategy.ul_begin {i} - @strategy.ul_item_begin [] + buf0 << @strategy.ul_begin{i} + buf0 << @strategy.ul_item_begin([]) end - @strategy.ul_begin {level} - @strategy.ul_item_begin [buf] + buf0 << @strategy.ul_begin{level} + buf0 << @strategy.ul_item_begin([buf]) elsif level > current_level # up level_diff = level - current_level level = current_level (1..level_diff).to_a.reverse.each do |i| - @strategy.ul_item_end - @strategy.ul_end {level + i} + buf0 << @strategy.ul_item_end + buf0 << @strategy.ul_end{level + i} end - @strategy.ul_item_end + buf0 << @strategy.ul_item_end # body - @strategy.ul_item_begin [buf] + buf0 <<@strategy.ul_item_begin([buf]) end end (1..level).to_a.reverse.each do |i| - @strategy.ul_item_end - @strategy.ul_end {i} + buf0 << @strategy.ul_item_end + buf0 << @strategy.ul_end{i} end + buf0 end def compile_olist(elem) - @strategy.ol_begin + buf0 = "" + buf0 << @strategy.ol_begin elem.each do |num, buf| - @strategy.ol_item buf, num + buf0 << @strategy.ol_item(buf, num) end - @strategy.ol_end + buf0 << @strategy.ol_end + buf0 end def compile_unknown_command(args, lines) - @strategy.unknown_command args, lines + @strategy.unknown_command(args, lines) end def compile_block(syntax, args, lines) @@ -364,7 +378,6 @@ require 'review/exception' end - def compile_inline(op, arg) unless inline_defined?(op) raise CompileError, "no such inline op: #{op}" @@ -401,13 +414,13 @@ Start = &. { tagged_section_init } Block* { close_all_tagged_section } ## using &. to detect EOF Block = BlankLine* ( SinglelineComment:c - | Headline:headline + | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c - ) + ) { @strategy.output << c } BlankLine = Newline @@ -447,7 +460,7 @@ BlockElementContent = ( SinglelineComment:c | BlockElementParagraph:c ) -BlockElementParagraph = &. { @blockElem = [] } BlockElementParagraphSub+:c { @blockElem } +BlockElementParagraph = &. { @blockElem = "" } BlockElementParagraphSub:c { @blockElem } BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c } @@ -486,7 +499,7 @@ UlistElement = " "+ "*"+:level " "* SinglelineContent:c { @ulist_elem << [level. Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) } -OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, c] } +OlistElement = " "+ < /\d/+ > { level=text } "." Space* SinglelineContent:c {@olist_elem << [level, [c]] } ## : 用語リスト ## 行頭から… の指定がない From bb69cd2fcf26953a960708f44b0ba802950a9883 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 1 Mar 2014 13:54:37 +0900 Subject: [PATCH 006/214] add literals_*.kpeg (for Ruby 1.8 and > 1.9) --- lib/review/compiler/literals_1_8.kpeg | 18 ++++++++++++++++++ lib/review/compiler/literals_1_9.kpeg | 22 ++++++++++++++++++++++ lib/review/htmlbuilder.rb | 4 ++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 lib/review/compiler/literals_1_8.kpeg create mode 100644 lib/review/compiler/literals_1_9.kpeg diff --git a/lib/review/compiler/literals_1_8.kpeg b/lib/review/compiler/literals_1_8.kpeg new file mode 100644 index 000000000..873ff4561 --- /dev/null +++ b/lib/review/compiler/literals_1_8.kpeg @@ -0,0 +1,18 @@ +%% name = ReVIEW::Compiler::Literals + +%% header { + +## +# Provides Literals appropriate for your ruby version. +#-- +# This set of literals is for Ruby 1.8 regular expressions. +} + +Alphanumeric = /[0-9A-Za-z\200-\377]/n +AlphanumericAscii = /[A-Za-z0-9]/ +LowerAlphabetAscii = /[a-z]/ +Digit = /[0-9]/ +BOM = "\357\273\277" +Newline = /\n|\r\n?/ +NonAlphanumeric = /[\000-\057\072-\100\133-\140\173-\177]/n +Spacechar = / |\t/ diff --git a/lib/review/compiler/literals_1_9.kpeg b/lib/review/compiler/literals_1_9.kpeg new file mode 100644 index 000000000..bf9d80c8b --- /dev/null +++ b/lib/review/compiler/literals_1_9.kpeg @@ -0,0 +1,22 @@ +%% name = ReVIEW::Compiler::Literals + +%% header { +# coding: UTF-8 + +## +#-- +# This set of literals is for Ruby 1.9 regular expressions and gives full +# unicode support. +# +# Unlike peg-markdown, this set of literals recognizes Unicode alphanumeric +# characters, newlines and spaces. +} + +Alphanumeric = /\p{Word}/ +AlphanumericAscii = /[A-Za-z0-9]/ +LowerAlphabetAscii = /[a-z]/ +Digit = /[0-9]/ +BOM = "\uFEFF" +Newline = /\n|\r\n?|\p{Zl}|\p{Zp}/ +NonAlphanumeric = /\p{^Word}/ +Spacechar = /\t|\p{Zs}/ diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index b1e38ead3..e9d69cde6 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -431,7 +431,7 @@ def list_header(id, caption) def list_body(id, lines) id ||= '' - buf = %Q[
      \n]
      +      buf = %Q[
      ]
             body = lines.inject(''){|i, j| i + detab(j) + "\n"}
             lexer = File.extname(id).gsub(/\./, '')
             buf << highlight(:body => body, :lexer => lexer, :format => 'html')
      @@ -487,7 +487,7 @@ def listnum_body(lines)
           def emlist(lines, caption = nil)
             buf = %Q[
      \n] buf << %Q(

      #{caption}

      \n) unless caption.nil? - buf << %Q[
      \n]
      +      buf << %Q[
      ]
             lines.each do |line|
               buf << detab(line) << "\n"
             end
      
      From 0f9c460ab50b6ab7840996e217d7de04c2498208 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Sat, 1 Mar 2014 14:08:53 +0900
      Subject: [PATCH 007/214] escape in highlight()
      
      ---
       lib/review/htmlutils.rb | 4 ++--
       1 file changed, 2 insertions(+), 2 deletions(-)
      
      diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb
      index db0ac4690..a77e16dc4 100644
      --- a/lib/review/htmlutils.rb
      +++ b/lib/review/htmlutils.rb
      @@ -49,10 +49,10 @@ def highlight(ops)
                          :formatter => format,
                          :lexer => lexer)
               rescue MentosError
      -          body
      +          escape_html(body)
               end
             rescue LoadError
      -          body
      +        escape_html(body)
             end
           end
         end
      
      From 4428945a40d18b0d2c70fa385ffcbe24d81f15c5 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Sat, 1 Mar 2014 14:34:49 +0900
      Subject: [PATCH 008/214] fix some error
      
      ---
       lib/review/compiler.rb    | 35 ++++++++++++++++++++++++++++++-----
       lib/review/htmlbuilder.rb |  4 ++--
       lib/review/review.kpeg    |  5 +++--
       3 files changed, 35 insertions(+), 9 deletions(-)
      
      diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb
      index 270739b66..8a79db634 100644
      --- a/lib/review/compiler.rb
      +++ b/lib/review/compiler.rb
      @@ -1221,7 +1221,7 @@ def _NonInlineElement
           return _tmp
         end
       
      -  # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline {           compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline)
      +  # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline {       compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })
         def _BlockElement
       
           _save = self.pos
      @@ -1303,7 +1303,7 @@ def _BlockElement
                 self.pos = _save1
                 break
               end
      -        @result = begin;            compile_command(symbol, args, contents) ; end
      +        @result = begin;        compile_command(symbol, args, contents) ; end
               _tmp = true
               unless _tmp
                 self.pos = _save1
      @@ -1350,6 +1350,12 @@ def _BlockElement
                 break
               end
               _tmp = apply(:_Newline)
      +        unless _tmp
      +          self.pos = _save6
      +          break
      +        end
      +        @result = begin;  compile_command(symbol, args, nil) ; end
      +        _tmp = true
               unless _tmp
                 self.pos = _save6
               end
      @@ -1531,7 +1537,7 @@ def _BlockElementContents
           return _tmp
         end
       
      -  # BlockElementContent = (SinglelineComment:c | BlockElement:c | BlockElementParagraph:c)
      +  # BlockElementContent = (SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { "" })
         def _BlockElementContent
       
           _save = self.pos
      @@ -1546,6 +1552,25 @@ def _BlockElementContent
             self.pos = _save
             _tmp = apply(:_BlockElementParagraph)
             c = @result
      +      break if _tmp
      +      self.pos = _save
      +
      +      _save1 = self.pos
      +      while true # sequence
      +        _tmp = apply(:_Newline)
      +        c = @result
      +        unless _tmp
      +          self.pos = _save1
      +          break
      +        end
      +        @result = begin;  "" ; end
      +        _tmp = true
      +        unless _tmp
      +          self.pos = _save1
      +        end
      +        break
      +      end # end sequence
      +
             break if _tmp
             self.pos = _save
             break
      @@ -2650,12 +2675,12 @@ def _Spacechar
         Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d Newline { d }")
         Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }")
         Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { text }")
      -  Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline {           compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline)")
      +  Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline {       compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })")
         Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" < /[^>\\r\\n]+/ > {symbol = text} \">\" \"{\" < InlineElementContents? > { contents = text } \"}\" { compile_inline(symbol,contents); }")
         Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" < /([^\\r\\n\\]\\\\]|\\\\[^\\r\\n])*/ > \"]\" { text }")
         Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }")
         Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c")
      -  Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c)")
      +  Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { \"\" })")
         Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }")
         Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline")
         Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }")
      diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb
      index e9d69cde6..05ac2b661 100644
      --- a/lib/review/htmlbuilder.rb
      +++ b/lib/review/htmlbuilder.rb
      @@ -225,7 +225,7 @@ def column_begin(level, label, caption)
           end
       
           def column_end(level)
      -      "
      \n" + "
      \n" end def xcolumn_begin(level, label, caption) @@ -440,7 +440,7 @@ def list_body(id, lines) end def source(lines, caption = nil) - buf = %Q[
      ] + buf = %Q[
      \n] buf << source_header(caption) buf << source_body(caption, lines) buf << "
      \n" diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index a1e71e694..f64f3f9e6 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -440,8 +440,8 @@ ContentText = NonInlineElement+:c { c } NonInlineElement = !InlineElement < /[^\r\n]/ > { text } BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline - { compile_command(symbol, args, contents) } - | "//" ElementName:symbol BracketArg*:args Space* Newline + { compile_command(symbol, args, contents) } + | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } ) InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" @@ -458,6 +458,7 @@ BlockElementContents = BlockElementContent+:c BlockElementContent = ( SinglelineComment:c | BlockElement:c | BlockElementParagraph:c + | Newline:c { "" } ) BlockElementParagraph = &. { @blockElem = "" } BlockElementParagraphSub:c { @blockElem } From 5a35b460ebcb12a50908412303bc7bfb31caae24 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 1 Mar 2014 14:46:44 +0900 Subject: [PATCH 009/214] Revert "for debug" This reverts commit 6684ac810d608f18c994ad0cb544427e2a7bab6b. --- bin/review-compile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/review-compile b/bin/review-compile index 4b9ffc617..8c3713cf0 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -22,10 +22,10 @@ require 'fileutils' require 'optparse' def main -# Signal.trap(:INT) { exit 1 } -# if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/ -# Signal.trap(:PIPE, 'IGNORE') -# end + Signal.trap(:INT) { exit 1 } + if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/ + Signal.trap(:PIPE, 'IGNORE') + end _main rescue Errno::EPIPE exit 0 From 4b38c033f7108b03b84f3e2cbefa2dd0d8f197b1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 1 Mar 2014 22:38:56 +0900 Subject: [PATCH 010/214] fix HTMLBuilder and tests for HTMLBuilder --- lib/review/builder.rb | 4 +- lib/review/compiler.rb | 245 +++++++++++++++++++++++++++++++------- lib/review/htmlbuilder.rb | 47 ++++---- lib/review/review.kpeg | 32 +++-- test/test_helper.rb | 30 +++-- test/test_htmlbuilder.rb | 237 +++++++++++++++++++----------------- 6 files changed, 396 insertions(+), 199 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 07f37f9e2..787597173 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -272,12 +272,12 @@ def raw(str) builders = matched[1].split(/,/).map{|i| i.gsub(/\s/, '') } c = self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase if builders.include?(c) - print matched[2].gsub("\\n", "\n") + matched[2].gsub("\\n", "\n") else "" end else - print str.gsub("\\n", "\n") + str.gsub("\\n", "\n") end end diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 8a79db634..f6b9247d6 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -400,6 +400,10 @@ def text(str) st.result end + def escape_text(text) + @strategy.nofunc_text(text) + end + class SyntaxElement def initialize(name, type, argc, &block) @name = name @@ -412,7 +416,7 @@ def initialize(name, type, argc, &block) def check_args(args) unless @argc_spec === args.size - raise CompileError, "wrong # of parameters (block command //#{@name}, expect #{@argc_spec} but #{args.size})" + raise ReVIEW::CompileError, "wrong # of parameters (block command //#{@name}, expect #{@argc_spec} but #{args.size})" end @checker.call(*args) if @checker end @@ -610,7 +614,7 @@ def compile_command(name, args, lines) end begin syntax.check_args args - rescue CompileError => err + rescue ReVIEW::CompileError => err error err.message args = ['(NoArgument)'] * syntax.min_argc end @@ -627,6 +631,7 @@ def compile_command(name, args, lines) def compile_headline(level, tag, label, caption) buf = "" @headline_indexs ||= [0] ## XXX + caption ||= "" caption.strip! index = level - 1 if tag @@ -637,7 +642,7 @@ def compile_headline(level, tag, label, caption) open_tag = tag[1..-1] prev_tag_info = @tagged_section.pop unless prev_tag_info.first == open_tag - raise CompileError, "#{open_tag} is not opened." + raise ReVIEW::CompileError, "#{open_tag} is not opened." end buf << close_tagged_section(*prev_tag_info) end @@ -735,7 +740,7 @@ def compile_single(syntax, args) def compile_inline(op, arg) unless inline_defined?(op) - raise CompileError, "no such inline op: #{op}" + raise ReVIEW::CompileError, "no such inline op: #{op}" end unless @strategy.respond_to?("inline_#{op}") raise "strategy does not support inline op: @<#{op}>" @@ -882,7 +887,7 @@ def _BlankLine return _tmp end - # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* { compile_headline(level, cmd, label, caption) } + # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) { compile_headline(level, cmd, label, caption) } def _Headline _save = self.pos @@ -926,17 +931,30 @@ def _Headline self.pos = _save break end + _save4 = self.pos _tmp = apply(:_SinglelineContent) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save4 + end caption = @result unless _tmp self.pos = _save break end - while true + + _save5 = self.pos + while true # choice _tmp = apply(:_Newline) - break unless _tmp - end - _tmp = true + break if _tmp + self.pos = _save5 + _tmp = apply(:_EOF) + break if _tmp + self.pos = _save5 + break + end # end choice + unless _tmp self.pos = _save break @@ -1016,7 +1034,7 @@ def _Paragraph return _tmp end - # ParagraphSub = (InlineElement:c { c } | < ContentText > { text })+:d Newline { d } + # ParagraphSub = (InlineElement:c { c } | < ContentText > { text })+:d { e=d.join("") } Newline { e } def _ParagraphSub _save = self.pos @@ -1133,12 +1151,18 @@ def _ParagraphSub self.pos = _save break end + @result = begin; e=d.join("") ; end + _tmp = true + unless _tmp + self.pos = _save + break + end _tmp = apply(:_Newline) unless _tmp self.pos = _save break end - @result = begin; d ; end + @result = begin; e ; end _tmp = true unless _tmp self.pos = _save @@ -1221,7 +1245,7 @@ def _NonInlineElement return _tmp end - # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) }) + # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) }) def _BlockElement _save = self.pos @@ -1303,7 +1327,7 @@ def _BlockElement self.pos = _save1 break end - @result = begin; compile_command(symbol, args, contents) ; end + @result = begin; compile_command(symbol, args, contents) ; end _tmp = true unless _tmp self.pos = _save1 @@ -1371,7 +1395,7 @@ def _BlockElement return _tmp end - # InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" { compile_inline(symbol,contents); } + # InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" { compile_inline(symbol,contents) } def _InlineElement _save = self.pos @@ -1431,7 +1455,7 @@ def _InlineElement self.pos = _save break end - @result = begin; compile_inline(symbol,contents); ; end + @result = begin; compile_inline(symbol,contents) ; end _tmp = true unless _tmp self.pos = _save @@ -1443,7 +1467,7 @@ def _InlineElement return _tmp end - # BracketArg = "[" < /([^\r\n\]\\]|\\[^\r\n])*/ > "]" { text } + # BracketArg = "[" BracketArgContentInline+:c "]" { c.join("") } def _BracketArg _save = self.pos @@ -1453,11 +1477,22 @@ def _BracketArg self.pos = _save break end - _text_start = self.pos - _tmp = scan(/\A(?-mix:([^\r\n\]\\]|\\[^\r\n])*)/) + _save1 = self.pos + _ary = [] + _tmp = apply(:_BracketArgContentInline) if _tmp - text = get_text(_text_start) + _ary << @result + while true + _tmp = apply(:_BracketArgContentInline) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 end + c = @result unless _tmp self.pos = _save break @@ -1467,7 +1502,7 @@ def _BracketArg self.pos = _save break end - @result = begin; text ; end + @result = begin; c.join("") ; end _tmp = true unless _tmp self.pos = _save @@ -1479,6 +1514,77 @@ def _BracketArg return _tmp end + # BracketArgContentInline = (InlineElement:c { c } | "\\]" { "]" } | < /[^\r\n\]]/ > { escape_text(text) }) + def _BracketArgContentInline + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _tmp = match_string("\\]") + unless _tmp + self.pos = _save2 + break + end + @result = begin; "]" ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\]])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save3 + break + end + @result = begin; escape_text(text) ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_BracketArgContentInline unless _tmp + return _tmp + end + # BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } def _BraceArg @@ -1817,7 +1923,7 @@ def _BlockElementContentText return _tmp end - # InlineElementContents = !"}" InlineElementContent+:c { c } + # InlineElementContents = !"}" InlineElementContent+ def _InlineElementContents _save = self.pos @@ -1831,27 +1937,16 @@ def _InlineElementContents break end _save2 = self.pos - _ary = [] _tmp = apply(:_InlineElementContent) if _tmp - _ary << @result while true _tmp = apply(:_InlineElementContent) - _ary << @result if _tmp break unless _tmp end _tmp = true - @result = _ary else self.pos = _save2 end - c = @result - unless _tmp - self.pos = _save - break - end - @result = begin; c ; end - _tmp = true unless _tmp self.pos = _save end @@ -2009,7 +2104,7 @@ def _ContentInlines return _tmp end - # ContentInline = (InlineElement:c { c } | !Newline < /[^\r\n]/ > {text }) + # ContentInline = (InlineElement:c { c } | !Newline < /[^\r\n]/ > { escape_text(text) }) def _ContentInline _save = self.pos @@ -2053,7 +2148,7 @@ def _ContentInline self.pos = _save2 break end - @result = begin; text ; end + @result = begin; escape_text(text) ; end _tmp = true unless _tmp self.pos = _save2 @@ -2070,7 +2165,7 @@ def _ContentInline return _tmp end - # Ulist = &. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } + # Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } def _Ulist _save = self.pos @@ -2088,6 +2183,11 @@ def _Ulist self.pos = _save break end + _tmp = apply(:_UlistElement) + unless _tmp + self.pos = _save + break + end _save2 = self.pos _save3 = self.pos @@ -2095,6 +2195,9 @@ def _Ulist _tmp = apply(:_UlistElement) break if _tmp self.pos = _save3 + _tmp = apply(:_UlistContLine) + break if _tmp + self.pos = _save3 _tmp = apply(:_SinglelineComment) break if _tmp self.pos = _save3 @@ -2109,6 +2212,9 @@ def _Ulist _tmp = apply(:_UlistElement) break if _tmp self.pos = _save4 + _tmp = apply(:_UlistContLine) + break if _tmp + self.pos = _save4 _tmp = apply(:_SinglelineComment) break if _tmp self.pos = _save4 @@ -2220,6 +2326,57 @@ def _UlistElement return _tmp end + # UlistContLine = " " " "+ !"*" SinglelineContent:c { @ulist_elem[-1][1] << c } + def _UlistContLine + + _save = self.pos + while true # sequence + _tmp = match_string(" ") + unless _tmp + self.pos = _save + break + end + _save1 = self.pos + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = match_string("*") + _tmp = _tmp ? nil : true + self.pos = _save2 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; @ulist_elem[-1][1] << c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_UlistContLine unless _tmp + return _tmp + end + # Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) } def _Olist @@ -2669,29 +2826,31 @@ def _Spacechar Rules[:_Start] = rule_info("Start", "&. { tagged_section_init } Block* { close_all_tagged_section }") Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { @strategy.output << c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") - Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* { compile_headline(level, cmd, label, caption) }") + Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) { compile_headline(level, cmd, label, caption) }") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c { compile_paragraph(c) }") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d Newline { d }") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d { e=d.join(\"\") } Newline { e }") Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { text }") - Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") - Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" < /[^>\\r\\n]+/ > {symbol = text} \">\" \"{\" < InlineElementContents? > { contents = text } \"}\" { compile_inline(symbol,contents); }") - Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" < /([^\\r\\n\\]\\\\]|\\\\[^\\r\\n])*/ > \"]\" { text }") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") + Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" < /[^>\\r\\n]+/ > {symbol = text} \">\" \"{\" < InlineElementContents? > { contents = text } \"}\" { compile_inline(symbol,contents) }") + Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline+:c \"]\" { c.join(\"\") }") + Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" { \"]\" } | < /[^\\r\\n\\]]/ > { escape_text(text) })") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { \"\" })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }") - Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c }") + Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText:c)") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(!InlineElement /[^\\r\\n}]/)+") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c (Newline | EOF) { c }") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c.join }") - Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > {text })") - Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) }") + Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > { escape_text(text) })") + Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) }") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c { @ulist_elem << [level.size, c] }") + Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c { @ulist_elem[-1][1] << c }") Rules[:_Olist] = rule_info("Olist", "{ @olist_elem = [] } (OlistElement | SinglelineComment)+:c { compile_olist(@olist_elem) }") Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { level=text } \".\" Space* SinglelineContent:c {@olist_elem << [level, [c]] }") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment):c Dlist?:cc") diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 05ac2b661..b740c48dc 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -340,7 +340,7 @@ def ul_item(lines) end def ul_item_begin(lines) - "
    1. #{lines.join}\n" + "
    2. #{lines.join}" end def ul_item_end @@ -508,9 +508,9 @@ def emlistnum(lines, caption = nil) end def cmd(lines, caption = nil) - buf == %Q[
      \n] + buf = %Q[
      \n] buf << %Q(

      #{caption}

      \n) unless caption.nil? - buf %Q[
      \n]
      +      buf << %Q[
      ]
             lines.each do |line|
               buf << detab(line) << "\n"
             end
      @@ -520,7 +520,7 @@ def cmd(lines, caption = nil)
           end
       
           def quotedlist(lines, css_class)
      -      buf << %Q[
      \n]
      +      buf = %Q[
      \n]
             lines.each do |line|
               buf << detab(line) << "\n"
             end
      @@ -685,11 +685,11 @@ def tr(rows)
           end
       
           def th(str)
      -      "#{str}\n"
      +      "#{str}"
           end
       
           def td(str)
      -      "#{str}\n"
      +      "#{str}"
           end
       
           def table_end
      @@ -900,23 +900,24 @@ def text(str)
           end
       
           def bibpaper(lines, id, caption)
      -      puts %Q[
      ] - bibpaper_header id, caption + buf = %Q[
      \n] + buf << bibpaper_header(id, caption) unless lines.empty? - bibpaper_bibpaper id, caption, lines + buf << bibpaper_bibpaper(id, caption, lines) end - puts "
      " + buf << "
      " << "\n" + buf end def bibpaper_header(id, caption) - print %Q() - print "[#{@chapter.bibpaper(id).number}]" - print %Q() - puts " #{caption}" + buf = %Q() + buf << "[#{@chapter.bibpaper(id).number}]" + buf << %Q() + buf << " #{caption}\n" end def bibpaper_bibpaper(id, caption, lines) - print split_paragraph(lines).join("") + split_paragraph(lines).join("") end def inline_bib(id) @@ -1087,19 +1088,21 @@ def compile_href(url, label) end def flushright(lines) + result = "" if ReVIEW.book.param["deprecated-blocklines"].nil? - puts split_paragraph(lines).join("\n").gsub("

      ", "

      ") + result << split_paragraph(lines).join("\n").gsub("

      ", "

      ") << "\n" else - puts %Q[

      ] - print %Q[
      ]
      -        lines.each {|line| puts detab(line) }
      -        puts '
      ' - puts '
      ' + result << %Q[
      \n] + result << %Q[
      ]
      +        lines.each {|line| result << detab(line) << "\n" }
      +        result << "
      \n" + result << "
      \n" end + result end def centering(lines) - puts split_paragraph(lines).join("\n").gsub("

      ", "

      ") + split_paragraph(lines).join("\n").gsub("

      ", "

      ") + "\n" end def image_ext diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index f64f3f9e6..a80e36f9d 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -45,6 +45,10 @@ require 'review/exception' st.result end + def escape_text(text) + @strategy.nofunc_text(text) + end + class SyntaxElement def initialize(name, type, argc, &block) @name = name @@ -57,7 +61,7 @@ require 'review/exception' def check_args(args) unless @argc_spec === args.size - raise CompileError, "wrong # of parameters (block command //#{@name}, expect #{@argc_spec} but #{args.size})" + raise ReVIEW::CompileError, "wrong # of parameters (block command //#{@name}, expect #{@argc_spec} but #{args.size})" end @checker.call(*args) if @checker end @@ -255,7 +259,7 @@ require 'review/exception' end begin syntax.check_args args - rescue CompileError => err + rescue ReVIEW::CompileError => err error err.message args = ['(NoArgument)'] * syntax.min_argc end @@ -272,6 +276,7 @@ require 'review/exception' def compile_headline(level, tag, label, caption) buf = "" @headline_indexs ||= [0] ## XXX + caption ||= "" caption.strip! index = level - 1 if tag @@ -282,7 +287,7 @@ require 'review/exception' open_tag = tag[1..-1] prev_tag_info = @tagged_section.pop unless prev_tag_info.first == open_tag - raise CompileError, "#{open_tag} is not opened." + raise ReVIEW::CompileError, "#{open_tag} is not opened." end buf << close_tagged_section(*prev_tag_info) end @@ -380,7 +385,7 @@ require 'review/exception' def compile_inline(op, arg) unless inline_defined?(op) - raise CompileError, "no such inline op: #{op}" + raise ReVIEW::CompileError, "no such inline op: #{op}" end unless @strategy.respond_to?("inline_#{op}") raise "strategy does not support inline op: @<#{op}>" @@ -424,7 +429,7 @@ Block = BlankLine* BlankLine = Newline -Headline = ( HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent:caption Newline* ) { compile_headline(level, cmd, label, caption) } +Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline|EOF) { compile_headline(level, cmd, label, caption) } HeadlinePrefix = < /={1,5}/ > { text.length } @@ -432,7 +437,7 @@ Paragraph = ParagraphSub+:c { compile_paragraph(c) } ParagraphSub = ( InlineElement:c { c } | < ContentText > { text } - )+:d Newline { d } + )+:d { e=d.join("") } Newline { e } ## ContentText = !Newline !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } ContentText = NonInlineElement+:c { c } @@ -440,14 +445,16 @@ ContentText = NonInlineElement+:c { c } NonInlineElement = !InlineElement < /[^\r\n]/ > { text } BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline - { compile_command(symbol, args, contents) } + { compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } ) InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" - { compile_inline(symbol,contents); } + { compile_inline(symbol,contents) } + +BracketArg = "[" BracketArgContentInline+:c "]" { c.join("") } -BracketArg = "[" < /([^\r\n\]\\]|\\[^\r\n])*/ > "]" { text } +BracketArgContentInline = ( InlineElement:c { c } | "\\]" { "]" } | < /[^\r\n\]]/ > { escape_text(text) } ) BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } @@ -469,7 +476,7 @@ BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } -InlineElementContents = !"}" InlineElementContent+:c { c } +InlineElementContents = !"}" InlineElementContent+ InlineElementContent = ( InlineElement:c | InlineElementContentText:c @@ -482,7 +489,7 @@ SinglelineContent = ContentInlines:c (Newline | EOF) { c } ContentInlines = ContentInline+:c { c.join } ContentInline = ( InlineElement:c { c } - | !Newline < /[^\r\n]/ > {text } + | !Newline < /[^\r\n]/ > { escape_text(text) } ) ## ContentInlineText = !InlineElement !Newline .:cc { p [:char,cc];cc } @@ -491,9 +498,10 @@ ContentInline = ( InlineElement:c { c } ## * 箇条書き ## 行頭から… の指定がない -Ulist = &. { @ulist_elem=[] } (UlistElement | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } +Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ (Newline | EOF) { compile_ulist(@ulist_elem) } UlistElement = " "+ "*"+:level " "* SinglelineContent:c { @ulist_elem << [level.size, c] } +UlistContLine = " " " "+ !"*" SinglelineContent:c { @ulist_elem[-1][1] << c } ## 1. 番号付き箇条書き ## 行頭から… の指定がない diff --git a/test/test_helper.rb b/test/test_helper.rb index ed10f6fa9..92fa03799 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,18 +1,32 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib/') require 'test/unit' + def compile_inline(str) + @compiler.setup_parser(str) + @compiler.tagged_section_init + @compiler.parse("Paragraph") + @compiler.result + end + + def compile_blockelem(str) + @compiler.setup_parser(str) + @compiler.tagged_section_init + @compiler.parse("BlockElement") + @compiler.result + end + def ul_helper(src, expect) - io = StringIO.new(src) - li = LineInput.new(io) - @compiler.__send__(:compile_ulist, li) - assert_equal expect, @builder.raw_result + @compiler.setup_parser(src) + @compiler.tagged_section_init + @compiler.parse("Ulist") + assert_equal expect, @compiler.result end def ol_helper(src, expect) - io = StringIO.new(src) - li = LineInput.new(io) - @compiler.__send__(:compile_olist, li) - assert_equal expect, @builder.raw_result + @compiler.setup_parser(src) + @compiler.tagged_section_init + @compiler.parse("Olist") + assert_equal expect, @compiler.result end def builder_helper(src, expect, method_sym) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 3d82f9013..acf2cdde4 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'test_helper' +require 'review' require 'review/compiler' require 'review/book' require 'review/htmlbuilder' @@ -35,110 +36,113 @@ def test_xmlns_ops_prefix_epub2 end def test_headline_level1 - @builder.headline(1,"test","this is test.") - assert_equal %Q|

      第1章 this is test.

      \n|, @builder.raw_result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|

      第1章 this is test.

      \n|, result end + def test_headline_level1_without_secno ReVIEW.book.param["secnolevel"] = 0 - @builder.headline(1,"test","this is test.") - assert_equal %Q|

      this is test.

      \n|, @builder.raw_result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|

      this is test.

      \n|, result end def test_headline_level1_with_inlinetag - @builder.headline(1,"test","this @{is} test.<&\">") - assert_equal %Q|

      第1章 this is test.<&">

      \n|, @builder.raw_result + @compiler.setup_parser("={test} this @{is} test.<&\">\n") + @compiler.tagged_section_init + @compiler.parse("Headline") + assert_equal %Q|

      第1章 this is test.<&">

      \n|, @compiler.result end def test_headline_level2 - @builder.headline(2,"test","this is test.") - assert_equal %Q|\n

      1.1 this is test.

      \n|, @builder.raw_result + result = @builder.headline(2,"test","this is test.") + assert_equal %Q|\n

      1.1 this is test.

      \n|, result end def test_headline_level3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\n

      this is test.

      \n|, @builder.raw_result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|\n

      this is test.

      \n|, result end def test_headline_level3_with_secno ReVIEW.book.param["secnolevel"] = 3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\n

      1.0.1 this is test.

      \n|, @builder.raw_result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|\n

      1.0.1 this is test.

      \n|, result end def test_label - @builder.label("label_test") - assert_equal %Q|\n|, @builder.raw_result + result = @builder.label("label_test") + assert_equal %Q|\n|, result end def test_href - ret = @builder.compile_href("http://github.com", "GitHub") - assert_equal %Q|GitHub|, ret + result = @builder.compile_href("http://github.com", "GitHub") + assert_equal %Q|GitHub|, result end def test_href_without_label - ret = @builder.compile_href("http://github.com",nil) - assert_equal %Q|http://github.com|, ret + result = @builder.compile_href("http://github.com",nil) + assert_equal %Q|http://github.com|, result end def test_inline_href - ret = @builder.inline_href("http://github.com, Git\\,Hub") - assert_equal %Q|Git,Hub|, ret + result = @builder.inline_href("http://github.com, Git\\,Hub") + assert_equal %Q|Git,Hub|, result end def test_inline_href_without_label - ret = @builder.inline_href("http://github.com") - assert_equal %Q|http://github.com|, ret + result = @builder.inline_href("http://github.com") + assert_equal %Q|http://github.com|, result end def test_inline_raw - ret = @builder.inline_raw("@{inline}") - assert_equal %Q|@{inline}|, ret + result = @builder.inline_raw("@{inline}") + assert_equal %Q|@{inline}|, result end def test_inline_in_table - ret = @builder.table(["1\t2", "------------", "3\t4<>&"]) - assert_equal %Q|
      \n\n\n\n
      12
      34<>&
      \n
      \n|, @builder.raw_result + result = @builder.table(["1\t2", "------------", "3\t4<>&"]) + assert_equal %Q|
      \n\n\n\n
      12
      34<>&
      \n
      \n|, result end def test_inline_br - ret = @builder.inline_br("") - assert_equal %Q|
      |, ret + result = @builder.inline_br("") + assert_equal %Q|
      |, result end def test_inline_i - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, result end def test_inline_i_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test inline<&;\\ test test2|, ret + result = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test inline<&;\\ test test2|, result end def test_inline_b - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, result end def test_inline_b_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test inline<&;\\ test test2|, ret + result = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test inline<&;\\ test test2|, result end def test_inline_tt - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, result end def test_inline_tti - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, result end def test_inline_ttb - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, result end def test_inline_hd_chap @@ -148,60 +152,63 @@ def @chapter.headline_index end @param["secnolevel"] = 2 - ret = @builder.compile_inline("test @{chap1|test} test2") - assert_equal %Q|test 「te_st」 test2|, ret + result = compile_inline("test @{chap1|test} test2") + assert_equal %Q|test 「te_st」 test2|, result @param["secnolevel"] = 3 - ret = @builder.compile_inline("test @{chap1|test} test2") - assert_equal %Q|test 「1.1.1 te_st」 test2|, ret + result = compile_inline("test @{chap1|test} test2") + assert_equal %Q|test 「1.1.1 te_st」 test2|, result end def test_inline_uchar - ret = @builder.compile_inline("test @{2460} test2") - assert_equal %Q|test ① test2|, ret + result = compile_inline("test @{2460} test2") + assert_equal %Q|test ① test2|, result end def test_inline_ruby - ret = @builder.compile_inline("@{粗雑,クルード}と思われているなら@{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。") - assert_equal "粗雑クルードと思われているなら繊細テクニカルにやり、繊細と思われているなら粗雑にやる。", ret + result = compile_inline("@{粗雑,クルード}と思われているなら@{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。") + assert_equal "粗雑クルードと思われているなら繊細テクニカルにやり、繊細と思われているなら粗雑にやる。", result end def test_inline_ruby_comma - ret = @builder.compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") - assert_equal "foo, bar, buzフー・バー・バズ", ret + result = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") + assert_equal "foo, bar, buzフー・バー・バズ", result end def test_inline_ref - ret = @builder.compile_inline("@{外部参照<>&}") - assert_equal %Q|「●● 外部参照<>&」|, ret + result = compile_inline("@{外部参照<>&}") + assert_equal %Q|「●● 外部参照<>&」|, result end def test_quote lines = ["foo", "bar", "","buz"] - @builder.quote(lines) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, @builder.raw_result + result = @builder.quote(lines) + assert_equal %Q|

      foobar

      \n

      buz

      \n|, result end + def test_memo - @builder.memo(["test1", "", "test2"], "this is @{test}<&>_") - assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test2

      \n
      \n|, @builder.raw_result + result = compile_blockelem("//memo[this is @{test}<&>_]{\ntest1\n\ntest2\n//}\n") + assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test2

      \n
      \n|, result end + def test_noindent + result = "" @builder.noindent - @builder.paragraph(["foo", "bar"]) - @builder.paragraph(["foo2", "bar2"]) - assert_equal %Q|

      foobar

      \n

      foo2bar2

      \n|, @builder.raw_result + result << @builder.paragraph(["foo", "bar"]) + result << @builder.paragraph(["foo2", "bar2"]) + assert_equal %Q|

      foobar

      \n

      foo2bar2

      \n|, result end def test_flushright - @builder.flushright(["foo", "bar", "", "buz"]) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, @builder.raw_result + result = @builder.flushright(["foo", "bar", "", "buz"]) + assert_equal %Q|

      foobar

      \n

      buz

      \n|, result end def test_centering - @builder.centering(["foo", "bar", "", "buz"]) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, @builder.raw_result + result = @builder.centering(["foo", "bar", "", "buz"]) + assert_equal %Q|

      foobar

      \n

      buz

      \n|, result end def test_image @@ -211,8 +218,8 @@ def @chapter.image(id) item end - @builder.image_image("sampleimg","sample photo",nil) - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.image_image("sampleimg","sample photo",nil) + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result end def test_image_with_metric @@ -222,8 +229,8 @@ def @chapter.image(id) item end - @builder.image_image("sampleimg","sample photo","scale=1.2") - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.image_image("sampleimg","sample photo","scale=1.2") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result end def test_image_with_metric2 @@ -232,8 +239,8 @@ def @chapter.image(id) item.instance_eval{@pathes=["./images/chap1-sampleimg.png"]} item end - @builder.image_image("sampleimg","sample photo","scale=1.2,html::class=sample,latex::ignore=params") - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.image_image("sampleimg","sample photo","scale=1.2,html::class=sample,latex::ignore=params") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result end def test_indepimage @@ -243,8 +250,8 @@ def @chapter.image(id) item end - @builder.indepimage("sampleimg","sample photo",nil) - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.indepimage("sampleimg","sample photo",nil) + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result end def test_indepimage_without_caption @@ -254,8 +261,8 @@ def @chapter.image(id) item end - @builder.indepimage("sampleimg",nil,nil) - assert_equal %Q|
      \n\n
      \n|, @builder.raw_result + result = @builder.indepimage("sampleimg",nil,nil) + assert_equal %Q|
      \n\n
      \n|, result end def test_indepimage_with_metric @@ -265,8 +272,8 @@ def @chapter.image(id) item end - @builder.indepimage("sampleimg","sample photo","scale=1.2") - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.indepimage("sampleimg","sample photo","scale=1.2") + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result end def test_indepimage_with_metric2 @@ -276,8 +283,8 @@ def @chapter.image(id) item end - @builder.indepimage("sampleimg","sample photo","scale=1.2, html::class=\"sample\",latex::ignore=params") - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, @builder.raw_result + result = @builder.indepimage("sampleimg","sample photo","scale=1.2, html::class=\"sample\",latex::ignore=params") + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result end def test_indepimage_without_caption_but_with_metric @@ -287,53 +294,54 @@ def @chapter.image(id) item end - @builder.indepimage("sampleimg",nil,"scale=1.2") - assert_equal %Q|
      \n\n
      \n|, @builder.raw_result + result = @builder.indepimage("sampleimg",nil,"scale=1.2") + assert_equal %Q|
      \n\n
      \n|, result end def test_list def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end - @builder.list(["test1", "test1.5", "", "test2"], "samplelist", "this is @{test}<&>_") + result = compile_blockelem("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest2\n//}\n") begin # FIXME: Use params instead of exception handling require 'pygments' - assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, @builder.raw_result + assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, result rescue LoadError - assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest2\n
      \n
      \n|, @builder.raw_result + assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, result end end + def test_emlist - @builder.emlist(["lineA","lineB"]) - assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, @builder.raw_result + result = @builder.emlist(["lineA","lineB"]) + assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, result end def test_emlist_caption - @builder.emlist(["lineA","lineB"],"cap1") - assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, @builder.raw_result + result = @builder.emlist(["lineA","lineB"],"cap1") + assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, result end def test_emlist_with_tab - @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) - assert_equal %Q|
      \n
              lineA\n                lineB\n        lineC\n
      \n
      \n|, @builder.raw_result + result = @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) + assert_equal %Q|
      \n
              lineA\n                lineB\n        lineC\n
      \n
      \n|, result end def test_emlist_with_4tab @builder.instance_eval{@tabwidth=4} - @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) - assert_equal %Q|
      \n
          lineA\n        lineB\n    lineC\n
      \n
      \n|, @builder.raw_result + result = @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) + assert_equal %Q|
      \n
          lineA\n        lineB\n    lineC\n
      \n
      \n|, result end def test_cmd - @builder.cmd(["lineA","lineB"]) - assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, @builder.raw_result + result = @builder.cmd(["lineA","lineB"]) + assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, result end def test_cmd_caption - @builder.cmd(["lineA","lineB"], "cap1") - assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, @builder.raw_result + result = @builder.cmd(["lineA","lineB"], "cap1") + assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, result end def test_bib @@ -349,8 +357,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - @builder.bibpaper(["a", "b"], "samplebib", "sample bib @{bold}") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, @builder.raw_result + result = compile_blockelem("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, result end def test_bibpaper_with_anchor @@ -358,8 +366,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - @builder.bibpaper(["a", "b"], "samplebib", "sample bib @{http://example.jp}") - assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, @builder.raw_result + result = compile_blockelem("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, result end def column_helper(review) @@ -416,6 +424,7 @@ def test_column_2 assert_equal expect, column_helper(review) end + def test_column_3 review =<<-EOS ===[column] test @@ -429,6 +438,7 @@ def test_column_3 end end + def test_ul src =<<-EOS * AAA @@ -466,6 +476,7 @@ def test_ul_nest1 ul_helper(src, expect) end + def test_ul_nest2 src =<<-EOS * AAA @@ -610,55 +621,57 @@ def test_inline_raw5 end def test_block_raw0 - @builder.raw("<>!\"\\n& ") + result = @builder.raw("<>!\"\\n& ") expect =<<-EOS <>!" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw1 - @builder.raw("|html|<>!\"\\n& ") + result = @builder.raw("|html|<>!\"\\n& ") expect =<<-EOS <>!" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw2 - @builder.raw("|html, latex|<>!\"\\n& ") + result = @builder.raw("|html, latex|<>!\"\\n& ") expect =<<-EOS <>!\" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw3 - @builder.raw("|latex, idgxml|<>!\"\\n& ") + result = @builder.raw("|latex, idgxml|<>!\"\\n& ") expect =<<-EOS EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw4 - @builder.raw("|html <>!\"\\n& ") + result = @builder.raw("|html <>!\"\\n& ") expect =<<-EOS |html <>!\" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_inline_fn fn = Book::FootnoteIndex.parse(['//footnote[foo][bar\\a\\$buz]']) @chapter.instance_eval{@footnote_index=fn} - @builder.footnote("foo",'bar\\a\\$buz') + result = @builder.footnote("foo",'bar\\a\\$buz') expect =<<-'EOS'

      [*1] bar\a\$buz

      EOS - assert_equal expect, @builder.raw_result + assert_equal expect, result end + + end From 9dcf7fb1dc39be30c3b5db277a4cc37873860ac5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 01:03:42 +0900 Subject: [PATCH 011/214] fix Builder --- lib/review/builder.rb | 46 ++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 787597173..16e99e545 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -77,26 +77,32 @@ def puts(*s) end def list(lines, id, caption) + buf = "" begin - list_header id, caption + buf << list_header(id, caption) rescue KeyError error "no such list: #{id}" end - list_body id, lines + buf << list_body(id, lines) + buf end def listnum(lines, id, caption) + buf = "" begin - list_header id, caption + buf << list_header(id, caption) rescue KeyError error "no such list: #{id}" end - listnum_body lines + buf << listnum_body(lines) + buf end def source(lines, caption) - source_header caption - source_body lines + buf = "" + buf << source_header(caption) + buf << source_body(lines) + buf end def image(lines, id, caption, metric = nil) @@ -109,6 +115,7 @@ def image(lines, id, caption, metric = nil) end def table(lines, id = nil, caption = nil) + buf = "" rows = [] sepidx = nil lines.each_with_index do |line, idx| @@ -123,26 +130,27 @@ def table(lines, id = nil, caption = nil) rows = adjust_n_cols(rows) begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end - return if rows.empty? - table_begin rows.first.size + return buf if rows.empty? + buf << table_begin(rows.first.size) if sepidx sepidx.times do - tr rows.shift.map {|s| th(s) } + buf << tr(rows.shift.map {|s| th(s) }) end rows.each do |cols| - tr cols.map {|s| td(s) } + buf << tr(cols.map {|s| td(s) }) end else rows.each do |cols| h, *cs = *cols - tr [th(h)] + cs.map {|s| td(s) } + buf << tr([th(h)] + cs.map {|s| td(s) }) end end - table_end + buf << table_end + buf end def adjust_n_cols(rows) @@ -252,12 +260,14 @@ def text(str) end def bibpaper(lines, id, caption) - bibpaper_header id, caption + buf = "" + buf << bibpaper_header(id, caption) unless lines.empty? - puts "" - bibpaper_bibpaper id, caption, lines + buf << "\n" + buf << bibpaper_bibpaper(id, caption, lines) end - puts "" + buf << "\n" + buf end def inline_hd(id) @@ -307,7 +317,7 @@ def entries @entries ||= Dir.entries(@book.basedir + @book.image_dir) end rescue Errno::ENOENT - @entries = [] + @entries = [] end def warn(msg) From 1b626f0b19e97e33e072462525818a4a721e6a36 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 01:05:02 +0900 Subject: [PATCH 012/214] fix review.kpeg; escape in InlineElementContent --- lib/review/compiler.rb | 172 ++++++++++++++++++++++++++--------------- lib/review/review.kpeg | 12 ++- 2 files changed, 117 insertions(+), 67 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index f6b9247d6..9cbd63ec8 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -390,6 +390,7 @@ def do_compile parse() end +=begin def text(str) st = @strategy.dup f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(str))) @@ -399,6 +400,7 @@ def text(str) parser.parse("InlineElementContents") st.result end +=end def escape_text(text) @strategy.nofunc_text(text) @@ -1395,7 +1397,7 @@ def _BlockElement return _tmp end - # InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" { compile_inline(symbol,contents) } + # InlineElement = "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) } def _InlineElement _save = self.pos @@ -1405,17 +1407,8 @@ def _InlineElement self.pos = _save break end - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^>\r\n]+)/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save - break - end - @result = begin; symbol = text; end - _tmp = true + _tmp = apply(:_InlineElementSymbol) + symbol = @result unless _tmp self.pos = _save break @@ -1430,32 +1423,50 @@ def _InlineElement self.pos = _save break end - _text_start = self.pos _save1 = self.pos _tmp = apply(:_InlineElementContents) + @result = nil unless _tmp unless _tmp _tmp = true self.pos = _save1 end - if _tmp - text = get_text(_text_start) + contents = @result + unless _tmp + self.pos = _save + break end + _tmp = match_string("}") unless _tmp self.pos = _save break end - @result = begin; contents = text ; end + @result = begin; compile_inline(symbol,contents) ; end _tmp = true unless _tmp self.pos = _save - break end - _tmp = match_string("}") + break + end # end sequence + + set_failed_rule :_InlineElement unless _tmp + return _tmp + end + + # InlineElementSymbol = < /[^>\r\n]+/ > { text } + def _InlineElementSymbol + + _save = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^>\r\n]+)/) + if _tmp + text = get_text(_text_start) + end unless _tmp self.pos = _save break end - @result = begin; compile_inline(symbol,contents) ; end + @result = begin; text ; end _tmp = true unless _tmp self.pos = _save @@ -1463,7 +1474,7 @@ def _InlineElement break end # end sequence - set_failed_rule :_InlineElement unless _tmp + set_failed_rule :_InlineElementSymbol unless _tmp return _tmp end @@ -1923,7 +1934,7 @@ def _BlockElementContentText return _tmp end - # InlineElementContents = !"}" InlineElementContent+ + # InlineElementContents = !"}" InlineElementContent+:c { c.join("") } def _InlineElementContents _save = self.pos @@ -1937,16 +1948,27 @@ def _InlineElementContents break end _save2 = self.pos + _ary = [] _tmp = apply(:_InlineElementContent) if _tmp + _ary << @result while true _tmp = apply(:_InlineElementContent) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else self.pos = _save2 end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c.join("") ; end + _tmp = true unless _tmp self.pos = _save end @@ -1957,7 +1979,7 @@ def _InlineElementContents return _tmp end - # InlineElementContent = (InlineElement:c | InlineElementContentText:c) + # InlineElementContent = (InlineElement:c | InlineElementContentText+:c) def _InlineElementContent _save = self.pos @@ -1966,7 +1988,21 @@ def _InlineElementContent c = @result break if _tmp self.pos = _save + _save1 = self.pos + _ary = [] _tmp = apply(:_InlineElementContentText) + if _tmp + _ary << @result + while true + _tmp = apply(:_InlineElementContentText) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end c = @result break if _tmp self.pos = _save @@ -1977,53 +2013,62 @@ def _InlineElementContent return _tmp end - # InlineElementContentText = (!InlineElement /[^\r\n}]/)+ + # InlineElementContentText = ("\\}" { "}" } | !InlineElement < /[^\r\n}]/ > { text }) def _InlineElementContentText + _save = self.pos + while true # choice - _save1 = self.pos - while true # sequence - _save2 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save2 - unless _tmp - self.pos = _save1 + _save1 = self.pos + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save1 + end break - end - _tmp = scan(/\A(?-mix:[^\r\n}])/) - unless _tmp - self.pos = _save1 - end - break - end # end sequence + end # end sequence - if _tmp - while true + break if _tmp + self.pos = _save + _save2 = self.pos + while true # sequence _save3 = self.pos - while true # sequence - _save4 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save4 - unless _tmp - self.pos = _save3 - break - end - _tmp = scan(/\A(?-mix:[^\r\n}])/) - unless _tmp - self.pos = _save3 - end + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 break - end # end sequence + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n}])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save2 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence - break unless _tmp - end - _tmp = true - else + break if _tmp self.pos = _save - end + break + end # end choice + set_failed_rule :_InlineElementContentText unless _tmp return _tmp end @@ -2833,7 +2878,8 @@ def _Spacechar Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { text }") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") - Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" < /[^>\\r\\n]+/ > {symbol = text} \">\" \"{\" < InlineElementContents? > { contents = text } \"}\" { compile_inline(symbol,contents) }") + Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" { compile_inline(symbol,contents) }") + Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< /[^>\\r\\n]+/ > { text }") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline+:c \"]\" { c.join(\"\") }") Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" { \"]\" } | < /[^\\r\\n\\]]/ > { escape_text(text) })") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") @@ -2842,9 +2888,9 @@ def _Spacechar Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }") - Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+") - Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText:c)") - Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(!InlineElement /[^\\r\\n}]/)+") + Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c.join(\"\") }") + Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText+:c)") + Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" { \"}\" } | !InlineElement < /[^\\r\\n}]/ > { text })") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c (Newline | EOF) { c }") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c.join }") Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > { escape_text(text) })") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index a80e36f9d..0d97f3575 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -35,6 +35,7 @@ require 'review/exception' parse() end +=begin def text(str) st = @strategy.dup f = LineInput.new(ReVIEW::Preprocessor::Strip.new(StringIO.new(str))) @@ -44,6 +45,7 @@ require 'review/exception' parser.parse("InlineElementContents") st.result end +=end def escape_text(text) @strategy.nofunc_text(text) @@ -449,9 +451,11 @@ BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline Blo | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } ) -InlineElement = "@<" < /[^>\r\n]+/ > {symbol = text} ">" "{" < InlineElementContents? > { contents = text } "}" +InlineElement = "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) } +InlineElementSymbol = < /[^>\r\n]+/ > { text } + BracketArg = "[" BracketArgContentInline+:c "]" { c.join("") } BracketArgContentInline = ( InlineElement:c { c } | "\\]" { "]" } | < /[^\r\n\]]/ > { escape_text(text) } ) @@ -476,13 +480,13 @@ BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } -InlineElementContents = !"}" InlineElementContent+ +InlineElementContents = !"}" InlineElementContent+:c { c.join("") } InlineElementContent = ( InlineElement:c - | InlineElementContentText:c + | InlineElementContentText+:c ) -InlineElementContentText = ( !InlineElement /[^\r\n}]/ )+ +InlineElementContentText = ( "\\}" { "}" } | !InlineElement < /[^\r\n}]/> { text } ) SinglelineContent = ContentInlines:c (Newline | EOF) { c } From f5c4899c93e046a0c9feea72098fe0217c1e3a7e Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 01:05:36 +0900 Subject: [PATCH 013/214] fix TOPBuilder and test for TOPBuilder --- lib/review/topbuilder.rb | 225 +++++++++++++++++++++++++-------------- test/test_topbuilder.rb | 130 +++++++++++----------- 2 files changed, 213 insertions(+), 142 deletions(-) diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb index 1b70f2f69..5bf0ba2ce 100644 --- a/lib/review/topbuilder.rb +++ b/lib/review/topbuilder.rb @@ -102,6 +102,10 @@ def builder_init_file end private :builder_init_file + def blank_reset + @blank_seen = false + end + def print(s) @blank_seen = false super @@ -115,8 +119,14 @@ def puts(s) private :puts def blank - @output.puts unless @blank_seen + # @output.puts unless @blank_seen + seen = @blank_seen @blank_seen = true + unless seen + "\n" + else + "" + end end private :blank @@ -137,8 +147,9 @@ def messages end def headline(level, label, caption) + buf = "" prefix = "" - blank + buf << blank case level when 1 if @chapter.number.to_s =~ /\A\d+\Z/ @@ -172,7 +183,9 @@ def headline(level, label, caption) raise "caption level too deep or unsupported: #{level}" end prefix = "" if (level.to_i > ReVIEW.book.param["secnolevel"]) - puts "■H#{level}■#{prefix}#{compile_inline(caption)}" + buf << "■H#{level}■#{prefix}#{caption}" + blank_reset + buf end def ul_begin @@ -180,7 +193,8 @@ def ul_begin end def ul_item(lines) - puts "●\t#{lines.join}" + blank_reset + "●\t#{lines.join}" end def ul_end @@ -188,17 +202,18 @@ def ul_end end def ol_begin - blank @olitem = 0 + blank end def ol_item(lines, num) - puts "#{num}\t#{lines.join}" + blank_reset + "#{num}\t#{lines.join}\n" end def ol_end - blank @olitem = nil + blank end def dl_begin @@ -206,13 +221,16 @@ def dl_begin end def dt(line) - puts "★#{line}☆" + blank_reset + "★#{line}☆\n" end def dd(lines) + buf = "" split_paragraph(lines).each do |paragraph| - puts "\t#{paragraph.gsub(/\n/, '')}" + buf << "\t#{paragraph.gsub(/\n/, '')}\n" end + blank_reset end def dl_end @@ -220,14 +238,17 @@ def dl_end end def paragraph(lines) - puts lines.join + blank_reset + lines.join+"\n" end def read(lines) - puts "◆→開始:#{@titles["lead"]}←◆" - puts split_paragraph(lines).join("\n") - puts "◆→終了:#{@titles["lead"]}←◆" - blank + buf = "" + buf << "◆→開始:#{@titles["lead"]}←◆\n" + buf << split_paragraph(lines).join("\n") << "\n" + buf << "◆→終了:#{@titles["lead"]}←◆\n" + buf << blank + buf end alias :lead read @@ -243,40 +264,51 @@ def inline_list(id) end def list_header(id, caption) - blank - puts "◆→開始:#{@titles["list"]}←◆" + buf = "" + buf << blank + buf << "◆→開始:#{@titles["list"]}←◆\n" if get_chap.nil? - puts %Q[#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << "\n" else - puts %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << "\n" end - blank + blank_reset + buf << blank + buf end def list_body(id, lines) + buf = "" lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts "◆→終了:#{@titles["list"]}←◆" - blank + buf << "◆→終了:#{@titles["list"]}←◆\n" + blank_reset + buf << blank end def base_block(type, lines, caption = nil) - blank - puts "◆→開始:#{@titles[type]}←◆" - puts "■#{compile_inline(caption)}" unless caption.nil? - puts lines.join("\n") - puts "◆→終了:#{@titles[type]}←◆" - blank + buf = "" + buf << blank + buf << "◆→開始:#{@titles[type]}←◆\n" + buf << "■#{caption}\n" unless caption.nil? + buf << lines.join("\n") << "\n" + buf << "◆→終了:#{@titles[type]}←◆\n" + blank_reset + buf << blank + buf end def base_parablock(type, lines, caption = nil) - blank - puts "◆→開始:#{@titles[type]}←◆" - puts "■#{compile_inline(caption)}" unless caption.nil? - puts split_paragraph(lines).join("\n") - puts "◆→終了:#{@titles[type]}←◆" - blank + buf = "" + buf << blank + buf << "◆→開始:#{@titles[type]}←◆\n" + buf << "■#{caption}\n" unless caption.nil? + buf << split_paragraph(lines).join("\n") << "\n" + buf << "◆→終了:#{@titles[type]}←◆\n" + blank_reset + buf << blank + buf end def emlist(lines, caption = nil) @@ -284,23 +316,29 @@ def emlist(lines, caption = nil) end def emlistnum(lines, caption = nil) + buf = "" blank - puts "◆→開始:#{@titles["emlist"]}←◆" - puts "■#{compile_inline(caption)}" unless caption.nil? + buf << "◆→開始:#{@titles["emlist"]}←◆\n" + buf << "■#{caption}\n" unless caption.nil? _lines = [] lines.each_with_index do |line, i| - puts (i + 1).to_s.rjust(2) + ": #{line}" + buf << (i + 1).to_s.rjust(2) + ": #{line}\n" end - puts "◆→終了:#{@titles["emlist"]}←◆" - blank + buf << "◆→終了:#{@titles["emlist"]}←◆\n" + blank_reset + buf << blank + buf end def listnum_body(lines) + buf = "" lines.each_with_index do |line, i| - puts (i + 1).to_s.rjust(2) + ": #{line}" + buf << (i + 1).to_s.rjust(2) + ": #{line}\n" end - puts "◆→終了:#{@titles["list"]}←◆" - blank + buf << "◆→終了:#{@titles["list"]}←◆\n" + blank_reset + buf << blank + buf end def cmd(lines, caption = nil) @@ -330,48 +368,62 @@ def inline_img(id) end def image(lines, id, caption, metric=nil) - blank - puts "◆→開始:#{@titles["image"]}←◆" + buf = "" + buf << blank + buf << "◆→開始:#{@titles["image"]}←◆\n" if get_chap.nil? - puts "#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}" + buf << "#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" else - puts "#{I18n.t("image")}#{I18n.t("format_number", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}" + buf << "#{I18n.t("image")}#{I18n.t("format_number", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" end - blank + blank_reset + buf << blank if @chapter.image(id).bound? - puts "◆→#{@chapter.image(id).path}←◆" + buf << "◆→#{@chapter.image(id).path}←◆\n" else lines.each do |line| - puts line + buf << line << "\n" end end - puts "◆→終了:#{@titles["image"]}←◆" - blank + buf << "◆→終了:#{@titles["image"]}←◆\n" + blank_reset + buf << blank + buf end def texequation(lines) - puts "◆→開始:#{@titles["texequation"]}←◆" - puts "#{lines.join("\n")}" - puts "◆→終了:#{@titles["texequation"]}←◆" - blank + buf = "" + buf << "◆→開始:#{@titles["texequation"]}←◆\n" + buf << "#{lines.join("\n")}\n" + buf << "◆→終了:#{@titles["texequation"]}←◆\n" + blank_reset + buf << blank + buf end def table_header(id, caption) - blank - puts "◆→開始:#{@titles["table"]}←◆" + buf = "" + buf << blank + buf << "◆→開始:#{@titles["table"]}←◆\n" if get_chap.nil? - puts "#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}" + buf << "#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" else - puts "#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}" + buf << "#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" end - blank + blank_reset + buf << blank + buf end def table_begin(ncols) + "" end def tr(rows) - puts rows.join("\t") + buf = "" + buf << rows.join("\t") << "\n" + blank_reset + buf end def th(str) @@ -383,19 +435,24 @@ def td(str) end def table_end - puts "◆→終了:#{@titles["table"]}←◆" - blank + buf = "" + buf << "◆→終了:#{@titles["table"]}←◆\n" + blank_reset + buf << blank + buf end def comment(lines, comment = nil) lines ||= [] lines.unshift comment unless comment.blank? str = lines.join("") - puts "◆→DTP連絡:#{str}←◆" + blank_reset + "◆→DTP連絡:#{str}←◆\n" end def footnote(id, str) - puts "【注#{@chapter.footnote(id).number}】#{compile_inline(str)}" + blank_reset + "【注#{@chapter.footnote(id).number}】#{str}\n" end def inline_fn(id) @@ -511,25 +568,33 @@ def inline_m(str) end def noindent - puts "◆→DTP連絡:次の1行インデントなし←◆" + blank_reset + "◆→DTP連絡:次の1行インデントなし←◆\n" end def nonum_begin(level, label, caption) - puts "■H#{level}■#{compile_inline(caption)}" + blank_reset + "■H#{level}■#{caption}\n" end def nonum_end(level) end def common_column_begin(type, caption) - blank - puts "◆→開始:#{@titles["type"]}←◆" - puts %Q[■#{compile_inline(caption)}] + buf = "" + buf << blank + buf << "◆→開始:#{@titles["type"]}←◆\n" + buf << %Q[■#{caption}\n] + blank_reset + buf end def common_column_end(type) - puts %Q[◆→終了:#{@titles["type"]}←◆] - blank + buf = "" + buf << %Q[◆→終了:#{@titles["type"]}←◆] + blank_reset + buf << blank + buf end def column_begin(level, label, caption) @@ -679,15 +744,18 @@ def insn(lines, caption = nil) alias :box insn def indepimage(id, caption=nil, metric=nil) - blank + buf = "" + buf << blank begin - puts "◆→画像 #{@chapter.image(id).path.sub(/\A\.\//, "")} #{metric.join(" ")}←◆" + buf << "◆→画像 #{@chapter.image(id).path.sub(/\A\.\//, "")} #{metric.join(" ")}←◆\n" rescue warn "no such image: #{id}" - puts "◆→画像 #{id}←◆" + buf << "◆→画像 #{id}←◆\n" end - puts "図 #{compile_inline(caption)}" if caption.present? - blank + buf << "図 #{caption}\n" if caption.present? + blank_reset + buf << blank + buf end alias :numberlessimage indepimage @@ -775,7 +843,8 @@ def inline_pageref(idref) end def circle_begin(level, label, caption) - puts "・\t#{caption}" + blank_reset + "・\t#{caption}\n" end def circle_end(level) diff --git a/test/test_topbuilder.rb b/test/test_topbuilder.rb index 87f15acf8..fcde88a69 100644 --- a/test/test_topbuilder.rb +++ b/test/test_topbuilder.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'test_helper' +require 'review' require 'review/compiler' require 'review/book' require 'review/topbuilder' @@ -32,30 +33,30 @@ def warn(msg) end def test_headline_level1 - @builder.headline(1,"test","this is test.") - assert_equal %Q|■H1■第1章 this is test.\n|, @builder.raw_result + result = @builder.headline(1,"test","this is test.\n") + assert_equal %Q|■H1■第1章 this is test.\n|, result end def test_headline_level1_without_secno @param["secnolevel"] = 0 - @builder.headline(1,"test","this is test.") - assert_equal %Q|■H1■this is test.\n|, @builder.raw_result + result = @builder.headline(1,"test","this is test.\n") + assert_equal %Q|■H1■this is test.\n|, result end def test_headline_level2 - @builder.headline(2,"test","this is test.") - assert_equal %Q|■H2■1.1 this is test.\n|, @builder.raw_result + result = @builder.headline(2,"test","this is test.\n") + assert_equal %Q|■H2■1.1 this is test.\n|, result end def test_headline_level3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|■H3■this is test.\n|, @builder.raw_result + result = @builder.headline(3,"test","this is test.\n") + assert_equal %Q|■H3■this is test.\n|, result end def test_headline_level3_with_secno @param["secnolevel"] = 3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|■H3■1.0.1 this is test.\n|, @builder.raw_result + result = @builder.headline(3,"test","this is test.\n") + assert_equal %Q|■H3■1.0.1 this is test.\n|, result end def test_href @@ -79,13 +80,13 @@ def test_inline_ruby end def test_inline_kw - ret = @builder.compile_inline("@{ISO, International Organization for Standardization } @{Ruby<>}") - assert_equal %Q|★ISO☆(International Organization for Standardization) ★Ruby<>☆|, ret + result = compile_inline("@{ISO, International Organization for Standardization } @{Ruby<>}") + assert_equal %Q|★ISO☆(International Organization for Standardization) ★Ruby<>☆|, result end def test_inline_maru - ret = @builder.compile_inline("@{1}@{20}@{A}@{z}") - assert_equal %Q|1◆→丸数字1←◆20◆→丸数字20←◆A◆→丸数字A←◆z◆→丸数字z←◆|, ret + result = compile_inline("@{1}@{20}@{A}@{z}") + assert_equal %Q|1◆→丸数字1←◆20◆→丸数字20←◆A◆→丸数字A←◆z◆→丸数字z←◆|, result end def test_inline_br @@ -94,93 +95,94 @@ def test_inline_br end def test_inline_i - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test ▲inline test☆ test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test ▲inline test☆ test2|, result end def test_inline_i_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test ▲inline<&;\\ test☆ test2|, ret + result = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test ▲inline<&;\\ test☆ test2|, result end def test_inline_b - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test ★inline test☆ test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test ★inline test☆ test2|, result end def test_inline_b_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test ★inline<&;\\ test☆ test2|, ret + result = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test ★inline<&;\\ test☆ test2|, result end def test_inline_tt - ret = @builder.compile_inline("test @{inline test} test2@{\\}}") - assert_equal %Q|test △inline test☆ test2△}☆|, ret + result = compile_inline("test @{inline test} test2@{\\}}") + assert_equal %Q|test △inline test☆ test2△}☆|, result end def test_inline_tti - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test ▲inline test☆◆→等幅フォントイタ←◆ test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test ▲inline test☆◆→等幅フォントイタ←◆ test2|, result end def test_inline_ttb - ret = @builder.compile_inline("test @{inline test} test2") - assert_equal %Q|test ★inline test☆◆→等幅フォント太字←◆ test2|, ret + result = compile_inline("test @{inline test} test2") + assert_equal %Q|test ★inline test☆◆→等幅フォント太字←◆ test2|, result end def test_inline_uchar - ret = @builder.compile_inline("test @{2460} test2") - assert_equal %Q|test ① test2|, ret + result = compile_inline("test @{2460} test2") + assert_equal %Q|test ① test2|, result end def test_inline_in_table - @builder.table(["★1☆\t▲2☆", "------------", "★3☆\t▲4☆<>&"]) - assert_equal %Q|★★1☆☆\t★▲2☆☆\n★3☆\t▲4☆<>&\n◆→終了:表←◆\n\n|, @builder.raw_result + result = @builder.table(["★1☆\t▲2☆", "------------", "★3☆\t▲4☆<>&"]) + assert_equal %Q|★★1☆☆\t★▲2☆☆\n★3☆\t▲4☆<>&\n◆→終了:表←◆\n\n|, result end def test_paragraph lines = ["foo","bar"] - @builder.paragraph(lines) - assert_equal %Q|foobar\n|, @builder.raw_result + result = @builder.paragraph(lines) + assert_equal %Q|foobar\n|, result end def test_tabbed_paragraph lines = ["\tfoo","bar"] - @builder.paragraph(lines) - assert_equal %Q|\tfoobar\n|, @builder.raw_result + result = @builder.paragraph(lines) + assert_equal %Q|\tfoobar\n|, result end def test_flushright - @builder.flushright(["foo", "bar", "","buz"]) - assert_equal %Q|◆→開始:右寄せ←◆\nfoobar\nbuz\n◆→終了:右寄せ←◆\n\n|, @builder.raw_result + result = @builder.flushright(["foo", "bar", "","buz"]) + assert_equal %Q|◆→開始:右寄せ←◆\nfoobar\nbuz\n◆→終了:右寄せ←◆\n\n|, result end def test_noindent - @builder.noindent - @builder.paragraph(["foo", "bar"]) - @builder.paragraph(["foo2", "bar2"]) - assert_equal %Q|◆→DTP連絡:次の1行インデントなし←◆\nfoobar\nfoo2bar2\n|, @builder.raw_result + result = "" + result << @builder.noindent + result << @builder.paragraph(["foo", "bar"]) + result << @builder.paragraph(["foo2", "bar2"]) + assert_equal %Q|◆→DTP連絡:次の1行インデントなし←◆\nfoobar\nfoo2bar2\n|, result end def test_list def @chapter.list(id) Book::ListIndex::Item.new("test",1) end - @builder.list(["foo", "bar"], "test", "this is @{test}<&>_") - assert_equal %Q|◆→開始:リスト←◆\nリスト1.1 this is ★test☆<&>_\n\nfoo\nbar\n◆→終了:リスト←◆\n\n|, @builder.raw_result + result = compile_blockelem("//list[test][this is @{test}<&>_]{\nfoo\nbar\n//}\n") + assert_equal %Q|◆→開始:リスト←◆\nリスト1.1 this is ★test☆<&>_\n\nfoo\nbar\n◆→終了:リスト←◆\n\n|, result end def test_listnum def @chapter.list(id) Book::ListIndex::Item.new("test",1) end - @builder.listnum(["foo", "bar"], "test", "this is @{test}<&>_") - assert_equal %Q|◆→開始:リスト←◆\nリスト1.1 this is ★test☆<&>_\n\n 1: foo\n 2: bar\n◆→終了:リスト←◆\n\n|, @builder.raw_result + result = compile_blockelem("//listnum[test][this is @{test}<&>_]{\nfoo\nbar\n//}\n") + assert_equal %Q|◆→開始:リスト←◆\nリスト1.1 this is ★test☆<&>_\n\n 1: foo\n 2: bar\n◆→終了:リスト←◆\n\n|, result end def test_emlistnum - @builder.emlistnum(["foo", "bar"], "this is @{test}<&>_") - assert_equal %Q|◆→開始:インラインリスト←◆\n■this is ★test☆<&>_\n 1: foo\n 2: bar\n◆→終了:インラインリスト←◆\n\n|, @builder.raw_result + result = compile_blockelem("//emlistnum[this is @{test}<&>_]{\nfoo\nbar\n//}\n") + assert_equal %Q|◆→開始:インラインリスト←◆\n■this is ★test☆<&>_\n 1: foo\n 2: bar\n◆→終了:インラインリスト←◆\n\n|, result end def test_image @@ -190,8 +192,8 @@ def @chapter.image(id) item end - @builder.image(["foo"], "sampleimg","sample photo",nil) - assert_equal %Q|◆→開始:図←◆\n図1.1 sample photo\n\n◆→./images/chap1-sampleimg.png←◆\n◆→終了:図←◆\n\n|, @builder.raw_result + result = @builder.image(["foo"], "sampleimg","sample photo",nil) + assert_equal %Q|◆→開始:図←◆\n図1.1 sample photo\n\n◆→./images/chap1-sampleimg.png←◆\n◆→終了:図←◆\n\n|, result end def test_image_with_metric @@ -201,13 +203,13 @@ def @chapter.image(id) item end - @builder.image(["foo"], "sampleimg","sample photo","scale=1.2") - assert_equal %Q|◆→開始:図←◆\n図1.1 sample photo\n\n◆→./images/chap1-sampleimg.png←◆\n◆→終了:図←◆\n\n|, @builder.raw_result + result = @builder.image(["foo"], "sampleimg","sample photo","scale=1.2") + assert_equal %Q|◆→開始:図←◆\n図1.1 sample photo\n\n◆→./images/chap1-sampleimg.png←◆\n◆→終了:図←◆\n\n|, result end def test_texequation - @builder.texequation(["\\sin", "1^{2}"]) - assert_equal %Q|◆→開始:TeX式←◆\n\\sin\n1^{2}\n◆→終了:TeX式←◆\n\n|, @builder.raw_result + result = @builder.texequation(["\\sin", "1^{2}"]) + assert_equal %Q|◆→開始:TeX式←◆\n\\sin\n1^{2}\n◆→終了:TeX式←◆\n\n|, result end def test_inline_raw0 @@ -235,46 +237,46 @@ def test_inline_raw5 end def test_block_raw0 - @builder.raw("<>!\"\\n& ") + result = @builder.raw("<>!\"\\n& ") expect =<<-EOS <>!" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw1 - @builder.raw("|top|<>!\"\\n& ") + result = @builder.raw("|top|<>!\"\\n& ") expect =<<-EOS <>!" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw2 - @builder.raw("|top, latex|<>!\"\\n& ") + result = @builder.raw("|top, latex|<>!\"\\n& ") expect =<<-EOS <>!\" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw3 - @builder.raw("|latex, idgxml|<>!\"\\n& ") + result = @builder.raw("|latex, idgxml|<>!\"\\n& ") expect =<<-EOS EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end def test_block_raw4 - @builder.raw("|top <>!\"\\n& ") + result = @builder.raw("|top <>!\"\\n& ") expect =<<-EOS |top <>!\" & EOS - assert_equal expect.chomp, @builder.raw_result + assert_equal expect.chomp, result end end From 216755c0e9d9fef44069ecbdb666b34698eba408 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 09:52:46 +0900 Subject: [PATCH 014/214] add test_helper method compile_headline --- test/test_helper.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_helper.rb b/test/test_helper.rb index 92fa03799..554aec2cd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,6 +15,13 @@ def compile_blockelem(str) @compiler.result end + def compile_headline(str) + @compiler.setup_parser(str) + @compiler.tagged_section_init + @compiler.parse("Headline") + @compiler.result + end + def ul_helper(src, expect) @compiler.setup_parser(src) @compiler.tagged_section_init From 416c28d10a827ccc4655cb5aeb2c691977adbb2e Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 11:11:19 +0900 Subject: [PATCH 015/214] fix escape; do escape only for Regexp pattern to avoid double escaping --- lib/review/compiler.rb | 21 +++++++++++---------- lib/review/review.kpeg | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 9cbd63ec8..d1f92ec7f 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1213,7 +1213,7 @@ def _ContentText return _tmp end - # NonInlineElement = !InlineElement < /[^\r\n]/ > { text } + # NonInlineElement = !InlineElement < /[^\r\n]/ > { escape_text(text) } def _NonInlineElement _save = self.pos @@ -1235,7 +1235,7 @@ def _NonInlineElement self.pos = _save break end - @result = begin; text ; end + @result = begin; escape_text(text) ; end _tmp = true unless _tmp self.pos = _save @@ -1850,7 +1850,7 @@ def _BlockElementParagraphSub return _tmp end - # BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } + # BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join("") } def _BlockElementContentText _save = self.pos @@ -1903,26 +1903,27 @@ def _BlockElementContentText self.pos = _save break end - _text_start = self.pos _save7 = self.pos + _ary = [] _tmp = apply(:_NonInlineElement) if _tmp + _ary << @result while true _tmp = apply(:_NonInlineElement) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else self.pos = _save7 end - if _tmp - text = get_text(_text_start) - end + c = @result unless _tmp self.pos = _save break end - @result = begin; text ; end + @result = begin; c.join("") ; end _tmp = true unless _tmp self.pos = _save @@ -2876,7 +2877,7 @@ def _Spacechar Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c { compile_paragraph(c) }") Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d { e=d.join(\"\") } Newline { e }") Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }") - Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { text }") + Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { escape_text(text) }") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" { compile_inline(symbol,contents) }") Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< /[^>\\r\\n]+/ > { text }") @@ -2887,7 +2888,7 @@ def _Spacechar Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { \"\" })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") - Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text }") + Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join(\"\") }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c.join(\"\") }") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText+:c)") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" { \"}\" } | !InlineElement < /[^\\r\\n}]/ > { text })") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 0d97f3575..8958a3b6a 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -444,7 +444,7 @@ ParagraphSub = ( InlineElement:c { c } ## ContentText = !Newline !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } ContentText = NonInlineElement+:c { c } -NonInlineElement = !InlineElement < /[^\r\n]/ > { text } +NonInlineElement = !InlineElement < /[^\r\n]/ > { escape_text(text) } BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } @@ -478,7 +478,7 @@ BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c } )+ Newline -BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist < NonInlineElement+ > { text } +BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join("") } InlineElementContents = !"}" InlineElementContent+:c { c.join("") } From 9d1b4d8cc9d4fc19e5595cc2f865a5329a3be230 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 11:11:55 +0900 Subject: [PATCH 016/214] fix highlight; do not escaping withoug highlighting --- lib/review/htmlutils.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index a77e16dc4..514ff535d 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -49,10 +49,10 @@ def highlight(ops) :formatter => format, :lexer => lexer) rescue MentosError - escape_html(body) + body end rescue LoadError - escape_html(body) + body end end end From 0604c468b2b8bfb11432b0dedd26dc52c5c146b5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 11:16:35 +0900 Subject: [PATCH 017/214] Contents in BlockElement should be escaped --- test/test_htmlbuilder.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index acf2cdde4..d1510afda 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -186,10 +186,9 @@ def test_quote assert_equal %Q|

      foobar

      \n

      buz

      \n|, result end - def test_memo result = compile_blockelem("//memo[this is @{test}<&>_]{\ntest1\n\ntest2\n//}\n") - assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test2

      \n
      \n|, result + assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test<i>2</i>

      \n
      \n|, result end From 238a080969d7b1ae8b31469831afb620b977ae14 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 14:29:04 +0900 Subject: [PATCH 018/214] fix inline symtax --- lib/review/review.kpeg | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 8958a3b6a..32e1b30b8 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -438,11 +438,11 @@ HeadlinePrefix = < /={1,5}/ > { text.length } Paragraph = ParagraphSub+:c { compile_paragraph(c) } ParagraphSub = ( InlineElement:c { c } - | < ContentText > { text } + | ContentText:c { c } )+:d { e=d.join("") } Newline { e } ## ContentText = !Newline !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } -ContentText = NonInlineElement+:c { c } +ContentText = NonInlineElement+:c { c.join("") } NonInlineElement = !InlineElement < /[^\r\n]/ > { escape_text(text) } @@ -451,8 +451,14 @@ BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline Blo | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } ) -InlineElement = "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" +InlineElement = ( RawInlineElement:c { c } + | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) } + ) + +RawInlineElement = "@{" RawInlineElementContent+:c "}" { c.join("") } + +RawInlineElementContent = ( "\\}" { "}" } | < /[^\r\n\}]/ > { text }) InlineElementSymbol = < /[^>\r\n]+/ > { text } @@ -486,7 +492,8 @@ InlineElementContent = ( InlineElement:c | InlineElementContentText+:c ) -InlineElementContentText = ( "\\}" { "}" } | !InlineElement < /[^\r\n}]/> { text } ) +## XXX '\' (excpet '\}') => '\' is OK? +InlineElementContentText = &. ( "\\}" { "}" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\}]/> { text } ) SinglelineContent = ContentInlines:c (Newline | EOF) { c } From 63b29ac56b1d544cbf844b3275536f3c038420cb Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 16:15:19 +0900 Subject: [PATCH 019/214] refactor BlockElementParagraph (remove instance variable) --- lib/review/compiler.rb | 479 ++++++++++++++++++++++++----------------- lib/review/review.kpeg | 8 +- 2 files changed, 280 insertions(+), 207 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index d1f92ec7f..d7dc2bc02 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1036,7 +1036,7 @@ def _Paragraph return _tmp end - # ParagraphSub = (InlineElement:c { c } | < ContentText > { text })+:d { e=d.join("") } Newline { e } + # ParagraphSub = (InlineElement:c { c } | ContentText:c { c })+:d { e=d.join("") } Newline { e } def _ParagraphSub _save = self.pos @@ -1068,16 +1068,13 @@ def _ParagraphSub _save4 = self.pos while true # sequence - _text_start = self.pos _tmp = apply(:_ContentText) - if _tmp - text = get_text(_text_start) - end + c = @result unless _tmp self.pos = _save4 break end - @result = begin; text ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save4 @@ -1118,16 +1115,13 @@ def _ParagraphSub _save7 = self.pos while true # sequence - _text_start = self.pos _tmp = apply(:_ContentText) - if _tmp - text = get_text(_text_start) - end + c = @result unless _tmp self.pos = _save7 break end - @result = begin; text ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save7 @@ -1176,7 +1170,7 @@ def _ParagraphSub return _tmp end - # ContentText = NonInlineElement+:c { c } + # ContentText = NonInlineElement+:c { c.join("") } def _ContentText _save = self.pos @@ -1201,7 +1195,7 @@ def _ContentText self.pos = _save break end - @result = begin; c ; end + @result = begin; c.join("") ; end _tmp = true unless _tmp self.pos = _save @@ -1397,40 +1391,122 @@ def _BlockElement return _tmp end - # InlineElement = "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) } + # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) }) def _InlineElement _save = self.pos - while true # sequence - _tmp = match_string("@<") - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_InlineElementSymbol) - symbol = @result - unless _tmp - self.pos = _save + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_RawInlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end break - end - _tmp = match_string(">") - unless _tmp - self.pos = _save + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_RawInlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("@<") + unless _tmp + self.pos = _save2 + break + end + _tmp = apply(:_InlineElementSymbol) + symbol = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string(">") + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save2 + break + end + _save4 = self.pos + _tmp = apply(:_InlineElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save4 + end + contents = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save2 + break + end + @result = begin; compile_inline(symbol,contents) ; end + _tmp = true + unless _tmp + self.pos = _save2 + end break - end - _tmp = match_string("{") + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElement unless _tmp + return _tmp + end + + # RawInlineElement = "@{" RawInlineElementContent+:c "}" { c.join("") } + def _RawInlineElement + + _save = self.pos + while true # sequence + _tmp = match_string("@{") unless _tmp self.pos = _save break end _save1 = self.pos - _tmp = apply(:_InlineElementContents) - @result = nil unless _tmp - unless _tmp + _ary = [] + _tmp = apply(:_RawInlineElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_RawInlineElementContent) + _ary << @result if _tmp + break unless _tmp + end _tmp = true + @result = _ary + else self.pos = _save1 end - contents = @result + c = @result unless _tmp self.pos = _save break @@ -1440,7 +1516,7 @@ def _InlineElement self.pos = _save break end - @result = begin; compile_inline(symbol,contents) ; end + @result = begin; c.join("") ; end _tmp = true unless _tmp self.pos = _save @@ -1448,7 +1524,59 @@ def _InlineElement break end # end sequence - set_failed_rule :_InlineElement unless _tmp + set_failed_rule :_RawInlineElement unless _tmp + return _tmp + end + + # RawInlineElementContent = ("\\}" { "}" } | < /[^\r\n\}]/ > { text }) + def _RawInlineElementContent + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\}])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save2 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_RawInlineElementContent unless _tmp return _tmp end @@ -1697,31 +1825,37 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = &. { @blockElem = "" } BlockElementParagraphSub:c { @blockElem } + # BlockElementParagraph = BlockElementParagraphSub+:c Newline { c.join("") } def _BlockElementParagraph _save = self.pos while true # sequence _save1 = self.pos - _tmp = get_byte - self.pos = _save1 - unless _tmp - self.pos = _save - break + _ary = [] + _tmp = apply(:_BlockElementParagraphSub) + if _tmp + _ary << @result + while true + _tmp = apply(:_BlockElementParagraphSub) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 end - @result = begin; @blockElem = "" ; end - _tmp = true + c = @result unless _tmp self.pos = _save break end - _tmp = apply(:_BlockElementParagraphSub) - c = @result + _tmp = apply(:_Newline) unless _tmp self.pos = _save break end - @result = begin; @blockElem ; end + @result = begin; c.join("") ; end _tmp = true unless _tmp self.pos = _save @@ -1733,118 +1867,21 @@ def _BlockElementParagraph return _tmp end - # BlockElementParagraphSub = (InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline + # BlockElementParagraphSub = (InlineElement:c | BlockElementContentText:c) def _BlockElementParagraphSub _save = self.pos - while true # sequence - _save1 = self.pos - - _save2 = self.pos - while true # choice - - _save3 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save3 - break - end - @result = begin; @blockElem << c ; end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - - _save4 = self.pos - while true # sequence - _tmp = apply(:_BlockElementContentText) - c = @result - unless _tmp - self.pos = _save4 - break - end - @result = begin; @blockElem << c ; end - _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - break - end # end choice - - if _tmp - while true - - _save5 = self.pos - while true # choice - - _save6 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save6 - break - end - @result = begin; @blockElem << c ; end - _tmp = true - unless _tmp - self.pos = _save6 - end - break - end # end sequence - - break if _tmp - self.pos = _save5 - - _save7 = self.pos - while true # sequence - _tmp = apply(:_BlockElementContentText) - c = @result - unless _tmp - self.pos = _save7 - break - end - @result = begin; @blockElem << c ; end - _tmp = true - unless _tmp - self.pos = _save7 - end - break - end # end sequence - - break if _tmp - self.pos = _save5 - break - end # end choice - - break unless _tmp - end - _tmp = true - else - self.pos = _save1 - end - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_Newline) - unless _tmp - self.pos = _save - end + while true # choice + _tmp = apply(:_InlineElement) + c = @result + break if _tmp + self.pos = _save + _tmp = apply(:_BlockElementContentText) + c = @result + break if _tmp + self.pos = _save break - end # end sequence + end # end choice set_failed_rule :_BlockElementParagraphSub unless _tmp return _tmp @@ -2014,61 +2051,95 @@ def _InlineElementContent return _tmp end - # InlineElementContentText = ("\\}" { "}" } | !InlineElement < /[^\r\n}]/ > { text }) + # InlineElementContentText = &. ("\\}" { "}" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\}]/ > { text }) def _InlineElementContentText _save = self.pos - while true # choice - + while true # sequence _save1 = self.pos - while true # sequence - _tmp = match_string("\\}") - unless _tmp - self.pos = _save1 - break - end - @result = begin; "}" ; end - _tmp = true - unless _tmp - self.pos = _save1 - end + _tmp = get_byte + self.pos = _save1 + unless _tmp + self.pos = _save break - end # end sequence - - break if _tmp - self.pos = _save + end _save2 = self.pos - while true # sequence + while true # choice + _save3 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save3 - unless _tmp - self.pos = _save2 + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save3 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save3 + end break - end - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n}])/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save2 + end # end sequence + + break if _tmp + self.pos = _save2 + + _save4 = self.pos + while true # sequence + _tmp = match_string("\\") + unless _tmp + self.pos = _save4 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save4 + end break - end - @result = begin; text ; end - _tmp = true - unless _tmp - self.pos = _save2 - end + end # end sequence + + break if _tmp + self.pos = _save2 + + _save5 = self.pos + while true # sequence + _save6 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save5 + break + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\\}])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save5 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 break - end # end sequence + end # end choice - break if _tmp - self.pos = _save + unless _tmp + self.pos = _save + end break - end # end choice + end # end sequence set_failed_rule :_InlineElementContentText unless _tmp return _tmp @@ -2875,23 +2946,25 @@ def _Spacechar Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) { compile_headline(level, cmd, label, caption) }") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c { compile_paragraph(c) }") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | < ContentText > { text })+:d { e=d.join(\"\") } Newline { e }") - Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c }") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | ContentText:c { c })+:d { e=d.join(\"\") } Newline { e }") + Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c.join(\"\") }") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { escape_text(text) }") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") - Rules[:_InlineElement] = rule_info("InlineElement", "\"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" { compile_inline(symbol,contents) }") + Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" { compile_inline(symbol,contents) })") + Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawInlineElementContent+:c \"}\" { c.join(\"\") }") + Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< /[^>\\r\\n]+/ > { text }") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline+:c \"]\" { c.join(\"\") }") Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" { \"]\" } | < /[^\\r\\n\\]]/ > { escape_text(text) })") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { \"\" })") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "&. { @blockElem = \"\" } BlockElementParagraphSub:c { @blockElem }") - Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c { @blockElem << c } | BlockElementContentText:c { @blockElem << c })+ Newline") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c Newline { c.join(\"\") }") + Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c | BlockElementContentText:c)") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join(\"\") }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c.join(\"\") }") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText+:c)") - Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" { \"}\" } | !InlineElement < /[^\\r\\n}]/ > { text })") + Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "&. (\"\\\\}\" { \"}\" } | \"\\\\\" { \"\\\\\" } | !InlineElement < /[^\\r\\n\\\\}]/ > { text })") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c (Newline | EOF) { c }") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c.join }") Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > { escape_text(text) })") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 32e1b30b8..449cdd0a8 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -478,11 +478,11 @@ BlockElementContent = ( SinglelineComment:c | Newline:c { "" } ) -BlockElementParagraph = &. { @blockElem = "" } BlockElementParagraphSub:c { @blockElem } +BlockElementParagraph = BlockElementParagraphSub+:c Newline { c.join("") } -BlockElementParagraphSub = ( InlineElement:c { @blockElem << c } - | BlockElementContentText:c { @blockElem << c } - )+ Newline +BlockElementParagraphSub = ( InlineElement:c + | BlockElementContentText:c + ) BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join("") } From df987673443e38c6d618b6043cb71de8410aaba2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 16:16:53 +0900 Subject: [PATCH 020/214] fix some LATEXBuilder error (WIP) --- lib/review/latexbuilder.rb | 345 ++++++++++++++++++++----------------- test/test_latexbuilder.rb | 89 +++++----- 2 files changed, 231 insertions(+), 203 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index aa409a4a1..55dae4b7a 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -75,139 +75,138 @@ def puts(*s) } def headline(level, label, caption) + buf = "" prefix = "" if level > ReVIEW.book.param["secnolevel"] || (@chapter.number.to_s.empty? && level > 1) prefix = "*" end - blank unless @output.pos == 0 - puts macro(HEADLINE[level]+prefix, compile_inline(caption)) + buf << macro(HEADLINE[level]+prefix, caption) << "\n" if prefix == "*" && level <= ReVIEW.book.param["toclevel"].to_i - puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}" + buf << "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{caption}}\n" end if level == 1 - puts macro('label', chapter_label) + buf << macro('label', chapter_label) << "\n" end + buf rescue error "unknown level: #{level}" end def nonum_begin(level, label, caption) - blank unless @output.pos == 0 - puts macro(HEADLINE[level]+"*", compile_inline(caption)) + "\n" + macro(HEADLINE[level]+"*", caption) + "\n" end def nonum_end(level) end def column_begin(level, label, caption) - blank - puts "\\begin{reviewcolumn}\n" - puts macro('reviewcolumnhead', nil, compile_inline(caption)) - + buf = "\n" + buf << "\\begin{reviewcolumn}\n" + buf << macro('reviewcolumnhead', nil, caption) << "\n" + buf end def column_end(level) - puts "\\end{reviewcolumn}\n" - blank + buf << "\\end{reviewcolumn}\n\n" end def captionblock(type, lines, caption) - puts "\\begin{reviewminicolumn}\n" + buf = "" + buf << "\\begin{reviewminicolumn}\n\n" unless caption.nil? - puts "\\reviewminicolumntitle{#{compile_inline(caption)}}\n" + buf << "\\reviewminicolumntitle{caption}}\n\n" end if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts blocked_lines.join("\n\n") + buf << blocked_lines.join("\n\n") << "\n" else lines.each do |line| - puts line + buf << line << "\n" end end - puts "\\end{reviewminicolumn}\n" + buf << "\\end{reviewminicolumn}\n\n" + buf end def box(lines, caption = nil) - blank + buf = "\n" if caption - puts macro('reviewboxcaption', "#{compile_inline(caption)}") + buf << macro('reviewboxcaption', "#{caption}") << "\n" end - puts '\begin{reviewbox}' + buf << '\begin{reviewbox}' << "\n" lines.each do |line| - puts detab(line) - end - puts '\end{reviewbox}' - blank + buf << detab(line) << "\n" + end< + buf << '\end{reviewbox}' << "\n" end def ul_begin - blank - puts '\begin{itemize}' + buf = "\n" + buf << '\begin{itemize}' << "\n" + buf end def ul_item(lines) str = lines.join str.sub!(/\A(\[)/){'\lbrack{}'} - puts '\item ' + str + '\item ' + str + "\n" end def ul_end - puts '\end{itemize}' - blank + '\end{itemize}' + "\n" end def ol_begin - blank - puts '\begin{enumerate}' + buf = "\n" + buf << '\begin{enumerate}' << "\n" if @ol_num - puts "\\setcounter{enumi}{#{@ol_num - 1}}" + buf << "\\setcounter{enumi}{#{@ol_num - 1}}\n" @ol_num = nil end + buf end def ol_item(lines, num) str = lines.join str.sub!(/\A(\[)/){'\lbrack{}'} - puts '\item ' + str + '\item ' + str + "\n" end def ol_end - puts '\end{enumerate}' - blank + '\end{enumerate}' + "\n" end def dl_begin - blank - puts '\begin{description}' + "\n" + '\begin{description}' + "\n" end def dt(str) str.sub!(/\[/){'\lbrack{}'} str.sub!(/\]/){'\rbrack{}'} - puts '\item[' + str + '] \mbox{} \\\\' + '\item[' + str + '] \mbox{} \\\\' + "\n" end def dd(lines) - puts lines.join + lines.join + "\n" end def dl_end - puts '\end{description}' - blank + '\end{description}' + "\n" end def paragraph(lines) - blank + buf = "\n" lines.each do |line| - puts line + buf << line << "\n" end - blank + buf << "\n" + buf end def parasep() - puts '\\parasep' + '\\parasep' + "\n" end def read(lines) @@ -217,83 +216,89 @@ def read(lines) alias lead read def emlist(lines, caption = nil) - blank + buf = "\n" if caption - puts macro('reviewemlistcaption', "#{compile_inline(caption)}") + buf << macro('reviewemlistcaption', "#{caption}") << "\n" end puts '\begin{reviewemlist}' lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts '\end{reviewemlist}' - blank + buf << '\end{reviewemlist}' << "\n" + buf end def emlistnum(lines, caption = nil) - blank + buf = "\n" if caption - puts macro('reviewemlistcaption', "#{compile_inline(caption)}") + buf << macro('reviewemlistcaption', "#{caption}") << "\n" end - puts '\begin{reviewemlist}' + buf << '\begin{reviewemlist}' << "\n" lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n" end - puts '\end{reviewemlist}' - blank + buf << '\end{reviewemlist}' << "\n" + buf end def listnum_body(lines) - puts '\begin{reviewlist}' + buf = "" + buf << '\begin{reviewlist}' lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n" end - puts '\end{reviewlist}' - blank - + buf << '\end{reviewlist}' << "\n" + buf end def cmd(lines, caption = nil) - blank + buf = "\n" if caption - puts macro('reviewcmdcaption', "#{compile_inline(caption)}") + buf << macro('reviewcmdcaption', "#{caption}") << "\n" end - puts '\begin{reviewcmd}' + buf << '\begin{reviewcmd}' "\n" lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts '\end{reviewcmd}' - blank + buf << '\end{reviewcmd}' << "\n" + buf end def list_header(id, caption) - puts macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}") + macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" end def list_body(id, lines) - puts '\begin{reviewlist}' + buf = "" + buf << '\begin{reviewlist}' << "\n" lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts '\end{reviewlist}' - puts "" + buf << '\end{reviewlist}' << "\n" + buf << "\n" + buf end def source(lines, caption) - puts '\begin{reviewlist}' - source_header caption - source_body lines - puts '\end{reviewlist}' - puts "" + buf = "\n" + buf << '\begin{reviewlist}' << "\n" + buf << source_header(caption) + buf << source_body(lines) + buf << '\end{reviewlist}' << "\n" + buf << "\n" + buf end def source_header(caption) - puts macro('reviewlistcaption', compile_inline(caption)) + macro('reviewlistcaption', caption) + "\n" end def source_body(lines) + buf = "" lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end + buf end @@ -305,31 +310,34 @@ def result_metric(array) end def image_image(id, caption, metric) + buf = "" metrics = parse_metric("latex", metric) # image is always bound here - puts '\begin{reviewimage}' + buf << '\begin{reviewimage}' << "\n" if metrics.present? - puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}" + buf << "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}\n" else - puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}" + buf << "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}\n" end if caption.present? - puts macro('caption', compile_inline(caption)) + buf << macro('caption', caption) << "\n" end - puts macro('label', image_label(id)) - puts '\end{reviewimage}' + buf << macro('label', image_label(id)) << "\n" + buf << '\end{reviewimage}' << "\n" + buf end def image_dummy(id, caption, lines) - puts '\begin{reviewdummyimage}' + buf << '\begin{reviewdummyimage}' << "\n" path = @chapter.image(id).path - puts "--[[path = #{path} (#{existence(id)})]]--" + buf << "--[[path = #{path} (#{existence(id)})]]--\n" lines.each do |line| - puts detab(line.rstrip) + buf << detab(line.rstrip) << "\n" end - puts macro('label', image_label(id)) - puts compile_inline(caption) - puts '\end{reviewdummyimage}' + buf << macro('label', image_label(id)) << "\n" + buf << caption << "\n" + buf << '\end{reviewdummyimage}' << "\n" + buf end def existence(id) @@ -358,23 +366,26 @@ def bib_label(id) private :bib_label def indepimage(id, caption=nil, metric=nil) + buf = "" metrics = parse_metric("latex", metric) - puts '\begin{reviewimage}' + buf << '\begin{reviewimage}' << "\n" if metrics.present? - puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}" + buf << "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}\n" else - puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}" + buf << "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}\n" end if caption.present? - puts macro('reviewindepimagecaption', - %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]) + buf << macro('reviewindepimagecaption', + %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{caption}]) << "\n" end - puts '\end{reviewimage}' + buf << '\end{reviewimage}' << "\n" + buf end alias :numberlessimage indepimage def table(lines, id = nil, caption = nil) + buf = "" rows = [] sepidx = nil lines.each_with_index do |line, idx| @@ -389,49 +400,54 @@ def table(lines, id = nil, caption = nil) rows = adjust_n_cols(rows) begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end - return if rows.empty? - table_begin rows.first.size + return buf if rows.empty? + buf << table_begin(rows.first.size) if sepidx sepidx.times do - tr rows.shift.map {|s| th(s) } + buf << tr(rows.shift.map {|s| th(s) }) end rows.each do |cols| - tr cols.map {|s| td(s) } + buf << tr(cols.map {|s| td(s) }) end else rows.each do |cols| h, *cs = *cols - tr [th(h)] + cs.map {|s| td(s) } + buf << tr([th(h)] + cs.map {|s| td(s) }) end end - table_end + buf << table_end + buf end def table_header(id, caption) + buf = "" if caption.present? @table_caption = true - puts '\begin{table}[h]' - puts macro('reviewtablecaption', compile_inline(caption)) + buf << '\begin{table}[h]' << "\n" + buf << macro('reviewtablecaption', caption) << "\n" end - puts macro('label', table_label(id)) + buf << macro('label', table_label(id)) << "\n" + buf end def table_begin(ncols) + buf = "" if @latex_tsize - puts macro('begin', 'reviewtable', @latex_tsize) + buf << macro('begin', 'reviewtable', @latex_tsize) << "\n" elsif @tsize cellwidth = @tsize.split(/\s*,\s*/) - puts macro('begin', 'reviewtable', '|'+(cellwidth.collect{|i| "p{#{i}mm}"}.join('|'))+'|') + buf << macro('begin', 'reviewtable', '|'+(cellwidth.collect{|i| "p{#{i}mm}"}.join('|'))+'|') << "\n" else - puts macro('begin', 'reviewtable', (['|'] * (ncols + 1)).join('l')) + buf << macro('begin', 'reviewtable', (['|'] * (ncols + 1)).join('l')) << "\n" end - puts '\hline' + buf << '\hline' << "\n" @tsize = nil @latex_tsize = nil + buf end def table_separator @@ -457,17 +473,21 @@ def td(s) end def tr(rows) - print rows.join(' & ') - puts ' \\\\ \hline' + buf = "" + buf << rows.join(' & ') << "\n" + buf << ' \\\\ \hline' << "\n" + buf end def table_end - puts macro('end', 'reviewtable') + buf = "" + buf << macro('end', 'reviewtable') << "\n" if @table_caption - puts '\end{table}' + buf << '\end{table}' << "\n" end @table_caption = nil - blank + buf << "\n" + buf end def quote(lines) @@ -485,65 +505,71 @@ def flushright(lines) end def texequation(lines) - blank - puts macro('begin','equation*') + buf = "\n" + buf << macro('begin','equation*') << "\n" lines.each do |line| - puts unescape_latex(line) + buf << unescape_latex(line) << "\n" end - puts macro('end', 'equation*') - blank + buf << macro('end', 'equation*') << "\n" + buf << "\n" + buf end def latex_block(type, lines) - blank - puts macro('begin', type) + buf = "\n" + buf << macro('begin', type) << "\n" if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts blocked_lines.join("\n\n") + buf << blocked_lines.join("\n\n") << "\n" else lines.each do |line| - puts line + buf << line << "\n" end end - puts macro('end', type) - blank + buf << macro('end', type) << "\n" + buf << "\n" + buf end private :latex_block def direct(lines, fmt) - return unless fmt == 'latex' + buf = "" + return buf unless fmt == 'latex' lines.each do |line| - puts line + buf << line << "\n" end + buf end def comment(lines, comment = nil) + buf = "" lines ||= [] lines.unshift comment unless comment.blank? if ReVIEW.book.param["draft"] str = lines.join("") - puts macro('pdfcomment', escape(str)) + buf << macro('pdfcomment', str) << "\n" end + buf end def hr - puts '\hrule' + '\hrule' + "\n" end def label(id) - puts macro('label', id) + macro('label', id) + "\n" end def pagebreak - puts '\pagebreak' + '\pagebreak' + "\n" end def linebreak - puts '\\\\' + '\\\\' + "\n" end def noindent - print '\noindent' + '\noindent' + "\n" end def inline_chapref(id) @@ -598,8 +624,8 @@ def inline_img(id) def footnote(id, content) if ReVIEW.book.param["footnotetext"] - puts macro("footnotetext[#{@chapter.footnote(id).number}]", - compile_inline(content.strip)) + macro("footnotetext[#{@chapter.footnote(id).number}]", + content.strip) + "\n" end end @@ -607,7 +633,7 @@ def inline_fn(id) if ReVIEW.book.param["footnotetext"] macro("footnotemark[#{@chapter.footnote(id).number}]", "") else - macro('footnote', compile_inline(@chapter.footnote(id).content.strip)) + macro('footnote', escape(@chapter.footnote(id).content.strip)) end end @@ -618,7 +644,7 @@ def inline_bou(str) end def compile_ruby(base, ruby) - macro('ruby', escape(base), escape(ruby)) + macro('ruby', base, ruby) end # math @@ -633,7 +659,7 @@ def inline_hi(str) # index -> italic def inline_i(str) - macro('textit', escape(str)) + macro('textit', str) end # index @@ -663,7 +689,7 @@ def inline_dtp(str) ## @ is same as @ def inline_code(str) - macro('texttt', escape(str)) + macro('texttt', str) end def nofunc_text(str) @@ -671,19 +697,19 @@ def nofunc_text(str) end def inline_tt(str) - macro('texttt', escape(str)) + macro('texttt', str) end def inline_del(str) - macro('reviewstrike', escape(str)) + macro('reviewstrike', str) end def inline_tti(str) - macro('texttt', macro('textit', escape(str))) + macro('texttt', macro('textit', str)) end def inline_ttb(str) - macro('texttt', macro('textbf', escape(str))) + macro('texttt', macro('textbf', str)) end def inline_bib(id) @@ -691,7 +717,7 @@ def inline_bib(id) end def inline_hd_chap(chap, id) - "「#{chap.headline_index.number(id)} #{compile_inline(chap.headline(id).caption)}」" + "「#{chap.headline_index.number(id)} #{escape(chap.headline(id).caption)}」" end def inline_raw(str) @@ -699,27 +725,27 @@ def inline_raw(str) end def inline_sub(str) - macro('textsubscript', escape(str)) + macro('textsubscript', str) end def inline_sup(str) - macro('textsuperscript', escape(str)) + macro('textsuperscript', str) end def inline_em(str) - macro('reviewem', escape(str)) + macro('reviewem', str) end def inline_strong(str) - macro('reviewstrong', escape(str)) + macro('reviewstrong', str) end def inline_u(str) - macro('Underline', escape(str)) + macro('Underline', str) end def inline_ami(str) - macro('reviewami', escape(str)) + macro('reviewami', str) end def inline_icon(id) @@ -728,25 +754,24 @@ def inline_icon(id) def inline_uchar(str) # with otf package - macro('UTF', escape(str)) + macro('UTF', str) end def inline_comment(str) if ReVIEW.book.param["draft"] - macro('pdfcomment', escape(str)) + macro('pdfcomment', str) else "" end end def bibpaper_header(id, caption) - puts "[#{@chapter.bibpaper(id).number}] #{compile_inline(caption)}" - puts macro('label', bib_label(id)) + "[#{@chapter.bibpaper(id).number}] #{caption}\n" + + macro('label', bib_label(id)) + "\n" end def bibpaper_bibpaper(id, caption, lines) - print split_paragraph(lines).join("") - puts "" + split_paragraph(lines).join("") + "\n" end def index(str) @@ -769,9 +794,9 @@ def index(str) def compile_kw(word, alt) if alt - macro('reviewkw', escape(word)) + "(#{escape(alt.strip)})" + macro('reviewkw', word) + "(#{alt.strip})" else - macro('reviewkw', escape(word)) + macro('reviewkw', word) end end diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 9c6fde4cd..8859e7e98 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- require 'test_helper' +require 'review' require 'review/compiler' require 'review/book' require 'review/latexbuilder' @@ -26,65 +27,65 @@ def setup end def test_headline_level1 - @builder.headline(1,"test","this is test.") - assert_equal %Q|\\chapter{this is test.}\n\\label{chap:chap1}\n|, @builder.result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|\\chapter{this is test.}\n\\label{chap:chap1}\n|, result end + def test_headline_level1_without_secno @param["secnolevel"] = 0 - @builder.headline(1,"test","this is test.") - assert_equal %Q|\\chapter*{this is test.}\n\\addcontentsline{toc}{chapter}{this is test.}\n\\label{chap:chap1}\n|, @builder.result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|\\chapter*{this is test.}\n\\addcontentsline{toc}{chapter}{this is test.}\n\\label{chap:chap1}\n|, result end def test_headline_level1_with_inlinetag - @builder.headline(1,"test","this @{is} test.<&\"_>") - assert_equal %Q|\\chapter{this \\textbf{is} test.\\textless{}\\&\"\\textunderscore{}\\textgreater{}}\n\\label{chap:chap1}\n|, @builder.result + result = compile_headline("={test} this @{is} test.<&\"_>\n") + assert_equal %Q|\\chapter{this \\textbf{is} test.\\textless{}\\&\"\\textunderscore{}\\textgreater{}}\n\\label{chap:chap1}\n|, result end def test_headline_level2 - @builder.headline(2,"test","this is test.") - assert_equal %Q|\\section{this is test.}\n|, @builder.result + result = compile_headline("=={test} this is test.\n") + assert_equal %Q|\\section{this is test.}\n|, result end def test_headline_level3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\\subsection*{this is test.}\n|, @builder.result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|\\subsection*{this is test.}\n|, result end - def test_headline_level3_with_secno @param["secnolevel"] = 3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\\subsection{this is test.}\n|, @builder.result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|\\subsection{this is test.}\n|, result end def test_label - @builder.label("label_test") - assert_equal %Q|\\label{label_test}\n|, @builder.result + result = @builder.label("label_test") + assert_equal %Q|\\label{label_test}\n|, result end def test_href - ret = @builder.compile_inline('@{http://github.com,GitHub}') + ret = compile_inline('@{http://github.com,GitHub}') assert_equal %Q|\\href{http://github.com}{GitHub}|, ret end def test_inline_href - ret = @builder.compile_inline('@{http://github.com,Git\\,Hub}') + ret = compile_inline('@{http://github.com,Git\\,Hub}') assert_equal %Q|\\href{http://github.com}{Git,Hub}|, ret end def test_href_without_label - ret = @builder.compile_inline('@{http://github.com}') + ret = compile_inline('@{http://github.com}') assert_equal %Q|\\url{http://github.com}|, ret end def test_href_with_underscore - ret = @builder.compile_inline('@{http://example.com/aaa/bbb, AAA_BBB}') + ret = compile_inline('@{http://example.com/aaa/bbb, AAA_BBB}') assert_equal %Q|\\href{http://example.com/aaa/bbb}{AAA\\textunderscore{}BBB}|, ret end def test_href_mailto - ret = @builder.compile_inline('@{mailto:takahashim@example.com, takahashim@example.com}') + ret = compile_inline('@{mailto:takahashim@example.com, takahashim@example.com}') assert_equal %Q|\\href{mailto:takahashim@example.com}{takahashim@example.com}|, ret end @@ -94,96 +95,97 @@ def test_inline_br end def test_inline_br_with_other_strings - ret = @builder.compile_inline("abc@
      {}def") + ret = compile_inline("abc@
      {}def") assert_equal %Q|abc\\\\\ndef|, ret end def test_inline_u - ret = @builder.compile_inline("abc@{def}ghi") + ret = compile_inline("abc@{def}ghi") assert_equal %Q|abc\\Underline{def}ghi|, ret end def test_inline_i - ret = @builder.compile_inline("abc@{def}ghi") + ret = compile_inline("abc@{def}ghi") assert_equal %Q|abc\\textit{def}ghi|, ret end def test_inline_i_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") + ret = compile_inline("test @{inline<&;\\ test} test2") assert_equal %Q|test \\textit{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, ret end def test_inline_dtp - ret = @builder.compile_inline("abc@{def}ghi") + ret = compile_inline("abc@{def}ghi") assert_equal %Q|abcghi|, ret end def test_inline_code - ret = @builder.compile_inline("abc@{def}ghi") + ret = compile_inline("abc@{def}ghi") assert_equal %Q|abc\\texttt{def}ghi|, ret end def test_inline_raw - ret = @builder.compile_inline("@{@{inline!$%\\}}") + ret = compile_inline("@{@{inline!$%\\}}") assert_equal "@{inline!$%}", ret end def test_inline_sup - ret = @builder.compile_inline("abc@{def}") + ret = compile_inline("abc@{def}") assert_equal %Q|abc\\textsuperscript{def}|, ret + end def test_inline_sub - ret = @builder.compile_inline("abc@{def}") + ret = compile_inline("abc@{def}") assert_equal %Q|abc\\textsubscript{def}|, ret end def test_inline_b - ret = @builder.compile_inline("abc@{def}") + ret = compile_inline("abc@{def}") assert_equal %Q|abc\\textbf{def}|, ret end def test_inline_b_and_escape - ret = @builder.compile_inline("test @{inline<&;\\ test} test2") + ret = compile_inline("test @{inline<&;\\ test} test2") assert_equal %Q|test \\textbf{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, ret end def test_inline_em - ret = @builder.compile_inline("abc@{def}") + ret = compile_inline("abc@{def}") assert_equal %Q|abc\\reviewem{def}|, ret end def test_inline_strong - ret = @builder.compile_inline("abc@{def}") + ret = compile_inline("abc@{def}") assert_equal %Q|abc\\reviewstrong{def}|, ret end def test_inline_u - ret = @builder.compile_inline("abc@{def}ghi") + ret = compile_inline("abc@{def}ghi") assert_equal %Q|abc\\Underline{def}ghi|, ret end def test_inline_m - ret = @builder.compile_inline("abc@{\\alpha^n = \inf < 2}ghi") + ret = compile_inline("abc@{\\alpha^n = \inf < 2}ghi") assert_equal "abc $\\alpha^n = inf < 2$ ghi", ret end def test_inline_tt - ret = @builder.compile_inline("test @{inline test} test2") + ret = compile_inline("test @{inline test} test2") assert_equal %Q|test \\texttt{inline test} test2|, ret end def test_inline_tt_endash - ret = @builder.compile_inline("test @{in-line --test ---foo ----bar -----buz} --test2") + ret = compile_inline("test @{in-line --test ---foo ----bar -----buz} --test2") assert_equal %Q|test \\texttt{in{-}line {-}{-}test {-}{-}{-}foo {-}{-}{-}{-}bar {-}{-}{-}{-}{-}buz} {-}{-}test2|, ret end def test_inline_tti - ret = @builder.compile_inline("test @{inline test} test2") + ret = compile_inline("test @{inline test} test2") assert_equal %Q|test \\texttt{\\textit{inline test}} test2|, ret end def test_inline_ttb - ret = @builder.compile_inline("test @{inline test} test2") + ret = compile_inline("test @{inline test} test2") assert_equal %Q|test \\texttt{\\textbf{inline test}} test2|, ret end @@ -193,20 +195,21 @@ def @chapter.headline_index Book::HeadlineIndex.new(items, self) end - ret = @builder.compile_inline("test @{chap1|test} test2") + ret = compile_inline("test @{chap1|test} test2") assert_equal %Q|test 「1.1.1 te\\textunderscore{}st」 test2|, ret end def test_inline_ruby_comma - ret = @builder.compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") + ret = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") assert_equal "\\ruby{foo, bar, buz}{フー・バー・バズ}", ret end def test_inline_uchar - ret = @builder.compile_inline("test @{2460} test2") + ret = compile_inline("test @{2460} test2") assert_equal %Q|test \\UTF{2460} test2|, ret end +=begin def test_jis_x_0201_kana ret = @builder.compile_inline("foo・カンジ、テスト") assert_equal %Q|foo\\aj半角{・}\\aj半角{カ}\\aj半角{ン}\\aj半角{シ}\\aj半角{゛}\\aj半角{、}テスト|, ret @@ -679,5 +682,5 @@ def test_block_raw4 EOS assert_equal expect.chomp, @builder.raw_result end - +=end end From e5a2ec2a350a31e8fc4f54ad1015a6fd0a7d0ed5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 20:44:40 +0900 Subject: [PATCH 021/214] fix I18n test --- test/test_i18n.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_i18n.rb b/test/test_i18n.rb index fb1a9058c..5c2acbe88 100644 --- a/test/test_i18n.rb +++ b/test/test_i18n.rb @@ -42,8 +42,8 @@ def test_nil def test_htmlbuilder _setup_htmlbuilder - @builder.headline(1,"test","this is test.") - assert_equal %Q|

      Chapter 1. this is test.

      \n|, @builder.raw_result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|

      Chapter 1. this is test.

      \n|, result end def _setup_htmlbuilder From a578c11de831b16a5b975894104665efe2a7c892 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 20:47:14 +0900 Subject: [PATCH 022/214] fix inline syntax; parsing args should be done in compiler, not builders --- lib/review/builder.rb | 17 +- lib/review/compiler.rb | 1141 ++++++++++++++++++++++++++++------------ lib/review/review.kpeg | 55 +- 3 files changed, 860 insertions(+), 353 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 16e99e545..d4fa2dc1a 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -236,22 +236,17 @@ def inline_bou(str) text(str) end - def inline_ruby(arg) - base, *ruby = *arg.scan(/(?:(?:(?:\\\\)*\\,)|[^,\\]+)+/) - base = base.gsub(/\\,/, ",") if base - ruby = ruby.join(",").gsub(/\\,/, ",") if ruby + def inline_ruby(base, ruby) compile_ruby(base, ruby) end - def inline_kw(arg) - word, alt = *arg.split(',', 2) + def inline_kw(word, alt = nil) compile_kw(word, alt) end - def inline_href(arg) - url, label = *arg.scan(/(?:(?:(?:\\\\)*\\,)|[^,\\]+)+/).map(&:lstrip) - url = url.gsub(/\\,/, ",").strip - label = label.gsub(/\\,/, ",").strip if label + def inline_href(url, label = nil) + url = url.strip + label = label.strip if label compile_href(url, label) end @@ -325,7 +320,7 @@ def warn(msg) end def error(msg) - raise ApplicationError, "#{@location}: error: #{msg}" + raise ApplicationError, "error: #{msg} at #{@compiler.show_pos} \n (#{@compiler.failure_oneline})" end def handle_metric(str) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index d7dc2bc02..fa9f39c4c 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -740,14 +740,14 @@ def compile_single(syntax, args) end - def compile_inline(op, arg) + def compile_inline(op, args) unless inline_defined?(op) raise ReVIEW::CompileError, "no such inline op: #{op}" end unless @strategy.respond_to?("inline_#{op}") raise "strategy does not support inline op: @<#{op}>" end - @strategy.__send__("inline_#{op}", arg) + @strategy.__send__("inline_#{op}", *args) rescue => err error err.message end @@ -756,6 +756,15 @@ def compile_paragraph(buf) @strategy.paragraph buf end + def compile_raw(builders, content) + c = @strategy.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase + if !builders || builders.include?(c) + content + else + "" + end + end + def warn(msg) @strategy.warn msg end @@ -1241,7 +1250,7 @@ def _NonInlineElement return _tmp end - # BlockElement = ("//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) }) + # BlockElement = ("//raw[" RawBlockBuilderSelect:b? RawBlockElementArg*:r1 "]" Space* Newline { compile_raw(b, r1.join("")) } | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } | !"//raw" "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) }) def _BlockElement _save = self.pos @@ -1249,62 +1258,36 @@ def _BlockElement _save1 = self.pos while true # sequence - _tmp = match_string("//") + _tmp = match_string("//raw[") unless _tmp self.pos = _save1 break end - _tmp = apply(:_ElementName) - symbol = @result + _save2 = self.pos + _tmp = apply(:_RawBlockBuilderSelect) + b = @result + unless _tmp + _tmp = true + self.pos = _save2 + end unless _tmp self.pos = _save1 break end _ary = [] while true - _tmp = apply(:_BracketArg) + _tmp = apply(:_RawBlockElementArg) _ary << @result if _tmp break unless _tmp end _tmp = true @result = _ary - args = @result - unless _tmp - self.pos = _save1 - break - end - _tmp = match_string("{") - unless _tmp - self.pos = _save1 - break - end - while true - _tmp = apply(:_Space) - break unless _tmp - end - _tmp = true - unless _tmp - self.pos = _save1 - break - end - _tmp = apply(:_Newline) + r1 = @result unless _tmp self.pos = _save1 break end - _save4 = self.pos - _tmp = apply(:_BlockElementContents) - @result = nil unless _tmp - unless _tmp - _tmp = true - self.pos = _save4 - end - contents = @result - unless _tmp - self.pos = _save1 - break - end - _tmp = match_string("//}") + _tmp = match_string("]") unless _tmp self.pos = _save1 break @@ -1323,7 +1306,7 @@ def _BlockElement self.pos = _save1 break end - @result = begin; compile_command(symbol, args, contents) ; end + @result = begin; compile_raw(b, r1.join("")) ; end _tmp = true unless _tmp self.pos = _save1 @@ -1334,17 +1317,25 @@ def _BlockElement break if _tmp self.pos = _save - _save6 = self.pos + _save5 = self.pos while true # sequence + _save6 = self.pos + _tmp = match_string("//raw") + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save5 + break + end _tmp = match_string("//") unless _tmp - self.pos = _save6 + self.pos = _save5 break end _tmp = apply(:_ElementName) symbol = @result unless _tmp - self.pos = _save6 + self.pos = _save5 break end _ary = [] @@ -1357,7 +1348,12 @@ def _BlockElement @result = _ary args = @result unless _tmp - self.pos = _save6 + self.pos = _save5 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save5 break end while true @@ -1366,49 +1362,49 @@ def _BlockElement end _tmp = true unless _tmp - self.pos = _save6 + self.pos = _save5 break end _tmp = apply(:_Newline) unless _tmp - self.pos = _save6 + self.pos = _save5 break end - @result = begin; compile_command(symbol, args, nil) ; end + _save9 = self.pos + _tmp = apply(:_BlockElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save9 + end + contents = @result + unless _tmp + self.pos = _save5 + break + end + _tmp = match_string("//}") + unless _tmp + self.pos = _save5 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end _tmp = true unless _tmp - self.pos = _save6 + self.pos = _save5 + break end - break - end # end sequence - - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_BlockElement unless _tmp - return _tmp - end - - # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) }) - def _InlineElement - - _save = self.pos - while true # choice - - _save1 = self.pos - while true # sequence - _tmp = apply(:_RawInlineElement) - c = @result + _tmp = apply(:_Newline) unless _tmp - self.pos = _save1 + self.pos = _save5 break end - @result = begin; c ; end + @result = begin; compile_command(symbol, args, contents) ; end _tmp = true unless _tmp - self.pos = _save1 + self.pos = _save5 end break end # end sequence @@ -1416,58 +1412,58 @@ def _InlineElement break if _tmp self.pos = _save - _save2 = self.pos + _save11 = self.pos while true # sequence - _save3 = self.pos - _tmp = apply(:_RawInlineElement) + _save12 = self.pos + _tmp = match_string("//raw") _tmp = _tmp ? nil : true - self.pos = _save3 + self.pos = _save12 unless _tmp - self.pos = _save2 + self.pos = _save11 break end - _tmp = match_string("@<") + _tmp = match_string("//") unless _tmp - self.pos = _save2 + self.pos = _save11 break end - _tmp = apply(:_InlineElementSymbol) + _tmp = apply(:_ElementName) symbol = @result unless _tmp - self.pos = _save2 + self.pos = _save11 break end - _tmp = match_string(">") - unless _tmp - self.pos = _save2 - break + _ary = [] + while true + _tmp = apply(:_BracketArg) + _ary << @result if _tmp + break unless _tmp end - _tmp = match_string("{") + _tmp = true + @result = _ary + args = @result unless _tmp - self.pos = _save2 + self.pos = _save11 break end - _save4 = self.pos - _tmp = apply(:_InlineElementContents) - @result = nil unless _tmp - unless _tmp - _tmp = true - self.pos = _save4 + while true + _tmp = apply(:_Space) + break unless _tmp end - contents = @result + _tmp = true unless _tmp - self.pos = _save2 + self.pos = _save11 break end - _tmp = match_string("}") + _tmp = apply(:_Newline) unless _tmp - self.pos = _save2 + self.pos = _save11 break end - @result = begin; compile_inline(symbol,contents) ; end + @result = begin; compile_command(symbol, args, nil) ; end _tmp = true unless _tmp - self.pos = _save2 + self.pos = _save11 end break end # end sequence @@ -1477,41 +1473,45 @@ def _InlineElement break end # end choice - set_failed_rule :_InlineElement unless _tmp + set_failed_rule :_BlockElement unless _tmp return _tmp end - # RawInlineElement = "@{" RawInlineElementContent+:c "}" { c.join("") } - def _RawInlineElement + # RawBlockBuilderSelect = "|" Space* RawBlockBuilderSelectSub:c Space* "|" { c.join("") } + def _RawBlockBuilderSelect _save = self.pos while true # sequence - _tmp = match_string("@{") + _tmp = match_string("|") unless _tmp self.pos = _save break end - _save1 = self.pos - _ary = [] - _tmp = apply(:_RawInlineElementContent) - if _tmp - _ary << @result - while true - _tmp = apply(:_RawInlineElementContent) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break end + _tmp = apply(:_RawBlockBuilderSelectSub) c = @result unless _tmp self.pos = _save break end - _tmp = match_string("}") + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = match_string("|") unless _tmp self.pos = _save break @@ -1524,24 +1524,68 @@ def _RawInlineElement break end # end sequence - set_failed_rule :_RawInlineElement unless _tmp + set_failed_rule :_RawBlockBuilderSelect unless _tmp return _tmp end - # RawInlineElementContent = ("\\}" { "}" } | < /[^\r\n\}]/ > { text }) - def _RawInlineElementContent + # RawBlockBuilderSelectSub = (LowerAlphabetAscii+:c1 Space* "," Space* RawBlockBuilderSelectSub:c2 { [c1.join("")]+ c2 } | LowerAlphabetAscii+:c1 { [c1.join("")] }) + def _RawBlockBuilderSelectSub _save = self.pos while true # choice _save1 = self.pos while true # sequence - _tmp = match_string("\\}") + _save2 = self.pos + _ary = [] + _tmp = apply(:_LowerAlphabetAscii) + if _tmp + _ary << @result + while true + _tmp = apply(:_LowerAlphabetAscii) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end + c1 = @result unless _tmp self.pos = _save1 break end - @result = begin; "}" ; end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save1 + break + end + _tmp = match_string(",") + unless _tmp + self.pos = _save1 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save1 + break + end + _tmp = apply(:_RawBlockBuilderSelectSub) + c2 = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; [c1.join("")]+ c2 ; end _tmp = true unless _tmp self.pos = _save1 @@ -1552,21 +1596,32 @@ def _RawInlineElementContent break if _tmp self.pos = _save - _save2 = self.pos + _save5 = self.pos while true # sequence - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n\}])/) + _save6 = self.pos + _ary = [] + _tmp = apply(:_LowerAlphabetAscii) if _tmp - text = get_text(_text_start) + _ary << @result + while true + _tmp = apply(:_LowerAlphabetAscii) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save6 end + c1 = @result unless _tmp - self.pos = _save2 + self.pos = _save5 break end - @result = begin; text ; end + @result = begin; [c1.join("")] ; end _tmp = true unless _tmp - self.pos = _save2 + self.pos = _save5 end break end # end sequence @@ -1576,33 +1631,614 @@ def _RawInlineElementContent break end # end choice - set_failed_rule :_RawInlineElementContent unless _tmp + set_failed_rule :_RawBlockBuilderSelectSub unless _tmp return _tmp end - # InlineElementSymbol = < /[^>\r\n]+/ > { text } - def _InlineElementSymbol + # RawBlockElementArg = !"]" ("\\]" { "]" } | "\\n" { "\n" } | < /[^\r\n\]]/ > { text }) + def _RawBlockElementArg _save = self.pos while true # sequence - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^>\r\n]+)/) - if _tmp - text = get_text(_text_start) - end + _save1 = self.pos + _tmp = match_string("]") + _tmp = _tmp ? nil : true + self.pos = _save1 unless _tmp self.pos = _save break end - @result = begin; text ; end - _tmp = true - unless _tmp - self.pos = _save - end + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = match_string("\\]") + unless _tmp + self.pos = _save3 + break + end + @result = begin; "]" ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save4 = self.pos + while true # sequence + _tmp = match_string("\\n") + unless _tmp + self.pos = _save4 + break + end + @result = begin; "\n" ; end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save5 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\]])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save5 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + break + end # end choice + + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_RawBlockElementArg unless _tmp + return _tmp + end + + # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) }) + def _InlineElement + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_RawInlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_RawInlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("@<") + unless _tmp + self.pos = _save2 + break + end + _tmp = apply(:_InlineElementSymbol) + symbol = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string(">") + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save2 + break + end + _save4 = self.pos + _tmp = apply(:_InlineElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save4 + end + contents = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save2 + break + end + @result = begin; compile_inline(symbol,contents) ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElement unless _tmp + return _tmp + end + + # RawInlineElement = "@{" RawInlineElementContent+:c "}" { c.join("") } + def _RawInlineElement + + _save = self.pos + while true # sequence + _tmp = match_string("@{") + unless _tmp + self.pos = _save + break + end + _save1 = self.pos + _ary = [] + _tmp = apply(:_RawInlineElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_RawInlineElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save + break + end + @result = begin; c.join("") ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_RawInlineElement unless _tmp + return _tmp + end + + # RawInlineElementContent = ("\\}" { "}" } | < /[^\r\n\}]/ > { text }) + def _RawInlineElementContent + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\}])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save2 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_RawInlineElementContent unless _tmp + return _tmp + end + + # InlineElementSymbol = < /[^>\r\n]+/ > { text } + def _InlineElementSymbol + + _save = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^>\r\n]+)/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementSymbol unless _tmp + return _tmp + end + + # InlineElementContents = !"}" InlineElementContentsSub:c { c } + def _InlineElementContents + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_InlineElementContentsSub) + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementContents unless _tmp + return _tmp + end + + # InlineElementContentsSub = !"}" (InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] }) + def _InlineElementContentsSub + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = apply(:_InlineElementContent) + c1 = @result + unless _tmp + self.pos = _save3 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save3 + break + end + _tmp = match_string(",") + unless _tmp + self.pos = _save3 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save3 + break + end + _tmp = apply(:_InlineElementContentsSub) + c2 = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; [c1]+c2 ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save6 = self.pos + while true # sequence + _tmp = apply(:_InlineElementContent) + c1 = @result + unless _tmp + self.pos = _save6 + break + end + @result = begin; [c1] ; end + _tmp = true + unless _tmp + self.pos = _save6 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + break + end # end choice + + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementContentsSub unless _tmp + return _tmp + end + + # InlineElementContent = (InlineElement:c { c } | InlineElementContentText+:c { c.join("") }) + def _InlineElementContent + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _ary = [] + _tmp = apply(:_InlineElementContentText) + if _tmp + _ary << @result + while true + _tmp = apply(:_InlineElementContentText) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save3 + end + c = @result + unless _tmp + self.pos = _save2 + break + end + @result = begin; c.join("") ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElementContent unless _tmp + return _tmp + end + + # InlineElementContentText = ("\\}" { "}" } | "\\," { "," } | "\\\\" { "\\" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\},]/ > { text }) + def _InlineElementContentText + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _tmp = match_string("\\,") + unless _tmp + self.pos = _save2 + break + end + @result = begin; "," ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _tmp = match_string("\\\\") + unless _tmp + self.pos = _save3 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos + while true # sequence + _tmp = match_string("\\") + unless _tmp + self.pos = _save4 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save5 = self.pos + while true # sequence + _save6 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save5 + break + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\\},])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save5 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + + break if _tmp + self.pos = _save break - end # end sequence + end # end choice - set_failed_rule :_InlineElementSymbol unless _tmp + set_failed_rule :_InlineElementContentText unless _tmp return _tmp end @@ -1653,7 +2289,7 @@ def _BracketArg return _tmp end - # BracketArgContentInline = (InlineElement:c { c } | "\\]" { "]" } | < /[^\r\n\]]/ > { escape_text(text) }) + # BracketArgContentInline = (InlineElement:c { c } | "\\]" { "]" } | "\\\\" { "\\" } | < /[^\r\n\]]/ > { escape_text(text) }) def _BracketArgContentInline _save = self.pos @@ -1697,6 +2333,24 @@ def _BracketArgContentInline self.pos = _save _save3 = self.pos + while true # sequence + _tmp = match_string("\\\\") + unless _tmp + self.pos = _save3 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos while true # sequence _text_start = self.pos _tmp = scan(/\A(?-mix:[^\r\n\]])/) @@ -1704,13 +2358,13 @@ def _BracketArgContentInline text = get_text(_text_start) end unless _tmp - self.pos = _save3 + self.pos = _save4 break end @result = begin; escape_text(text) ; end _tmp = true unless _tmp - self.pos = _save3 + self.pos = _save4 end break end # end sequence @@ -1972,179 +2626,6 @@ def _BlockElementContentText return _tmp end - # InlineElementContents = !"}" InlineElementContent+:c { c.join("") } - def _InlineElementContents - - _save = self.pos - while true # sequence - _save1 = self.pos - _tmp = match_string("}") - _tmp = _tmp ? nil : true - self.pos = _save1 - unless _tmp - self.pos = _save - break - end - _save2 = self.pos - _ary = [] - _tmp = apply(:_InlineElementContent) - if _tmp - _ary << @result - while true - _tmp = apply(:_InlineElementContent) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save2 - end - c = @result - unless _tmp - self.pos = _save - break - end - @result = begin; c.join("") ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_InlineElementContents unless _tmp - return _tmp - end - - # InlineElementContent = (InlineElement:c | InlineElementContentText+:c) - def _InlineElementContent - - _save = self.pos - while true # choice - _tmp = apply(:_InlineElement) - c = @result - break if _tmp - self.pos = _save - _save1 = self.pos - _ary = [] - _tmp = apply(:_InlineElementContentText) - if _tmp - _ary << @result - while true - _tmp = apply(:_InlineElementContentText) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 - end - c = @result - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_InlineElementContent unless _tmp - return _tmp - end - - # InlineElementContentText = &. ("\\}" { "}" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\}]/ > { text }) - def _InlineElementContentText - - _save = self.pos - while true # sequence - _save1 = self.pos - _tmp = get_byte - self.pos = _save1 - unless _tmp - self.pos = _save - break - end - - _save2 = self.pos - while true # choice - - _save3 = self.pos - while true # sequence - _tmp = match_string("\\}") - unless _tmp - self.pos = _save3 - break - end - @result = begin; "}" ; end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - - _save4 = self.pos - while true # sequence - _tmp = match_string("\\") - unless _tmp - self.pos = _save4 - break - end - @result = begin; "\\" ; end - _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - - _save5 = self.pos - while true # sequence - _save6 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save6 - unless _tmp - self.pos = _save5 - break - end - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n\\}])/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save5 - break - end - @result = begin; text ; end - _tmp = true - unless _tmp - self.pos = _save5 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - break - end # end choice - - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_InlineElementContentText unless _tmp - return _tmp - end - # SinglelineContent = ContentInlines:c (Newline | EOF) { c } def _SinglelineContent @@ -2949,22 +3430,26 @@ def _Spacechar Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | ContentText:c { c })+:d { e=d.join(\"\") } Newline { e }") Rules[:_ContentText] = rule_info("ContentText", "NonInlineElement+:c { c.join(\"\") }") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < /[^\\r\\n]/ > { escape_text(text) }") - Rules[:_BlockElement] = rule_info("BlockElement", "(\"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect:b? RawBlockElementArg*:r1 \"]\" Space* Newline { compile_raw(b, r1.join(\"\")) } | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline { compile_command(symbol, args, contents) } | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) })") + Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c.join(\"\") }") + Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(LowerAlphabetAscii+:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [c1.join(\"\")]+ c2 } | LowerAlphabetAscii+:c1 { [c1.join(\"\")] })") + Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < /[^\\r\\n\\]]/ > { text })") Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" { compile_inline(symbol,contents) })") Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawInlineElementContent+:c \"}\" { c.join(\"\") }") Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< /[^>\\r\\n]+/ > { text }") + Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") + Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") + Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c { c } | InlineElementContentText+:c { c.join(\"\") })") + Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" { \"}\" } | \"\\\\,\" { \",\" } | \"\\\\\\\\\" { \"\\\\\" } | \"\\\\\" { \"\\\\\" } | !InlineElement < /[^\\r\\n\\\\},]/ > { text })") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline+:c \"]\" { c.join(\"\") }") - Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" { \"]\" } | < /[^\\r\\n\\]]/ > { escape_text(text) })") + Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" { \"]\" } | \"\\\\\\\\\" { \"\\\\\" } | < /[^\\r\\n\\]]/ > { escape_text(text) })") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c | BlockElement:c | BlockElementParagraph:c | Newline:c { \"\" })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c Newline { c.join(\"\") }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c | BlockElementContentText:c)") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join(\"\") }") - Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContent+:c { c.join(\"\") }") - Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:c | InlineElementContentText+:c)") - Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "&. (\"\\\\}\" { \"}\" } | \"\\\\\" { \"\\\\\" } | !InlineElement < /[^\\r\\n\\\\}]/ > { text })") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c (Newline | EOF) { c }") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c.join }") Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < /[^\\r\\n]/ > { escape_text(text) })") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 449cdd0a8..1f1dc1d5f 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -385,14 +385,14 @@ require 'review/exception' end - def compile_inline(op, arg) + def compile_inline(op, args) unless inline_defined?(op) raise ReVIEW::CompileError, "no such inline op: #{op}" end unless @strategy.respond_to?("inline_#{op}") raise "strategy does not support inline op: @<#{op}>" end - @strategy.__send__("inline_#{op}", arg) + @strategy.__send__("inline_#{op}", *args) rescue => err error err.message end @@ -401,6 +401,15 @@ require 'review/exception' @strategy.paragraph buf end + def compile_raw(builders, content) + c = @strategy.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase + if !builders || builders.include?(c) + content + else + "" + end + end + def warn(msg) @strategy.warn msg end @@ -446,11 +455,23 @@ ContentText = NonInlineElement+:c { c.join("") } NonInlineElement = !InlineElement < /[^\r\n]/ > { escape_text(text) } -BlockElement = ( "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline +BlockElement = ( "//raw[" RawBlockBuilderSelect:b? RawBlockElementArg*:r1 "]" Space* Newline + { compile_raw(b, r1.join("")) } + | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline { compile_command(symbol, args, contents) } - | "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } + | !"//raw" "//" ElementName:symbol BracketArg*:args Space* Newline { compile_command(symbol, args, nil) } ) +RawBlockBuilderSelect = "|" Space* RawBlockBuilderSelectSub:c Space* "|" { c.join("") } + +RawBlockBuilderSelectSub = ( LowerAlphabetAscii+:c1 Space* "," Space* RawBlockBuilderSelectSub:c2 + { [c1.join("")]+ c2 } + | LowerAlphabetAscii+:c1 + { [c1.join("")] } + ) + +RawBlockElementArg = !"]" ( "\\]" { "]" } | "\\n" { "\n" } | < /[^\r\n\]]/ > { text } ) + InlineElement = ( RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" { compile_inline(symbol,contents) } @@ -462,9 +483,24 @@ RawInlineElementContent = ( "\\}" { "}" } | < /[^\r\n\}]/ > { text }) InlineElementSymbol = < /[^>\r\n]+/ > { text } +InlineElementContents = !"}" InlineElementContentsSub:c { c } + +InlineElementContentsSub = !"}" ( InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } + | InlineElementContent:c1 { [c1] } + ) + + +InlineElementContent = ( InlineElement:c { c } + | InlineElementContentText+:c { c.join("") } + ) + +## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? +InlineElementContentText = ( "\\}" { "}" } | "\\," { "," } | "\\\\" { "\\" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\},]/> { text } ) + BracketArg = "[" BracketArgContentInline+:c "]" { c.join("") } -BracketArgContentInline = ( InlineElement:c { c } | "\\]" { "]" } | < /[^\r\n\]]/ > { escape_text(text) } ) +## XXX '\' (excpet '\]' and '\\' ) => '\' is ??? +BracketArgContentInline = ( InlineElement:c { c } | "\\]" { "]" } | "\\\\" { "\\" } | < /[^\r\n\]]/ > { escape_text(text) } ) BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } @@ -486,15 +522,6 @@ BlockElementParagraphSub = ( InlineElement:c BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c.join("") } -InlineElementContents = !"}" InlineElementContent+:c { c.join("") } - -InlineElementContent = ( InlineElement:c - | InlineElementContentText+:c - ) - -## XXX '\' (excpet '\}') => '\' is OK? -InlineElementContentText = &. ( "\\}" { "}" } | "\\" { "\\" } | !InlineElement < /[^\r\n\\}]/> { text } ) - SinglelineContent = ContentInlines:c (Newline | EOF) { c } ContentInlines = ContentInline+:c { c.join } From 5ea16974c0131c7b60a45a2e20d3a05c856ade29 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 20:48:06 +0900 Subject: [PATCH 023/214] fix compiler tests --- test/test_compiler.rb | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/test/test_compiler.rb b/test/test_compiler.rb index fd18b907c..cd497dbe5 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'test_helper' +require 'review' require 'review/compiler' require 'review/book' require 'review/latexbuilder' @@ -9,37 +10,52 @@ class CompilerTest < Test::Unit::TestCase include ReVIEW def setup - @builder = LATEXBuilder.new() - @c = Compiler.new(@builder) + @builder = HTMLBuilder.new() + @param = { + "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder + "inencoding" => "UTF-8", + "outencoding" => "UTF-8", + "subdirmode" => nil, + "stylesheet" => nil, # for HTMLBuilder + } + ReVIEW.book.param = @param + @compiler = ReVIEW::Compiler.new(@builder) + @chapter = Book::Chapter.new(Book::Base.new(nil), 1, '-', nil, StringIO.new) + location = Location.new(nil, nil) + @builder.bind(@compiler, @chapter, location) + + def @compiler.compile_command(name, args, lines) + args + end end def test_parse_args - args = @c.__send__(:parse_args, "[foo][bar]") + args = compile_blockelem("//dummy[foo][bar]\n") assert_equal ["foo","bar"], args end def test_parse_args_with_brace1 - args = @c.__send__(:parse_args, "[fo[\\][\\]o][bar]") + args = compile_blockelem("//dummy[fo[\\][\\]o][bar]") assert_equal ["fo[][]o","bar"], args end def test_parse_args_with_brace2 - args = @c.__send__(:parse_args, "[f\\]o\\]o][bar]") + args = compile_blockelem("//dummy[f\\]o\\]o][bar]") assert_equal ["f]o]o","bar"], args end def test_parse_args_with_backslash - args = @c.__send__(:parse_args, "[foo][bar\\buz]") + args = compile_blockelem("//dummy[foo][bar\\buz]") assert_equal ["foo","bar\\buz"], args end def test_parse_args_with_backslash2 - args = @c.__send__(:parse_args, "[foo][bar\\#\\[\\!]") + args = compile_blockelem("//dummy[foo][bar\\#\\[\\!]") assert_equal ["foo","bar\\#\\[\\!"], args end def test_parse_args_with_backslash3 - args = @c.__send__(:parse_args, "[foo][bar\\\\buz]") + args = compile_blockelem("//dummy[foo][bar\\\\buz]") assert_equal ["foo","bar\\buz"], args end end From 8742a72c9572c358205ca4fcad75a67e2e9eb140 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 2 Mar 2014 20:51:18 +0900 Subject: [PATCH 024/214] fix idgxmlbuilder --- lib/review/idgxmlbuilder.rb | 387 ++++++++++++++++++++---------------- test/test_idgxmlbuilder.rb | 232 ++++++++++----------- 2 files changed, 338 insertions(+), 281 deletions(-) diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 69579bad0..73396cd8d 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -73,9 +73,13 @@ def builder_init_file @texblockequation = 0 @texinlineequation = 0 - print %Q(\n) - print %Q(<#{@rootelement} xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">) - alias puts print unless ReVIEW.book.param["nolf"].nil? + @output << %Q(\n) + @output << %Q(<#{@rootelement} xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">) + if ReVIEW.book.param["nolf"].nil? + @lf = "\n" + else + @lf = "" + end @secttags = true unless ReVIEW.book.param["structuredxml"].nil? end private :builder_init_file @@ -95,7 +99,7 @@ def result def warn(msg) if @no_error @warns.push [@location.filename, @location.lineno, msg] - puts "----WARNING: #{escape_html(msg)}----" + buf << "----WARNING: #{escape_html(msg)}----" << @lf else $stderr.puts "#{@location}: warning: #{msg}" end @@ -104,7 +108,7 @@ def warn(msg) def error(msg) if @no_error @errors.push [@location.filename, @location.lineno, msg] - puts "----ERROR: #{escape_html(msg)}----" + buf << "----ERROR: #{escape_html(msg)}----" << @lf else $stderr.puts "#{@location}: error: #{msg}" end @@ -135,17 +139,18 @@ def warning_messages end def headline(level, label, caption) + buf = "" prefix = "" case level when 1 unless @secttags.nil? - print "" if @subsubsubsection > 0 - print "" if @subsubsection > 0 - print "" if @subsection > 0 - print "" if @section > 0 + buf << "" if @subsubsubsection > 0 + buf << "" if @subsubsection > 0 + buf << "" if @subsection > 0 + buf << "" if @section > 0 end - print %Q() unless @secttags.nil? + buf << %Q() unless @secttags.nil? if @chapter.number.blank? prefix = "" @@ -166,13 +171,13 @@ def headline(level, label, caption) @subsubsubsection = 0 when 2 unless @secttags.nil? - print "" if @subsubsubsection > 0 - print "" if @subsubsection > 0 - print "" if @subsection > 0 - print "" if @section > 0 + buf << "" if @subsubsubsection > 0 + buf << "" if @subsubsection > 0 + buf << "" if @subsection > 0 + buf << "" if @section > 0 end @section += 1 - print %Q() unless @secttags.nil? + buf << %Q() unless @secttags.nil? if ReVIEW.book.param["secnolevel"] >= 2 if @chapter.number.blank? or @chapter.on_POSTDEF? prefix = "" @@ -186,13 +191,13 @@ def headline(level, label, caption) @subsubsubsection = 0 when 3 unless @secttags.nil? - print "" if @subsubsubsection > 0 - print "" if @subsubsection > 0 - print "" if @subsection > 0 + buf << "" if @subsubsubsection > 0 + buf << "" if @subsubsection > 0 + buf << "" if @subsection > 0 end @subsection += 1 - print %Q() unless @secttags.nil? + buf << %Q() unless @secttags.nil? if ReVIEW.book.param["secnolevel"] >= 3 if @chapter.number.blank? or @chapter.on_POSTDEF? prefix = "" @@ -205,12 +210,12 @@ def headline(level, label, caption) @subsubsubsection = 0 when 4 unless @secttags.nil? - print "" if @subsubsubsection > 0 - print "" if @subsubsection > 0 + buf << "" if @subsubsubsection > 0 + buf << "" if @subsubsection > 0 end @subsubsection += 1 - print %Q() unless @secttags.nil? + buf << %Q() unless @secttags.nil? if ReVIEW.book.param["secnolevel"] >= 4 if @chapter.number.blank? or @chapter.on_POSTDEF? prefix = "" @@ -222,11 +227,11 @@ def headline(level, label, caption) @subsubsubsection = 0 when 5 unless @secttags.nil? - print "" if @subsubsubsection > 0 + buf << "" if @subsubsubsection > 0 end @subsubsubsection += 1 - print %Q() unless @secttags.nil? + buf << %Q() unless @secttags.nil? if ReVIEW.book.param["secnolevel"] >= 5 if @chapter.number.blank? or @chapter.on_POSTDEF? prefix = "" @@ -241,65 +246,68 @@ def headline(level, label, caption) prefix = "" if (level.to_i > ReVIEW.book.param["secnolevel"]) label = label.nil? ? "" : " id=\"#{label}\"" - toccaption = escape_html(compile_inline(caption.gsub(/@\{.+?\}/, '')).gsub(/<[^>]+>/, '')) - puts %Q(#{prefix}#{compile_inline(caption)}) + toccaption = escape_html(caption.gsub(/@\{.+?\}/, '').gsub(/<[^>]+>/, '')) + buf << %Q(#{prefix}#{caption}) << @lf + + buf end def ul_begin level = block_given? ? yield : "" level = nil if level == 1 - puts "" + "" + @lf end def ul_item(lines) - puts %Q(
    3. #{lines.join.chomp}
    4. ) + %Q(
    5. #{lines.join.chomp}
    6. ) + @lf end def ul_item_begin(lines) - print %Q(
    7. #{lines.join.chomp}) + %Q(
    8. #{lines.join.chomp}) end def ul_item_end - puts "
    9. " + "
    10. " + @lf end def choice_single_begin - puts "" + "" + @lf end def choice_multi_begin - puts "" + "" + @lf end def choice_single_end - puts "" + "" + @lf end def choice_multi_end - puts "" + "" + @lf end def ul_end level = block_given? ? yield : "" level = nil if level == 1 - puts "" + "" + @lf end def ol_begin - puts '
        ' if !@ol_num @ol_num = 1 end + '
          ' + @lf end def ol_item(lines, num) - puts %Q(
        1. #{lines.join.chomp}
        2. ) + buf = %Q(
        3. #{lines.join.chomp}
        4. ) + @lf @ol_num += 1 + buf end def ol_end - puts '
        ' @ol_num = nil + '
      ' + @lf end def olnum(num) @@ -307,39 +315,41 @@ def olnum(num) end def dl_begin - puts '
      ' + '
      ' + @lf end def dt(line) - puts "
      #{line}
      " + "
      #{line}
      " + @lf end def dd(lines) - puts "
      #{lines.join.chomp}
      " + "
      #{lines.join.chomp}
      " + @lf end def dl_end - puts '
      ' + '
      ' + @lf end def paragraph(lines) + buf = "" if @noindent.nil? if lines[0] =~ /\A(\t+)/ - puts %Q(

      #{lines.join('').sub(/\A\t+/, "")}

      ) + buf << %Q(

      #{lines.join('').sub(/\A\t+/, "")}

      ) << @lf else - puts "

      #{lines.join}

      " + buf << "

      #{lines.join}

      " << @lf end else - puts %Q(

      #{lines.join}

      ) + buf << %Q(

      #{lines.join}

      ) << @lf @noindent = nil end + buf end def read(lines) if ReVIEW.book.param["deprecated-blocklines"].nil? - puts %Q[#{split_paragraph(lines).join}] + %Q[#{split_paragraph(lines).join}] + @lf else - puts %Q[

      #{lines.join}

      ] + %Q[

      #{lines.join}

      ] + @lf end end @@ -355,34 +365,40 @@ def inline_list(id) end def list_header(id, caption) - puts %Q[] + buf = "" + buf << %Q[] << @lf if get_chap.nil? - puts %Q[#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << @lf else - puts %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << @lf end + buf end def codelines_body(lines) + buf = "" no = 1 lines.each do |line| unless ReVIEW.book.param["listinfo"].nil? - print "" + buf << "" end - print detab(line) - print "\n" - print "" unless ReVIEW.book.param["listinfo"].nil? + buf << detab(line) + buf << "\n" + buf << "" unless ReVIEW.book.param["listinfo"].nil? no += 1 end + buf end def list_body(id, lines) - print %Q(
      )
      -      codelines_body(lines)
      -      puts "
      " + buf = "" + buf << %Q(
      )
      +      buf << codelines_body(lines)
      +      buf << "
      " << @lf + buf end def emlist(lines, caption = nil) @@ -398,21 +414,23 @@ def emlistnum(lines, caption = nil) end def listnum_body(lines) - print %Q(
      )
      +      buf = ""
      +      buf << %Q(
      )
             no = 1
             lines.each_with_index do |line, i|
               unless ReVIEW.book.param["listinfo"].nil?
      -          print ""
      +          buf << ""
               end
      -        print detab("" + (i + 1).to_s.rjust(2) + ": " + line)
      -        print "\n"
      -        print "" unless ReVIEW.book.param["listinfo"].nil?
      +        buf << detab("" + (i + 1).to_s.rjust(2) + ": " + line)
      +        buf << "\n"
      +        buf << "" unless ReVIEW.book.param["listinfo"].nil?
               no += 1
             end
      -      puts "
      " + buf << "
      " << @lf + buf end def cmd(lines, caption = nil) @@ -420,32 +438,34 @@ def cmd(lines, caption = nil) end def quotedlist(lines, css_class, caption) - print %Q[] - puts "#{compile_inline(caption)}" unless caption.nil? - print %Q[
      ]
      +      buf = ""
      +      buf << %Q[]
      +      buf << "#{caption}" + @lf unless caption.nil?
      +      buf << %Q[
      ]
             no = 1
             lines.each do |line|
               unless ReVIEW.book.param["listinfo"].nil?
      -          print ""
      +          buf << ""
               end
      -        print detab(line)
      -        print "\n"
      -        print "" unless ReVIEW.book.param["listinfo"].nil?
      +        buf << detab(line)
      +        buf << "\n"
      +        buf << "" unless ReVIEW.book.param["listinfo"].nil?
               no += 1
             end
      -      puts '
      ' + buf << '
      ' << @lf + buf end private :quotedlist def quote(lines) if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts "#{blocked_lines.join("")}" + "#{blocked_lines.join("")}" + @lf else - puts "#{lines.join("\n")}" + "#{lines.join("\n")}" + @lf end end @@ -477,14 +497,17 @@ def result_metric(array) end def image_image(id, caption, metric=nil) + buf = "" metrics = parse_metric("idgxml", metric) - puts "" - puts %Q[] - image_header id, caption - puts "" + buf << "" << @lf + buf << %Q[] << @lf + buf << image_header(id, caption) + buf << "" << @lf + buf end def image_dummy(id, caption, lines) + buf = "" if ReVIEW.book.param["subdirmode"] warn "image file not exist: images/#{@chapter.id}/#{id}.eps" unless File.exist?("images/#{@chapter.id}/#{id}.eps") elsif ReVIEW.book.param["singledirmode"] @@ -492,41 +515,44 @@ def image_dummy(id, caption, lines) else warn "image file not exist: images/#{@chapter.id}-#{id}.eps" unless File.exist?("images/#{@chapter.id}-#{id}.eps") end - puts "" - print %Q[
      ]
      +      buf << "" << @lf
      +      buf << %Q[
      ]
             lines.each do |line|
      -        print detab(line)
      -        print "\n"
      +        buf << detab(line)
      +        buf << "\n"
             end
      -      print %Q[
      ] + buf << %Q[
      ] image_header id, caption - puts "" + buf << "" << @lf + buf end def image_header(id, caption) if get_chap.nil? - puts %Q[#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + %Q[#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] + @lf else - puts %Q[#{I18n.t("image")}#{I18n.t("format_number", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + %Q[#{I18n.t("image")}#{I18n.t("format_number", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] + @lf end end def texequation(lines) + buf = "" @texblockequation += 1 - puts %Q[] - puts '
      '
      -      puts "#{lines.join("\n")}"
      -      puts '
      ' - puts '
      ' + buf << %Q[] << @lf + buf << '
      ' << @lf
      +      buf << "#{lines.join("\n")}" << @lf
      +      buf << '
      ' << @lf + buf << '
      ' << @lf end def table(lines, id = nil, caption = nil) + buf = "" tablewidth = nil col = 0 unless ReVIEW.book.param["tableopt"].nil? tablewidth = ReVIEW.book.param["tableopt"].split(",")[0].to_f / 0.351 # mm -> pt end - puts "" + buf << "
      " rows = [] sepidx = nil lines.each_with_index do |line, idx| @@ -566,66 +592,71 @@ def table(lines, id = nil, caption = nil) end begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end return if rows.empty? if tablewidth.nil? - print "" + buf << "" else - print %Q[] + buf << %Q[] end if sepidx sepidx.times do |y| if tablewidth.nil? - puts %Q[#{rows.shift}] + buf << %Q[#{rows.shift}] << @lf else i = 0 rows.shift.split(/\t/).each_with_index do |cell, x| - print %Q[] + buf << %Q[] i += 1 end end end end - trputs(tablewidth, rows, cellwidth, sepidx) - puts "
      #{cell.sub("DUMMYCELLSPLITTER", "")}#{cell.sub("DUMMYCELLSPLITTER", "")}
      " + buf << trputs(tablewidth, rows, cellwidth, sepidx) + buf << "" << @lf @tsize = nil + buf end def trputs(tablewidth, rows, cellwidth, sepidx) + buf = "" sepidx = 0 if sepidx.nil? if tablewidth.nil? lastline = rows.pop - rows.each {|row| puts %Q[#{row}] } - puts %Q[#{lastline}] unless lastline.nil? + rows.each {|row| buf << %Q[#{row}] } + buf << %Q[#{lastline}] + @lf unless lastline.nil? else rows.each_with_index do |row, y| i = 0 row.split(/\t/).each_with_index do |cell, x| - print %Q[#{cell.sub("DUMMYCELLSPLITTER", "")}] + buf << %Q[#{cell.sub("DUMMYCELLSPLITTER", "")}] i += 1 end end end + buf end def table_header(id, caption) + buf = "" if get_chap.nil? - puts %Q[#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] + @lf else - puts %Q[#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] + @lf end + buf end def table_begin(ncols) end def tr(rows) - puts %Q[#{rows.join("\t")}] + %Q[#{rows.join("\t")}] + @lf end def th(str) @@ -637,11 +668,11 @@ def td(str) end def table_end - print "" + "" end def comment(str) - print %Q() + %Q() end def footnote(id, str) @@ -649,7 +680,7 @@ def footnote(id, str) end def inline_fn(id) - %Q[#{compile_inline(@chapter.footnote(id).content.strip)}] + %Q[#{@chapter.footnote(id).content.strip}] end def compile_ruby(base, ruby) @@ -800,34 +831,36 @@ def noindent def linebreak # FIXME:pが閉じちゃってるので一度戻らないといけないが、難しい…。 - puts "
      " + "
      " + @lf end def pagebreak - puts "" + "" + @lf end def nonum_begin(level, label, caption) - puts %Q[#{compile_inline(caption)}] + %Q[#{caption}] + @lf end def nonum_end(level) end def circle_begin(level, label, caption) - puts %Q[•#{compile_inline(caption)}] + %Q[•#{caption}] + @lf end def circle_end(level) end def common_column_begin(type, caption) - print "<#{type}column>" - puts %Q[#{compile_inline(caption)}] + buf = "" + buf << "<#{type}column>" + buf << %Q[#{caption}] << @lf + buf end def common_column_end(type) - puts "" + "" + @lf end def column_begin(level, label, caption) @@ -880,50 +913,52 @@ def insideout_end(level) def ref_begin(level, label, caption) if !label.nil? - puts "" + "" + @lf else - puts "" + "" + @lf end end def ref_end(level) - puts "" + "" + @lf end def sup_begin(level, label, caption) if !label.nil? - puts "" + "" + @lf else - puts "" + "" + @lf end end def sup_end(level) - puts "" + "" + @lf end def flushright(lines) if ReVIEW.book.param["deprecated-blocklines"].nil? - puts split_paragraph(lines).join.gsub("

      ", "

      ") + split_paragraph(lines).join.gsub("

      ", "

      ") + @lf else - puts "

      #{lines.join("\n")}

      " + "

      #{lines.join("\n")}

      " + @lf end end def centering(lines) - puts split_paragraph(lines).join.gsub("

      ", "

      ") + split_paragraph(lines).join.gsub("

      ", "

      ") + @lf end def captionblock(type, lines, caption, specialstyle = nil) - print "<#{type}>" + buf = "" + buf << "<#{type}>" style = specialstyle.nil? ? "#{type}-title" : specialstyle - puts "#{compile_inline(caption)}" unless caption.nil? + buf << "#{caption}" + @lf unless caption.nil? if ReVIEW.book.param["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts "#{blocked_lines.join}" + buf << "#{blocked_lines.join}" << @lf else - puts "#{lines.join("\n")}" + buf << "#{lines.join("\n")}" << @lf end + buf end def note(lines, caption = nil) @@ -1007,8 +1042,9 @@ def expert(lines) end def syntaxblock(type, lines, caption) + buf = "" if caption.nil? - puts %Q[<#{type}>] + buf << %Q[<#{type}>] << @lf else titleopentag = %Q[caption aid:pstyle="#{type}-title"] titleclosetag = "caption" @@ -1016,22 +1052,23 @@ def syntaxblock(type, lines, caption) titleopentag = %Q[floattitle type="insn"] titleclosetag = "floattitle" end - puts %Q[<#{type}><#{titleopentag}>#{compile_inline(caption)}] + buf << %Q[<#{type}><#{titleopentag}>#{caption}] << @lf end no = 1 lines.each do |line| unless ReVIEW.book.param["listinfo"].nil? - print %Q[] + buf << %Q[] end - print detab(line) - print "\n" - print "" unless ReVIEW.book.param["listinfo"].nil? + buf << detab(line) + buf << "\n" + buf << "" unless ReVIEW.book.param["listinfo"].nil? no += 1 end - puts "" + buf << "" << @lf + buf end def insn(lines, caption = nil) @@ -1043,22 +1080,24 @@ def box(lines, caption = nil) end def indepimage(id, caption=nil, metric=nil) + buf = "" metrics = parse_metric("idgxml", metric) - puts "" + buf << "" << @lf begin - puts %Q[] + buf << %Q[] << @lf rescue warn %Q[no such image: #{id}] end - puts %Q[#{compile_inline(caption)}] if !caption.nil? && !caption.empty? - puts "" + buf << %Q[#{caption}] + @lf if !caption.nil? && !caption.empty? + buf << "" << @lf + buf end alias :numberlessimage indepimage def label(id) # FIXME - print "


      " + "
      " end def bpo(lines) - puts %Q[#{lines.join("\n")}] + %Q[#{lines.join("\n")}] + @lf end def inline_dtp(str) @@ -1090,12 +1129,14 @@ def inline_br(str) end def rawblock(lines) + buf = "" no = 1 lines.each do |l| - print l.gsub("<", "<").gsub(">", ">").gsub(""", "\"").gsub("&", "&") - print "\n" unless lines.length == no + buf << l.gsub("<", "<").gsub(">", ">").gsub(""", "\"").gsub("&", "&") + buf << "\n" unless lines.length == no no = no + 1 end + buf end def text(str) @@ -1148,31 +1189,39 @@ def inline_title(id) end def source_header(caption) - puts %Q[] - puts %Q[#{compile_inline(caption)}] + buf = "" + buf << %Q[] << @lf + buf << %Q[#{caption}] << @lf + buf end def source_body(lines) - puts %Q[
      ]
      -      codelines_body(lines)
      -      puts %Q[
      ] + buf = "" + buf << %Q[
      ] << @lf
      +      buf << codelines_body(lines)
      +      buf << %Q[
      ] << @lf + buf end def bibpaper(lines, id, caption) - bibpaper_header id, caption + buf = "" + buf << bibpaper_header(id, caption) unless lines.empty? - bibpaper_bibpaper id, caption, lines + buf << bibpaper_bibpaper(id, caption, lines) end - puts %Q() + buf << %Q() << @lf + buf end def bibpaper_header(id, caption) - puts %Q() - puts "[#{@chapter.bibpaper(id).number}] #{compile_inline(caption)}" + buf = "" + buf << %Q() << @lf + buf << "[#{@chapter.bibpaper(id).number}] #{caption}" << @lf + buf end def bibpaper_bibpaper(id, caption, lines) - print split_paragraph(lines).join("") + split_paragraph(lines).join("") end def inline_bib(id) @@ -1183,10 +1232,10 @@ def inline_hd_chap(chap, id) if chap.number n = chap.headline_index.number(id) if ReVIEW.book.param["secnolevel"] >= n.split('.').size - return "「#{n} #{compile_inline(chap.headline(id).caption)}」" + return "「#{n} #{chap.headline(id).caption}」" end end - "「#{compile_inline(chap.headline(id).caption)}」" + "「#{chap.headline(id).caption}」" end def inline_recipe(id) diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index 0b9484608..1cd661bdd 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'test_helper' +require 'review' require 'review/compiler' require 'review/book' require 'review/idgxmlbuilder' @@ -27,40 +28,40 @@ def setup end def test_headline_level1 - @builder.headline(1,"test","this is test.") - assert_equal %Q|\n第1章 this is test.|, @builder.raw_result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|第1章 this is test.|, result end def test_headline_level1_without_secno @param["secnolevel"] = 0 - @builder.headline(1,"test","this is test.") - assert_equal %Q|\nthis is test.|, @builder.raw_result + result = @builder.headline(1,"test","this is test.") + assert_equal %Q|this is test.|, result end def test_headline_level2 - @builder.headline(2,"test","this is test.") - assert_equal %Q|\n1.1 this is test.|, @builder.raw_result + result = @builder.headline(2,"test","this is test.") + assert_equal %Q|1.1 this is test.|, result end def test_headline_level3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\nthis is test.|, @builder.raw_result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|this is test.|, result end def test_headline_level3_with_secno @param["secnolevel"] = 3 - @builder.headline(3,"test","this is test.") - assert_equal %Q|\n1.0.1 this is test.|, @builder.raw_result + result = @builder.headline(3,"test","this is test.") + assert_equal %Q|1.0.1 this is test.|, result end def test_label - @builder.label("label_test") - assert_equal %Q|\n
      \n" + buf end def cmd(lines, caption = nil) @@ -586,29 +592,32 @@ def doorquote(lines, ref) end def talk(lines) - puts %Q[
      ] + buf = "" + buf << %Q[
      \n] if @book.config["deprecated-blocklines"].nil? blocked_lines = split_paragraph(lines) - puts "#{blocked_lines.join("\n")}" + buf << "#{blocked_lines.join("\n")}\n" else - print '
      '
      -        puts "#{lines.join("\n")}"
      -        puts '
      ' + buf << '
      '
      +        buf << "#{lines.join("\n")}\n"
      +        buf << "
      \n" end - puts '
      ' + buf << "
      \n" + buf end def texequation(lines) - puts %Q[
      ] + buf << %Q[
      \n] if @book.config["mathml"] p = MathML::LaTeX::Parser.new(:symbol=>MathML::Symbol::CharacterReference) - puts p.parse(unescape_html(lines.join("\n")), true) + buf << p.parse(unescape_html(lines.join("\n")), true) << "\n else - print '
      '
      -        puts "#{lines.join("\n")}"
      -        puts '
      ' + buf << '
      '
      +        buf << "#{lines.join("\n")}\n"
      +        buf << "
      \n" end - puts '
      ' + buf << "
      \n" + buf end def handle_metric(str) From d95356577e154b8a06e83c2272b0333a868152b7 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 15 Aug 2014 23:22:05 +0900 Subject: [PATCH 076/214] fix markdownbuilder --- lib/review/markdownbuilder.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index 264bd51a8..3685e3523 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -21,21 +21,31 @@ def builder_init_file end private :builder_init_file - def puts(str) + def blank_reset @blank_seen = false - super end +# def puts(str) +# @blank_seen = false +# "#{str}\n" +# end + def blank - @output.puts unless @blank_seen + buf = "" + unless @blank_seen + buf = "\n" + end @blank_seen = true + buf end def headline(level, label, caption) - blank + buf = "" + buf << blank prefix = "#" * level - puts "#{prefix} #{caption}" - blank + buf << "#{prefix} #{caption}\n" + buf << "\n" + buf end def quote(lines) From b94288dd7c4db2d07707cd65e527f1b95f5079cb Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 15 Aug 2014 23:30:42 +0900 Subject: [PATCH 077/214] MARKDOWNBuilder: add test --- test/test_markdownbuilder.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_markdownbuilder.rb b/test/test_markdownbuilder.rb index 9fb953c6e..ccfe95f63 100644 --- a/test/test_markdownbuilder.rb +++ b/test/test_markdownbuilder.rb @@ -24,6 +24,12 @@ def setup @builder.bind(@compiler, @chapter, location) end + def test_quote + lines = ["foo", "bar", "","buz"] + result = @builder.quote(lines) + assert_equal %Q|\n> foobar\n> \n> buz\n\n|, @builder.raw_result + end + def test_inline_em assert_equal "test*foo*abc", @builder.compile_inline("test@{foo}abc") end From 04e6f5b81779342328f8a3792e9d631aece84de2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 00:30:28 +0900 Subject: [PATCH 078/214] HTMLBuilder: fix LF of bibpaper --- lib/review/htmlbuilder.rb | 2 +- test/test_htmlbuilder.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 396911823..c0710395a 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -956,7 +956,7 @@ def bibpaper_header(id, caption) buf = %Q() buf << "[#{@chapter.bibpaper(id).number}]" buf << %Q() - buf << " #{(caption)}" + buf << " #{(caption)}" << "\n" end def bibpaper_bibpaper(id, caption, lines) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 0fa3ad865..c70772862 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -470,7 +470,7 @@ def @chapter.bibpaper(id) end result = compile_blockelem("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib bold

      ab

      \n|, result + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, result end def test_bibpaper_normalized @@ -478,8 +478,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("sample=bib",1,"sample bib") end - @builder.bibpaper(["a", "b"], "sample=bib", "sample bib @{bold}") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, @builder.raw_result + result = compile_blockelem("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, result end def test_bibpaper_with_anchor @@ -488,7 +488,7 @@ def @chapter.bibpaper(id) end result = compile_blockelem("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib http://example.jp

      ab

      \n|, result + assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, result end =begin From ca603ab9ae74bea1e1e56bd05cb226cd158fb0f4 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 00:31:10 +0900 Subject: [PATCH 079/214] support MARKDOWNBuilder --- lib/review/markdownbuilder.rb | 147 +++++++++++++++++++++++----------- test/test_markdownbuilder.rb | 10 +-- 2 files changed, 104 insertions(+), 53 deletions(-) diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index 3685e3523..27fb630e3 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -44,69 +44,94 @@ def headline(level, label, caption) buf << blank prefix = "#" * level buf << "#{prefix} #{caption}\n" + blank_reset buf << "\n" buf end def quote(lines) - blank - puts split_paragraph(lines).map{|line| "> #{line}"}.join("\n> \n") - blank + buf = "" + buf << blank + buf << split_paragraph(lines).map{|line| "> #{line}"}.join("\n> \n") << "\n" + blank_reset + buf << "\n" + buf end def paragraph(lines) - puts lines.join - puts "\n" + buf = lines.join << "\n" + blank_reset + buf << "\n" + buf end def ul_begin - blank if @ul_indent == 0 + buf = "" + buf << blank if @ul_indent == 0 @ul_indent += 1 + buf end def ul_item(lines) - puts " " * (@ul_indent - 1) + "* " + "#{lines.join}" + blank_reset + " " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n" end def ul_item_begin(lines) - puts " " * (@ul_indent - 1) + "* " + "#{lines.join}" + blank_reset + " " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n" end def ul_item_end + "" end def ul_end + buf = "" @ul_indent -= 1 - blank if @ul_indent == 0 + buf << blank if @ul_indent == 0 + buf end def ol_begin - blank + buf = "" + buf << blank + buf end def ol_item(lines, num) - puts "#{num}. #{lines.join}" + buf = "" + buf << "#{num}. #{lines.join}" << "\n" + blank_reset + buf end def ol_end - blank + buf = "" + buf << blank + buf end def emlist(lines, caption = nil) - blank + buf = "" + buf << blank if caption - puts caption + buf << caption << "\n" end - puts "```" + buf << "```\n" + blank_reset lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts "```" - blank + buf << "```\n" + buf << blank + buf end def hr - puts "----" + buf << "----\n" + blank_reset + buf end def compile_href(url, label) @@ -140,30 +165,41 @@ def inline_tt(str) def image_image(id, caption, metric) - blank - puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" - blank + buf = "" + buf << blank + buf << "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" << "\n" + blank_reset + buf << blank + buf end def image_dummy(id, caption, lines) - puts lines.join + buf = "" + buf << lines.join << "\n" + blank_reset + buf end def inline_img(id) "#{I18n.t("image")}#{@chapter.image(id).number}" rescue KeyError error "unknown image: #{id}" - nofunc_text("[UnknownImage:#{id}]") + "[UnknownImage:#{id}]" end def indepimage(id, caption="", metric=nil) - blank - puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" - blank + buf = "" + buf << blank + buf << "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" << "\n" + blank_reset + buf << blank + buf end def pagebreak - puts "{pagebreak}" + buf = "" + buf << "{pagebreak}" << "\n" + buf end def image_ext @@ -171,14 +207,18 @@ def image_ext end def cmd(lines) - puts "```" + buf = "" + buf << "```" << "\n" + blank_reset lines.each do |line| - puts detab(line) + buf << detab(line) << "\n" end - puts "```" + buf << "```" << "\n" + buf end def table(lines, id = nil, caption = nil) + buf = "" rows = [] sepidx = nil lines.each_with_index do |line, idx| @@ -188,52 +228,58 @@ def table(lines, id = nil, caption = nil) sepidx ||= idx next end - rows.push line.strip.split(/\t+/).map {|s| s.sub(/\A\./, '') } + rows.push line.strip.split(/\t+/).map{|s| s.sub(/\A\./, '') } end rows = adjust_n_cols(rows) begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end - table_begin rows.first.size - return if rows.empty? + buf << table_begin(rows.first.size) + return buf if rows.empty? if sepidx sepidx.times do - tr rows.shift.map {|s| th(s) } + buf << tr(rows.shift.map{|s| th(s) }) end - table_border rows.first.size + buf << table_border(rows.first.size) rows.each do |cols| - tr cols.map {|s| td(s) } + buf << tr(cols.map{|s| td(s) }) end else rows.each do |cols| h, *cs = *cols - tr [th(h)] + cs.map {|s| td(s) } + tr([th(h)] + cs.map {|s| td(s) }) end end - table_end + buf << table_end + buf end def table_header(id, caption) + buf = "" if get_chap.nil? - puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] << "\n" else - puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] + buf << %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] << "\n" end - blank + blank_reset + buf << blank + buf end def table_begin(ncols) + "" end def tr(rows) - puts "|#{rows.join("|")}|" + "|#{rows.join("|")}|\n" end def table_border(ncols) - puts (0..ncols).map{"|"}.join(":--") + blank_reset + (0..ncols).map{"|"}.join(":--") + "\n" end def th(str) @@ -245,12 +291,17 @@ def td(str) end def table_end - blank + buf = "" + buf << blank + buf end def footnote(id, str) - puts "[^#{id}]: #{compile_inline(str)}" - blank + buf = "" + buf << "[^#{id}]: #{compile_inline(str)}" << "\n" + blank_reset + buf << blank + buf end def inline_fn(id) diff --git a/test/test_markdownbuilder.rb b/test/test_markdownbuilder.rb index 9ee1ebfb2..cf5cd6fe0 100644 --- a/test/test_markdownbuilder.rb +++ b/test/test_markdownbuilder.rb @@ -28,7 +28,7 @@ def setup def test_quote lines = ["foo", "bar", "","buz"] result = @builder.quote(lines) - assert_equal %Q|\n> foobar\n> \n> buz\n\n|, @builder.raw_result + assert_equal %Q|\n> foobar\n> \n> buz\n\n|, result end def test_inline_em @@ -59,12 +59,12 @@ def test_ul_nest1 end def test_cmd - @builder.cmd(["lineA","lineB"]) - assert_equal "```\nlineA\nlineB\n```\n", @builder.raw_result + result = @builder.cmd(["lineA","lineB"]) + assert_equal "```\nlineA\nlineB\n```\n", result end def test_table - @builder.table(["testA\ttestB","------------","contentA\tcontentB"]) - assert_equal "|testA|testB|\n|:--|:--|\n|contentA|contentB|\n\n", @builder.raw_result + result = @builder.table(["testA\ttestB","------------","contentA\tcontentB"]) + assert_equal "|testA|testB|\n|:--|:--|\n|contentA|contentB|\n\n", result end end From 4648be19990d96b99a9dff5edfa7f5c466bf27a0 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 00:49:25 +0900 Subject: [PATCH 080/214] TOPBuilder: fix LF --- lib/review/topbuilder.rb | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb index eaaf9193f..2276fc20b 100644 --- a/lib/review/topbuilder.rb +++ b/lib/review/topbuilder.rb @@ -106,20 +106,7 @@ def blank_reset @blank_seen = false end - def print(s) - @blank_seen = false - super - end - private :print - - def puts(s) - @blank_seen = false - super - end - private :puts - def blank - # @output.puts unless @blank_seen seen = @blank_seen @blank_seen = true unless seen @@ -190,7 +177,7 @@ def ul_begin def ul_item(lines) blank_reset - "●\t#{lines.join}" + "●\t#{lines.join}\n" end def ul_end @@ -227,6 +214,7 @@ def dd(lines) buf << "\t#{paragraph.gsub(/\n/, '')}\n" end blank_reset + buf end def dl_end @@ -243,6 +231,7 @@ def read(lines) buf << "◆→開始:#{@titles["lead"]}←◆\n" buf << split_paragraph(lines).join("\n") << "\n" buf << "◆→終了:#{@titles["lead"]}←◆\n" + blank_reset buf << blank buf end @@ -313,7 +302,7 @@ def emlist(lines, caption = nil) def emlistnum(lines, caption = nil) buf = "" - blank + buf << blank buf << "◆→開始:#{@titles["emlist"]}←◆\n" buf << "■#{caption}\n" unless caption.nil? _lines = [] @@ -587,7 +576,7 @@ def common_column_begin(type, caption) def common_column_end(type) buf = "" - buf << %Q[◆→終了:#{@titles["type"]}←◆] + buf << %Q[◆→終了:#{@titles[type]}←◆\n] blank_reset buf << blank buf From 3dbe788aaaa3b007d333f64953b739472c3fdfd5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 02:12:02 +0900 Subject: [PATCH 081/214] fix error --- lib/review/latexbuilder.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 309493499..abbc16592 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -109,7 +109,9 @@ def column_begin(level, label, caption) end def column_end(level) + buf = "" buf << "\\end{reviewcolumn}\n\n" + buf end def captionblock(type, lines, caption) From c37c261411ec6c1420339ee37413bc67a13527a2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 02:13:26 +0900 Subject: [PATCH 082/214] merge commit 7c3384897795d94a05ab42b9777427c1ba5dc4d1 --- lib/review/review.kpeg | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 0e8dcea95..d35cc5dd6 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -179,6 +179,7 @@ require 'review/node' definline :hd definline :href definline :recipe + definline :column definline :abbr definline :acronym From 118d51db7745a489c51c5129d924098366d22abe Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 02:14:18 +0900 Subject: [PATCH 083/214] add Node#to_json --- lib/review/node.rb | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index f0e40c8f5..18ff1577a 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -1,4 +1,6 @@ require 'core_extensions' +require 'json' + module ReVIEW class Node attr_accessor :content @@ -24,16 +26,21 @@ def to_s_by(meth) end end - def inspect + def to_json if content.kind_of? String - '"'+@content.gsub(/\"/,'\\"')+'"' + val = '"'+@content.gsub(/\"/,'\\"')+'"' elsif content == nil - "null" + val = "null" elsif !content.kind_of? Array - @content.inspect + val = @content.to_json else - "["+@content.map(&:inspect).join(",")+"]" + val = "["+@content.map(&:to_json).join(",")+"]" end + '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '","c":' + val + '}' + end + + def inspect + self.to_json end end @@ -46,6 +53,13 @@ def to_doc label = @label @compiler.compile_headline(@level, cmd, label, content_str) end + + def to_json + '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + %Q|"cmd":"#{@cmd.to_json}",|+ + %Q|"label":"#{@label.to_json}",|+ + '"c":' + @content.to_json + '}' + end end class ParagraphNode < Node @@ -82,6 +96,12 @@ def to_doc #content_str = super @compiler.compile_inline(@symbol, @content) end + + def to_json + '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + %Q|"symbol":"#{@symbol}",| + + '"c":[' + @content.map(&:to_json).join(",") + ']}' + end end class InlineElementContentNode < Node From 5de538fa23ba303a5836fc3f43cba8102d1c2fd5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 11:14:29 +0900 Subject: [PATCH 084/214] uncomment column test --- test/test_htmlbuilder.rb | 11 +++++------ test/test_latexbuilder.rb | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index c70772862..bfd9cad56 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -491,12 +491,11 @@ def @chapter.bibpaper(id) assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, result end -=begin + def column_helper(review) - #chap_singleton = class << @chapter; self; end - #chap_singleton.send(:define_method, :content) { review } - #@compiler.compile(@chapter).match(/\n(.+)<\/body>/m)[1] - compile_block(review) + chap_singleton = class << @chapter; self; end + chap_singleton.send(:define_method, :content) { review } + @compiler.compile(@chapter).match(/\n(.+)<\/body>/m)[1] end def test_column_1 @@ -584,7 +583,7 @@ def test_column_ref assert_equal expect, column_helper(review) end -=end + def test_ul src =<<-EOS diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 10095a989..6091f2d47 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -419,7 +419,6 @@ def @chapter.bibpaper(id) assert_equal %Q|[1] sample bib\n\\label{bib:samplebib}\n\n|, ret end -=begin def column_helper(review) chap_singleton = class << @chapter; self; end chap_singleton.send(:define_method, :content) { review } @@ -501,7 +500,6 @@ def test_column_3 end end -=end def test_ul src =<<-EOS * AAA From 0eb0899baf44ff61eaac16adc338de7bd43e8ba6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 11:15:58 +0900 Subject: [PATCH 085/214] support column --- lib/review/node.rb | 7 ++- lib/review/review.kpeg | 110 +++++++++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 45 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index 18ff1577a..10cd13323 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -189,10 +189,13 @@ def to_doc class DocumentNode < Node end - class TaggedSectionNode < Node + class ColumnNode < Node def to_doc - @compiler.compile_column(@content) + level = @level + label = @label + caption = @caption ? @caption.to_doc : nil + @compiler.compile_column(level, label, caption, @content) end end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index d35cc5dd6..4dd9a9187 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -38,9 +38,48 @@ require 'review/node' def convert_ast ast = @strategy.ast + ast = convert_column(ast) @strategy.output << ast.to_doc end + def flush_column(new_content) + if @current_column + new_content << @current_column + @current_column = nil + end + end + + def convert_column(ast) + @column_stack = [] + content = ast.content + new_content = [] + @current_content = new_content + content.each do |elem| + if elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc == "column" + flush_column(new_content) + @current_content = [] + @current_column = ReVIEW::ColumnNode.new(elem.compiler, elem.level, + elem.label, elem.content, @current_content) + next + elsif elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc =~ %r|^/| + cmd_name = elem.cmd.to_doc[1..-1] + if cmd_name != "column" + raise ReVIEW::CompileError, "#{cmd_name} is not opened." + end + flush_column(new_content) + @current_content = new_content + next + elsif elem.kind_of?(ReVIEW::HeadlineNode) && @current_column && elem.level <= @current_column.level + flush_column(new_content) + @current_content = new_content + end + @current_content << elem + end + flush_column(new_content) + ast.content = new_content + ast + end + def compile_text(text) @strategy.nofunc_text(text) end @@ -213,37 +252,26 @@ require 'review/node' def tagged_section_init - @tagged_section = [] + # noop end def open_tagged_section(tag, level, label, caption) - buf = "" - mid = "#{tag}_begin" - unless @strategy.respond_to?(mid) - error "strategy does not support tagged section: #{tag}" - return - end - @tagged_section.push [tag, level] - buf << @strategy.__send__(mid, level, label, caption) - buf + #noop end def close_tagged_section(tag, level) - buf = "" - mid = "#{tag}_end" - if @strategy.respond_to?(mid) - buf << @strategy.__send__(mid, level) - else - error "strategy does not support block op: #{mid}" - end - buf + # noop end def close_all_tagged_section - buf = "" - until @tagged_section.empty? - buf << close_tagged_section(* @tagged_section.pop) - end + # noop + end + + def compile_column(level, label, caption, content) + buf = "" + buf << @strategy.__send__("column_begin", level, label, caption) + buf << content.to_doc + buf << @strategy.__send__("column_end", level) buf end @@ -276,36 +304,32 @@ require 'review/node' caption ||= "" caption.strip! index = level - 1 - if tag - if tag !~ /\A\// - buf << close_current_tagged_section(level) - buf << open_tagged_section(tag, level, label, caption) - else - open_tag = tag[1..-1] - prev_tag_info = @tagged_section.pop - if !prev_tag_info || prev_tag_info.first != open_tag - raise ReVIEW::CompileError, "#{open_tag} is not opened." - end - buf << close_tagged_section(*prev_tag_info) - end - else + # if tag + # if tag !~ /\A\// + # buf << close_current_tagged_section(level) + # buf << open_tagged_section(tag, level, label, caption) + # else + # open_tag = tag[1..-1] + # prev_tag_info = @tagged_section.pop + # if !prev_tag_info || prev_tag_info.first != open_tag + # raise ReVIEW::CompileError, "#{open_tag} is not opened." + # end + # buf << close_tagged_section(*prev_tag_info) + # end + # else if @headline_indexs.size > (index + 1) @headline_indexs = @headline_indexs[0..index] end @headline_indexs[index] = 0 if @headline_indexs[index].nil? @headline_indexs[index] += 1 - buf << close_current_tagged_section(level) + # buf << close_current_tagged_section(level) buf << @strategy.headline(level, label, caption) - end + # end buf end def close_current_tagged_section(level) - buf = "" - while @tagged_section.last and @tagged_section.last[1] >= level - buf << close_tagged_section(* @tagged_section.pop) - end - buf + # noop end def comment(text) @@ -465,7 +489,7 @@ require 'review/node' %% dlist_element = ast DlistElementNode(compiler, text, content) %% bracket_arg = ast BracketArgNode(compiler, content) %% document = ast DocumentNode(compiler, content) -%% tagged_section = ast TaggedSectionNode(compiler, level, label, caption, content) +%% column = ast ColumnNode(compiler, level, label, caption, content) # %% dummy From 4bb92fb735eb8c082b2b367502b68c90effccd7a Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 11:16:15 +0900 Subject: [PATCH 086/214] LATEXBuilder: fix LF --- lib/review/latexbuilder.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index abbc16592..eb540cadd 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -48,6 +48,11 @@ def blank end private :blank + def blank_reset + @blank_needed = false + end + private :blank_reset + HEADLINE = { 1 => 'chapter', 2 => 'section', @@ -95,7 +100,12 @@ def nonum_end(level) end def column_begin(level, label, caption) - buf = "\n" + buf = "" + blank + if @blank_needed + buf << "\n" + blank_reset + end buf << "\\begin{reviewcolumn}\n" if label buf << "\\hypertarget{#{column_label(label)}}{}\n" @@ -106,11 +116,13 @@ def column_begin(level, label, caption) if level <= @book.config["toclevel"].to_i buf << "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{caption}}" << "\n" end + buf end def column_end(level) buf = "" - buf << "\\end{reviewcolumn}\n\n" + buf << "\\end{reviewcolumn}\n" + blank buf end From 23e16d6a32a4e967d192aa84a75b422b027720ce Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 16 Aug 2014 11:16:28 +0900 Subject: [PATCH 087/214] regenerate --- lib/review/compiler.rb | 147 ++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 61 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 3a33461e0..15b4b1ece 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -393,9 +393,48 @@ def do_compile def convert_ast ast = @strategy.ast + ast = convert_column(ast) @strategy.output << ast.to_doc end + def flush_column(new_content) + if @current_column + new_content << @current_column + @current_column = nil + end + end + + def convert_column(ast) + @column_stack = [] + content = ast.content + new_content = [] + @current_content = new_content + content.each do |elem| + if elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc == "column" + flush_column(new_content) + @current_content = [] + @current_column = ReVIEW::ColumnNode.new(elem.compiler, elem.level, + elem.label, elem.content, @current_content) + next + elsif elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc =~ %r|^/| + cmd_name = elem.cmd.to_doc[1..-1] + if cmd_name != "column" + raise ReVIEW::CompileError, "#{cmd_name} is not opened." + end + flush_column(new_content) + @current_content = new_content + next + elsif elem.kind_of?(ReVIEW::HeadlineNode) && @current_column && elem.level <= @current_column.level + flush_column(new_content) + @current_content = new_content + end + @current_content << elem + end + flush_column(new_content) + ast.content = new_content + ast + end + def compile_text(text) @strategy.nofunc_text(text) end @@ -534,6 +573,7 @@ def inline_defined?(name) definline :hd definline :href definline :recipe + definline :column definline :abbr definline :acronym @@ -567,37 +607,26 @@ def inline_defined?(name) def tagged_section_init - @tagged_section = [] + # noop end def open_tagged_section(tag, level, label, caption) - buf = "" - mid = "#{tag}_begin" - unless @strategy.respond_to?(mid) - error "strategy does not support tagged section: #{tag}" - return - end - @tagged_section.push [tag, level] - buf << @strategy.__send__(mid, level, label, caption) - buf + #noop end def close_tagged_section(tag, level) - buf = "" - mid = "#{tag}_end" - if @strategy.respond_to?(mid) - buf << @strategy.__send__(mid, level) - else - error "strategy does not support block op: #{mid}" - end - buf + # noop end def close_all_tagged_section - buf = "" - until @tagged_section.empty? - buf << close_tagged_section(* @tagged_section.pop) - end + # noop + end + + def compile_column(level, label, caption, content) + buf = "" + buf << @strategy.__send__("column_begin", level, label, caption) + buf << content.to_doc + buf << @strategy.__send__("column_end", level) buf end @@ -630,36 +659,32 @@ def compile_headline(level, tag, label, caption) caption ||= "" caption.strip! index = level - 1 - if tag - if tag !~ /\A\// - buf << close_current_tagged_section(level) - buf << open_tagged_section(tag, level, label, caption) - else - open_tag = tag[1..-1] - prev_tag_info = @tagged_section.pop - if !prev_tag_info || prev_tag_info.first != open_tag - raise ReVIEW::CompileError, "#{open_tag} is not opened." - end - buf << close_tagged_section(*prev_tag_info) - end - else + # if tag + # if tag !~ /\A\// + # buf << close_current_tagged_section(level) + # buf << open_tagged_section(tag, level, label, caption) + # else + # open_tag = tag[1..-1] + # prev_tag_info = @tagged_section.pop + # if !prev_tag_info || prev_tag_info.first != open_tag + # raise ReVIEW::CompileError, "#{open_tag} is not opened." + # end + # buf << close_tagged_section(*prev_tag_info) + # end + # else if @headline_indexs.size > (index + 1) @headline_indexs = @headline_indexs[0..index] end @headline_indexs[index] = 0 if @headline_indexs[index].nil? @headline_indexs[index] += 1 - buf << close_current_tagged_section(level) + # buf << close_current_tagged_section(level) buf << @strategy.headline(level, label, caption) - end + # end buf end def close_current_tagged_section(level) - buf = "" - while @tagged_section.last and @tagged_section.last[1] >= level - buf << close_tagged_section(* @tagged_section.pop) - end - buf + # noop end def comment(text) @@ -772,8 +797,8 @@ def compile_inline(op, args) else @strategy.__send__("inline_#{op}", *(args.map(&:to_doc))) end - rescue => err - error err.message +# rescue => err +# error err.message end def compile_paragraph(buf) @@ -832,6 +857,20 @@ def initialize(compiler, content) attr_reader :compiler attr_reader :content end + class ColumnNode < Node + def initialize(compiler, level, label, caption, content) + @compiler = compiler + @level = level + @label = label + @caption = caption + @content = content + end + attr_reader :compiler + attr_reader :level + attr_reader :label + attr_reader :caption + attr_reader :content + end class DlistNode < Node def initialize(compiler, content) @compiler = compiler @@ -942,20 +981,6 @@ def initialize(compiler, content) attr_reader :compiler attr_reader :content end - class TaggedSectionNode < Node - def initialize(compiler, level, label, caption, content) - @compiler = compiler - @level = level - @label = label - @caption = caption - @content = content - end - attr_reader :compiler - attr_reader :level - attr_reader :label - attr_reader :caption - attr_reader :content - end class TextNode < Node def initialize(compiler, content) @compiler = compiler @@ -993,6 +1018,9 @@ def brace(compiler, content) def bracket_arg(compiler, content) ::ReVIEW::BracketArgNode.new(compiler, content) end + def column(compiler, level, label, caption, content) + ::ReVIEW::ColumnNode.new(compiler, level, label, caption, content) + end def dlist(compiler, content) ::ReVIEW::DlistNode.new(compiler, content) end @@ -1029,9 +1057,6 @@ def singleline_comment(compiler, content) def singleline_content(compiler, content) ::ReVIEW::SinglelineContentNode.new(compiler, content) end - def tagged_section(compiler, level, label, caption, content) - ::ReVIEW::TaggedSectionNode.new(compiler, level, label, caption, content) - end def text(compiler, content) ::ReVIEW::TextNode.new(compiler, content) end From 34f69fd8619a23e22694770e731472ab75f11657 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 18 Aug 2014 02:05:35 +0900 Subject: [PATCH 088/214] Builder: argument of inline_* is more ther one --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 4dd9a9187..ffd70236b 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -438,7 +438,7 @@ require 'review/node' raise "strategy does not support inline op: @<#{op}>" end if !args - @strategy.__send__("inline_#{op}") + @strategy.__send__("inline_#{op}", "") else @strategy.__send__("inline_#{op}", *(args.map(&:to_doc))) end From 3e7732de093cff5e0285e731616969e50100e30a Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 18 Aug 2014 02:06:14 +0900 Subject: [PATCH 089/214] Builder: "\n" in raw string should be LF --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index ffd70236b..1a5dd6b08 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -453,7 +453,7 @@ require 'review/node' def compile_raw(builders, content) c = @strategy.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase if !builders || builders.include?(c) - content + content.gsub("\\n", "\n") else "" end From b2c6c9ea5f1cda7a0e833d8d3d0631035f100ef1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 18 Aug 2014 02:07:13 +0900 Subject: [PATCH 090/214] null BracketArg should be allowed --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 1a5dd6b08..32a7e4fdc 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -578,7 +578,7 @@ InlineElementContentText = ( "\\}" ~text(self, "}") | !InlineElement < /[^\r\n\\},]/> ~text(self,text) ) -BracketArg = "[" BracketArgContentInline+:content "]" ~bracket_arg(self, content) +BracketArg = "[" BracketArgContentInline*:content "]" ~bracket_arg(self, content) ## XXX '\' (excpet '\]' and '\\' ) => '\' is ??? BracketArgContentInline = ( InlineElement:c { c } From 74d24e7c710bb471b073edab986036cb2ffa3c14 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 18 Aug 2014 02:08:16 +0900 Subject: [PATCH 091/214] Revert "fix # of args" This reverts commit 7319a1da77b90c06a241a4dd59317a2fc2cbc9de. --- lib/review/latexbuilder.rb | 2 +- test/test_latexbuilder.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index eb540cadd..02166b03a 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -721,7 +721,7 @@ def inline_b(str) end # line break - def inline_br() + def inline_br(str) "\\\\\n" end diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 6091f2d47..c91c51f77 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -89,7 +89,7 @@ def test_href_mailto end def test_inline_br - ret = @builder.inline_br() + ret = @builder.inline_br("") assert_equal %Q|\\\\\n|, ret end From 8b05240c5ffc14f761fd60076e5b09d507945240 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 18 Aug 2014 03:39:12 +0900 Subject: [PATCH 092/214] remove escaped quotation --- lib/review/htmlbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index c0710395a..a9d7b981d 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -625,7 +625,7 @@ def handle_metric(str) return "width=\"#{($1.to_f * 100).round}%\"" else k, v = str.split('=', 2) - return %Q|#{k}=\"#{v.sub(/\A["']/, '').sub(/["']\Z/, '')}\"| + return %Q|#{k}=\"#{v.sub(/\A"/, '').sub(/\A["']/, '').sub(/"\Z/, '').sub(/["']\Z/, '')}\"| end end From 10b1d21e2e5a894a1c37af5f63af0b96b527ebf9 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 19 Aug 2014 11:39:21 +0900 Subject: [PATCH 093/214] compile_block should support multi blocks --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index cff04f787..380d96b1d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,7 +15,7 @@ def compile_document(str, force_to_s=true) def compile_block(str, force_to_s=true) @compiler.setup_parser(str) @compiler.tagged_section_init - @compiler.parse("Block") + @compiler.parse("Document") if force_to_s @compiler.result.to_doc else From 630ba8162856cb29d13f7297f246c70bdd55307f Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 19 Aug 2014 11:45:12 +0900 Subject: [PATCH 094/214] merge tests from master --- test/test_htmlbuilder.rb | 372 +++++++++++++++++++------------------- test/test_latexbuilder.rb | 361 ++++++++++++++++++------------------ 2 files changed, 355 insertions(+), 378 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index bfd9cad56..d5318250a 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/htmlbuilder' @@ -36,8 +35,8 @@ def test_xmlns_ops_prefix_epub2 end def test_headline_level1 - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|

      第1章 this is test.

      \n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|

      第1章 this is test.

      \n|, actual end def test_headline_level1_postdef @@ -46,8 +45,8 @@ def on_APPENDIX? true end end - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|

      付録1 this is test.

      \n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|

      付録1 this is test.

      \n|, actual end def test_headline_level2_postdef @@ -56,8 +55,8 @@ def on_APPENDIX? true end end - result = @builder.headline(2,"test","this is test.") - assert_equal %Q|\n

      1.1 this is test.

      \n|, result + actual = compile_block("=={test} this is test.\n") + assert_equal %Q|\n

      1.1 this is test.

      \n|, actual end def test_headline_level1_postdef_roman @@ -67,8 +66,8 @@ def on_APPENDIX? true end end - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|

      付録I this is test.

      \n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|

      付録I this is test.

      \n|, actual end def test_headline_level2_postdef_roman @@ -78,8 +77,8 @@ def on_APPENDIX? true end end - result = @builder.headline(2,"test","this is test.") - assert_equal %Q|\n

      I.1 this is test.

      \n|, result + actual = compile_block("=={test} this is test.\n") + assert_equal %Q|\n

      I.1 this is test.

      \n|, actual end def test_headline_level1_postdef_alpha @@ -89,8 +88,8 @@ def on_APPENDIX? true end end - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|

      付録A this is test.

      \n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|

      付録A this is test.

      \n|, actual end def test_headline_level2_postdef_alpha @@ -100,120 +99,115 @@ def on_APPENDIX? true end end - result = @builder.headline(2,"test","this is test.") - assert_equal %Q|\n

      A.1 this is test.

      \n|, result + actual = compile_block("=={test} this is test.\n") + assert_equal %Q|\n

      A.1 this is test.

      \n|, actual end def test_headline_level1_without_secno ReVIEW.book.config["secnolevel"] = 0 - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|

      this is test.

      \n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|

      this is test.

      \n|, actual end def test_headline_level1_with_tricky_id - result = @builder.headline(1,"123 あ_;","this is test.") - assert_equal %Q|

      第1章 this is test.

      \n|, result + actual = compile_block("={123 あ_;} this is test.\n") + assert_equal %Q|

      第1章 this is test.

      \n|, actual end def test_headline_level1_with_inlinetag - result = compile_headline("={test} this @{is} test.<&\">\n") - assert_equal %Q|

      第1章 this is test.<&">

      \n|, result + actual = compile_block("={test} this @{is} test.<&\">\n") + assert_equal %Q|

      第1章 this is test.<&">

      \n|, actual end def test_headline_level2 - result = @builder.headline(2,"test","this is test.") - assert_equal %Q|\n

      1.1 this is test.

      \n|, result + actual = compile_block("=={test} this is test.\n") + assert_equal %Q|\n

      1.1 this is test.

      \n|, actual end def test_headline_level3 - result = @builder.headline(3,"test","this is test.") - assert_equal %Q|\n

      this is test.

      \n|, result + actual = compile_block("==={test} this is test.\n") + assert_equal %Q|\n

      this is test.

      \n|, actual end def test_headline_level3_with_secno ReVIEW.book.config["secnolevel"] = 3 - result = @builder.headline(3,"test","this is test.") - assert_equal %Q|\n

      1.0.1 this is test.

      \n|, result + actual = compile_block("==={test} this is test.\n") + assert_equal %Q|\n

      1.0.1 this is test.

      \n|, actual end def test_label - result = @builder.label("label_test") - assert_equal %Q|\n|, result + actual = compile_block("//label[label_test]\n") + assert_equal %Q|\n|, actual end def test_label_with_tricky_id - result = @builder.label("123 あ_;") - assert_equal %Q|\n|, result + actual = compile_block("//label[123 あ_;]\n") + assert_equal %Q|\n|, actual end def test_href - result = @builder.compile_href("http://github.com", "GitHub") - assert_equal %Q|GitHub|, result + actual = compile_inline("@{http://github.com,GitHub}") + assert_equal %Q|GitHub|, actual end def test_href_without_label - result = compile_inline("@{http://github.com}") - assert_equal %Q|http://github.com|, result + actual = compile_inline("@{http://github.com}") + assert_equal %Q|http://github.com|, actual end def test_inline_href - result = compile_inline("@{http://github.com, Git\\,Hub}") - assert_equal %Q|Git,Hub|, result - end - - def test_inline_href_without_label - result = @builder.inline_href("http://github.com") - assert_equal %Q|http://github.com|, result + actual = compile_inline("@{http://github.com,Git\\,Hub}") + assert_equal %Q|Git,Hub|, actual end def test_inline_raw - result = @builder.inline_raw("@{inline}") - assert_equal %Q|@{inline}|, result + actual = compile_inline("@{@{inline\\}}") + assert_equal %Q|@{inline}|, actual end def test_inline_in_table - result = @builder.table(["1\t2", "------------", "3\t4<>&"]) - assert_equal %Q|
      \n\n\n\n
      12
      34<>&
      \n
      \n|, result + actual = compile_block("//table{\n@{1}\t@{2}\n------------\n@{3}\t@{4}<>&\n//}\n") + assert_equal %Q|
      \n\n\n\n
      12
      34<>&
      \n
      \n|, actual end def test_inline_br - result = @builder.inline_br("") - assert_equal %Q|
      |, result + actual = compile_inline("@
      {}") + assert_equal %Q|
      |, actual end def test_inline_i - result = compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, result + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, actual end def test_inline_i_and_escape - result = compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test inline<&;\\ test test2|, result + actual = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test inline<&;\\ test test2|, actual end def test_inline_b - result = compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, result + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, actual end def test_inline_b_and_escape - result = compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test inline<&;\\ test test2|, result + actual = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test inline<&;\\ test test2|, actual end def test_inline_tt - result = compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, result + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, actual end def test_inline_tti - result = compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, result + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, actual end def test_inline_ttb - result = compile_inline("test @{inline test} test2") - assert_equal %Q|test inline test test2|, result + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test inline test test2|, actual end def test_inline_hd_chap @@ -223,32 +217,32 @@ def @chapter.headline_index end @config["secnolevel"] = 2 - ret = compile_inline("test @{chap1|test} test2") - assert_equal %Q|test 「te_st」 test2|, ret + actual = compile_inline("test @{chap1|test} test2") + assert_equal %Q|test 「te_st」 test2|, actual @config["secnolevel"] = 3 - ret = compile_inline("test @{chap1|test} test2") - assert_equal %Q|test 「1.1.1 te_st」 test2|, ret + actual = compile_inline("test @{chap1|test} test2") + assert_equal %Q|test 「1.1.1 te_st」 test2|, actual end def test_inline_uchar - result = compile_inline("test @{2460} test2") - assert_equal %Q|test ① test2|, result + actual = compile_inline("test @{2460} test2") + assert_equal %Q|test ① test2|, actual end def test_inline_ruby - result = compile_inline("@{粗雑,クルード}と思われているなら@{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。") - assert_equal "粗雑クルードと思われているなら繊細テクニカルにやり、繊細と思われているなら粗雑にやる。", result + actual = compile_inline("@{粗雑,クルード}と思われているなら@{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。") + assert_equal "粗雑クルードと思われているなら繊細テクニカルにやり、繊細と思われているなら粗雑にやる。", actual end def test_inline_ruby_comma - result = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") - assert_equal "foo, bar, buzフー・バー・バズ", result + actual = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") + assert_equal "foo, bar, buzフー・バー・バズ", actual end def test_inline_ref - result = compile_inline("@{外部参照<>&}") - assert_equal %Q|「●● 外部参照<>&」|, result + actual = compile_inline("@{外部参照<>&}") + assert_equal %Q|「●● 外部参照<>&」|, actual end def test_inline_mathml @@ -259,39 +253,35 @@ def test_inline_mathml return true end @config["mathml"] = true - ret = @builder.compile_inline("@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}") + actual = compile_inline("@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}") @config["mathml"] = nil - assert_equal "-b±b2-4ac2a", ret + assert_equal "-b±b2-4ac2a", actual end def test_quote - lines = ["foo", "bar", "","buz"] - result = @builder.quote(lines) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, result + actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|

      foobar

      \n

      buz

      \n|, actual end def test_memo - result = compile_blockelem("//memo[this is @{test}<&>_]{\ntest1\n\ntest2\n//}\n") - assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test<i>2</i>

      \n
      \n|, result + actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}\n") + assert_equal %Q|
      \n

      this is test<&>_

      \n

      test1

      \n

      test2

      \n
      \n|, actual end - def test_noindent - result = "" @builder.noindent - result << @builder.paragraph(["foo", "bar"]) - result << @builder.paragraph(["foo2", "bar2"]) - assert_equal %Q|

      foobar

      \n

      foo2bar2

      \n|, result + actual = compile_block("foo\nbar\n\nfoo2\nbar2\n") + assert_equal %Q|

      foobar

      \n

      foo2bar2

      \n|, actual end def test_flushright - result = @builder.flushright(["foo", "bar", "", "buz"]) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, result + actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|

      foobar

      \n

      buz

      \n|, actual end def test_centering - result = @builder.centering(["foo", "bar", "", "buz"]) - assert_equal %Q|

      foobar

      \n

      buz

      \n|, result + actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|

      foobar

      \n

      buz

      \n|, actual end def test_image @@ -301,8 +291,8 @@ def @chapter.image(id) item end - result = @builder.image_image("sampleimg","sample photo",nil) - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result + actual = compile_block("//image[sampleimg][sample photo]{\n//}\n") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, actual end def test_image_with_metric @@ -312,8 +302,8 @@ def @chapter.image(id) item end - result = @builder.image_image("sampleimg","sample photo","scale=1.2") - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result + actual = compile_block("//image[sampleimg][sample photo][scale=1.2]{\n//}\n") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, actual end def test_image_with_metric2 @@ -322,8 +312,9 @@ def @chapter.image(id) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end - result = @builder.image_image("sampleimg","sample photo","scale=1.2,html::class=sample,latex::ignore=params") - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result + + actual = compile_block("//image[sampleimg][sample photo][scale=1.2,html::class=sample,latex::ignore=params]{\n//}\n") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, actual end def test_image_with_tricky_id @@ -333,8 +324,8 @@ def @chapter.image(id) item end - result = @builder.image_image("123 あ_;","sample photo",nil) - assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, result + actual = compile_block("//image[123 あ_;][sample photo]{\n//}\n") + assert_equal %Q|
      \nsample photo\n

      \n図1.1: sample photo\n

      \n
      \n|, actual end def test_indepimage @@ -344,8 +335,8 @@ def @chapter.image(id) item end - result = @builder.indepimage("sampleimg","sample photo",nil) - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result + actual = compile_block("//indepimage[sampleimg][sample photo]\n") + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, actual end def test_indepimage_without_caption @@ -355,8 +346,8 @@ def @chapter.image(id) item end - result = @builder.indepimage("sampleimg",nil,nil) - assert_equal %Q|
      \n\n
      \n|, result + actual = compile_block("//indepimage[sampleimg]\n") + assert_equal %Q|
      \n\n
      \n|, actual end def test_indepimage_with_metric @@ -366,8 +357,8 @@ def @chapter.image(id) item end - result = @builder.indepimage("sampleimg","sample photo","scale=1.2") - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result + actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2]\n") + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, actual end def test_indepimage_with_metric2 @@ -377,8 +368,8 @@ def @chapter.image(id) item end - result = @builder.indepimage("sampleimg","sample photo","scale=1.2, html::class=\"sample\",latex::ignore=params") - assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, result + actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2, html::class=\"sample\",latex::ignore=params]\n") + assert_equal %Q|
      \nsample photo\n

      \n図: sample photo\n

      \n
      \n|, actual end def test_indepimage_without_caption_but_with_metric @@ -388,17 +379,16 @@ def @chapter.image(id) item end - result = @builder.indepimage("sampleimg",nil,"scale=1.2") - assert_equal %Q|
      \n\n
      \n|, result + actual = compile_block("//indepimage[sampleimg][][scale=1.2]\n") + assert_equal %Q|
      \n\n
      \n|, actual end def test_list def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end - result = compile_blockelem("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") - - assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest2\n
      \n
      \n|, result + actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") + assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest2\n
      \n
      \n|, actual end def test_list_pygments @@ -410,42 +400,41 @@ def @chapter.list(id) rescue LoadError return true end - @book.config["pygments"] = true - result = @builder.list(["test1", "test1.5", "", "test2"], "samplelist", "this is @{test}<&>_") + ReVIEW.book.config["pygments"] = true + actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") - assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, result + assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, actual end - def test_emlist - result = @builder.emlist(["lineA","lineB"]) - assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, result + actual = compile_block("//emlist{\nlineA\nlineB\n//}\n") + assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, actual end def test_emlist_caption - result = @builder.emlist(["lineA","lineB"],"cap1") - assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, result + actual = compile_block("//emlist[cap1]{\nlineA\nlineB\n//}\n") + assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, actual end def test_emlist_with_tab - result = @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) - assert_equal %Q|
      \n
              lineA\n                lineB\n        lineC\n
      \n
      \n|, result + actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n") + assert_equal %Q|
      \n
              lineA\n                lineB\n        lineC\n
      \n
      \n|, actual end def test_emlist_with_4tab - @builder.instance_eval{@tabwidth=4} - result = @builder.emlist(["\tlineA","\t\tlineB","\tlineC"]) - assert_equal %Q|
      \n
          lineA\n        lineB\n    lineC\n
      \n
      \n|, result + @config["tabwidth"] = 4 + actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n") + assert_equal %Q|
      \n
          lineA\n        lineB\n    lineC\n
      \n
      \n|, actual end def test_cmd - result = @builder.cmd(["lineA","lineB"]) - assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, result + actual = compile_block("//cmd{\nlineA\nlineB\n//}\n") + assert_equal %Q|
      \n
      lineA\nlineB\n
      \n
      \n|, actual end def test_cmd_caption - result = @builder.cmd(["lineA","lineB"], "cap1") - assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, result + actual = compile_block("//cmd[cap1]{\nlineA\nlineB\n//}\n") + assert_equal %Q|
      \n

      cap1

      \n
      lineA\nlineB\n
      \n
      \n|, actual end def test_bib @@ -453,7 +442,7 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - assert_equal %Q|[1]|, @builder.inline_bib("samplebib") + assert_equal %Q|[1]|, compile_inline("@{samplebib}") end def test_bib_noramlized @@ -461,7 +450,7 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("sampleb=ib",1,"sample bib") end - assert_equal %Q|[1]|, @builder.inline_bib("sample=bib") + assert_equal %Q|[1]|, compile_inline("@{sample=bib}") end def test_bibpaper @@ -469,8 +458,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - result = compile_blockelem("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, result + actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, actual end def test_bibpaper_normalized @@ -478,8 +467,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("sample=bib",1,"sample bib") end - result = compile_blockelem("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, result + actual = compile_block("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, actual end def test_bibpaper_with_anchor @@ -487,11 +476,10 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - result = compile_blockelem("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, result + actual = compile_block("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") + assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, actual end - def column_helper(review) chap_singleton = class << @chapter; self; end chap_singleton.send(:define_method, :content) { review } @@ -510,7 +498,7 @@ def test_column_1 ===[/column] EOS - expect =<<-EOS + expected =<<-EOS

      prev column

      @@ -522,7 +510,7 @@ def test_column_1

      inside column

      EOS - assert_equal expect, column_helper(review) + assert_equal expected, column_helper(review) end def test_column_2 @@ -533,7 +521,7 @@ def test_column_2 === next level EOS - expect =<<-EOS + expected =<<-EOS

      test

      @@ -543,10 +531,9 @@ def test_column_2

      next level

      EOS - assert_equal expect, column_helper(review) + assert_equal expected, column_helper(review) end - def test_column_3 review =<<-EOS ===[column] test @@ -570,7 +557,7 @@ def test_column_ref this is @{foo}. EOS - expect =<<-EOS + expected =<<-EOS

      test

      @@ -581,7 +568,7 @@ def test_column_ref

      this is test.

      EOS - assert_equal expect, column_helper(review) + assert_equal expected, column_helper(review) end @@ -590,8 +577,9 @@ def test_ul * AAA * BBB EOS - expect = "
        \n
      • AAA
      • \n
      • BBB
      • \n
      \n" - ul_helper(src, expect) + expected = "
        \n
      • AAA
      • \n
      • BBB
      • \n
      \n" + actual = compile_block(src) + assert_equal expected, actual end def test_ul_cont @@ -601,18 +589,18 @@ def test_ul_cont * BBB -BB EOS - expect = "
        \n
      • AAA-AA
      • \n
      • BBB-BB
      • \n
      \n" - ul_helper(src, expect) + expected = "
        \n
      • AAA-AA
      • \n
      • BBB-BB
      • \n
      \n" + actual = compile_block(src) + assert_equal expected, actual end - def test_ul_nest1 src =<<-EOS * AAA ** AA EOS - expect =<<-EOS + expected =<<-EOS
      • AAA
        • AA
        • @@ -620,10 +608,10 @@ def test_ul_nest1
        EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end - def test_ul_nest2 src =<<-EOS * AAA @@ -632,7 +620,7 @@ def test_ul_nest2 ** BB EOS - expect =<<-EOS + expected =<<-EOS
        • AAA
          • AA
          • @@ -644,7 +632,8 @@ def test_ul_nest2
          EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_nest3 @@ -655,7 +644,7 @@ def test_ul_nest3 ** BB EOS - expect =<<-EOS + expected =<<-EOS
            • AAA
            • @@ -668,7 +657,8 @@ def test_ul_nest3
            EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_nest4 @@ -680,7 +670,7 @@ def test_ul_nest4 ** BB EOS - expect =<<-EOS + expected =<<-EOS
            • A
              • AA
                  @@ -695,7 +685,8 @@ def test_ul_nest4
                EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_nest5 @@ -707,7 +698,7 @@ def test_ul_nest5 ** BB EOS - expect =<<-EOS + expected =<<-EOS
                • A
                  • AA
                      @@ -725,7 +716,8 @@ def test_ul_nest5
                    EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ol @@ -734,87 +726,87 @@ def test_ol 3. BBB EOS - expect =<<-EOS + expected =<<-EOS
                    1. AAA
                    2. BBB
                    EOS - ol_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_inline_raw0 - assert_equal "normal", @builder.inline_raw("normal") + assert_equal "normal", compile_inline("@{normal}") end def test_inline_raw1 - assert_equal "body", @builder.inline_raw("|html|body") + assert_equal "body", compile_inline("@{|html|body}") end def test_inline_raw2 - assert_equal "body", @builder.inline_raw("|html, latex|body") + assert_equal "body", compile_inline("@{|html, latex|body}") end def test_inline_raw3 - assert_equal "", @builder.inline_raw("|idgxml, latex|body") + assert_equal "", compile_inline("@{|idgxml, latex|body}") end def test_inline_raw4 - assert_equal "|html body", @builder.inline_raw("|html body") + assert_equal "|html body", compile_inline("@{|html body}") end def test_inline_raw5 - assert_equal "nor\nmal", @builder.inline_raw("|html|nor\\nmal") + assert_equal "nor\nmal", compile_inline("@{|html|nor\\nmal}") end def test_block_raw0 - result = @builder.raw("<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, result + actual = compile_block("//raw[<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw1 - result = @builder.raw("|html|<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, result + actual = compile_block("//raw[|html|<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw2 - result = @builder.raw("|html, latex|<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, result + actual = compile_block("//raw[|html, latex|<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw3 - result = @builder.raw("|latex, idgxml|<>!\"\\n& ") - expect = '' - assert_equal expect.chomp, result + actual = compile_block("//raw[|latex, idgxml|<>!\"\\n& ]\n") + expected = '' + assert_equal expected, actual end def test_block_raw4 - result = @builder.raw("|html <>!\"\\n& ") - expect = %Q(|html <>!\"\n& ) - assert_equal expect.chomp, result + actual = compile_block("//raw[|html <>!\"\\n& ]\n") + expected = %Q(|html <>!\"\n& ) + assert_equal expected, actual end def test_inline_fn fn = Book::FootnoteIndex.parse(['//footnote[foo][bar\\a\\$buz]']) @chapter.instance_eval{@footnote_index=fn} - result = @builder.footnote("foo",'bar\\a\\$buz') - expect =<<-'EOS' + actual = compile_block("//footnote[foo][bar\\a\\$buz]\n") + expected =<<-'EOS'

                    [*1] bar\a\$buz

                    EOS - assert_equal expect, result + assert_equal expected, actual end def test_inline_fn_with_tricky_id fn = Book::FootnoteIndex.parse(['//footnote[123 あ_;][bar\\a\\$buz]']) @chapter.instance_eval{@footnote_index=fn} - result = @builder.footnote("123 あ_;",'bar\\a\\$buz') - expect =<<-'EOS' + actual = compile_block("//footnote[123 あ_;][bar\\a\\$buz]\n") + expected =<<-'EOS'

                    [*1] bar\a\$buz

                    EOS - assert_equal expect, result + assert_equal expected, actual end - end diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index c91c51f77..b7007c18c 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/latexbuilder' @@ -26,171 +25,160 @@ def setup end def test_headline_level1 - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|\\chapter{this is test.}\n\\label{chap:chap1}\n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|\\chapter{this is test.}\n\\label{chap:chap1}\n|, actual end - def test_headline_level1_without_secno @config["secnolevel"] = 0 - result = @builder.headline(1,"test","this is test.") - assert_equal %Q|\\chapter*{this is test.}\n\\addcontentsline{toc}{chapter}{this is test.}\n\\label{chap:chap1}\n|, result + actual = compile_block("={test} this is test.\n") + assert_equal %Q|\\chapter*{this is test.}\n\\addcontentsline{toc}{chapter}{this is test.}\n\\label{chap:chap1}\n|, actual end def test_headline_level1_with_inlinetag - result = compile_headline("={test} this @{is} test.<&\"_>\n") - assert_equal %Q|\\chapter{this \\textbf{is} test.\\textless{}\\&\"\\textunderscore{}\\textgreater{}}\n\\label{chap:chap1}\n|, result + actual = compile_block("={test} this @{is} test.<&\"_>\n") + assert_equal %Q|\\chapter{this \\textbf{is} test.\\textless{}\\&\"\\textunderscore{}\\textgreater{}}\n\\label{chap:chap1}\n|, actual end def test_headline_level2 - result = compile_headline("=={test} this is test.\n") - assert_equal %Q|\\section{this is test.}\n\\label{sec:1-1}\n|, result + actual = compile_block("=={test} this is test.\n") + assert_equal %Q|\\section{this is test.}\n\\label{sec:1-1}\n|, actual end def test_headline_level3 - result = @builder.headline(3,"test","this is test.") - assert_equal %Q|\\subsection*{this is test.}\n\\label{sec:1-0-1}\n|, result + actual = compile_block("==={test} this is test.\n") + assert_equal %Q|\\subsection*{this is test.}\n\\label{sec:1-0-1}\n|, actual end + def test_headline_level3_with_secno @config["secnolevel"] = 3 - result = @builder.headline(3,"test","this is test.") - assert_equal %Q|\\subsection{this is test.}\n\\label{sec:1-0-1}\n|, result + actual = compile_block("==={test} this is test.\n") + assert_equal %Q|\\subsection{this is test.}\n\\label{sec:1-0-1}\n|, actual end def test_label - result = @builder.label("label_test") - assert_equal %Q|\\label{label_test}\n|, result + actual = compile_block("//label[label_test]\n") + assert_equal %Q|\\label{label_test}\n|, actual end def test_href - ret = compile_inline('@{http://github.com,GitHub}') - assert_equal %Q|\\href{http://github.com}{GitHub}|, ret + actual = compile_inline("@{http://github.com,GitHub}") + assert_equal %Q|\\href{http://github.com}{GitHub}|, actual end def test_inline_href - ret = compile_inline('@{http://github.com,Git\\,Hub}') - assert_equal %Q|\\href{http://github.com}{Git,Hub}|, ret + actual = compile_inline('@{http://github.com,Git\\,Hub}') + assert_equal %Q|\\href{http://github.com}{Git,Hub}|, actual end def test_href_without_label - ret = compile_inline('@{http://github.com}') - assert_equal %Q|\\url{http://github.com}|, ret + actual = compile_inline('@{http://github.com}') + assert_equal %Q|\\url{http://github.com}|, actual end def test_href_with_underscore - ret = compile_inline('@{http://example.com/aaa/bbb, AAA_BBB}') - assert_equal %Q|\\href{http://example.com/aaa/bbb}{AAA\\textunderscore{}BBB}|, ret + actual = compile_inline('@{http://example.com/aaa/bbb, AAA_BBB}') + assert_equal %Q|\\href{http://example.com/aaa/bbb}{AAA\\textunderscore{}BBB}|, actual end def test_href_mailto - ret = compile_inline('@{mailto:takahashim@example.com, takahashim@example.com}') - assert_equal %Q|\\href{mailto:takahashim@example.com}{takahashim@example.com}|, ret + actual = compile_inline('@{mailto:takahashim@example.com, takahashim@example.com}') + assert_equal %Q|\\href{mailto:takahashim@example.com}{takahashim@example.com}|, actual end def test_inline_br - ret = @builder.inline_br("") - assert_equal %Q|\\\\\n|, ret + actual = compile_inline("@
                    {}") + assert_equal %Q|\\\\\n|, actual end def test_inline_br_with_other_strings - ret = compile_inline("abc@
                    {}def") - assert_equal %Q|abc\\\\\ndef|, ret - end - - def test_inline_u - ret = compile_inline("abc@{def}ghi") - assert_equal %Q|abc\\Underline{def}ghi|, ret + actual = compile_inline("abc@
                    {}def") + assert_equal %Q|abc\\\\\ndef|, actual end def test_inline_i - ret = compile_inline("abc@{def}ghi") - assert_equal %Q|abc\\textit{def}ghi|, ret + actual = compile_inline("abc@{def}ghi") + assert_equal %Q|abc\\textit{def}ghi|, actual end def test_inline_i_and_escape - ret = compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test \\textit{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, ret + actual = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test \\textit{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, actual end def test_inline_dtp - ret = compile_inline("abc@{def}ghi") - assert_equal %Q|abcghi|, ret + actual = compile_inline("abc@{def}ghi") + assert_equal %Q|abcghi|, actual end def test_inline_code - ret = compile_inline("abc@{def}ghi") - assert_equal %Q|abc\\texttt{def}ghi|, ret + actual = compile_inline("abc@{def}ghi") + assert_equal %Q|abc\\texttt{def}ghi|, actual end def test_inline_raw - ret = compile_inline("@{@{inline!$%\\}}") - assert_equal "@{inline!$%}", ret + actual = compile_inline("@{@{inline!$%\\}}") + assert_equal "@{inline!$%}", actual end def test_inline_sup - ret = compile_inline("abc@{def}") - assert_equal %Q|abc\\textsuperscript{def}|, ret - + actual = compile_inline("abc@{def}") + assert_equal %Q|abc\\textsuperscript{def}|, actual end def test_inline_sub - ret = compile_inline("abc@{def}") - assert_equal %Q|abc\\textsubscript{def}|, ret + actual = compile_inline("abc@{def}") + assert_equal %Q|abc\\textsubscript{def}|, actual end def test_inline_b - ret = compile_inline("abc@{def}") - assert_equal %Q|abc\\textbf{def}|, ret - end - - def test_escape - ret = compile_inline("test \\ test2") - assert_equal %Q|test \\reviewbackslash{} test2|, ret + actual = compile_inline("abc@{def}") + assert_equal %Q|abc\\textbf{def}|, actual end def test_inline_b_and_escape - ret = compile_inline("test @{inline<&;\\ test} test2") - assert_equal %Q|test \\textbf{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, ret + actual = compile_inline("test @{inline<&;\\ test} test2") + assert_equal %Q|test \\textbf{inline\\textless{}\\&;\\reviewbackslash{} test} test2|, actual end def test_inline_em - ret = compile_inline("abc@{def}") - assert_equal %Q|abc\\reviewem{def}|, ret + actual = compile_inline("abc@{def}") + assert_equal %Q|abc\\reviewem{def}|, actual end def test_inline_strong - ret = compile_inline("abc@{def}") - assert_equal %Q|abc\\reviewstrong{def}|, ret + actual = compile_inline("abc@{def}") + assert_equal %Q|abc\\reviewstrong{def}|, actual end def test_inline_u - ret = compile_inline("abc@{def}ghi") - assert_equal %Q|abc\\Underline{def}ghi|, ret + actual = compile_inline("abc@{def}ghi") + assert_equal %Q|abc\\Underline{def}ghi|, actual end def test_inline_m - ret = compile_inline("abc@{\\alpha^n = \inf < 2}ghi") - assert_equal "abc $\\alpha^n = inf < 2$ ghi", ret + actual = compile_inline("abc@{\\alpha^n = \inf < 2}ghi") + assert_equal "abc $\\alpha^n = inf < 2$ ghi", actual end def test_inline_tt - ret = compile_inline("test @{inline test} test2") - assert_equal %Q|test \\texttt{inline test} test2|, ret + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test \\texttt{inline test} test2|, actual end def test_inline_tt_endash - ret = compile_inline("test @{in-line --test ---foo ----bar -----buz} --test2") - assert_equal %Q|test \\texttt{in{-}line {-}{-}test {-}{-}{-}foo {-}{-}{-}{-}bar {-}{-}{-}{-}{-}buz} {-}{-}test2|, ret + actual = compile_inline("test @{in-line --test ---foo ----bar -----buz} --test2") + assert_equal %Q|test \\texttt{in{-}line {-}{-}test {-}{-}{-}foo {-}{-}{-}{-}bar {-}{-}{-}{-}{-}buz} {-}{-}test2|, actual end def test_inline_tti - ret = compile_inline("test @{inline test} test2") - assert_equal %Q|test \\texttt{\\textit{inline test}} test2|, ret + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test \\texttt{\\textit{inline test}} test2|, actual end def test_inline_ttb - ret = compile_inline("test @{inline test} test2") - assert_equal %Q|test \\texttt{\\textbf{inline test}} test2|, ret + actual = compile_inline("test @{inline test} test2") + assert_equal %Q|test \\texttt{\\textbf{inline test}} test2|, actual end def test_inline_hd_chap @@ -200,107 +188,94 @@ def @chapter.headline_index end @config["secnolevel"] = 3 - ret = compile_inline("test @{chap1|test} test2") - assert_equal %Q|test 「1.1.1 te\\textunderscore{}st」 test2|, ret + actual = compile_inline("test @{chap1|test} test2") + assert_equal %Q|test 「1.1.1 te\\textunderscore{}st」 test2|, actual end def test_inline_ruby_comma - ret = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") - assert_equal "\\ruby{foo, bar, buz}{フー・バー・バズ}", ret + actual = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") + assert_equal "\\ruby{foo, bar, buz}{フー・バー・バズ}", actual end def test_inline_uchar - ret = compile_inline("test @{2460} test2") - assert_equal %Q|test \\UTF{2460} test2|, ret + actual = compile_inline("test @{2460} test2") + assert_equal %Q|test \\UTF{2460} test2|, actual end def test_inline_idx - ret = compile_inline("@{__TEST%$}, @{__TEST%$}") - assert_equal %Q|\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}\\index{__TEST%$}, \\index{__TEST%$}|, ret + actual = compile_inline("@{__TEST%$}, @{__TEST%$}") + assert_equal %Q|\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}\\index{__TEST%$}, \\index{__TEST%$}|, actual end def test_jis_x_0201_kana - ret = compile_inline("foo・カンジ、テスト") - assert_equal %Q|foo\\aj半角{・}\\aj半角{カ}\\aj半角{ン}\\aj半角{シ}\\aj半角{゛}\\aj半角{、}テスト|, ret + actual = compile_inline("foo・カンジ、テスト") + assert_equal %Q|foo\\aj半角{・}\\aj半角{カ}\\aj半角{ン}\\aj半角{シ}\\aj半角{゛}\\aj半角{、}テスト|, actual end def test_dlist - ret = compile_block("\n: foo\n foo.\n bar.\n") - assert_equal %Q|\n\\begin{description}\n\\item[foo] \\mbox{} \\\\\nfoo.\nbar.\n\\end{description}\n|, ret + actual = compile_block(": foo\n foo.\n bar.\n") + assert_equal %Q|\n\\begin{description}\n\\item[foo] \\mbox{} \\\\\nfoo.bar.\n\\end{description}\n|, actual end def test_dlist_with_bracket - ret = @builder.dl_begin - ret << @builder.dt("foo[bar]") - ret << @builder.dd(["foo.\n", "bar.\n"]) - ret << @builder.dl_end - assert_equal %Q|\n\\begin{description}\n\\item[foo\\lbrack{}bar\\rbrack{}] \\mbox{} \\\\\nfoo.\nbar.\n\\end{description}\n|, ret + actual = compile_block(": foo[bar]\n foo.\n bar.\n") + assert_equal %Q|\n\\begin{description}\n\\item[foo\\lbrack{}bar\\rbrack{}] \\mbox{} \\\\\nfoo.bar.\n\\end{description}\n|, actual end def test_cmd - lines = ["foo", "bar", "","buz"] - ret = @builder.cmd(lines) - assert_equal %Q|\n\\begin{reviewcmd}\nfoo\nbar\n\nbuz\n\\end{reviewcmd}\n|, ret + actual = compile_block("//cmd{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\begin{reviewcmd}\nfoo\nbar\n\nbuz\n\\end{reviewcmd}\n|, actual end def test_cmd_caption - lines = ["foo", "bar", "","buz"] - ret = @builder.cmd(lines, "cap1") - assert_equal %Q|\n\\reviewcmdcaption{cap1}\n\\begin{reviewcmd}\nfoo\nbar\n\nbuz\n\\end{reviewcmd}\n|, ret + actual = compile_block("//cmd[cap1]{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\reviewcmdcaption{cap1}\n\\begin{reviewcmd}\nfoo\nbar\n\nbuz\n\\end{reviewcmd}\n|, actual end def test_emlist - lines = ["foo", "bar", "","buz"] - ret = @builder.emlist(lines) - assert_equal %Q|\n\\begin{reviewemlist}\nfoo\nbar\n\nbuz\n\\end{reviewemlist}\n|, ret + actual = compile_block("//emlist{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\begin{reviewemlist}\nfoo\nbar\n\nbuz\n\\end{reviewemlist}\n|, actual end def test_emlist_caption - lines = ["foo", "bar", "","buz"] - ret = @builder.emlist(lines, "cap1") - assert_equal %Q|\n\\reviewemlistcaption{cap1}\n\\begin{reviewemlist}\nfoo\nbar\n\nbuz\n\\end{reviewemlist}\n|, ret + actual = compile_block("//emlist[cap1]{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\reviewemlistcaption{cap1}\n\\begin{reviewemlist}\nfoo\nbar\n\nbuz\n\\end{reviewemlist}\n|, actual end def test_emlist_with_tab - lines = ["\tfoo", "\t\tbar", "","\tbuz"] - ret = @builder.emlist(lines) - assert_equal %Q|\n\\begin{reviewemlist}\n foo\n bar\n\n buz\n\\end{reviewemlist}\n|, ret + actual = compile_block("//emlist{\n\tfoo\n\t\tbar\n\n\tbuz\n//}\n") + assert_equal %Q|\n\\begin{reviewemlist}\n foo\n bar\n\n buz\n\\end{reviewemlist}\n|, actual end def test_emlist_with_tab4 - lines = ["\tfoo", "\t\tbar", "","\tbuz"] - @builder.instance_eval{@tabwidth=4} - ret = @builder.emlist(lines) - assert_equal %Q|\n\\begin{reviewemlist}\n foo\n bar\n\n buz\n\\end{reviewemlist}\n|, ret + @config["tabwidth"] = 4 + actual = compile_block("//emlist{\n\tfoo\n\t\tbar\n\n\tbuz\n//}\n") + assert_equal %Q|\n\\begin{reviewemlist}\n foo\n bar\n\n buz\n\\end{reviewemlist}\n|, actual end def test_quote - lines = ["foo", "bar", "","buz"] - ret = @builder.quote(lines) - assert_equal %Q|\n\\begin{quote}\nfoobar\n\nbuz\n\\end{quote}\n|, ret + actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\begin{quote}\nfoobar\n\nbuz\n\\end{quote}\n|, actual end def test_memo - ret = compile_blockelem("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}<>\n//}\n") - assert_equal %Q|\\begin{reviewminicolumn}\n\\reviewminicolumntitle{this is \\textbf{test}\\textless{}\\&\\textgreater{}\\textunderscore{}}\ntest1\n\ntest\\textit{2}\\textless{}\\textgreater{}\n\\end{reviewminicolumn}\n|, ret + actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}") + assert_equal %Q|\\begin{reviewminicolumn}\n\\reviewminicolumntitle{this is \\textbf{test}\\textless{}\\&\\textgreater{}\\textunderscore{}}\ntest1\n\ntest\\textit{2}\n\\end{reviewminicolumn}\n|, actual end def test_flushright - ret = @builder.flushright(["foo", "bar", "","buz"]) - assert_equal %Q|\n\\begin{flushright}\nfoobar\n\nbuz\n\\end{flushright}\n|, ret + actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\begin{flushright}\nfoobar\n\nbuz\n\\end{flushright}\n|, actual end def test_centering - ret = @builder.centering(["foo", "bar", "","buz"]) - assert_equal %Q|\n\\begin{center}\nfoobar\n\nbuz\n\\end{center}\n|, ret + actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n") + assert_equal %Q|\n\\begin{center}\nfoobar\n\nbuz\n\\end{center}\n|, actual end def test_noindent - # ret = @builder.noindent - # ret << @builder.paragraph(["foo", "bar"]) - # ret << @builder.paragraph(["foo2", "bar2"]) - ret = compile_document("//noindent\nfoo\nbar\n\nfoo2\nbar2\n") - assert_equal %Q|\\noindent\n\nfoobar\n\n\nfoo2bar2\n\n|, ret + actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n") + assert_equal %Q|\\noindent\nfoo\nbar\n\nfoo2\nbar2\n|, actual end def test_image @@ -310,8 +285,8 @@ def @chapter.image(id) item end - ret = @builder.image_image("sampleimg","sample photo",nil) - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, ret + actual = compile_block("//image[sampleimg][sample photo]{\n//}\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, actual end def test_image_with_metric @@ -321,8 +296,8 @@ def @chapter.image(id) item end - ret = @builder.image_image("sampleimg","sample photo","scale=1.2") - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, ret + actual = compile_block("//image[sampleimg][sample photo][scale=1.2]{\n//}\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, actual end def test_image_with_metric2 @@ -332,8 +307,8 @@ def @chapter.image(id) item end - ret = @builder.image_image("sampleimg","sample photo","scale=1.2, html::class=\"sample\", latex::height=3cm") - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2,height=3cm]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, ret + actual = compile_block("//image[sampleimg][sample photo][scale=1.2,html::class=sample,latex::ignore=params]{\n//}\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2,ignore=params]{./images/chap1-sampleimg.png}\n\\caption{sample photo}\n\\label{image:chap1:sampleimg}\n\\end{reviewimage}\n|, actual end def test_indepimage @@ -343,8 +318,8 @@ def @chapter.image(id) item end - ret = @builder.indepimage("sampleimg","sample photo",nil) - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, ret + actual = compile_block("//indepimage[sampleimg][sample photo]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, actual end def test_indepimage_without_caption @@ -355,8 +330,8 @@ def @chapter.image(id) end # FIXME: indepimage's caption should not be with a counter. - ret = @builder.indepimage("sampleimg",nil,nil) - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\end{reviewimage}\n|, ret + actual = compile_block("//indepimage[sampleimg]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\end{reviewimage}\n|, actual end def test_indepimage_with_metric @@ -366,8 +341,8 @@ def @chapter.image(id) item end - ret = @builder.indepimage("sampleimg","sample photo","scale=1.2") - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, ret + actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, actual end def test_indepimage_with_metric2 @@ -377,8 +352,8 @@ def @chapter.image(id) item end - ret = @builder.indepimage("sampleimg","sample photo","scale=1.2, latex::height=3cm, html::class=\"sample\"") - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2,height=3cm]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, ret + actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2, html::class=\"sample\",latex::ignore=params]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2,ignore=params]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, actual end def test_indepimage_without_caption_but_with_metric @@ -389,8 +364,8 @@ def @chapter.image(id) end # FIXME: indepimage's caption should not be with a counter. - ret = @builder.indepimage("sampleimg",nil,"scale=1.2") - assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\end{reviewimage}\n|, ret + actual = compile_block("//indepimage[sampleimg][][scale=1.2]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[scale=1.2]{./images/chap1-sampleimg.png}\n\\end{reviewimage}\n|, actual end def test_bib @@ -398,7 +373,7 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - assert_equal "\\reviewbibref{[1]}{bib:samplebib}", @builder.inline_bib("samplebib") + assert_equal "\\reviewbibref{[1]}{bib:samplebib}", compile_inline("@{samplebib}") end def test_bibpaper @@ -406,8 +381,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - ret = compile_blockelem("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|[1] sample bib \\textbf{bold}\n\\label{bib:samplebib}\n\nab\n\n|, ret + actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") + assert_equal %Q|[1] sample bib \\textbf{bold}\n\\label{bib:samplebib}\n\nab\n\n|, actual end def test_bibpaper_without_body @@ -415,8 +390,8 @@ def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end - ret = @builder.bibpaper([], "samplebib", "sample bib") - assert_equal %Q|[1] sample bib\n\\label{bib:samplebib}\n\n|, ret + actual = compile_block("//bibpaper[samplebib][sample bib]\n") + assert_equal %Q|[1] sample bib\n\\label{bib:samplebib}\n\n|, actual end def column_helper(review) @@ -437,7 +412,7 @@ def test_column_1 ===[/column] EOS - expect =<<-EOS + expected =<<-EOS \\begin{reviewcolumn} \\hypertarget{column:chap1:1}{} @@ -458,7 +433,7 @@ def test_column_1 \\end{reviewcolumn} EOS @config["toclevel"] = 3 - assert_equal expect, column_helper(review) + assert_equal expected, column_helper(review) end def test_column_2 @@ -469,7 +444,7 @@ def test_column_2 === next level EOS - expect =<<-EOS + expected =<<-EOS \\begin{reviewcolumn} \\hypertarget{column:chap1:1}{} @@ -484,7 +459,7 @@ def test_column_2 EOS @config["toclevel"] = 1 - assert_equal expect, column_helper(review) + assert_equal expected, column_helper(review) end def test_column_3 @@ -505,14 +480,15 @@ def test_ul * AAA * BBB EOS - expect =<<-EOS + expected =<<-EOS \\begin{itemize} \\item AAA \\item BBB \\end{itemize} EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_with_bracket @@ -520,14 +496,15 @@ def test_ul_with_bracket * AAA * []BBB EOS - expect =<<-EOS + expected =<<-EOS \\begin{itemize} \\item AAA \\item \\lbrack{}]BBB \\end{itemize} EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_cont @@ -537,14 +514,15 @@ def test_cont * BBB -BB EOS - expect =<<-EOS + expected =<<-EOS \\begin{itemize} \\item AAA{-}AA \\item BBB{-}BB \\end{itemize} EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_nest1 @@ -553,7 +531,7 @@ def test_ul_nest1 ** AA EOS - expect =<<-EOS + expected =<<-EOS \\begin{itemize} \\item AAA @@ -561,9 +539,11 @@ def test_ul_nest1 \\begin{itemize} \\item AA \\end{itemize} + \\end{itemize} EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ul_nest3 @@ -574,7 +554,7 @@ def test_ul_nest3 ** BB EOS - expect =<<-EOS + expected =<<-EOS \\begin{itemize} \\item AAA @@ -582,14 +562,17 @@ def test_ul_nest3 \\begin{itemize} \\item AA \\end{itemize} + \\item BBB \\begin{itemize} \\item BB \\end{itemize} + \\end{itemize} EOS - ul_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ol @@ -598,14 +581,15 @@ def test_ol 3. BBB EOS - expect =<<-EOS + expected =<<-EOS \\begin{enumerate} \\item AAA \\item BBB \\end{enumerate} EOS - ol_helper(src, expect) + actual = compile_block(src) + assert_equal expected, actual end def test_ol_with_bracket @@ -613,73 +597,74 @@ def test_ol_with_bracket 1. AAA 2. []BBB EOS - expect =<<-EOS + expected =<<-EOS \\begin{enumerate} \\item AAA \\item \\lbrack{}]BBB \\end{enumerate} EOS - ret = compile_block(src) - assert_equal expect, ret + actual = compile_block(src) + assert_equal expected, actual end def test_inline_raw0 - assert_equal "normal", @builder.inline_raw("normal") + assert_equal "normal", compile_inline("@{normal}") end def test_inline_raw1 - assert_equal "body", @builder.inline_raw("|latex|body") + assert_equal "body", compile_inline("@{|latex|body}") end def test_inline_raw2 - assert_equal "body", @builder.inline_raw("|html, latex|body") + assert_equal "body", compile_inline("@{|html, latex|body}") end def test_inline_raw3 - assert_equal "", @builder.inline_raw("|idgxml, html|body") + assert_equal "", compile_inline("@{|idgxml, html|body}") end def test_inline_raw4 - assert_equal "|latex body", @builder.inline_raw("|latex body") + assert_equal "|latex body", compile_inline("@{|latex body}") end def test_inline_raw5 - assert_equal "nor\nmal", @builder.inline_raw("|latex|nor\\nmal") + assert_equal "nor\nmal", compile_inline("@{|latex|nor\\nmal}") end def test_inline_endash - ret = compile_inline("- -- --- ----") - assert_equal "{-} {-}{-} {-}{-}{-} {-}{-}{-}{-}", ret + actual = compile_inline("- -- --- ----") + assert_equal "{-} {-}{-} {-}{-}{-} {-}{-}{-}{-}", actual end def test_block_raw0 - ret = @builder.raw("<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, ret + actual = compile_block("//raw[<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw1 - ret = @builder.raw("|latex|<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, ret + actual = compile_block("//raw[|latex|<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw2 - ret = @builder.raw("|html, latex|<>!\"\\n& ") - expect = %Q(<>!\"\n& ) - assert_equal expect.chomp, ret + actual = compile_block("//raw[|html, latex|<>!\"\\n& ]\n") + expected = %Q(<>!\"\n& ) + assert_equal expected, actual end def test_block_raw3 - ret = @builder.raw("|html, idgxml|<>!\"\\n& ") - expect = '' - assert_equal expect.chomp, ret + actual = compile_block("//raw[|html, idgxml|<>!\"\\n& ]\n") + expected = '' + assert_equal expected, actual end def test_block_raw4 - ret = @builder.raw("|latex <>!\"\\n& ") - expect = %Q(|latex <>!\"\n& ) - assert_equal expect.chomp, ret + actual = compile_block("//raw[|latex <>!\"\\n& ]\n") + expected = %Q(|latex <>!\"\n& ) + assert_equal expected, actual end + end From 2d058e9e24e1d3a893bffcffa7ba5c1228beed48 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 19 Aug 2014 11:45:23 +0900 Subject: [PATCH 095/214] regenerate --- lib/review/compiler.rb | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 15b4b1ece..3876debd7 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -793,12 +793,12 @@ def compile_inline(op, args) raise "strategy does not support inline op: @<#{op}>" end if !args - @strategy.__send__("inline_#{op}") + @strategy.__send__("inline_#{op}", "") else @strategy.__send__("inline_#{op}", *(args.map(&:to_doc))) end -# rescue => err -# error err.message + rescue => err + error err.message end def compile_paragraph(buf) @@ -808,7 +808,7 @@ def compile_paragraph(buf) def compile_raw(builders, content) c = @strategy.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase if !builders || builders.include?(c) - content + content.gsub("\\n", "\n") else "" end @@ -2639,7 +2639,7 @@ def _InlineElementContentText return _tmp end - # BracketArg = "[" BracketArgContentInline+:content "]" {bracket_arg(self, content)} + # BracketArg = "[" BracketArgContentInline*:content "]" {bracket_arg(self, content)} def _BracketArg _save = self.pos @@ -2649,21 +2649,14 @@ def _BracketArg self.pos = _save break end - _save1 = self.pos _ary = [] - _tmp = apply(:_BracketArgContentInline) - if _tmp - _ary << @result - while true - _tmp = apply(:_BracketArgContentInline) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 + while true + _tmp = apply(:_BracketArgContentInline) + _ary << @result if _tmp + break unless _tmp end + _tmp = true + @result = _ary content = @result unless _tmp self.pos = _save @@ -3982,7 +3975,7 @@ def _Spacechar Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:content {inline_element_content(self, content)} | InlineElementContentText+:content {inline_element_content(self, content)})") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") - Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline+:content \"]\" {bracket_arg(self, content)}") + Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline*:content \"]\" {bracket_arg(self, content)}") Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") From 420a331f00575f88878063bf40fd99d305e5ba0d Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 25 Aug 2014 13:22:32 +0900 Subject: [PATCH 096/214] move Array extensions: core_extensions -> review/extensions --- lib/review/extentions.rb | 1 + lib/{core_extensions.rb => review/extentions/array.rb} | 4 ---- lib/review/node.rb | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) rename lib/{core_extensions.rb => review/extentions/array.rb} (82%) diff --git a/lib/review/extentions.rb b/lib/review/extentions.rb index c627226af..6d3ae12d5 100644 --- a/lib/review/extentions.rb +++ b/lib/review/extentions.rb @@ -1,3 +1,4 @@ require 'review/extentions/object' require 'review/extentions/string' +require 'review/extentions/array' diff --git a/lib/core_extensions.rb b/lib/review/extentions/array.rb similarity index 82% rename from lib/core_extensions.rb rename to lib/review/extentions/array.rb index 424b0d05e..24c9b74a6 100644 --- a/lib/core_extensions.rb +++ b/lib/review/extentions/array.rb @@ -1,7 +1,3 @@ -# -# core extensions for Re:VIEW -# - class Array # for ReVIEW::Node diff --git a/lib/review/node.rb b/lib/review/node.rb index 10cd13323..9c50c43e2 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -1,4 +1,3 @@ -require 'core_extensions' require 'json' module ReVIEW From 508fe08d6a6642c1727c76918ebb0250898cd18e Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 26 Aug 2014 22:06:00 +0900 Subject: [PATCH 097/214] add argument of Node to compile_command, compile_block and compile_single --- lib/review/compiler.rb | 14 +++++++------- lib/review/node.rb | 2 +- lib/review/review.kpeg | 14 +++++++------- test/test_compiler.rb | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 3876debd7..b372b4fad 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -630,7 +630,7 @@ def compile_column(level, label, caption, content) buf end - def compile_command(name, args, lines) + def compile_command(name, args, lines, node) syntax = syntax_descriptor(name) if !syntax || !@strategy.respond_to?(syntax.name) error "strategy does not support command: //#{name}" @@ -644,12 +644,12 @@ def compile_command(name, args, lines) args = ['(NoArgument)'] * syntax.min_argc end if syntax.block_allowed? - compile_block syntax, args, lines + compile_block(syntax, args, lines, node) else if lines error "block is not allowed for command //#{syntax.name}; ignore" end - compile_single syntax, args + compile_single(syntax, args, node) end end @@ -756,10 +756,10 @@ def compile_unknown_command(args, lines) @strategy.unknown_command(args, lines) end - def compile_block(syntax, args, lines) + def compile_block(syntax, args, lines, node) node_name = "node_#{syntax.name}".to_sym if @strategy.respond_to?(node_name) - @strategy.__send__(node_name, (lines || default_block(syntax)), *args) + @strategy.__send__(node_name, node) else @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) end @@ -772,10 +772,10 @@ def default_block(syntax) [] end - def compile_single(syntax, args) + def compile_single(syntax, args, node) node_name = "node_#{syntax.name}".to_sym if @strategy.respond_to?(node_name) - @strategy.__send__(node_name, *args) + @strategy.__send__(node_name, node) else @strategy.__send__(syntax.name, *args) end diff --git a/lib/review/node.rb b/lib/review/node.rb index 9c50c43e2..3a0c7f9c8 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -80,7 +80,7 @@ def to_doc else content_lines = nil end - @compiler.compile_command(@name, args, content_lines) + @compiler.compile_command(@name, args, content_lines, self) end end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 32a7e4fdc..d15aa3983 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -275,7 +275,7 @@ require 'review/node' buf end - def compile_command(name, args, lines) + def compile_command(name, args, lines, node) syntax = syntax_descriptor(name) if !syntax || !@strategy.respond_to?(syntax.name) error "strategy does not support command: //#{name}" @@ -289,12 +289,12 @@ require 'review/node' args = ['(NoArgument)'] * syntax.min_argc end if syntax.block_allowed? - compile_block syntax, args, lines + compile_block(syntax, args, lines, node) else if lines error "block is not allowed for command //#{syntax.name}; ignore" end - compile_single syntax, args + compile_single(syntax, args, node) end end @@ -401,10 +401,10 @@ require 'review/node' @strategy.unknown_command(args, lines) end - def compile_block(syntax, args, lines) + def compile_block(syntax, args, lines, node) node_name = "node_#{syntax.name}".to_sym if @strategy.respond_to?(node_name) - @strategy.__send__(node_name, (lines || default_block(syntax)), *args) + @strategy.__send__(node_name, node) else @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) end @@ -417,10 +417,10 @@ require 'review/node' [] end - def compile_single(syntax, args) + def compile_single(syntax, args, node) node_name = "node_#{syntax.name}".to_sym if @strategy.respond_to?(node_name) - @strategy.__send__(node_name, *args) + @strategy.__send__(node_name, node) else @strategy.__send__(syntax.name, *args) end diff --git a/test/test_compiler.rb b/test/test_compiler.rb index 8bab76261..becd3efd1 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -24,7 +24,7 @@ def setup location = Location.new(nil, nil) @builder.bind(@compiler, @chapter, location) - def @compiler.compile_command(name, args, lines) + def @compiler.compile_command(name, args, lines, node) args end From b0407c19e2f84b8a33102682c0bcf770fabc4a7d Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 26 Aug 2014 22:07:09 +0900 Subject: [PATCH 098/214] LATEXBuilder: new handler for `//label` use Node, not converted text --- lib/review/latexbuilder.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 02166b03a..cb85354f8 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -588,6 +588,11 @@ def label(id) macro('label', id) + "\n" end + def node_label(node) + id = node.args[0].to_raw + macro('label', id) + "\n" + end + def pagebreak '\pagebreak' + "\n" end From a227a0f27a2dded759874bef7350c1bdd410719c Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 26 Aug 2014 22:10:06 +0900 Subject: [PATCH 099/214] add NEWLINE --- test/test_latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index dd74f1240..a09588266 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -261,7 +261,7 @@ def test_quote end def test_memo - actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}") + actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}\n") assert_equal %Q|\\begin{reviewminicolumn}\n\\reviewminicolumntitle{this is \\textbf{test}\\textless{}\\&\\textgreater{}\\textunderscore{}}\ntest1\n\ntest\\textit{2}\n\\end{reviewminicolumn}\n|, actual end From d0875cda22ca01ec04a2d42ed67b5df6f1b101fc Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 26 Aug 2014 22:54:28 +0900 Subject: [PATCH 100/214] remove unused newline --- lib/review/latexbuilder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index cb85354f8..1f6a46bb3 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -215,7 +215,7 @@ def dl_end def paragraph(lines) buf = "\n" lines.each do |line| - buf << line << "\n" + buf << line end buf << "\n" buf @@ -602,7 +602,7 @@ def linebreak end def noindent - '\noindent' + "\n" + '\noindent' end def inline_chapref(id) From ae203c63b670045f42213e363ebcc70914376b0d Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 26 Aug 2014 22:55:28 +0900 Subject: [PATCH 101/214] new LATEXBuilder remove unused extra NEWLINE --- test/test_latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index a09588266..3f14197eb 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -277,7 +277,7 @@ def test_centering def test_noindent actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n") - assert_equal %Q|\\noindent\nfoo\nbar\n\nfoo2\nbar2\n|, actual + assert_equal %Q|\\noindent\nfoobar\n\nfoo2bar2\n|, actual end def test_image From b49db581b44b3a70a1f660e43229c86532ba24bb Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 00:22:52 +0900 Subject: [PATCH 102/214] remove extra newline --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index d15aa3983..d0262c057 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -390,7 +390,7 @@ require 'review/node' buf << @strategy.dl_begin content.each do |element| buf << @strategy.dt(element.text.to_doc) - buf << @strategy.dd(element.content.map{|s| s.to_doc+"\n"}) + buf << @strategy.dd(element.content.map{|s| s.to_doc}) end buf << @strategy.dl_end buf From 49b30af1f7ec2c1faf0ca4bd055980ad05c9000d Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 00:23:38 +0900 Subject: [PATCH 103/214] LATEXBuilder: add newline at last of dd --- lib/review/latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 1f6a46bb3..c7ccdefa6 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -205,7 +205,7 @@ def dt(str) end def dd(lines) - lines.join + lines.join + "\n" end def dl_end From ef8c1b86cd8222c47133ece8c10959a35e6fe056 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 00:24:09 +0900 Subject: [PATCH 104/214] regenarate --- lib/review/compiler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index b372b4fad..6e96d5c01 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -745,7 +745,7 @@ def compile_dlist(content) buf << @strategy.dl_begin content.each do |element| buf << @strategy.dt(element.text.to_doc) - buf << @strategy.dd(element.content.map{|s| s.to_doc+"\n"}) + buf << @strategy.dd(element.content.map{|s| s.to_doc}) end buf << @strategy.dl_end buf From 1cbbb5193389582e04a1787e23ea55b7d8cd9e65 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 00:24:45 +0900 Subject: [PATCH 105/214] LATEXBuilder: fix tests; remove extra newlines --- test/test_latexbuilder.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 3f14197eb..25ad84831 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -420,7 +420,6 @@ def test_column_1 \\addcontentsline{toc}{subsection}{prev column} inside prev column - \\end{reviewcolumn} \\begin{reviewcolumn} @@ -429,7 +428,6 @@ def test_column_1 \\addcontentsline{toc}{subsection}{test} inside column - \\end{reviewcolumn} EOS @config["toclevel"] = 3 @@ -451,9 +449,7 @@ def test_column_2 \\reviewcolumnhead{}{test} inside column - \\end{reviewcolumn} - \\subsection*{next level} \\label{sec:1-0-1} EOS @@ -539,7 +535,6 @@ def test_ul_nest1 \\begin{itemize} \\item AA \\end{itemize} - \\end{itemize} EOS actual = compile_block(src) @@ -562,13 +557,11 @@ def test_ul_nest3 \\begin{itemize} \\item AA \\end{itemize} - \\item BBB \\begin{itemize} \\item BB \\end{itemize} - \\end{itemize} EOS actual = compile_block(src) From e89049f2eeaee5aced2f81200609e670788f67a6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 03:43:29 +0900 Subject: [PATCH 106/214] IDGXMLBuilder: add node_indepimage() instead of indepimage() --- lib/review/idgxmlbuilder.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index b0a9740cf..932ea529d 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -1107,6 +1107,24 @@ def indepimage(id, caption=nil, metric=nil) buf end + def node_indepimage(node) + id = node.args[0].to_raw + caption = node.args[1] ? node.args[1].to_doc : nil + metric = node.args[2] ? node.args[2].to_raw : nil + buf = "" + metrics = parse_metric("idgxml", metric) + buf << "" << @lf + begin + buf << %Q[] << @lf + rescue + warn %Q[no such image: #{id}] + end + buf << %Q[#{caption}] + @lf if caption.present? + buf << "" << @lf + buf + end + + alias_method :numberlessimage, :indepimage def label(id) From 5c968d8b7f4d7977c548033e1b448b9139d680f1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 03:46:59 +0900 Subject: [PATCH 107/214] remove local var ast --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index d0262c057..f66c344d1 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -38,7 +38,7 @@ require 'review/node' def convert_ast ast = @strategy.ast - ast = convert_column(ast) + convert_column(ast) @strategy.output << ast.to_doc end From f954c0e6a8515099cb784da202fb61d5c6e71735 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 04:20:05 +0900 Subject: [PATCH 108/214] add Node#parse_args --- lib/review/idgxmlbuilder.rb | 4 +--- lib/review/node.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 932ea529d..3753d8095 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -1108,9 +1108,7 @@ def indepimage(id, caption=nil, metric=nil) end def node_indepimage(node) - id = node.args[0].to_raw - caption = node.args[1] ? node.args[1].to_doc : nil - metric = node.args[2] ? node.args[2].to_raw : nil + id, caption, metric = node.parse_args(:raw, :doc, :raw) buf = "" metrics = parse_metric("idgxml", metric) buf << "" << @lf diff --git a/lib/review/node.rb b/lib/review/node.rb index 3a0c7f9c8..dc8619bc6 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -82,6 +82,17 @@ def to_doc end @compiler.compile_command(@name, args, content_lines, self) end + + def parse_args(*patterns) + patterns.map.with_index do |pattern, i| + if @args[i] + @args[i].__send__("to_#{pattern}") + else + nil + end + end + end + end class InlineElementNode < Node From 5350f286dda1ee693915c3566148ee5685d75c46 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 04:20:17 +0900 Subject: [PATCH 109/214] use JSON gem for 1.8 --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index 666e8ddd5..95b8b1a1b 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,8 @@ source 'https://rubygems.org' # Specify your gem's dependencies in review.gemspec gemspec +gem "json" + group :development do gem "kpeg" end \ No newline at end of file From aa108c450e3d8975a089299fb3f1acc3deed5007 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 04:44:16 +0900 Subject: [PATCH 110/214] support Ruby 1.8 :( --- lib/review/node.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index dc8619bc6..eb49c5c67 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -84,13 +84,15 @@ def to_doc end def parse_args(*patterns) - patterns.map.with_index do |pattern, i| + ret = [] + patterns.each_index do |i| if @args[i] - @args[i].__send__("to_#{pattern}") + ret << @args[i].__send__("to_#{patterns[i]}") else - nil + ret << nil end end + ret end end From 863d73d01efd023fd98b4d580b620e10465d2e90 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 16:36:25 +0900 Subject: [PATCH 111/214] fix order of libs --- test/test_latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 25ad84831..73c35e250 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- require 'test_helper' -require 'review/compiler' require 'review/book' +require 'review/compiler' require 'review/latexbuilder' require 'review/i18n' From 1deba6e1352d964387700e564398235476494166 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 16:37:28 +0900 Subject: [PATCH 112/214] supress $stderr in test --- test/test_pdfmaker.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_pdfmaker.rb b/test/test_pdfmaker.rb index 38a66438e..44476012a 100644 --- a/test/test_pdfmaker.rb +++ b/test/test_pdfmaker.rb @@ -24,11 +24,14 @@ def test_check_book_existed Dir.mktmpdir do |dir| Dir.chdir(dir) do FileUtils.touch(File.join(dir, "sample.pdf")) + $stderr = StringIO.new assert_raises SystemExit do @maker.check_book(@config) end end end + ensure + $stderr = STDERR end def test_check_book_none From 325d549202d8bf5f6df95ccab0f5fcff21f722c1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 16:39:13 +0900 Subject: [PATCH 113/214] add $KCODE into ReVIEW::Compiler to set Regexp UTF-8 --- lib/review/review.kpeg | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index f66c344d1..54a8673bf 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -1,6 +1,7 @@ %% name = ReVIEW::Compiler %% { +$KCODE='u' ## for Ruby 1.8 :-( class Error; end From 1bbb236d5f161e963b6ff7695773fe4e91a1a0fb Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 16:39:44 +0900 Subject: [PATCH 114/214] regenerate --- lib/review/compiler.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 6e96d5c01..e00dc10ab 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -356,6 +356,7 @@ def self.rule_info(name, rendered) # :startdoc: +$KCODE='u' ## for 1.8 class Error; end @@ -393,7 +394,7 @@ def do_compile def convert_ast ast = @strategy.ast - ast = convert_column(ast) + convert_column(ast) @strategy.output << ast.to_doc end From 2b96f8402205ed72dd7d9e8f13b5ba5a010657bf Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 20:27:22 +0900 Subject: [PATCH 115/214] remove comment --- lib/review/builder.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 3df25556a..3c58ec6bb 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -65,17 +65,11 @@ def result alias_method :raw_result, :result def print(*s) - raise NotImplementedError, "XXX: do not use print" -# @output.print(*s.map{|i| -# convert_outencoding(i, @book.param["outencoding"]) -# }) + raise NotImplementedError, "XXX: `print` method is obsoleted. Do not use it." end def puts(*s) - raise NotImplementedError, "XXX: do not use puts" -# @output.puts *s.map{|i| -# convert_outencoding(i, @book.param["outencoding"]) -# } + raise NotImplementedError, "XXX: `puts` method is obsoleted. Do not use it." end def target_name From 8fbe005179bbe37b06a076e6aaa9a872e6d67dca Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 21:12:32 +0900 Subject: [PATCH 116/214] move $KCODE to supress warning in 1.9 --- lib/review/compiler.rb | 2 -- lib/review/compiler/literals_1_8.kpeg | 1 + lib/review/compiler/literals_1_8.rb | 2 ++ lib/review/review.kpeg | 2 -- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index e00dc10ab..f2c2a7230 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -356,8 +356,6 @@ def self.rule_info(name, rendered) # :startdoc: -$KCODE='u' ## for 1.8 - class Error; end require 'review/location' diff --git a/lib/review/compiler/literals_1_8.kpeg b/lib/review/compiler/literals_1_8.kpeg index 873ff4561..e431d9168 100644 --- a/lib/review/compiler/literals_1_8.kpeg +++ b/lib/review/compiler/literals_1_8.kpeg @@ -1,6 +1,7 @@ %% name = ReVIEW::Compiler::Literals %% header { +$KCODE='u' ## for Ruby 1.8 :-( ## # Provides Literals appropriate for your ruby version. diff --git a/lib/review/compiler/literals_1_8.rb b/lib/review/compiler/literals_1_8.rb index dc47d1aaa..ab2e3fe49 100644 --- a/lib/review/compiler/literals_1_8.rb +++ b/lib/review/compiler/literals_1_8.rb @@ -1,3 +1,5 @@ +$KCODE='u' ## for Ruby 1.8 :-( + ## # Provides Literals appropriate for your ruby version. #-- diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 54a8673bf..3f49570b4 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -1,8 +1,6 @@ %% name = ReVIEW::Compiler %% { -$KCODE='u' ## for Ruby 1.8 :-( - class Error; end require 'review/location' From 76bcfefe96eaac4fe6aa515a1caf6c92085d2de1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 21:13:12 +0900 Subject: [PATCH 117/214] Revert "support Ruby 1.8 :(" This reverts commit aa108c450e3d8975a089299fb3f1acc3deed5007. --- lib/review/node.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index eb49c5c67..dc8619bc6 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -84,15 +84,13 @@ def to_doc end def parse_args(*patterns) - ret = [] - patterns.each_index do |i| + patterns.map.with_index do |pattern, i| if @args[i] - ret << @args[i].__send__("to_#{patterns[i]}") + @args[i].__send__("to_#{pattern}") else - ret << nil + nil end end - ret end end From dcc83143c13b4e2f3c024f557b6ea3fc6df394d2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 27 Aug 2014 21:18:41 +0900 Subject: [PATCH 118/214] add compat method of Enumerable#map in 1.8 --- lib/review/extentions/array.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/review/extentions/array.rb b/lib/review/extentions/array.rb index 24c9b74a6..d83e1627f 100644 --- a/lib/review/extentions/array.rb +++ b/lib/review/extentions/array.rb @@ -11,4 +11,15 @@ def to_doc def to_raw self.map(&:to_raw).join("") end + + if [].map.kind_of?(Array) + # Ruby 1.8 + def map(&block) + if !block_given? + return to_enum :map + else + collect(&block) ## XXX same as original + end + end + end end From 98065589edcc7a3f33ccd07d2aec4901a777d79c Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 28 Aug 2014 03:14:20 +0900 Subject: [PATCH 119/214] remove unused methods --- lib/review/compiler.rb | 59 +++++++----------------------------------- lib/review/review.kpeg | 49 +++++------------------------------ test/test_helper.rb | 5 ---- 3 files changed, 16 insertions(+), 97 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index f2c2a7230..fd40da059 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -605,22 +605,6 @@ def inline_defined?(name) definline :include - def tagged_section_init - # noop - end - - def open_tagged_section(tag, level, label, caption) - #noop - end - - def close_tagged_section(tag, level) - # noop - end - - def close_all_tagged_section - # noop - end - def compile_column(level, label, caption, content) buf = "" buf << @strategy.__send__("column_begin", level, label, caption) @@ -658,34 +642,15 @@ def compile_headline(level, tag, label, caption) caption ||= "" caption.strip! index = level - 1 - # if tag - # if tag !~ /\A\// - # buf << close_current_tagged_section(level) - # buf << open_tagged_section(tag, level, label, caption) - # else - # open_tag = tag[1..-1] - # prev_tag_info = @tagged_section.pop - # if !prev_tag_info || prev_tag_info.first != open_tag - # raise ReVIEW::CompileError, "#{open_tag} is not opened." - # end - # buf << close_tagged_section(*prev_tag_info) - # end - # else - if @headline_indexs.size > (index + 1) - @headline_indexs = @headline_indexs[0..index] - end - @headline_indexs[index] = 0 if @headline_indexs[index].nil? - @headline_indexs[index] += 1 - # buf << close_current_tagged_section(level) - buf << @strategy.headline(level, label, caption) - # end + if @headline_indexs.size > (index + 1) + @headline_indexs = @headline_indexs[0..index] + end + @headline_indexs[index] = 0 if @headline_indexs[index].nil? + @headline_indexs[index] += 1 + buf << @strategy.headline(level, label, caption) buf end - def close_current_tagged_section(level) - # noop - end - def comment(text) @strategy.comment(text) end @@ -1078,7 +1043,7 @@ def _root return _tmp end - # Start = &. { tagged_section_init } Document:c { close_all_tagged_section; @strategy.ast = c } + # Start = &. Document:c { @strategy.ast = c } def _Start _save = self.pos @@ -1090,19 +1055,13 @@ def _Start self.pos = _save break end - @result = begin; tagged_section_init ; end - _tmp = true - unless _tmp - self.pos = _save - break - end _tmp = apply(:_Document) c = @result unless _tmp self.pos = _save break end - @result = begin; close_all_tagged_section; @strategy.ast = c ; end + @result = begin; @strategy.ast = c ; end _tmp = true unless _tmp self.pos = _save @@ -3952,7 +3911,7 @@ def _Spacechar Rules = {} Rules[:_root] = rule_info("root", "Start") - Rules[:_Start] = rule_info("Start", "&. { tagged_section_init } Document:c { close_all_tagged_section; @strategy.ast = c }") + Rules[:_Start] = rule_info("Start", "&. Document:c { @strategy.ast = c }") Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 3f49570b4..7a7a8e3ad 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -250,22 +250,6 @@ require 'review/node' definline :include - def tagged_section_init - # noop - end - - def open_tagged_section(tag, level, label, caption) - #noop - end - - def close_tagged_section(tag, level) - # noop - end - - def close_all_tagged_section - # noop - end - def compile_column(level, label, caption, content) buf = "" buf << @strategy.__send__("column_begin", level, label, caption) @@ -303,34 +287,15 @@ require 'review/node' caption ||= "" caption.strip! index = level - 1 - # if tag - # if tag !~ /\A\// - # buf << close_current_tagged_section(level) - # buf << open_tagged_section(tag, level, label, caption) - # else - # open_tag = tag[1..-1] - # prev_tag_info = @tagged_section.pop - # if !prev_tag_info || prev_tag_info.first != open_tag - # raise ReVIEW::CompileError, "#{open_tag} is not opened." - # end - # buf << close_tagged_section(*prev_tag_info) - # end - # else - if @headline_indexs.size > (index + 1) - @headline_indexs = @headline_indexs[0..index] - end - @headline_indexs[index] = 0 if @headline_indexs[index].nil? - @headline_indexs[index] += 1 - # buf << close_current_tagged_section(level) - buf << @strategy.headline(level, label, caption) - # end + if @headline_indexs.size > (index + 1) + @headline_indexs = @headline_indexs[0..index] + end + @headline_indexs[index] = 0 if @headline_indexs[index].nil? + @headline_indexs[index] += 1 + buf << @strategy.headline(level, label, caption) buf end - def close_current_tagged_section(level) - # noop - end - def comment(text) @strategy.comment(text) end @@ -494,7 +459,7 @@ require 'review/node' root = Start -Start = &. { tagged_section_init } Document:c { close_all_tagged_section; @strategy.ast = c } +Start = &. Document:c { @strategy.ast = c } Document = Block*:c ~document(self, c) diff --git a/test/test_helper.rb b/test/test_helper.rb index 33afa31d3..a11e5f38f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,7 +3,6 @@ def compile_document(str, force_to_s=true) @compiler.setup_parser(str) - @compiler.tagged_section_init @compiler.parse("Document") if force_to_s @compiler.result.to_doc @@ -14,7 +13,6 @@ def compile_document(str, force_to_s=true) def compile_block(str, force_to_s=true) @compiler.setup_parser(str) - @compiler.tagged_section_init @compiler.parse("Document") if force_to_s @compiler.result.to_doc @@ -25,7 +23,6 @@ def compile_block(str, force_to_s=true) def compile_inline(str, force_to_s=true) @compiler.setup_parser(str) - @compiler.tagged_section_init @compiler.parse("Paragraph") if force_to_s @compiler.result.map(&:to_doc).join @@ -36,7 +33,6 @@ def compile_inline(str, force_to_s=true) def compile_blockelem(str, force_to_s=true) @compiler.setup_parser(str) - @compiler.tagged_section_init @compiler.parse("BlockElement") if force_to_s @compiler.result.to_doc @@ -47,7 +43,6 @@ def compile_blockelem(str, force_to_s=true) def compile_headline(str) @compiler.setup_parser(str) - @compiler.tagged_section_init @compiler.parse("Headline") @compiler.result.to_doc end From 8cd0cae79fb3526b35e6977d0880b0ac2585e9a5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 30 Aug 2014 16:45:51 +0900 Subject: [PATCH 120/214] do not need compile_inline because content should have been already compiled --- lib/review/builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 3c58ec6bb..8b19c1e7a 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -184,7 +184,7 @@ def adjust_n_cols(rows) # end def inline_chapref(id) - compile_inline @chapter.env.chapter_index.display_string(id) + @chapter.env.chapter_index.display_string(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") From d15bd28df3e6123836160158776e85847fd8eadc Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 30 Aug 2014 16:49:18 +0900 Subject: [PATCH 121/214] add arguments for defsyntax; array to specify whether arguments need compiling (:doc) or not (:raw) --- lib/review/compiler.rb | 35 ++++++++++++++++++++++++++--------- lib/review/node.rb | 2 +- lib/review/review.kpeg | 35 ++++++++++++++++++++++++++--------- test/test_compiler.rb | 4 ++-- test/test_latexbuilder.rb | 11 +++++++++++ 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index fd40da059..b0afa2c78 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -439,10 +439,11 @@ def compile_text(text) end class SyntaxElement - def initialize(name, type, argc, &block) + def initialize(name, type, argc, esc, &block) @name = name @type = type @argc_spec = argc + @esc_patterns = esc @checker = block end @@ -464,6 +465,20 @@ def min_argc end end + def parse_args(args) + if @esc_patterns + args.map.with_index do |pattern, i| + if @esc_patterns[i] + args[i].__send__("to_#{@esc_patterns[i]}") + else + args[i].to_doc + end + end + else + args.map(&:to_doc) + end + end + def block_required? @type == :block end @@ -475,16 +490,16 @@ def block_allowed? SYNTAX = {} - def self.defblock(name, argc, optional = false, &block) - defsyntax name, (optional ? :optional : :block), argc, &block + def self.defblock(name, argc, optional = false, esc = nil, &block) + defsyntax(name, (optional ? :optional : :block), argc, esc, &block) end def self.defsingle(name, argc, &block) defsyntax name, :line, argc, &block end - def self.defsyntax(name, type, argc, &block) - SYNTAX[name] = SyntaxElement.new(name, type, argc, &block) + def self.defsyntax(name, type, argc, esc = nil, &block) + SYNTAX[name] = SyntaxElement.new(name, type, argc, esc, &block) end def syntax_defined?(name) @@ -515,12 +530,12 @@ def inline_defined?(name) defblock :read, 0 defblock :lead, 0 - defblock :list, 2 + defblock :list, 2, nil, [:raw,:doc] defblock :emlist, 0..1 defblock :cmd, 0..1 defblock :table, 0..2 defblock :quote, 0 - defblock :image, 2..3, true + defblock :image, 2..3, true, [:raw,:doc,:raw] defblock :source, 0..1 defblock :listnum, 2 defblock :emlistnum, 0..1 @@ -725,7 +740,8 @@ def compile_block(syntax, args, lines, node) if @strategy.respond_to?(node_name) @strategy.__send__(node_name, node) else - @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) + args_conv = syntax.parse_args(args) + @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args_conv) end end @@ -741,7 +757,8 @@ def compile_single(syntax, args, node) if @strategy.respond_to?(node_name) @strategy.__send__(node_name, node) else - @strategy.__send__(syntax.name, *args) + args_conv = syntax.parse_args(args) + @strategy.__send__(syntax.name, *args_conv) end end diff --git a/lib/review/node.rb b/lib/review/node.rb index dc8619bc6..ec3243444 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -80,7 +80,7 @@ def to_doc else content_lines = nil end - @compiler.compile_command(@name, args, content_lines, self) + @compiler.compile_command(@name, @args, content_lines, self) end def parse_args(*patterns) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 7a7a8e3ad..383c1432c 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -84,10 +84,11 @@ require 'review/node' end class SyntaxElement - def initialize(name, type, argc, &block) + def initialize(name, type, argc, esc, &block) @name = name @type = type @argc_spec = argc + @esc_patterns = esc @checker = block end @@ -109,6 +110,20 @@ require 'review/node' end end + def parse_args(args) + if @esc_patterns + args.map.with_index do |pattern, i| + if @esc_patterns[i] + args[i].__send__("to_#{@esc_patterns[i]}") + else + args[i].to_doc + end + end + else + args.map(&:to_doc) + end + end + def block_required? @type == :block end @@ -120,16 +135,16 @@ require 'review/node' SYNTAX = {} - def self.defblock(name, argc, optional = false, &block) - defsyntax name, (optional ? :optional : :block), argc, &block + def self.defblock(name, argc, optional = false, esc = nil, &block) + defsyntax(name, (optional ? :optional : :block), argc, esc, &block) end def self.defsingle(name, argc, &block) defsyntax name, :line, argc, &block end - def self.defsyntax(name, type, argc, &block) - SYNTAX[name] = SyntaxElement.new(name, type, argc, &block) + def self.defsyntax(name, type, argc, esc = nil, &block) + SYNTAX[name] = SyntaxElement.new(name, type, argc, esc, &block) end def syntax_defined?(name) @@ -160,12 +175,12 @@ require 'review/node' defblock :read, 0 defblock :lead, 0 - defblock :list, 2 + defblock :list, 2, nil, [:raw,:doc] defblock :emlist, 0..1 defblock :cmd, 0..1 defblock :table, 0..2 defblock :quote, 0 - defblock :image, 2..3, true + defblock :image, 2..3, true, [:raw,:doc,:raw] defblock :source, 0..1 defblock :listnum, 2 defblock :emlistnum, 0..1 @@ -370,7 +385,8 @@ require 'review/node' if @strategy.respond_to?(node_name) @strategy.__send__(node_name, node) else - @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args) + args_conv = syntax.parse_args(args) + @strategy.__send__(syntax.name, (lines || default_block(syntax)), *args_conv) end end @@ -386,7 +402,8 @@ require 'review/node' if @strategy.respond_to?(node_name) @strategy.__send__(node_name, node) else - @strategy.__send__(syntax.name, *args) + args_conv = syntax.parse_args(args) + @strategy.__send__(syntax.name, *args_conv) end end diff --git a/test/test_compiler.rb b/test/test_compiler.rb index becd3efd1..d38c461e1 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -31,8 +31,8 @@ def @compiler.compile_command(name, args, lines, node) end def test_parse_args - args = compile_blockelem("//dummy[foo][bar]\n") - assert_equal ["foo","bar"], args + args = compile_blockelem("//dummy[foo][bar]\n", false) + assert_equal ["foo","bar"], args.parse_args(:doc,:doc) end def test_parse_args_with_brace1 diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 73c35e250..e8a494054 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -324,6 +324,17 @@ def @chapter.image(id) assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, actual end + def test_indepimage_esc + def @chapter.image(id) + item = Book::ImageIndex::Item.new("sampleimg",1) + item.instance_eval{@path="./images/chap1-sampleimg.png"} + item + end + + actual = compile_block("//indepimage[sean_2_10][sample photo][]\n") + assert_equal %Q|\\begin{reviewimage}\n\\includegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}\n\\reviewindepimagecaption{図: sample photo}\n\\end{reviewimage}\n|, actual + end + def test_indepimage_without_caption def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) From 160977a6859741d7c60bc3918a2b3cb51bee38c6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 9 Nov 2014 20:12:56 +0900 Subject: [PATCH 122/214] remove require --- test/test_compiler.rb | 1 - test/test_idgxmlbuilder.rb | 1 - test/test_markdownbuilder.rb | 1 - test/test_topbuilder.rb | 1 - 4 files changed, 4 deletions(-) diff --git a/test/test_compiler.rb b/test/test_compiler.rb index d38c461e1..befaa5dd2 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/latexbuilder' diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index d3d045668..818fe31ee 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/idgxmlbuilder' diff --git a/test/test_markdownbuilder.rb b/test/test_markdownbuilder.rb index 5a34c1b7f..45aca6dc3 100644 --- a/test/test_markdownbuilder.rb +++ b/test/test_markdownbuilder.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/markdownbuilder' diff --git a/test/test_topbuilder.rb b/test/test_topbuilder.rb index a2ce59ccb..476f44863 100644 --- a/test/test_topbuilder.rb +++ b/test/test_topbuilder.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'test_helper' -require 'review' require 'review/compiler' require 'review/book' require 'review/topbuilder' From 03b01982c9ae78f9fd9a65bc164e6527941f11aa Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 22 Jan 2015 23:30:52 +0900 Subject: [PATCH 123/214] add kpeg --- review.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/review.gemspec b/review.gemspec index 1dffde763..1a2ac41f2 100644 --- a/review.gemspec +++ b/review.gemspec @@ -26,5 +26,6 @@ Gem::Specification.new do |gem| gem.add_development_dependency("rake") gem.add_development_dependency("test-unit") + gem.add_development_dependency("kpeg") end From 2d5fa045a8490c3ce066efd73f164d0ebb62c512 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 22 Jan 2015 23:32:08 +0900 Subject: [PATCH 124/214] fix tests; block has LF as terminator --- test/test_htmlbuilder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 0b1e8fc52..10f5cb174 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -298,7 +298,7 @@ def @chapter.image(id) item end - actual = compile_block "@{sampleimg}" + actual = compile_block "@{sampleimg}\n" expected = "

                    図1.1「sample photo」

                    \n" assert_equal expected, actual end @@ -310,7 +310,7 @@ def @chapter.image(id) item end - actual = compile_block "@{sampleimg}" + actual = compile_block "@{sampleimg}\n" expected = "

                    図1.1

                    \n" assert_equal expected, actual end From e96c7cce6222301f3948d4b796b121188076884e Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 25 Jan 2015 01:57:37 +0900 Subject: [PATCH 125/214] no need to escape in inline_tag and block_tag --- lib/review/htmlbuilder.rb | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 371fa53a1..480c67e14 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -861,9 +861,9 @@ def inline_fn(id) def compile_ruby(base, ruby) if @book.config["htmlversion"].to_i == 5 - %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{escape_html(ruby)}#{I18n.t("ruby_postfix")}] + %Q[#{(base)}#{I18n.t("ruby_prefix")}#{(ruby)}#{I18n.t("ruby_postfix")}] else - %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] + %Q[#{(base)}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] end end @@ -885,26 +885,26 @@ def inline_b(str) end def inline_ami(str) - %Q(#{escape_html(str)}) + %Q(#{(str)}) end def inline_bou(str) - %Q(#{escape_html(str)}) + %Q(#{(str)}) end def inline_tti(str) if @book.config["htmlversion"].to_i == 5 - %Q(#{escape_html(str)}) + %Q(#{(str)}) else - %Q(#{escape_html(str)}) + %Q(#{(str)}) end end def inline_ttb(str) if @book.config["htmlversion"].to_i == 5 - %Q(#{escape_html(str)}) + %Q(#{(str)}) else - %Q(#{escape_html(str)}) + %Q(#{(str)}) end end @@ -914,14 +914,14 @@ def inline_dtp(str) def inline_code(str) if @book.config["htmlversion"].to_i == 5 - %Q(#{escape_html(str)}) + %Q(#{(str)}) else - %Q(#{escape_html(str)}) + %Q(#{(str)}) end end def inline_idx(str) - %Q(#{escape_html(str)}) + %Q(#{(str)}) end def inline_hidx(str) @@ -940,7 +940,7 @@ def inline_m(str) :symbol => MathML::Symbol::CharacterReference) %Q[#{parser.parse(str, nil)}] else - %Q[#{escape_html(str)}] + %Q[#{(str)}] end end @@ -996,9 +996,9 @@ def column_label(id) def inline_column(id) if @book.config["chapterlink"] - %Q(#{I18n.t("column", escape_html(@chapter.column(id).caption))}) + %Q(#{I18n.t("column", (@chapter.column(id).caption))}) else - I18n.t("column", escape_html(@chapter.column(id).caption)) + I18n.t("column", (@chapter.column(id).caption)) end rescue KeyError error "unknown column: #{id}" @@ -1054,7 +1054,7 @@ def inline_img(id) end def inline_asis(str, tag) - %Q(<#{tag}>#{escape_html(str)}) + %Q(<#{tag}>#{(str)}) end def inline_abbr(str) @@ -1111,9 +1111,9 @@ def inline_sup(str) def inline_tt(str) if @book.config["htmlversion"].to_i == 5 - %Q(#{escape_html(str)}) + %Q(#{(str)}) else - %Q(#{escape_html(str)}) + %Q(#{(str)}) end end @@ -1126,11 +1126,11 @@ def inline_ins(str) end def inline_u(str) - %Q(#{escape_html(str)}) + %Q(#{(str)}) end def inline_recipe(str) - %Q(「#{escape_html(str)}」) + %Q(「#{(str)}」) end def inline_icon(id) @@ -1147,7 +1147,7 @@ def inline_uchar(str) def inline_comment(str) if @book.config["draft"] - %Q(#{escape_html(str)}) + %Q(#{(str)}) else %Q() end @@ -1162,7 +1162,7 @@ def nofunc_text(str) end def compile_href(url, label) - %Q(#{label.nil? ? escape_html(url) : escape_html(label)}) + %Q(#{label.nil? ? (url) : (label)}) end def flushright(lines) From 738be858df907017a5dd8858b911cb3b68b8784b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 25 Jan 2015 02:01:03 +0900 Subject: [PATCH 126/214] add nesting test --- test/test_htmlbuilder.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 10f5cb174..384e4d1c3 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -875,4 +875,23 @@ def test_inline_fn_with_tricky_id EOS assert_equal expected, actual end + + + ## inline nesting tests + def test_href_nest + actual = compile_inline("@{http://github.com,@{G}itHub}") + assert_equal %Q|GitHub|, actual + end + + def test_inline_tti_nest + actual = compile_inline("test @{aa@{inline test}bb} test2") + assert_equal %Q|test aainline testbb test2|, actual + end + + def test_inline_ttib_nest + actual = compile_inline("test @{aa@{inline @{te}st}bb} test2") + assert_equal %Q|test aainline testbb test2|, actual + end + + end From 9871f5d5a7bacc260285d142fa97bb159d4afe00 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 25 Jan 2015 02:01:20 +0900 Subject: [PATCH 127/214] fix inline rules --- lib/review/compiler.rb | 171 ++++++++++++++++++----------------------- lib/review/review.kpeg | 14 ++-- 2 files changed, 83 insertions(+), 102 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 5b1a5e2f4..ba981dde8 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1347,104 +1347,18 @@ def _Paragraph return _tmp end - # ParagraphSub = (InlineElement:c { c } | ContentText:c { c })+:d { e=d.flatten } Newline { e } + # ParagraphSub = Inline+:d { e=d.flatten } Newline { e } def _ParagraphSub _save = self.pos while true # sequence _save1 = self.pos _ary = [] - - _save2 = self.pos - while true # choice - - _save3 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save3 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - - _save4 = self.pos - while true # sequence - _tmp = apply(:_ContentText) - c = @result - unless _tmp - self.pos = _save4 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save2 - break - end # end choice - + _tmp = apply(:_Inline) if _tmp _ary << @result while true - - _save5 = self.pos - while true # choice - - _save6 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save6 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save6 - end - break - end # end sequence - - break if _tmp - self.pos = _save5 - - _save7 = self.pos - while true # sequence - _tmp = apply(:_ContentText) - c = @result - unless _tmp - self.pos = _save7 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save7 - end - break - end # end sequence - - break if _tmp - self.pos = _save5 - break - end # end choice - + _tmp = apply(:_Inline) _ary << @result if _tmp break unless _tmp end @@ -1481,6 +1395,24 @@ def _ParagraphSub return _tmp end + # Inline = (InlineElement | ContentText) + def _Inline + + _save = self.pos + while true # choice + _tmp = apply(:_InlineElement) + break if _tmp + self.pos = _save + _tmp = apply(:_ContentText) + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_Inline unless _tmp + return _tmp + end + # ContentText = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } def _ContentText @@ -2437,21 +2369,58 @@ def _InlineElementContentsSub return _tmp end - # InlineElementContent = (InlineElement:content {inline_element_content(self, content)} | InlineElementContentText+:content {inline_element_content(self, content)}) + # InlineElementContent = InlineElementContentSub+:d { d } def _InlineElementContent + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_InlineElementContentSub) + if _tmp + _ary << @result + while true + _tmp = apply(:_InlineElementContentSub) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + d = @result + unless _tmp + self.pos = _save + break + end + @result = begin; d ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementContent unless _tmp + return _tmp + end + + # InlineElementContentSub = (InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)}) + def _InlineElementContentSub + _save = self.pos while true # choice _save1 = self.pos while true # sequence _tmp = apply(:_InlineElement) - content = @result + c = @result unless _tmp self.pos = _save1 break end - @result = begin; inline_element_content(self, content); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save1 @@ -2465,6 +2434,14 @@ def _InlineElementContent _save2 = self.pos while true # sequence _save3 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _save4 = self.pos _ary = [] _tmp = apply(:_InlineElementContentText) if _tmp @@ -2477,7 +2454,7 @@ def _InlineElementContent _tmp = true @result = _ary else - self.pos = _save3 + self.pos = _save4 end content = @result unless _tmp @@ -2497,7 +2474,7 @@ def _InlineElementContent break end # end choice - set_failed_rule :_InlineElementContent unless _tmp + set_failed_rule :_InlineElementContentSub unless _tmp return _tmp end @@ -3936,7 +3913,8 @@ def _Spacechar Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "!/\\/\\/A-Za-z/ ParagraphSub+:c {paragraph(self, c.flatten)}") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "(InlineElement:c { c } | ContentText:c { c })+:d { e=d.flatten } Newline { e }") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "Inline+:d { e=d.flatten } Newline { e }") + Rules[:_Inline] = rule_info("Inline", "(InlineElement | ContentText)") Rules[:_ContentText] = rule_info("ContentText", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* Newline {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* Newline {block_element(self, symbol, args, nil)})") @@ -3949,7 +3927,8 @@ def _Spacechar Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ > { text }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") - Rules[:_InlineElementContent] = rule_info("InlineElementContent", "(InlineElement:content {inline_element_content(self, content)} | InlineElementContentText+:content {inline_element_content(self, content)})") + Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") + Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline*:content \"]\" {bracket_arg(self, content)}") Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 7f6b2027e..26e55dd7b 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -501,9 +501,9 @@ HeadlinePrefix = < /={1,5}/ > { text.length } Paragraph = !/\/\/A-Za-z/ ParagraphSub+:c ~paragraph(self, c.flatten) -ParagraphSub = ( InlineElement:c { c } - | ContentText:c { c } - )+:d { e=d.flatten } Newline { e } +ParagraphSub = Inline+:d { e=d.flatten } Newline { e } + +Inline = ( InlineElement | ContentText) ContentText = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } @@ -548,9 +548,11 @@ InlineElementContentsSub = !"}" ( InlineElementContent:c1 Space* "," Space* Inli ) -InlineElementContent = ( InlineElement:content ~inline_element_content(self, content) - | InlineElementContentText+:content ~inline_element_content(self, content) - ) +InlineElementContent = InlineElementContentSub+:d { d } + +InlineElementContentSub = ( InlineElement:c { c } + | !InlineElement InlineElementContentText+:content ~inline_element_content(self, content) + ) ## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? InlineElementContentText = ( "\\}" ~text(self, "}") From 12d8a0654e7c36807cf72049707f0283351ad85b Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 11 Feb 2015 20:06:19 +0900 Subject: [PATCH 128/214] kpeg: ContentInline should use NonInlineElement --- lib/review/compiler.rb | 33 +++------------------------------ lib/review/review.kpeg | 2 +- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index ba981dde8..4cb8b87d8 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -3089,7 +3089,7 @@ def _ContentInlines return _tmp end - # ContentInline = (InlineElement:c { c } | !Newline < NonNewLine > {text(self,text)}) + # ContentInline = (InlineElement:c { c } | NonInlineElement) def _ContentInline _save = self.pos @@ -3113,34 +3113,7 @@ def _ContentInline break if _tmp self.pos = _save - - _save2 = self.pos - while true # sequence - _save3 = self.pos - _tmp = apply(:_Newline) - _tmp = _tmp ? nil : true - self.pos = _save3 - unless _tmp - self.pos = _save2 - break - end - _text_start = self.pos - _tmp = apply(:_NonNewLine) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save2 - break - end - @result = begin; text(self,text); end - _tmp = true - unless _tmp - self.pos = _save2 - end - break - end # end sequence - + _tmp = apply(:_NonInlineElement) break if _tmp self.pos = _save break @@ -3940,7 +3913,7 @@ def _Spacechar Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c {singleline_content(self,c)}") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c }") - Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | !Newline < NonNewLine > {text(self,text)})") + Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | NonInlineElement)") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c (EOF | Newline) { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c (EOF | Newline) { @ulist_elem[-1].concat(c) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 26e55dd7b..d74a39e28 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -598,7 +598,7 @@ SinglelineContent = ContentInlines:c ~singleline_content(self,c) ContentInlines = ContentInline+:c { c } ContentInline = ( InlineElement:c { c } - | !Newline < NonNewLine > ~text(self,text) + | NonInlineElement ) ## ContentInlineText = !InlineElement !Newline .:cc { p [:char,cc];cc } From 87cfaf91aa2494e365c46e9da6d72b6245e18cdd Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 11 Feb 2015 21:09:12 +0900 Subject: [PATCH 129/214] kpeg: simplify more --- lib/review/compiler.rb | 89 +++++++++--------------------------------- lib/review/review.kpeg | 8 ++-- 2 files changed, 22 insertions(+), 75 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 4cb8b87d8..d03514d70 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1302,20 +1302,12 @@ def _HeadlinePrefix return _tmp end - # Paragraph = !/\/\/A-Za-z/ ParagraphSub+:c {paragraph(self, c.flatten)} + # Paragraph = ParagraphSub+:c {paragraph(self, c.flatten)} def _Paragraph _save = self.pos while true # sequence _save1 = self.pos - _tmp = scan(/\A(?-mix:\/\/A-Za-z)/) - _tmp = _tmp ? nil : true - self.pos = _save1 - unless _tmp - self.pos = _save - break - end - _save2 = self.pos _ary = [] _tmp = apply(:_ParagraphSub) if _tmp @@ -1328,7 +1320,7 @@ def _Paragraph _tmp = true @result = _ary else - self.pos = _save2 + self.pos = _save1 end c = @result unless _tmp @@ -1395,27 +1387,9 @@ def _ParagraphSub return _tmp end - # Inline = (InlineElement | ContentText) + # Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist (InlineElement | NonInlineElement) def _Inline - _save = self.pos - while true # choice - _tmp = apply(:_InlineElement) - break if _tmp - self.pos = _save - _tmp = apply(:_ContentText) - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_Inline unless _tmp - return _tmp - end - - # ContentText = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } - def _ContentText - _save = self.pos while true # sequence _save1 = self.pos @@ -1466,35 +1440,25 @@ def _ContentText self.pos = _save break end + _save7 = self.pos - _ary = [] - _tmp = apply(:_NonInlineElement) - if _tmp - _ary << @result - while true - _tmp = apply(:_NonInlineElement) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else + while true # choice + _tmp = apply(:_InlineElement) + break if _tmp + self.pos = _save7 + _tmp = apply(:_NonInlineElement) + break if _tmp self.pos = _save7 - end - c = @result - unless _tmp - self.pos = _save break - end - @result = begin; c ; end - _tmp = true + end # end choice + unless _tmp self.pos = _save end break end # end sequence - set_failed_rule :_ContentText unless _tmp + set_failed_rule :_Inline unless _tmp return _tmp end @@ -3089,28 +3053,12 @@ def _ContentInlines return _tmp end - # ContentInline = (InlineElement:c { c } | NonInlineElement) + # ContentInline = (InlineElement | NonInlineElement) def _ContentInline _save = self.pos while true # choice - - _save1 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save1 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save1 - end - break - end # end sequence - + _tmp = apply(:_InlineElement) break if _tmp self.pos = _save _tmp = apply(:_NonInlineElement) @@ -3885,10 +3833,9 @@ def _Spacechar Rules[:_BlankLine] = rule_info("BlankLine", "Newline") Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") - Rules[:_Paragraph] = rule_info("Paragraph", "!/\\/\\/A-Za-z/ ParagraphSub+:c {paragraph(self, c.flatten)}") + Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") Rules[:_ParagraphSub] = rule_info("ParagraphSub", "Inline+:d { e=d.flatten } Newline { e }") - Rules[:_Inline] = rule_info("Inline", "(InlineElement | ContentText)") - Rules[:_ContentText] = rule_info("ContentText", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") + Rules[:_Inline] = rule_info("Inline", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist (InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* Newline {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* Newline {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") @@ -3913,7 +3860,7 @@ def _Spacechar Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c {singleline_content(self,c)}") Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c }") - Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement:c { c } | NonInlineElement)") + Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement | NonInlineElement)") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c (EOF | Newline) { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c (EOF | Newline) { @ulist_elem[-1].concat(c) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index d74a39e28..9638740e9 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -499,13 +499,13 @@ Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* Singlel HeadlinePrefix = < /={1,5}/ > { text.length } -Paragraph = !/\/\/A-Za-z/ ParagraphSub+:c ~paragraph(self, c.flatten) +Paragraph = ParagraphSub+:c ~paragraph(self, c.flatten) ParagraphSub = Inline+:d { e=d.flatten } Newline { e } -Inline = ( InlineElement | ContentText) +Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist ( InlineElement | NonInlineElement) -ContentText = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } +#ContentText = NonInlineElement NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) @@ -597,7 +597,7 @@ SinglelineContent = ContentInlines:c ~singleline_content(self,c) ContentInlines = ContentInline+:c { c } -ContentInline = ( InlineElement:c { c } +ContentInline = ( InlineElement | NonInlineElement ) From 814b6319c92e08210c9b8960530449cd70bc29f1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 02:02:19 +0900 Subject: [PATCH 130/214] kpeg: add EOL( Newline or EOF) --- lib/review/compiler.rb | 103 ++++++++++++++++------------------------- lib/review/review.kpeg | 18 +++---- test/test_compiler.rb | 10 ++-- 3 files changed, 54 insertions(+), 77 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index d03514d70..893ae1137 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1192,7 +1192,7 @@ def _BlankLine return _tmp end - # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) {headline(self, level, cmd, label, caption)} + # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)} def _Headline _save = self.pos @@ -1248,18 +1248,7 @@ def _Headline self.pos = _save break end - - _save5 = self.pos - while true # choice - _tmp = apply(:_Newline) - break if _tmp - self.pos = _save5 - _tmp = apply(:_EOF) - break if _tmp - self.pos = _save5 - break - end # end choice - + _tmp = apply(:_EOL) unless _tmp self.pos = _save break @@ -1496,7 +1485,7 @@ def _NonInlineElement return _tmp end - # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* Newline {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* Newline {block_element(self, symbol, args, nil)}) + # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)}) def _BlockElement _save = self.pos @@ -1548,7 +1537,7 @@ def _BlockElement self.pos = _save1 break end - _tmp = apply(:_Newline) + _tmp = apply(:_EOL) unless _tmp self.pos = _save1 break @@ -1643,7 +1632,7 @@ def _BlockElement self.pos = _save5 break end - _tmp = apply(:_Newline) + _tmp = apply(:_EOL) unless _tmp self.pos = _save5 break @@ -1702,7 +1691,7 @@ def _BlockElement self.pos = _save11 break end - _tmp = apply(:_Newline) + _tmp = apply(:_EOL) unless _tmp self.pos = _save11 break @@ -3149,7 +3138,7 @@ def _Ulist return _tmp end - # UlistElement = " "+ "*"+:level " "* SinglelineContent:c (EOF | Newline) { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } + # UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } def _UlistElement _save = self.pos @@ -3204,18 +3193,7 @@ def _UlistElement self.pos = _save break end - - _save4 = self.pos - while true # choice - _tmp = apply(:_EOF) - break if _tmp - self.pos = _save4 - _tmp = apply(:_Newline) - break if _tmp - self.pos = _save4 - break - end # end choice - + _tmp = apply(:_EOL) unless _tmp self.pos = _save break @@ -3232,7 +3210,7 @@ def _UlistElement return _tmp end - # UlistContLine = " " " "+ !"*" SinglelineContent:c (EOF | Newline) { @ulist_elem[-1].concat(c) } + # UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } def _UlistContLine _save = self.pos @@ -3271,18 +3249,7 @@ def _UlistContLine self.pos = _save break end - - _save3 = self.pos - while true # choice - _tmp = apply(:_EOF) - break if _tmp - self.pos = _save3 - _tmp = apply(:_Newline) - break if _tmp - self.pos = _save3 - break - end # end choice - + _tmp = apply(:_EOL) unless _tmp self.pos = _save break @@ -3364,7 +3331,7 @@ def _Olist return _tmp end - # OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c (EOF | Newline) {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } + # OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } def _OlistElement _save = self.pos @@ -3429,18 +3396,7 @@ def _OlistElement self.pos = _save break end - - _save4 = self.pos - while true # choice - _tmp = apply(:_EOF) - break if _tmp - self.pos = _save4 - _tmp = apply(:_Newline) - break if _tmp - self.pos = _save4 - break - end # end choice - + _tmp = apply(:_EOL) unless _tmp self.pos = _save break @@ -3626,7 +3582,7 @@ def _DlistElementContent return _tmp end - # SinglelineComment = "#@" < NonNewLine+ > Newline {singleline_comment(self, text)} + # SinglelineComment = "#@" < NonNewLine+ > EOL {singleline_comment(self, text)} def _SinglelineComment _save = self.pos @@ -3655,7 +3611,7 @@ def _SinglelineComment self.pos = _save break end - _tmp = apply(:_Newline) + _tmp = apply(:_EOL) unless _tmp self.pos = _save break @@ -3723,6 +3679,24 @@ def _Space return _tmp end + # EOL = (Newline | EOF) + def _EOL + + _save = self.pos + while true # choice + _tmp = apply(:_Newline) + break if _tmp + self.pos = _save + _tmp = apply(:_EOF) + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_EOL unless _tmp + return _tmp + end + # EOF = !. def _EOF _save = self.pos @@ -3831,13 +3805,13 @@ def _Spacechar Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") - Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline | EOF) {headline(self, level, cmd, label, caption)}") + Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") Rules[:_ParagraphSub] = rule_info("ParagraphSub", "Inline+:d { e=d.flatten } Newline { e }") Rules[:_Inline] = rule_info("Inline", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist (InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") - Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* Newline {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* Newline {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* Newline {block_element(self, symbol, args, nil)})") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })") Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewLine > { text })") @@ -3862,17 +3836,18 @@ def _Spacechar Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c }") Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement | NonInlineElement)") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") - Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c (EOF | Newline) { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") - Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c (EOF | Newline) { @ulist_elem[-1].concat(c) }") + Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") + Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) }") Rules[:_Olist] = rule_info("Olist", "{ @olist_elem = [] } (OlistElement | SinglelineComment)+:c {olist(self, @olist_elem)}") - Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { num=text } \".\" Space* SinglelineContent:c (EOF | Newline) {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) }") + Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { num=text } \".\" Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) }") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") - Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > Newline {singleline_comment(self, text)}") + Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > EOL {singleline_comment(self, text)}") Rules[:_NonNewLine] = rule_info("NonNewLine", "/[^\\r\\n]/") Rules[:_Digits] = rule_info("Digits", "Digit+:c { c }") Rules[:_Space] = rule_info("Space", "/[ \\t]/") + Rules[:_EOL] = rule_info("EOL", "(Newline | EOF)") Rules[:_EOF] = rule_info("EOF", "!.") Rules[:_ElementName] = rule_info("ElementName", "< LowerAlphabetAscii+ > { text }") Rules[:_Alphanumeric] = rule_info("Alphanumeric", "%literals.Alphanumeric") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 9638740e9..017bc36ed 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -495,7 +495,7 @@ Block = BlankLine* BlankLine = Newline -Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption (Newline|EOF) ~headline(self, level, cmd, label, caption) +Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL ~headline(self, level, cmd, label, caption) HeadlinePrefix = < /={1,5}/ > { text.length } @@ -509,11 +509,11 @@ Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist ( Inli NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) -BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* Newline +BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) - | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* Newline + | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL ~block_element(self, symbol, args, contents) - | !"//raw" "//" ElementName:symbol BracketArg*:args Space* Newline ~block_element(self, symbol, args, nil) + | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL ~block_element(self, symbol, args, nil) ) RawBlockBuilderSelect = "|" Space* RawBlockBuilderSelectSub:c Space* "|" { c } @@ -609,15 +609,15 @@ ContentInline = ( InlineElement Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ ~ulist(self, @ulist_elem) -UlistElement = " "+ "*"+:level " "* SinglelineContent:c (EOF|Newline) { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } -UlistContLine = " " " "+ !"*" SinglelineContent:c (EOF|Newline) { @ulist_elem[-1].concat(c) } +UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } +UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } ## 1. 番号付き箇条書き ## 行頭から… の指定がない Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c ~olist(self, @olist_elem) -OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c (EOF|Newline) {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } +OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } ## : 用語リスト ## 行頭から… の指定がない @@ -627,7 +627,7 @@ DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementCo DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } -SinglelineComment = ("#@" < NonNewLine+ > Newline) ~singleline_comment(self, text) +SinglelineComment = ("#@" < NonNewLine+ > EOL) ~singleline_comment(self, text) NonNewLine = /[^\r\n]/ @@ -636,6 +636,8 @@ Digits = Digit+:c { c } ## Space = /[  \t]/ Space = /[ \t]/ +EOL = (Newline|EOF) + EOF = !. ElementName = < LowerAlphabetAscii+ > { text } diff --git a/test/test_compiler.rb b/test/test_compiler.rb index befaa5dd2..0ca7497d3 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -36,27 +36,27 @@ def test_parse_args def test_parse_args_with_brace1 args = compile_blockelem("//dummy[fo[\\][\\]o][bar]", false) - assert_equal ["fo[][]o","bar"], args.map(&:to_doc) + assert_equal ["fo[][]o","bar"], args.parse_args(:doc, :doc) end def test_parse_args_with_brace2 args = compile_blockelem("//dummy[f\\]o\\]o][bar]", false) - assert_equal ["f]o]o","bar"], args.map(&:to_doc) + assert_equal ["f]o]o","bar"], args.parse_args(:doc, :doc) end def test_parse_args_with_backslash args = compile_blockelem("//dummy[foo][bar\\buz]", false) - assert_equal ["foo","bar\\buz"], args.map(&:to_doc) + assert_equal ["foo","bar\\buz"], args.parse_args(:doc, :doc) end def test_parse_args_with_backslash2 args = compile_blockelem("//dummy[foo][bar\\#\\[\\!]", false) - assert_equal ["foo","bar\\#\\[\\!"], args.map(&:to_doc) + assert_equal ["foo","bar\\#\\[\\!"], args.parse_args(:doc, :doc) end def test_parse_args_with_backslash3 args = compile_blockelem("//dummy[foo][bar\\\\buz]", false) - assert_equal ["foo","bar\\buz"], args.map(&:to_doc) + assert_equal ["foo","bar\\buz"], args.parse_args(:doc, :doc) end def test_compile_inline From 16345ab3d9fcd1c1d9e98a3c18726e6d3d12ca98 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 02:09:05 +0900 Subject: [PATCH 131/214] remove commentout --- lib/review/review.kpeg | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 017bc36ed..9106a076f 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -505,8 +505,6 @@ ParagraphSub = Inline+:d { e=d.flatten } Newline { e } Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist ( InlineElement | NonInlineElement) -#ContentText = NonInlineElement - NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL @@ -601,9 +599,6 @@ ContentInline = ( InlineElement | NonInlineElement ) -## ContentInlineText = !InlineElement !Newline .:cc { p [:char,cc];cc } - - ## * 箇条書き ## 行頭から… の指定がない From da43dcc66aeffacbb3cd3dca8e236d9264d40606 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 02:15:04 +0900 Subject: [PATCH 132/214] fix bug --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 9106a076f..921192324 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -576,7 +576,7 @@ BlockElementContents = BlockElementContent+:c { c } ## 各要素は Newline で終わらなければならない ### SinglelineContentではないような気がするけど改行は落としてあるのでとりあえずこれで -BlockElementContent = ( SinglelineComment:c ~singleline_content(self, c) +BlockElementContent = ( SinglelineComment:c { c } | BlockElement:c ~singleline_content(self, c) | BlockElementParagraph:c ~singleline_content(self, c) | Newline:c ~singleline_content(self, "") From dc47ba0acf5accac506eef237a6eb5e41b86150e Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 02:15:22 +0900 Subject: [PATCH 133/214] ContentInline and Inline should be same --- lib/review/compiler.rb | 172 ++++++++++++++--------------------------- lib/review/review.kpeg | 11 +-- 2 files changed, 63 insertions(+), 120 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 893ae1137..ebea4c0f2 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1328,57 +1328,9 @@ def _Paragraph return _tmp end - # ParagraphSub = Inline+:d { e=d.flatten } Newline { e } + # ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e } def _ParagraphSub - _save = self.pos - while true # sequence - _save1 = self.pos - _ary = [] - _tmp = apply(:_Inline) - if _tmp - _ary << @result - while true - _tmp = apply(:_Inline) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 - end - d = @result - unless _tmp - self.pos = _save - break - end - @result = begin; e=d.flatten ; end - _tmp = true - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_Newline) - unless _tmp - self.pos = _save - break - end - @result = begin; e ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_ParagraphSub unless _tmp - return _tmp - end - - # Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist (InlineElement | NonInlineElement) - def _Inline - _save = self.pos while true # sequence _save1 = self.pos @@ -1429,24 +1381,63 @@ def _Inline self.pos = _save break end - _save7 = self.pos - while true # choice - _tmp = apply(:_InlineElement) - break if _tmp - self.pos = _save7 - _tmp = apply(:_NonInlineElement) - break if _tmp + _ary = [] + _tmp = apply(:_Inline) + if _tmp + _ary << @result + while true + _tmp = apply(:_Inline) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else self.pos = _save7 + end + d = @result + unless _tmp + self.pos = _save break - end # end choice - + end + @result = begin; e=d.flatten ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end + @result = begin; e ; end + _tmp = true unless _tmp self.pos = _save end break end # end sequence + set_failed_rule :_ParagraphSub unless _tmp + return _tmp + end + + # Inline = (InlineElement | NonInlineElement) + def _Inline + + _save = self.pos + while true # choice + _tmp = apply(:_InlineElement) + break if _tmp + self.pos = _save + _tmp = apply(:_NonInlineElement) + break if _tmp + self.pos = _save + break + end # end choice + set_failed_rule :_Inline unless _tmp return _tmp end @@ -2747,7 +2738,7 @@ def _BlockElementContents return _tmp end - # BlockElementContent = (SinglelineComment:c {singleline_content(self, c)} | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, "")}) + # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, "")}) def _BlockElementContent _save = self.pos @@ -2761,7 +2752,7 @@ def _BlockElementContent self.pos = _save1 break end - @result = begin; singleline_content(self, c); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save1 @@ -2982,41 +2973,18 @@ def _BlockElementContentText return _tmp end - # SinglelineContent = ContentInlines:c {singleline_content(self,c)} + # SinglelineContent = Inline+:c {singleline_content(self,c)} def _SinglelineContent - _save = self.pos - while true # sequence - _tmp = apply(:_ContentInlines) - c = @result - unless _tmp - self.pos = _save - break - end - @result = begin; singleline_content(self,c); end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_SinglelineContent unless _tmp - return _tmp - end - - # ContentInlines = ContentInline+:c { c } - def _ContentInlines - _save = self.pos while true # sequence _save1 = self.pos _ary = [] - _tmp = apply(:_ContentInline) + _tmp = apply(:_Inline) if _tmp _ary << @result while true - _tmp = apply(:_ContentInline) + _tmp = apply(:_Inline) _ary << @result if _tmp break unless _tmp end @@ -3030,7 +2998,7 @@ def _ContentInlines self.pos = _save break end - @result = begin; c ; end + @result = begin; singleline_content(self,c); end _tmp = true unless _tmp self.pos = _save @@ -3038,25 +3006,7 @@ def _ContentInlines break end # end sequence - set_failed_rule :_ContentInlines unless _tmp - return _tmp - end - - # ContentInline = (InlineElement | NonInlineElement) - def _ContentInline - - _save = self.pos - while true # choice - _tmp = apply(:_InlineElement) - break if _tmp - self.pos = _save - _tmp = apply(:_NonInlineElement) - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_ContentInline unless _tmp + set_failed_rule :_SinglelineContent unless _tmp return _tmp end @@ -3808,8 +3758,8 @@ def _Spacechar Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "Inline+:d { e=d.flatten } Newline { e }") - Rules[:_Inline] = rule_info("Inline", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist (InlineElement | NonInlineElement)") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e }") + Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") @@ -3828,13 +3778,11 @@ def _Spacechar Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") - Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c {singleline_content(self, c)} | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") + Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c Newline { c.flatten }") Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c | BlockElementContentText:c)") Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") - Rules[:_SinglelineContent] = rule_info("SinglelineContent", "ContentInlines:c {singleline_content(self,c)}") - Rules[:_ContentInlines] = rule_info("ContentInlines", "ContentInline+:c { c }") - Rules[:_ContentInline] = rule_info("ContentInline", "(InlineElement | NonInlineElement)") + Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 921192324..1d9dde521 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -501,9 +501,9 @@ HeadlinePrefix = < /={1,5}/ > { text.length } Paragraph = ParagraphSub+:c ~paragraph(self, c.flatten) -ParagraphSub = Inline+:d { e=d.flatten } Newline { e } +ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e } -Inline = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist ( InlineElement | NonInlineElement) +Inline = ( InlineElement | NonInlineElement) NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) @@ -591,13 +591,8 @@ BlockElementParagraphSub = ( InlineElement:c BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } -SinglelineContent = ContentInlines:c ~singleline_content(self,c) +SinglelineContent = Inline+:c ~singleline_content(self,c) -ContentInlines = ContentInline+:c { c } - -ContentInline = ( InlineElement - | NonInlineElement - ) ## * 箇条書き ## 行頭から… の指定がない From 6d0a8bbcc16045b9ff2563e6a5e2053eca901955 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 03:00:08 +0900 Subject: [PATCH 134/214] kpeg: use Inline in BlockElementParagraph --- lib/review/compiler.rb | 81 ++++++------------------------------------ lib/review/review.kpeg | 8 +---- 2 files changed, 12 insertions(+), 77 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index ebea4c0f2..f9d7ccb06 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -2826,71 +2826,9 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = BlockElementParagraphSub+:c Newline { c.flatten } + # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten } def _BlockElementParagraph - _save = self.pos - while true # sequence - _save1 = self.pos - _ary = [] - _tmp = apply(:_BlockElementParagraphSub) - if _tmp - _ary << @result - while true - _tmp = apply(:_BlockElementParagraphSub) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 - end - c = @result - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_Newline) - unless _tmp - self.pos = _save - break - end - @result = begin; c.flatten ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_BlockElementParagraph unless _tmp - return _tmp - end - - # BlockElementParagraphSub = (InlineElement:c | BlockElementContentText:c) - def _BlockElementParagraphSub - - _save = self.pos - while true # choice - _tmp = apply(:_InlineElement) - c = @result - break if _tmp - self.pos = _save - _tmp = apply(:_BlockElementContentText) - c = @result - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_BlockElementParagraphSub unless _tmp - return _tmp - end - - # BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } - def _BlockElementContentText - _save = self.pos while true # sequence _save1 = self.pos @@ -2943,11 +2881,11 @@ def _BlockElementContentText end _save7 = self.pos _ary = [] - _tmp = apply(:_NonInlineElement) + _tmp = apply(:_Inline) if _tmp _ary << @result while true - _tmp = apply(:_NonInlineElement) + _tmp = apply(:_Inline) _ary << @result if _tmp break unless _tmp end @@ -2961,7 +2899,12 @@ def _BlockElementContentText self.pos = _save break end - @result = begin; c ; end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end + @result = begin; c.flatten ; end _tmp = true unless _tmp self.pos = _save @@ -2969,7 +2912,7 @@ def _BlockElementContentText break end # end sequence - set_failed_rule :_BlockElementContentText unless _tmp + set_failed_rule :_BlockElementParagraph unless _tmp return _tmp end @@ -3779,9 +3722,7 @@ def _Spacechar Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c Newline { c.flatten }") - Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "(InlineElement:c | BlockElementContentText:c)") - Rules[:_BlockElementContentText] = rule_info("BlockElementContentText", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c }") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 1d9dde521..b03a5e491 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -583,13 +583,7 @@ BlockElementContent = ( SinglelineComment:c { c } ) ## flattenでよい? -BlockElementParagraph = BlockElementParagraphSub+:c Newline { c.flatten } - -BlockElementParagraphSub = ( InlineElement:c - | BlockElementContentText:c - ) - -BlockElementContentText = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist NonInlineElement+:c { c } +BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten } SinglelineContent = Inline+:c ~singleline_content(self,c) From 6bf16c15c07df21a6f12138ed557a7657114a5dd Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 03:26:41 +0900 Subject: [PATCH 135/214] kpeg: Inline+ -> SinglelineContent --- lib/review/compiler.rb | 52 ++++++++---------------------------------- lib/review/review.kpeg | 4 ++-- test/test_compiler.rb | 13 ++++++----- test/test_helper.rb | 3 ++- 4 files changed, 20 insertions(+), 52 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index f9d7ccb06..122700098 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1328,7 +1328,7 @@ def _Paragraph return _tmp end - # ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e } + # ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } def _ParagraphSub _save = self.pos @@ -1381,28 +1381,8 @@ def _ParagraphSub self.pos = _save break end - _save7 = self.pos - _ary = [] - _tmp = apply(:_Inline) - if _tmp - _ary << @result - while true - _tmp = apply(:_Inline) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save7 - end - d = @result - unless _tmp - self.pos = _save - break - end - @result = begin; e=d.flatten ; end - _tmp = true + _tmp = apply(:_SinglelineContent) + c = @result unless _tmp self.pos = _save break @@ -1412,7 +1392,7 @@ def _ParagraphSub self.pos = _save break end - @result = begin; e ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -2826,7 +2806,7 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten } + # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } def _BlockElementParagraph _save = self.pos @@ -2879,21 +2859,7 @@ def _BlockElementParagraph self.pos = _save break end - _save7 = self.pos - _ary = [] - _tmp = apply(:_Inline) - if _tmp - _ary << @result - while true - _tmp = apply(:_Inline) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save7 - end + _tmp = apply(:_SinglelineContent) c = @result unless _tmp self.pos = _save @@ -2904,7 +2870,7 @@ def _BlockElementParagraph self.pos = _save break end - @result = begin; c.flatten ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -3701,7 +3667,7 @@ def _Spacechar Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e }") + Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") @@ -3722,7 +3688,7 @@ def _Spacechar Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten }") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index b03a5e491..1a482bd3d 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -501,7 +501,7 @@ HeadlinePrefix = < /={1,5}/ > { text.length } Paragraph = ParagraphSub+:c ~paragraph(self, c.flatten) -ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:d { e=d.flatten } Newline { e } +ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } Inline = ( InlineElement | NonInlineElement) @@ -583,7 +583,7 @@ BlockElementContent = ( SinglelineComment:c { c } ) ## flattenでよい? -BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist Inline+:c Newline { c.flatten } +BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } SinglelineContent = Inline+:c ~singleline_content(self,c) diff --git a/test/test_compiler.rb b/test/test_compiler.rb index 0ca7497d3..99aa15b87 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -4,6 +4,7 @@ require 'review/compiler' require 'review/book' require 'review/latexbuilder' +require 'review/htmlbuilder' class CompilerTest < Test::Unit::TestCase include ReVIEW @@ -60,11 +61,11 @@ def test_parse_args_with_backslash3 end def test_compile_inline - def @compiler.compile_inline(op, args) - return args.map(&:to_doc).join("") + def @compiler.inline_ruby(*args) + return args end args = compile_inline("@{abc}",false) - assert_equal "abc", args.map(&:to_doc).join("") + assert_equal "abc", args.content[0].content.to_doc end def test_inline_ruby @@ -75,11 +76,11 @@ def test_inline_ruby end def test_compile_inline_backslash - def @compiler.compile_inline(op, args) - return args.map(&:to_doc).join("") + def @compiler.inline_dummy(*args) + return args end args = compile_inline("@{abc\\d\\#a}", false) - assert_equal "abc\\d\\#a", args.map(&:to_doc).join("") + assert_equal "abc\\d\\#a", args.content[0].content.to_doc end end diff --git a/test/test_helper.rb b/test/test_helper.rb index a11e5f38f..09d222592 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,7 +25,8 @@ def compile_inline(str, force_to_s=true) @compiler.setup_parser(str) @compiler.parse("Paragraph") if force_to_s - @compiler.result.map(&:to_doc).join +## @compiler.result.map(&:to_doc).join + @compiler.result.to_doc else @compiler.result end From ae0f88ddcae3dcd2f06c38a5ccd924cd46018ed9 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 03:32:30 +0900 Subject: [PATCH 136/214] kpeg: fix rule order --- lib/review/compiler.rb | 2046 ++++++++++++++++++++-------------------- lib/review/review.kpeg | 81 +- 2 files changed, 1063 insertions(+), 1064 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 122700098..0b2f023a1 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1192,6 +1192,52 @@ def _BlankLine return _tmp end + # SinglelineComment = "#@" < NonNewLine+ > EOL {singleline_comment(self, text)} + def _SinglelineComment + + _save = self.pos + while true # sequence + _tmp = match_string("\#@") + unless _tmp + self.pos = _save + break + end + _text_start = self.pos + _save1 = self.pos + _tmp = apply(:_NonNewLine) + if _tmp + while true + _tmp = apply(:_NonNewLine) + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_EOL) + unless _tmp + self.pos = _save + break + end + @result = begin; singleline_comment(self, text); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_SinglelineComment unless _tmp + return _tmp + end + # Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)} def _Headline @@ -1404,58 +1450,6 @@ def _ParagraphSub return _tmp end - # Inline = (InlineElement | NonInlineElement) - def _Inline - - _save = self.pos - while true # choice - _tmp = apply(:_InlineElement) - break if _tmp - self.pos = _save - _tmp = apply(:_NonInlineElement) - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_Inline unless _tmp - return _tmp - end - - # NonInlineElement = !InlineElement < NonNewLine > {text(self, text)} - def _NonInlineElement - - _save = self.pos - while true # sequence - _save1 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save1 - unless _tmp - self.pos = _save - break - end - _text_start = self.pos - _tmp = apply(:_NonNewLine) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save - break - end - @result = begin; text(self, text); end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_NonInlineElement unless _tmp - return _tmp - end - # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)}) def _BlockElement @@ -1929,15 +1923,55 @@ def _RawBlockElementArg return _tmp end - # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" {inline_element(self, symbol,contents)}) - def _InlineElement + # BracketArg = "[" BracketArgContentInline*:content "]" {bracket_arg(self, content)} + def _BracketArg + + _save = self.pos + while true # sequence + _tmp = match_string("[") + unless _tmp + self.pos = _save + break + end + _ary = [] + while true + _tmp = apply(:_BracketArgContentInline) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + content = @result + unless _tmp + self.pos = _save + break + end + _tmp = match_string("]") + unless _tmp + self.pos = _save + break + end + @result = begin; bracket_arg(self, content); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BracketArg unless _tmp + return _tmp + end + + # BracketArgContentInline = (InlineElement:c { c } | "\\]" {text(self, "]")} | "\\\\" {text(self, "\\")} | < /[^\r\n\]]/ > {text(self, text)}) + def _BracketArgContentInline _save = self.pos while true # choice _save1 = self.pos while true # sequence - _tmp = apply(:_RawInlineElement) + _tmp = apply(:_InlineElement) c = @result unless _tmp self.pos = _save1 @@ -1956,56 +1990,55 @@ def _InlineElement _save2 = self.pos while true # sequence - _save3 = self.pos - _tmp = apply(:_RawInlineElement) - _tmp = _tmp ? nil : true - self.pos = _save3 + _tmp = match_string("\\]") unless _tmp self.pos = _save2 break end - _tmp = match_string("@<") + @result = begin; text(self, "]"); end + _tmp = true unless _tmp self.pos = _save2 - break end - _tmp = apply(:_InlineElementSymbol) - symbol = @result + break + end # end sequence + + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _tmp = match_string("\\\\") unless _tmp - self.pos = _save2 + self.pos = _save3 break end - _tmp = match_string(">") + @result = begin; text(self, "\\"); end + _tmp = true unless _tmp - self.pos = _save2 - break + self.pos = _save3 end - _tmp = match_string("{") - unless _tmp - self.pos = _save2 - break + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\]])/) + if _tmp + text = get_text(_text_start) end - _save4 = self.pos - _tmp = apply(:_InlineElementContents) - @result = nil unless _tmp unless _tmp - _tmp = true self.pos = _save4 - end - contents = @result - unless _tmp - self.pos = _save2 - break - end - _tmp = match_string("}") - unless _tmp - self.pos = _save2 break end - @result = begin; inline_element(self, symbol,contents); end + @result = begin; text(self, text); end _tmp = true unless _tmp - self.pos = _save2 + self.pos = _save4 end break end # end sequence @@ -2015,83 +2048,98 @@ def _InlineElement break end # end choice - set_failed_rule :_InlineElement unless _tmp + set_failed_rule :_BracketArgContentInline unless _tmp return _tmp end - # RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" {raw(self, builders,c)} - def _RawInlineElement + # BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } + def _BraceArg _save = self.pos while true # sequence - _tmp = match_string("@{") + _tmp = match_string("{") unless _tmp self.pos = _save break end - _save1 = self.pos - _tmp = apply(:_RawBlockBuilderSelect) - @result = nil unless _tmp + _text_start = self.pos + _tmp = scan(/\A(?-mix:([^\r\n}\\]|\\[^\r\n])*)/) + if _tmp + text = get_text(_text_start) + end unless _tmp - _tmp = true - self.pos = _save1 + self.pos = _save + break end - builders = @result + _tmp = match_string("}") unless _tmp self.pos = _save break end - _save2 = self.pos + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BraceArg unless _tmp + return _tmp + end + + # BlockElementContents = BlockElementContent+:c { c } + def _BlockElementContents + + _save = self.pos + while true # sequence + _save1 = self.pos _ary = [] - _tmp = apply(:_RawInlineElementContent) + _tmp = apply(:_BlockElementContent) if _tmp _ary << @result while true - _tmp = apply(:_RawInlineElementContent) + _tmp = apply(:_BlockElementContent) _ary << @result if _tmp break unless _tmp end _tmp = true @result = _ary else - self.pos = _save2 + self.pos = _save1 end c = @result unless _tmp self.pos = _save break end - _tmp = match_string("}") - unless _tmp - self.pos = _save - break - end - @result = begin; raw(self, builders,c); end - _tmp = true + @result = begin; c ; end + _tmp = true unless _tmp self.pos = _save end break end # end sequence - set_failed_rule :_RawInlineElement unless _tmp + set_failed_rule :_BlockElementContents unless _tmp return _tmp end - # RawInlineElementContent = ("\\}" { "}" } | < /[^\r\n\}]/ > { text }) - def _RawInlineElementContent + # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, "")}) + def _BlockElementContent _save = self.pos while true # choice _save1 = self.pos while true # sequence - _tmp = match_string("\\}") + _tmp = apply(:_SinglelineComment) + c = @result unless _tmp self.pos = _save1 break end - @result = begin; "}" ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save1 @@ -2104,16 +2152,13 @@ def _RawInlineElementContent _save2 = self.pos while true # sequence - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n\}])/) - if _tmp - text = get_text(_text_start) - end + _tmp = apply(:_BlockElement) + c = @result unless _tmp self.pos = _save2 break end - @result = begin; text ; end + @result = begin; singleline_content(self, c); end _tmp = true unless _tmp self.pos = _save2 @@ -2121,70 +2166,117 @@ def _RawInlineElementContent break end # end sequence + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _tmp = apply(:_BlockElementParagraph) + c = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; singleline_content(self, c); end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos + while true # sequence + _tmp = apply(:_Newline) + c = @result + unless _tmp + self.pos = _save4 + break + end + @result = begin; singleline_content(self, ""); end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + break if _tmp self.pos = _save break end # end choice - set_failed_rule :_RawInlineElementContent unless _tmp + set_failed_rule :_BlockElementContent unless _tmp return _tmp end - # InlineElementSymbol = < AlphanumericAscii+ > { text } - def _InlineElementSymbol + # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } + def _BlockElementParagraph _save = self.pos while true # sequence - _text_start = self.pos _save1 = self.pos - _tmp = apply(:_AlphanumericAscii) - if _tmp - while true - _tmp = apply(:_AlphanumericAscii) - break unless _tmp - end - _tmp = true - else - self.pos = _save1 + _tmp = match_string("//}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break end - if _tmp - text = get_text(_text_start) + _save2 = self.pos + _tmp = apply(:_SinglelineComment) + _tmp = _tmp ? nil : true + self.pos = _save2 + unless _tmp + self.pos = _save + break end + _save3 = self.pos + _tmp = apply(:_BlockElement) + _tmp = _tmp ? nil : true + self.pos = _save3 unless _tmp self.pos = _save break end - @result = begin; text ; end - _tmp = true + _save4 = self.pos + _tmp = apply(:_Ulist) + _tmp = _tmp ? nil : true + self.pos = _save4 unless _tmp self.pos = _save + break end - break - end # end sequence - - set_failed_rule :_InlineElementSymbol unless _tmp - return _tmp - end - - # InlineElementContents = !"}" InlineElementContentsSub:c { c } - def _InlineElementContents - - _save = self.pos - while true # sequence - _save1 = self.pos - _tmp = match_string("}") + _save5 = self.pos + _tmp = apply(:_Olist) _tmp = _tmp ? nil : true - self.pos = _save1 + self.pos = _save5 unless _tmp self.pos = _save break end - _tmp = apply(:_InlineElementContentsSub) + _save6 = self.pos + _tmp = apply(:_Dlist) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) c = @result unless _tmp self.pos = _save break end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end @result = begin; c ; end _tmp = true unless _tmp @@ -2193,358 +2285,77 @@ def _InlineElementContents break end # end sequence - set_failed_rule :_InlineElementContents unless _tmp + set_failed_rule :_BlockElementParagraph unless _tmp return _tmp end - # InlineElementContentsSub = !"}" (InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] }) - def _InlineElementContentsSub + # Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)} + def _Ulist _save = self.pos while true # sequence _save1 = self.pos - _tmp = match_string("}") - _tmp = _tmp ? nil : true + _tmp = get_byte self.pos = _save1 unless _tmp self.pos = _save break end - + @result = begin; @ulist_elem=[] ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_UlistElement) + unless _tmp + self.pos = _save + break + end _save2 = self.pos - while true # choice - - _save3 = self.pos - while true # sequence - _tmp = apply(:_InlineElementContent) - c1 = @result - unless _tmp - self.pos = _save3 - break - end - while true - _tmp = apply(:_Space) - break unless _tmp - end - _tmp = true - unless _tmp - self.pos = _save3 - break - end - _tmp = match_string(",") - unless _tmp - self.pos = _save3 - break - end - while true - _tmp = apply(:_Space) - break unless _tmp - end - _tmp = true - unless _tmp - self.pos = _save3 - break - end - _tmp = apply(:_InlineElementContentsSub) - c2 = @result - unless _tmp - self.pos = _save3 - break - end - @result = begin; [c1]+c2 ; end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence + _save3 = self.pos + while true # choice + _tmp = apply(:_UlistElement) break if _tmp - self.pos = _save2 - - _save6 = self.pos - while true # sequence - _tmp = apply(:_InlineElementContent) - c1 = @result - unless _tmp - self.pos = _save6 - break - end - @result = begin; [c1] ; end - _tmp = true - unless _tmp - self.pos = _save6 - end - break - end # end sequence - + self.pos = _save3 + _tmp = apply(:_UlistContLine) break if _tmp - self.pos = _save2 + self.pos = _save3 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save3 break end # end choice - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_InlineElementContentsSub unless _tmp - return _tmp - end - - # InlineElementContent = InlineElementContentSub+:d { d } - def _InlineElementContent - - _save = self.pos - while true # sequence - _save1 = self.pos - _ary = [] - _tmp = apply(:_InlineElementContentSub) if _tmp - _ary << @result while true - _tmp = apply(:_InlineElementContentSub) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 - end - d = @result - unless _tmp - self.pos = _save - break - end - @result = begin; d ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_InlineElementContent unless _tmp - return _tmp - end - - # InlineElementContentSub = (InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)}) - def _InlineElementContentSub - - _save = self.pos - while true # choice - - _save1 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save1 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save1 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save2 = self.pos - while true # sequence - _save3 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save3 - unless _tmp - self.pos = _save2 - break - end - _save4 = self.pos - _ary = [] - _tmp = apply(:_InlineElementContentText) - if _tmp - _ary << @result - while true - _tmp = apply(:_InlineElementContentText) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save4 - end - content = @result - unless _tmp - self.pos = _save2 - break - end - @result = begin; inline_element_content(self, content); end - _tmp = true - unless _tmp - self.pos = _save2 - end - break - end # end sequence - - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_InlineElementContentSub unless _tmp - return _tmp - end - - # InlineElementContentText = ("\\}" {text(self, "}")} | "\\," {text(self, ",")} | "\\\\" {text(self, "\\" )} | "\\" {text(self, "\\" )} | !InlineElement < /[^\r\n\\},]/ > {text(self,text)}) - def _InlineElementContentText - _save = self.pos - while true # choice - - _save1 = self.pos - while true # sequence - _tmp = match_string("\\}") - unless _tmp - self.pos = _save1 - break - end - @result = begin; text(self, "}"); end - _tmp = true - unless _tmp - self.pos = _save1 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save2 = self.pos - while true # sequence - _tmp = match_string("\\,") - unless _tmp - self.pos = _save2 - break - end - @result = begin; text(self, ","); end - _tmp = true - unless _tmp - self.pos = _save2 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save3 = self.pos - while true # sequence - _tmp = match_string("\\\\") - unless _tmp - self.pos = _save3 - break - end - @result = begin; text(self, "\\" ); end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save4 = self.pos - while true # sequence - _tmp = match_string("\\") - unless _tmp - self.pos = _save4 - break - end - @result = begin; text(self, "\\" ); end - _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save + _save4 = self.pos + while true # choice + _tmp = apply(:_UlistElement) + break if _tmp + self.pos = _save4 + _tmp = apply(:_UlistContLine) + break if _tmp + self.pos = _save4 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save4 + break + end # end choice - _save5 = self.pos - while true # sequence - _save6 = self.pos - _tmp = apply(:_InlineElement) - _tmp = _tmp ? nil : true - self.pos = _save6 - unless _tmp - self.pos = _save5 - break - end - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n\\},])/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save5 - break + break unless _tmp end - @result = begin; text(self,text); end _tmp = true - unless _tmp - self.pos = _save5 - end - break - end # end sequence - - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_InlineElementContentText unless _tmp - return _tmp - end - - # BracketArg = "[" BracketArgContentInline*:content "]" {bracket_arg(self, content)} - def _BracketArg - - _save = self.pos - while true # sequence - _tmp = match_string("[") - unless _tmp - self.pos = _save - break - end - _ary = [] - while true - _tmp = apply(:_BracketArgContentInline) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - content = @result - unless _tmp - self.pos = _save - break + else + self.pos = _save2 end - _tmp = match_string("]") unless _tmp self.pos = _save break end - @result = begin; bracket_arg(self, content); end + @result = begin; ulist(self, @ulist_elem); end _tmp = true unless _tmp self.pos = _save @@ -2552,309 +2363,111 @@ def _BracketArg break end # end sequence - set_failed_rule :_BracketArg unless _tmp + set_failed_rule :_Ulist unless _tmp return _tmp end - # BracketArgContentInline = (InlineElement:c { c } | "\\]" {text(self, "]")} | "\\\\" {text(self, "\\")} | < /[^\r\n\]]/ > {text(self, text)}) - def _BracketArgContentInline + # UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } + def _UlistElement _save = self.pos - while true # choice - + while true # sequence _save1 = self.pos - while true # sequence - _tmp = apply(:_InlineElement) - c = @result - unless _tmp - self.pos = _save1 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save1 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save2 = self.pos - while true # sequence - _tmp = match_string("\\]") - unless _tmp - self.pos = _save2 - break - end - @result = begin; text(self, "]"); end - _tmp = true - unless _tmp - self.pos = _save2 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save3 = self.pos - while true # sequence - _tmp = match_string("\\\\") - unless _tmp - self.pos = _save3 - break - end - @result = begin; text(self, "\\"); end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save4 = self.pos - while true # sequence - _text_start = self.pos - _tmp = scan(/\A(?-mix:[^\r\n\]])/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save4 - break + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp end - @result = begin; text(self, text); end _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_BracketArgContentInline unless _tmp - return _tmp - end - - # BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } - def _BraceArg - - _save = self.pos - while true # sequence - _tmp = match_string("{") - unless _tmp - self.pos = _save - break - end - _text_start = self.pos - _tmp = scan(/\A(?-mix:([^\r\n}\\]|\\[^\r\n])*)/) - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save - break + else + self.pos = _save1 end - _tmp = match_string("}") unless _tmp self.pos = _save break end - @result = begin; text ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_BraceArg unless _tmp - return _tmp - end - - # BlockElementContents = BlockElementContent+:c { c } - def _BlockElementContents - - _save = self.pos - while true # sequence - _save1 = self.pos + _save2 = self.pos _ary = [] - _tmp = apply(:_BlockElementContent) + _tmp = match_string("*") if _tmp _ary << @result while true - _tmp = apply(:_BlockElementContent) + _tmp = match_string("*") _ary << @result if _tmp break unless _tmp end _tmp = true @result = _ary else - self.pos = _save1 + self.pos = _save2 end - c = @result + level = @result unless _tmp self.pos = _save break end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save + while true + _tmp = match_string(" ") + break unless _tmp end - break - end # end sequence - - set_failed_rule :_BlockElementContents unless _tmp - return _tmp - end - - # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, "")}) - def _BlockElementContent - - _save = self.pos - while true # choice - - _save1 = self.pos - while true # sequence - _tmp = apply(:_SinglelineComment) - c = @result - unless _tmp - self.pos = _save1 - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save1 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save2 = self.pos - while true # sequence - _tmp = apply(:_BlockElement) - c = @result - unless _tmp - self.pos = _save2 - break - end - @result = begin; singleline_content(self, c); end - _tmp = true - unless _tmp - self.pos = _save2 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save3 = self.pos - while true # sequence - _tmp = apply(:_BlockElementParagraph) - c = @result - unless _tmp - self.pos = _save3 - break - end - @result = begin; singleline_content(self, c); end - _tmp = true - unless _tmp - self.pos = _save3 - end - break - end # end sequence - - break if _tmp - self.pos = _save - - _save4 = self.pos - while true # sequence - _tmp = apply(:_Newline) - c = @result - unless _tmp - self.pos = _save4 - break - end - @result = begin; singleline_content(self, ""); end - _tmp = true - unless _tmp - self.pos = _save4 - end - break - end # end sequence - - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_BlockElementContent unless _tmp - return _tmp - end - - # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } - def _BlockElementParagraph - - _save = self.pos - while true # sequence - _save1 = self.pos - _tmp = match_string("//}") - _tmp = _tmp ? nil : true - self.pos = _save1 + _tmp = true unless _tmp self.pos = _save break end - _save2 = self.pos - _tmp = apply(:_SinglelineComment) - _tmp = _tmp ? nil : true - self.pos = _save2 + _tmp = apply(:_SinglelineContent) + c = @result unless _tmp self.pos = _save break end - _save3 = self.pos - _tmp = apply(:_BlockElement) - _tmp = _tmp ? nil : true - self.pos = _save3 + _tmp = apply(:_EOL) unless _tmp self.pos = _save break end - _save4 = self.pos - _tmp = apply(:_Ulist) - _tmp = _tmp ? nil : true - self.pos = _save4 + @result = begin; @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_UlistElement unless _tmp + return _tmp + end + + # UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } + def _UlistContLine + + _save = self.pos + while true # sequence + _tmp = match_string(" ") unless _tmp self.pos = _save break end - _save5 = self.pos - _tmp = apply(:_Olist) - _tmp = _tmp ? nil : true - self.pos = _save5 + _save1 = self.pos + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end unless _tmp self.pos = _save break end - _save6 = self.pos - _tmp = apply(:_Dlist) + _save2 = self.pos + _tmp = match_string("*") _tmp = _tmp ? nil : true - self.pos = _save6 + self.pos = _save2 unless _tmp self.pos = _save break @@ -2865,12 +2478,12 @@ def _BlockElementParagraph self.pos = _save break end - _tmp = apply(:_Newline) + _tmp = apply(:_EOL) unless _tmp self.pos = _save break end - @result = begin; c ; end + @result = begin; @ulist_elem[-1].concat(c) ; end _tmp = true unless _tmp self.pos = _save @@ -2878,22 +2491,50 @@ def _BlockElementParagraph break end # end sequence - set_failed_rule :_BlockElementParagraph unless _tmp + set_failed_rule :_UlistContLine unless _tmp return _tmp end - # SinglelineContent = Inline+:c {singleline_content(self,c)} - def _SinglelineContent + # Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c {olist(self, @olist_elem)} + def _Olist _save = self.pos while true # sequence + @result = begin; @olist_elem = [] ; end + _tmp = true + unless _tmp + self.pos = _save + break + end _save1 = self.pos _ary = [] - _tmp = apply(:_Inline) + + _save2 = self.pos + while true # choice + _tmp = apply(:_OlistElement) + break if _tmp + self.pos = _save2 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save2 + break + end # end choice + if _tmp _ary << @result while true - _tmp = apply(:_Inline) + + _save3 = self.pos + while true # choice + _tmp = apply(:_OlistElement) + break if _tmp + self.pos = _save3 + _tmp = apply(:_SinglelineComment) + break if _tmp + self.pos = _save3 + break + end # end choice + _ary << @result if _tmp break unless _tmp end @@ -2907,7 +2548,7 @@ def _SinglelineContent self.pos = _save break end - @result = begin; singleline_content(self,c); end + @result = begin; olist(self, @olist_elem); end _tmp = true unless _tmp self.pos = _save @@ -2915,77 +2556,140 @@ def _SinglelineContent break end # end sequence - set_failed_rule :_SinglelineContent unless _tmp + set_failed_rule :_Olist unless _tmp return _tmp end - # Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)} - def _Ulist + # OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } + def _OlistElement _save = self.pos while true # sequence _save1 = self.pos - _tmp = get_byte - self.pos = _save1 + _tmp = match_string(" ") + if _tmp + while true + _tmp = match_string(" ") + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end unless _tmp self.pos = _save break end - @result = begin; @ulist_elem=[] ; end + _text_start = self.pos + _save2 = self.pos + _tmp = scan(/\A(?-mix:\d)/) + if _tmp + while true + _tmp = scan(/\A(?-mix:\d)/) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + @result = begin; num=text ; end _tmp = true unless _tmp self.pos = _save break end - _tmp = apply(:_UlistElement) + _tmp = match_string(".") unless _tmp self.pos = _save break end - _save2 = self.pos + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_EOL) + unless _tmp + self.pos = _save + break + end + @result = begin; @olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence - _save3 = self.pos + set_failed_rule :_OlistElement unless _tmp + return _tmp + end + + # Dlist = (DlistElement | SinglelineComment)+:content {dlist(self, content)} + def _Dlist + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + + _save2 = self.pos while true # choice - _tmp = apply(:_UlistElement) - break if _tmp - self.pos = _save3 - _tmp = apply(:_UlistContLine) + _tmp = apply(:_DlistElement) break if _tmp - self.pos = _save3 + self.pos = _save2 _tmp = apply(:_SinglelineComment) break if _tmp - self.pos = _save3 + self.pos = _save2 break end # end choice if _tmp + _ary << @result while true - _save4 = self.pos + _save3 = self.pos while true # choice - _tmp = apply(:_UlistElement) - break if _tmp - self.pos = _save4 - _tmp = apply(:_UlistContLine) + _tmp = apply(:_DlistElement) break if _tmp - self.pos = _save4 + self.pos = _save3 _tmp = apply(:_SinglelineComment) break if _tmp - self.pos = _save4 + self.pos = _save3 break end # end choice + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else - self.pos = _save2 + self.pos = _save1 end + content = @result unless _tmp self.pos = _save break end - @result = begin; ulist(self, @ulist_elem); end + @result = begin; dlist(self, content); end _tmp = true unless _tmp self.pos = _save @@ -2993,52 +2697,36 @@ def _Ulist break end # end sequence - set_failed_rule :_Ulist unless _tmp + set_failed_rule :_Dlist unless _tmp return _tmp end - # UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } - def _UlistElement + # DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)} + def _DlistElement _save = self.pos while true # sequence - _save1 = self.pos - _tmp = match_string(" ") - if _tmp - while true - _tmp = match_string(" ") - break unless _tmp - end - _tmp = true - else - self.pos = _save1 + while true + _tmp = match_string(" ") + break unless _tmp end + _tmp = true unless _tmp self.pos = _save break end - _save2 = self.pos - _ary = [] - _tmp = match_string("*") - if _tmp - _ary << @result - while true - _tmp = match_string("*") - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save2 + _tmp = match_string(":") + unless _tmp + self.pos = _save + break end - level = @result + _tmp = match_string(" ") unless _tmp self.pos = _save break end while true - _tmp = match_string(" ") + _tmp = apply(:_Space) break unless _tmp end _tmp = true @@ -3047,17 +2735,37 @@ def _UlistElement break end _tmp = apply(:_SinglelineContent) - c = @result + text = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) unless _tmp self.pos = _save break end - _tmp = apply(:_EOL) + _save3 = self.pos + _ary = [] + _tmp = apply(:_DlistElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_DlistElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save3 + end + content = @result unless _tmp self.pos = _save break end - @result = begin; @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) ; end + @result = begin; dlist_element(self, text, content); end _tmp = true unless _tmp self.pos = _save @@ -3065,39 +2773,16 @@ def _UlistElement break end # end sequence - set_failed_rule :_UlistElement unless _tmp + set_failed_rule :_DlistElement unless _tmp return _tmp end - # UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } - def _UlistContLine + # DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } + def _DlistElementContent _save = self.pos while true # sequence - _tmp = match_string(" ") - unless _tmp - self.pos = _save - break - end - _save1 = self.pos - _tmp = match_string(" ") - if _tmp - while true - _tmp = match_string(" ") - break unless _tmp - end - _tmp = true - else - self.pos = _save1 - end - unless _tmp - self.pos = _save - break - end - _save2 = self.pos - _tmp = match_string("*") - _tmp = _tmp ? nil : true - self.pos = _save2 + _tmp = scan(/\A(?-mix:[ \t]+)/) unless _tmp self.pos = _save break @@ -3108,12 +2793,13 @@ def _UlistContLine self.pos = _save break end - _tmp = apply(:_EOL) + _tmp = apply(:_Newline) + n = @result unless _tmp self.pos = _save break end - @result = begin; @ulist_elem[-1].concat(c) ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -3121,50 +2807,22 @@ def _UlistContLine break end # end sequence - set_failed_rule :_UlistContLine unless _tmp + set_failed_rule :_DlistElementContent unless _tmp return _tmp end - # Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c {olist(self, @olist_elem)} - def _Olist + # SinglelineContent = Inline+:c {singleline_content(self,c)} + def _SinglelineContent _save = self.pos while true # sequence - @result = begin; @olist_elem = [] ; end - _tmp = true - unless _tmp - self.pos = _save - break - end _save1 = self.pos _ary = [] - - _save2 = self.pos - while true # choice - _tmp = apply(:_OlistElement) - break if _tmp - self.pos = _save2 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save2 - break - end # end choice - + _tmp = apply(:_Inline) if _tmp _ary << @result while true - - _save3 = self.pos - while true # choice - _tmp = apply(:_OlistElement) - break if _tmp - self.pos = _save3 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save3 - break - end # end choice - + _tmp = apply(:_Inline) _ary << @result if _tmp break unless _tmp end @@ -3178,7 +2836,7 @@ def _Olist self.pos = _save break end - @result = begin; olist(self, @olist_elem); end + @result = begin; singleline_content(self,c); end _tmp = true unless _tmp self.pos = _save @@ -3186,42 +2844,43 @@ def _Olist break end # end sequence - set_failed_rule :_Olist unless _tmp + set_failed_rule :_SinglelineContent unless _tmp return _tmp end - # OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } - def _OlistElement + # Inline = (InlineElement | NonInlineElement) + def _Inline + + _save = self.pos + while true # choice + _tmp = apply(:_InlineElement) + break if _tmp + self.pos = _save + _tmp = apply(:_NonInlineElement) + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_Inline unless _tmp + return _tmp + end + + # NonInlineElement = !InlineElement < NonNewLine > {text(self, text)} + def _NonInlineElement _save = self.pos while true # sequence _save1 = self.pos - _tmp = match_string(" ") - if _tmp - while true - _tmp = match_string(" ") - break unless _tmp - end - _tmp = true - else - self.pos = _save1 - end + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save1 unless _tmp self.pos = _save break end _text_start = self.pos - _save2 = self.pos - _tmp = scan(/\A(?-mix:\d)/) - if _tmp - while true - _tmp = scan(/\A(?-mix:\d)/) - break unless _tmp - end - _tmp = true - else - self.pos = _save2 - end + _tmp = apply(:_NonNewLine) if _tmp text = get_text(_text_start) end @@ -3229,38 +2888,156 @@ def _OlistElement self.pos = _save break end - @result = begin; num=text ; end + @result = begin; text(self, text); end _tmp = true unless _tmp self.pos = _save - break end - _tmp = match_string(".") + break + end # end sequence + + set_failed_rule :_NonInlineElement unless _tmp + return _tmp + end + + # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" {inline_element(self, symbol,contents)}) + def _InlineElement + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_RawInlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_RawInlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("@<") + unless _tmp + self.pos = _save2 + break + end + _tmp = apply(:_InlineElementSymbol) + symbol = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string(">") + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save2 + break + end + _save4 = self.pos + _tmp = apply(:_InlineElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save4 + end + contents = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save2 + break + end + @result = begin; inline_element(self, symbol,contents); end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElement unless _tmp + return _tmp + end + + # RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" {raw(self, builders,c)} + def _RawInlineElement + + _save = self.pos + while true # sequence + _tmp = match_string("@{") unless _tmp self.pos = _save break end - while true - _tmp = apply(:_Space) - break unless _tmp + _save1 = self.pos + _tmp = apply(:_RawBlockBuilderSelect) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save1 end - _tmp = true + builders = @result unless _tmp self.pos = _save break end - _tmp = apply(:_SinglelineContent) + _save2 = self.pos + _ary = [] + _tmp = apply(:_RawInlineElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_RawInlineElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end c = @result unless _tmp self.pos = _save break end - _tmp = apply(:_EOL) + _tmp = match_string("}") unless _tmp self.pos = _save break end - @result = begin; @olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) ; end + @result = begin; raw(self, builders,c); end _tmp = true unless _tmp self.pos = _save @@ -3268,58 +3045,87 @@ def _OlistElement break end # end sequence - set_failed_rule :_OlistElement unless _tmp + set_failed_rule :_RawInlineElement unless _tmp return _tmp end - # Dlist = (DlistElement | SinglelineComment)+:content {dlist(self, content)} - def _Dlist + # RawInlineElementContent = ("\\}" { "}" } | < /[^\r\n\}]/ > { text }) + def _RawInlineElementContent _save = self.pos - while true # sequence + while true # choice + _save1 = self.pos - _ary = [] + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; "}" ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save _save2 = self.pos - while true # choice - _tmp = apply(:_DlistElement) - break if _tmp - self.pos = _save2 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save2 + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\}])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save2 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save2 + end break - end # end choice + end # end sequence - if _tmp - _ary << @result - while true + break if _tmp + self.pos = _save + break + end # end choice - _save3 = self.pos - while true # choice - _tmp = apply(:_DlistElement) - break if _tmp - self.pos = _save3 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save3 - break - end # end choice + set_failed_rule :_RawInlineElementContent unless _tmp + return _tmp + end - _ary << @result if _tmp + # InlineElementSymbol = < AlphanumericAscii+ > { text } + def _InlineElementSymbol + + _save = self.pos + while true # sequence + _text_start = self.pos + _save1 = self.pos + _tmp = apply(:_AlphanumericAscii) + if _tmp + while true + _tmp = apply(:_AlphanumericAscii) break unless _tmp end _tmp = true - @result = _ary else self.pos = _save1 end - content = @result + if _tmp + text = get_text(_text_start) + end unless _tmp self.pos = _save break end - @result = begin; dlist(self, content); end + @result = begin; text ; end _tmp = true unless _tmp self.pos = _save @@ -3327,75 +3133,30 @@ def _Dlist break end # end sequence - set_failed_rule :_Dlist unless _tmp + set_failed_rule :_InlineElementSymbol unless _tmp return _tmp end - # DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)} - def _DlistElement + # InlineElementContents = !"}" InlineElementContentsSub:c { c } + def _InlineElementContents _save = self.pos while true # sequence - while true - _tmp = match_string(" ") - break unless _tmp - end - _tmp = true - unless _tmp - self.pos = _save - break - end - _tmp = match_string(":") - unless _tmp - self.pos = _save - break - end - _tmp = match_string(" ") - unless _tmp - self.pos = _save - break - end - while true - _tmp = apply(:_Space) - break unless _tmp - end - _tmp = true - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_SinglelineContent) - text = @result - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_Newline) + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 unless _tmp self.pos = _save break end - _save3 = self.pos - _ary = [] - _tmp = apply(:_DlistElementContent) - if _tmp - _ary << @result - while true - _tmp = apply(:_DlistElementContent) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save3 - end - content = @result + _tmp = apply(:_InlineElementContentsSub) + c = @result unless _tmp self.pos = _save break end - @result = begin; dlist_element(self, text, content); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -3403,79 +3164,132 @@ def _DlistElement break end # end sequence - set_failed_rule :_DlistElement unless _tmp + set_failed_rule :_InlineElementContents unless _tmp return _tmp end - # DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } - def _DlistElementContent + # InlineElementContentsSub = !"}" (InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] }) + def _InlineElementContentsSub _save = self.pos while true # sequence - _tmp = scan(/\A(?-mix:[ \t]+)/) - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_SinglelineContent) - c = @result + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 unless _tmp self.pos = _save break end - _tmp = apply(:_Newline) - n = @result - unless _tmp - self.pos = _save + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = apply(:_InlineElementContent) + c1 = @result + unless _tmp + self.pos = _save3 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save3 + break + end + _tmp = match_string(",") + unless _tmp + self.pos = _save3 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save3 + break + end + _tmp = apply(:_InlineElementContentsSub) + c2 = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; [c1]+c2 ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save6 = self.pos + while true # sequence + _tmp = apply(:_InlineElementContent) + c1 = @result + unless _tmp + self.pos = _save6 + break + end + @result = begin; [c1] ; end + _tmp = true + unless _tmp + self.pos = _save6 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 break - end - @result = begin; c ; end - _tmp = true + end # end choice + unless _tmp self.pos = _save end break end # end sequence - set_failed_rule :_DlistElementContent unless _tmp + set_failed_rule :_InlineElementContentsSub unless _tmp return _tmp end - # SinglelineComment = "#@" < NonNewLine+ > EOL {singleline_comment(self, text)} - def _SinglelineComment + # InlineElementContent = InlineElementContentSub+:d { d } + def _InlineElementContent _save = self.pos while true # sequence - _tmp = match_string("\#@") - unless _tmp - self.pos = _save - break - end - _text_start = self.pos _save1 = self.pos - _tmp = apply(:_NonNewLine) + _ary = [] + _tmp = apply(:_InlineElementContentSub) if _tmp + _ary << @result while true - _tmp = apply(:_NonNewLine) + _tmp = apply(:_InlineElementContentSub) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else self.pos = _save1 end - if _tmp - text = get_text(_text_start) - end - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_EOL) + d = @result unless _tmp self.pos = _save break end - @result = begin; singleline_comment(self, text); end + @result = begin; d ; end _tmp = true unless _tmp self.pos = _save @@ -3483,7 +3297,193 @@ def _SinglelineComment break end # end sequence - set_failed_rule :_SinglelineComment unless _tmp + set_failed_rule :_InlineElementContent unless _tmp + return _tmp + end + + # InlineElementContentSub = (InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)}) + def _InlineElementContentSub + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_InlineElement) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save2 + break + end + _save4 = self.pos + _ary = [] + _tmp = apply(:_InlineElementContentText) + if _tmp + _ary << @result + while true + _tmp = apply(:_InlineElementContentText) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save4 + end + content = @result + unless _tmp + self.pos = _save2 + break + end + @result = begin; inline_element_content(self, content); end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElementContentSub unless _tmp + return _tmp + end + + # InlineElementContentText = ("\\}" {text(self, "}")} | "\\," {text(self, ",")} | "\\\\" {text(self, "\\" )} | "\\" {text(self, "\\" )} | !InlineElement < /[^\r\n\\},]/ > {text(self,text)}) + def _InlineElementContentText + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = match_string("\\}") + unless _tmp + self.pos = _save1 + break + end + @result = begin; text(self, "}"); end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _tmp = match_string("\\,") + unless _tmp + self.pos = _save2 + break + end + @result = begin; text(self, ","); end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _tmp = match_string("\\\\") + unless _tmp + self.pos = _save3 + break + end + @result = begin; text(self, "\\" ); end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos + while true # sequence + _tmp = match_string("\\") + unless _tmp + self.pos = _save4 + break + end + @result = begin; text(self, "\\" ); end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save5 = self.pos + while true # sequence + _save6 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save5 + break + end + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^\r\n\\},])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save5 + break + end + @result = begin; text(self,text); end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_InlineElementContentText unless _tmp return _tmp end @@ -3664,32 +3664,21 @@ def _Spacechar Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") + Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > EOL {singleline_comment(self, text)}") Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") - Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") - Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })") Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewLine > { text })") - Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" {inline_element(self, symbol,contents)})") - Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawBlockBuilderSelect?:builders RawInlineElementContent+:c \"}\" {raw(self, builders,c)}") - Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") - Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ > { text }") - Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") - Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") - Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") - Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") - Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline*:content \"]\" {bracket_arg(self, content)}") Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") - Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) }") @@ -3698,7 +3687,18 @@ def _Spacechar Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") - Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > EOL {singleline_comment(self, text)}") + Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") + Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") + Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") + Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" {inline_element(self, symbol,contents)})") + Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawBlockBuilderSelect?:builders RawInlineElementContent+:c \"}\" {raw(self, builders,c)}") + Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") + Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ > { text }") + Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") + Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") + Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") + Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") + Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") Rules[:_NonNewLine] = rule_info("NonNewLine", "/[^\\r\\n]/") Rules[:_Digits] = rule_info("Digits", "Digit+:c { c }") Rules[:_Space] = rule_info("Space", "/[ \\t]/") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 1a482bd3d..9fb5e669e 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -495,6 +495,8 @@ Block = BlankLine* BlankLine = Newline +SinglelineComment = ("#@" < NonNewLine+ > EOL) ~singleline_comment(self, text) + Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL ~headline(self, level, cmd, label, caption) HeadlinePrefix = < /={1,5}/ > { text.length } @@ -503,10 +505,6 @@ Paragraph = ParagraphSub+:c ~paragraph(self, c.flatten) ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } -Inline = ( InlineElement | NonInlineElement) - -NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) - BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL @@ -527,39 +525,6 @@ RawBlockElementArg = !"]" ( "\\]" { "]" } | < NonNewLine > { text } ) -InlineElement = ( RawInlineElement:c { c } - | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, symbol,contents) - ) - -RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" ~raw(self, builders,c) - -RawInlineElementContent = ( "\\}" { "}" } - | < /[^\r\n\}]/ > { text } - ) - -InlineElementSymbol = < AlphanumericAscii+ > { text } - -InlineElementContents = !"}" InlineElementContentsSub:c { c } - -InlineElementContentsSub = !"}" ( InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } - | InlineElementContent:c1 { [c1] } - ) - - -InlineElementContent = InlineElementContentSub+:d { d } - -InlineElementContentSub = ( InlineElement:c { c } - | !InlineElement InlineElementContentText+:content ~inline_element_content(self, content) - ) - -## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? -InlineElementContentText = ( "\\}" ~text(self, "}") - | "\\," ~text(self, ",") - | "\\\\" ~text(self, "\\" ) - | "\\" ~text(self, "\\" ) - | !InlineElement < /[^\r\n\\},]/> ~text(self,text) - ) - BracketArg = "[" BracketArgContentInline*:content "]" ~bracket_arg(self, content) ## XXX '\' (excpet '\]' and '\\' ) => '\' is ??? @@ -585,9 +550,6 @@ BlockElementContent = ( SinglelineComment:c { c } ## flattenでよい? BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } -SinglelineContent = Inline+:c ~singleline_content(self,c) - - ## * 箇条書き ## 行頭から… の指定がない @@ -611,7 +573,44 @@ DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementCo DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } -SinglelineComment = ("#@" < NonNewLine+ > EOL) ~singleline_comment(self, text) +SinglelineContent = Inline+:c ~singleline_content(self,c) + +Inline = ( InlineElement | NonInlineElement) + +NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) + +InlineElement = ( RawInlineElement:c { c } + | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, symbol,contents) + ) + +RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" ~raw(self, builders,c) + +RawInlineElementContent = ( "\\}" { "}" } + | < /[^\r\n\}]/ > { text } + ) + +InlineElementSymbol = < AlphanumericAscii+ > { text } + +InlineElementContents = !"}" InlineElementContentsSub:c { c } + +InlineElementContentsSub = !"}" ( InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } + | InlineElementContent:c1 { [c1] } + ) + + +InlineElementContent = InlineElementContentSub+:d { d } + +InlineElementContentSub = ( InlineElement:c { c } + | !InlineElement InlineElementContentText+:content ~inline_element_content(self, content) + ) + +## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? +InlineElementContentText = ( "\\}" ~text(self, "}") + | "\\," ~text(self, ",") + | "\\\\" ~text(self, "\\" ) + | "\\" ~text(self, "\\" ) + | !InlineElement < /[^\r\n\\},]/> ~text(self,text) + ) NonNewLine = /[^\r\n]/ From 265ce5fad7414d35b2a8417c504ea4e7faba20ca Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 03:34:07 +0900 Subject: [PATCH 137/214] remove comment --- lib/review/review.kpeg | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 9fb5e669e..a7583d0da 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -547,7 +547,6 @@ BlockElementContent = ( SinglelineComment:c { c } | Newline:c ~singleline_content(self, "") ) -## flattenでよい? BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } ## * 箇条書き From 1439bc1e153e45d74569e6719c149c6aaf4b1777 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 12:01:19 +0900 Subject: [PATCH 138/214] kpeg: replace Newline -> BlankLine --- lib/review/compiler.rb | 6 +++--- lib/review/review.kpeg | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 0b2f023a1..622f69f76 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -2125,7 +2125,7 @@ def _BlockElementContents return _tmp end - # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, "")}) + # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | BlankLine:c {singleline_content(self, "")}) def _BlockElementContent _save = self.pos @@ -2190,7 +2190,7 @@ def _BlockElementContent _save4 = self.pos while true # sequence - _tmp = apply(:_Newline) + _tmp = apply(:_BlankLine) c = @result unless _tmp self.pos = _save4 @@ -3677,7 +3677,7 @@ def _Spacechar Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") - Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | Newline:c {singleline_content(self, \"\")})") + Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | BlankLine:c {singleline_content(self, \"\")})") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index a7583d0da..96ca09736 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -544,7 +544,7 @@ BlockElementContents = BlockElementContent+:c { c } BlockElementContent = ( SinglelineComment:c { c } | BlockElement:c ~singleline_content(self, c) | BlockElementParagraph:c ~singleline_content(self, c) - | Newline:c ~singleline_content(self, "") + | BlankLine:c ~singleline_content(self, "") ) BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } From f1f4da196c7e84bc1c45307dd365527c8c4d0f3e Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 14:16:06 +0900 Subject: [PATCH 139/214] add NewLineNode --- lib/review/node.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/review/node.rb b/lib/review/node.rb index ec3243444..2a273e80b 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -93,6 +93,10 @@ def parse_args(*patterns) end end + def raw_lines + self.to_raw.split(/\n/) + end + end class InlineElementNode < Node @@ -130,6 +134,13 @@ def to_doc end end + class NewLineNode < Node + + def to_doc + "" + end + end + class RawNode < Node def to_doc From c977d0104f9382d2e5b7e7deaea8c1f88a932efb Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 15:38:07 +0900 Subject: [PATCH 140/214] remove split_paragraph and try to nest blocks --- lib/review/htmlbuilder.rb | 72 ++++++++++++++++----------------------- lib/review/review.kpeg | 17 +++++---- test/test_htmlbuilder.rb | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 48 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 480c67e14..47c8d6ca5 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -34,13 +34,6 @@ class HTMLBuilder < Builder Compiler.defblock(:point, 0..1) Compiler.defblock(:shoot, 0..1) - def pre_paragraph - '

                    ' - end - def post_paragraph - '

                    ' - end - def extname ".#{@book.config["htmlext"]}" end @@ -290,10 +283,9 @@ def captionblock(type, lines, caption) buf << %Q[

                    #{caption}

                    \n] end if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - buf << blocked_lines.join("\n") << "\n" + buf << lines.join("") else - lines.each {|l| buf << "

                    #{l}

                    \n" } + error "deprecated-blocklines is obsoleted." end buf << "
      \n" buf @@ -427,16 +419,18 @@ def parasep() def read(lines) if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - %Q[
      \n#{blocked_lines.join("\n")}\n
      \n] + %Q[
      \n#{lines.join("")}\n
      \n] else - %Q[

      \n#{lines.join("\n")}\n

      \n] + error "deprecated-blocklines is obsoleted." end end alias_method :lead, :read - def list(lines, id, caption) + def node_list(node) + id, caption = node.parse_args(:raw, :doc) + lines = node.raw_lines + buf = %Q[
      \n] begin buf << list_header(id, caption) @@ -511,7 +505,10 @@ def listnum_body(lines) buf end - def emlist(lines, caption = nil) + def node_emlist(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -525,7 +522,10 @@ def emlist(lines, caption = nil) buf end - def emlistnum(lines, caption = nil) + def node_emlistnum(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -539,7 +539,10 @@ def emlistnum(lines, caption = nil) buf end - def cmd(lines, caption = nil) + def node_cmd(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -564,29 +567,21 @@ def quotedlist(lines, css_class) def quote(lines) if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - "
      #{blocked_lines.join("\n")}
      \n" + "
      #{lines.join("")}
      \n" else - "
      #{lines.join("\n")}
      \n" + error "deprecated-blocklines is obsoleted." end end def doorquote(lines, ref) buf = "" if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) buf << %Q[
      \n] - buf << "#{blocked_lines.join("\n")}\n" + buf << "#{lines.join("")}\n" buf << %Q[

      #{ref}より

      \n] buf << %Q[
      \n] else - buf << <<-QUOTE -
      -
      #{lines.join("\n")}
      -
      -#{ref}より
      -
      -QUOTE + error "deprecated-blocklines is obsoleted." end buf end @@ -595,12 +590,9 @@ def talk(lines) buf = "" buf << %Q[
      \n] if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - buf << "#{blocked_lines.join("\n")}\n" - else - buf << '
      '
               buf << "#{lines.join("\n")}\n"
      -        buf << "
      \n" + else + error "deprecated-blocklines is obsoleted." end buf << "
      \n" buf @@ -966,7 +958,7 @@ def bibpaper_header(id, caption) end def bibpaper_bibpaper(id, caption, lines) - split_paragraph(lines).join("") + lines.join("") end def inline_bib(id) @@ -1168,19 +1160,15 @@ def compile_href(url, label) def flushright(lines) result = "" if @book.config["deprecated-blocklines"].nil? - result << split_paragraph(lines).join("\n").gsub("

      ", "

      ") << "\n" + result << lines.join("").gsub("

      ", "

      ") else - result << %Q[

      \n] - result << %Q[
      ]
      -        lines.each {|line| result << detab(line) << "\n" }
      -        result << "
      \n" - result << "
      \n" + error "deprecated-blocklines is obsoleted." end result end def centering(lines) - split_paragraph(lines).join("\n").gsub("

      ", "

      ") + "\n" + lines.join("").gsub("

      ", "

      ") end def image_ext diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 96ca09736..888febbee 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -276,7 +276,7 @@ require 'review/node' def compile_command(name, args, lines, node) syntax = syntax_descriptor(name) - if !syntax || !@strategy.respond_to?(syntax.name) + if !syntax || (!@strategy.respond_to?(syntax.name) && !@strategy.respond_to?("node_#{syntax.name}")) error "strategy does not support command: //#{name}" compile_unknown_command args, lines return @@ -472,6 +472,7 @@ require 'review/node' %% bracket_arg = ast BracketArgNode(compiler, content) %% document = ast DocumentNode(compiler, content) %% column = ast ColumnNode(compiler, level, label, caption, content) +%% newline = ast NewLineNode(compiler, content) # %% dummy @@ -483,7 +484,7 @@ Document = Block*:c ~document(self, c) ## TODO InlineElement の後に Ulist / Olist / Dlist が来ると先頭行じゃなくてマッチできてしまうかも ## using &. to detect EOF -Block = BlankLine* +Block = BlankLine*:c { c } ( SinglelineComment:c | Headline:c | BlockElement:c @@ -542,12 +543,16 @@ BlockElementContents = BlockElementContent+:c { c } ## 各要素は Newline で終わらなければならない ### SinglelineContentではないような気がするけど改行は落としてあるのでとりあえずこれで BlockElementContent = ( SinglelineComment:c { c } - | BlockElement:c ~singleline_content(self, c) + | BlockElement:c { c } + | Ulist:c + | Olist:c + | BlankLine:c ~newline(self, c) | BlockElementParagraph:c ~singleline_content(self, c) - | BlankLine:c ~singleline_content(self, "") ) -BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } +## it's like Paragraph, but it's in a block, so do not allow '//}\n' +BlockElementParagraph = BlockElementParagraphSub+:c ~paragraph(self, c.flatten) +BlockElementParagraphSub = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } ## * 箇条書き ## 行頭から… の指定がない @@ -630,6 +635,6 @@ AlphanumericAscii = %literals.AlphanumericAscii LowerAlphabetAscii = %literals.LowerAlphabetAscii Digit = %literals.Digit BOM = %literals.BOM -Newline = %literals.Newline +Newline = %literals.Newline:n ~newline(self, "\n") NonAlphanumeric = %literals.NonAlphanumeric Spacechar = %literals.Spacechar diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 384e4d1c3..44c38ef80 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -786,6 +786,71 @@ def test_ul_nest5 assert_equal expected, actual end + def test_ul_in_note + src =<<-EOS +//note{ + +aaaa +bbb + + * A + * B +//} +EOS + + expected =<<-EOS +

      +

      aaaabbb

      +
        +
      • A
      • +
      • B
      • +
      +
      +EOS + actual = compile_block(src) + assert_equal expected, actual + end + + def test_ul_in_note_and_emlist + src =<<-EOS +//note{ + +aaaa +bbb + +//emlist{ +abc +ddd + * A + * B +//} + + * A + * B +//} +EOS + + expected =<<-EOS +
      +

      aaaabbb

      +
      +
      +abc
      +ddd
      + * A
      + * B
      +
      +
      +
        +
      • A
      • +
      • B
      • +
      +
      +EOS + actual = compile_block(src) + assert_equal expected, actual + end + def test_ol src =<<-EOS 3. AAA From 29580042de9f79bb559142a3192d3c05d7fc8dc6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Feb 2015 17:18:15 +0900 Subject: [PATCH 141/214] add CodeElementNode and defcodeblock --- lib/review/compiler.rb | 434 +++++++++++++++++++++++++++++++++----- lib/review/htmlbuilder.rb | 15 +- lib/review/node.rb | 30 ++- lib/review/review.kpeg | 43 ++-- test/test_htmlbuilder.rb | 11 +- 5 files changed, 462 insertions(+), 71 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 622f69f76..1be8f0b52 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -480,11 +480,15 @@ def parse_args(args) end def block_required? - @type == :block + @type == :block or @type == :code_block end def block_allowed? - @type == :block or @type == :optional + @type == :block or @type == :code_block or @type == :optional + end + + def code_block? + @type == :code_block end end @@ -494,6 +498,10 @@ def self.defblock(name, argc, optional = false, esc = nil, &block) defsyntax(name, (optional ? :optional : :block), argc, esc, &block) end + def self.defcodeblock(name, argc, optional = false, esc = nil, &block) + defsyntax(name, :code_block, argc, esc, &block) + end + def self.defsingle(name, argc, &block) defsyntax name, :line, argc, &block end @@ -530,21 +538,22 @@ def inline_defined?(name) defblock :read, 0 defblock :lead, 0 - defblock :list, 2, nil, [:raw,:doc] - defblock :emlist, 0..1 - defblock :cmd, 0..1 - defblock :table, 0..2 defblock :quote, 0 defblock :image, 2..3, true, [:raw,:doc,:raw] - defblock :source, 0..1 - defblock :listnum, 2 - defblock :emlistnum, 0..1 defblock :bibpaper, 2..3, true defblock :doorquote, 1 defblock :talk, 0 - defblock :texequation, 0 defblock :graph, 1..3 + defcodeblock :emlist, 0..1 + defcodeblock :cmd, 0..1 + defcodeblock :source, 0..1 + defcodeblock :list, 2, nil + defcodeblock :listnum, 2 + defcodeblock :emlistnum, 0..1 + defcodeblock :texequation, 0 + defcodeblock :table, 0..2 + defblock :address, 0 defblock :blockquote, 0 defblock :bpo, 0 @@ -631,7 +640,7 @@ def compile_column(level, label, caption, content) def compile_command(name, args, lines, node) syntax = syntax_descriptor(name) - if !syntax || !@strategy.respond_to?(syntax.name) + if !syntax || (!@strategy.respond_to?(syntax.name) && !@strategy.respond_to?("node_#{syntax.name}")) error "strategy does not support command: //#{name}" compile_unknown_command args, lines return @@ -839,6 +848,18 @@ def initialize(compiler, content) attr_reader :compiler attr_reader :content end + class CodeBlockElementNode < Node + def initialize(compiler, name, args, content) + @compiler = compiler + @name = name + @args = args + @content = content + end + attr_reader :compiler + attr_reader :name + attr_reader :args + attr_reader :content + end class ColumnNode < Node def initialize(compiler, level, label, caption, content) @compiler = compiler @@ -911,6 +932,14 @@ def initialize(compiler, content) attr_reader :compiler attr_reader :content end + class NewLineNode < Node + def initialize(compiler, content) + @compiler = compiler + @content = content + end + attr_reader :compiler + attr_reader :content + end class OlistNode < Node def initialize(compiler, content) @compiler = compiler @@ -1000,6 +1029,9 @@ def brace(compiler, content) def bracket_arg(compiler, content) ::ReVIEW::BracketArgNode.new(compiler, content) end + def code_block_element(compiler, name, args, content) + ::ReVIEW::CodeBlockElementNode.new(compiler, name, args, content) + end def column(compiler, level, label, caption, content) ::ReVIEW::ColumnNode.new(compiler, level, label, caption, content) end @@ -1021,6 +1053,9 @@ def inline_element(compiler, symbol, content) def inline_element_content(compiler, content) ::ReVIEW::InlineElementContentNode.new(compiler, content) end + def newline(compiler, content) + ::ReVIEW::NewLineNode.new(compiler, content) + end def olist(compiler, content) ::ReVIEW::OlistNode.new(compiler, content) end @@ -1121,16 +1156,26 @@ def _Document return _tmp end - # Block = BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c } + # Block = BlankLine*:c { c } (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c } def _Block _save = self.pos while true # sequence + _ary = [] while true _tmp = apply(:_BlankLine) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true unless _tmp self.pos = _save break @@ -1450,7 +1495,7 @@ def _ParagraphSub return _tmp end - # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)}) + # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL {code_block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)}) def _BlockElement _save = self.pos @@ -1539,6 +1584,13 @@ def _BlockElement self.pos = _save5 break end + _save7 = self.pos + _tmp = begin; syntax = syntax_descriptor(symbol); syntax.code_block? ; end + self.pos = _save7 + unless _tmp + self.pos = _save5 + break + end _ary = [] while true _tmp = apply(:_BracketArg) @@ -1571,12 +1623,12 @@ def _BlockElement self.pos = _save5 break end - _save9 = self.pos - _tmp = apply(:_BlockElementContents) + _save10 = self.pos + _tmp = apply(:_CodeBlockElementContents) @result = nil unless _tmp unless _tmp _tmp = true - self.pos = _save9 + self.pos = _save10 end contents = @result unless _tmp @@ -1602,7 +1654,7 @@ def _BlockElement self.pos = _save5 break end - @result = begin; block_element(self, symbol, args, contents); end + @result = begin; code_block_element(self, symbol, args, contents); end _tmp = true unless _tmp self.pos = _save5 @@ -1613,25 +1665,25 @@ def _BlockElement break if _tmp self.pos = _save - _save11 = self.pos + _save12 = self.pos while true # sequence - _save12 = self.pos + _save13 = self.pos _tmp = match_string("//raw") _tmp = _tmp ? nil : true - self.pos = _save12 + self.pos = _save13 unless _tmp - self.pos = _save11 + self.pos = _save12 break end _tmp = match_string("//") unless _tmp - self.pos = _save11 + self.pos = _save12 break end _tmp = apply(:_ElementName) symbol = @result unless _tmp - self.pos = _save11 + self.pos = _save12 break end _ary = [] @@ -1644,7 +1696,43 @@ def _BlockElement @result = _ary args = @result unless _tmp - self.pos = _save11 + self.pos = _save12 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save12 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save12 + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save12 + break + end + _save16 = self.pos + _tmp = apply(:_BlockElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save16 + end + contents = @result + unless _tmp + self.pos = _save12 + break + end + _tmp = match_string("//}") + unless _tmp + self.pos = _save12 break end while true @@ -1653,18 +1741,77 @@ def _BlockElement end _tmp = true unless _tmp - self.pos = _save11 + self.pos = _save12 break end _tmp = apply(:_EOL) unless _tmp - self.pos = _save11 + self.pos = _save12 + break + end + @result = begin; block_element(self, symbol, args, contents); end + _tmp = true + unless _tmp + self.pos = _save12 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save18 = self.pos + while true # sequence + _save19 = self.pos + _tmp = match_string("//raw") + _tmp = _tmp ? nil : true + self.pos = _save19 + unless _tmp + self.pos = _save18 + break + end + _tmp = match_string("//") + unless _tmp + self.pos = _save18 + break + end + _tmp = apply(:_ElementName) + symbol = @result + unless _tmp + self.pos = _save18 + break + end + _ary = [] + while true + _tmp = apply(:_BracketArg) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + args = @result + unless _tmp + self.pos = _save18 + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save18 + break + end + _tmp = apply(:_EOL) + unless _tmp + self.pos = _save18 break end @result = begin; block_element(self, symbol, args, nil); end _tmp = true unless _tmp - self.pos = _save11 + self.pos = _save18 end break end # end sequence @@ -2125,7 +2272,7 @@ def _BlockElementContents return _tmp end - # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | BlankLine:c {singleline_content(self, "")}) + # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c }) def _BlockElementContent _save = self.pos @@ -2158,7 +2305,7 @@ def _BlockElementContent self.pos = _save2 break end - @result = begin; singleline_content(self, c); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save2 @@ -2166,18 +2313,26 @@ def _BlockElementContent break end # end sequence + break if _tmp + self.pos = _save + _tmp = apply(:_Ulist) + c = @result + break if _tmp + self.pos = _save + _tmp = apply(:_Olist) + c = @result break if _tmp self.pos = _save _save3 = self.pos while true # sequence - _tmp = apply(:_BlockElementParagraph) + _tmp = apply(:_BlankLine) c = @result unless _tmp self.pos = _save3 break end - @result = begin; singleline_content(self, c); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save3 @@ -2190,13 +2345,13 @@ def _BlockElementContent _save4 = self.pos while true # sequence - _tmp = apply(:_BlankLine) + _tmp = apply(:_BlockElementParagraph) c = @result unless _tmp self.pos = _save4 break end - @result = begin; singleline_content(self, ""); end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save4 @@ -2213,9 +2368,46 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = !"//}" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } + # BlockElementParagraph = BlockElementParagraphSub+:c {paragraph(self, c.flatten)} def _BlockElementParagraph + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_BlockElementParagraphSub) + if _tmp + _ary << @result + while true + _tmp = apply(:_BlockElementParagraphSub) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; paragraph(self, c.flatten); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BlockElementParagraph unless _tmp + return _tmp + end + + # BlockElementParagraphSub = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } + def _BlockElementParagraphSub + _save = self.pos while true # sequence _save1 = self.pos @@ -2227,7 +2419,7 @@ def _BlockElementParagraph break end _save2 = self.pos - _tmp = apply(:_SinglelineComment) + _tmp = apply(:_BlankLine) _tmp = _tmp ? nil : true self.pos = _save2 unless _tmp @@ -2235,7 +2427,7 @@ def _BlockElementParagraph break end _save3 = self.pos - _tmp = apply(:_BlockElement) + _tmp = apply(:_SinglelineComment) _tmp = _tmp ? nil : true self.pos = _save3 unless _tmp @@ -2243,7 +2435,7 @@ def _BlockElementParagraph break end _save4 = self.pos - _tmp = apply(:_Ulist) + _tmp = apply(:_BlockElement) _tmp = _tmp ? nil : true self.pos = _save4 unless _tmp @@ -2251,7 +2443,7 @@ def _BlockElementParagraph break end _save5 = self.pos - _tmp = apply(:_Olist) + _tmp = apply(:_Ulist) _tmp = _tmp ? nil : true self.pos = _save5 unless _tmp @@ -2259,13 +2451,21 @@ def _BlockElementParagraph break end _save6 = self.pos - _tmp = apply(:_Dlist) + _tmp = apply(:_Olist) _tmp = _tmp ? nil : true self.pos = _save6 unless _tmp self.pos = _save break end + _save7 = self.pos + _tmp = apply(:_Dlist) + _tmp = _tmp ? nil : true + self.pos = _save7 + unless _tmp + self.pos = _save + break + end _tmp = apply(:_SinglelineContent) c = @result unless _tmp @@ -2273,6 +2473,44 @@ def _BlockElementParagraph break end _tmp = apply(:_Newline) + n = @result + unless _tmp + self.pos = _save + break + end + @result = begin; [c, n] ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_BlockElementParagraphSub unless _tmp + return _tmp + end + + # CodeBlockElementContents = CodeBlockElementContent+:c { c } + def _CodeBlockElementContents + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_CodeBlockElementContent) + if _tmp + _ary << @result + while true + _tmp = apply(:_CodeBlockElementContent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + c = @result unless _tmp self.pos = _save break @@ -2285,7 +2523,90 @@ def _BlockElementParagraph break end # end sequence - set_failed_rule :_BlockElementParagraph unless _tmp + set_failed_rule :_CodeBlockElementContents unless _tmp + return _tmp + end + + # CodeBlockElementContent = (SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] }) + def _CodeBlockElementContent + + _save = self.pos + while true # choice + + _save1 = self.pos + while true # sequence + _tmp = apply(:_SinglelineComment) + c = @result + unless _tmp + self.pos = _save1 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save1 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _tmp = apply(:_BlankLine) + c = @result + unless _tmp + self.pos = _save2 + break + end + @result = begin; ::ReVIEW::TextNode.new(self, "\n") ; end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save3 = self.pos + while true # sequence + _save4 = self.pos + _tmp = match_string("//}") + _tmp = _tmp ? nil : true + self.pos = _save4 + unless _tmp + self.pos = _save3 + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save3 + break + end + _tmp = apply(:_Newline) + n = @result + unless _tmp + self.pos = _save3 + break + end + @result = begin; [c, ::ReVIEW::TextNode.new(self, "\n")] ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_CodeBlockElementContent unless _tmp return _tmp end @@ -3637,9 +3958,25 @@ def _BOM return _tmp end - # Newline = %literals.Newline + # Newline = %literals.Newline:n {newline(self, "\n")} def _Newline - _tmp = @_grammar_literals.external_invoke(self, :_Newline) + + _save = self.pos + while true # sequence + _tmp = @_grammar_literals.external_invoke(self, :_Newline) + n = @result + unless _tmp + self.pos = _save + break + end + @result = begin; newline(self, "\n"); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + set_failed_rule :_Newline unless _tmp return _tmp end @@ -3662,14 +3999,14 @@ def _Spacechar Rules[:_root] = rule_info("root", "Start") Rules[:_Start] = rule_info("Start", "&. Document:c { @strategy.ast = c }") Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") - Rules[:_Block] = rule_info("Block", "BlankLine* (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") + Rules[:_Block] = rule_info("Block", "BlankLine*:c { c } (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > EOL {singleline_comment(self, text)}") Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") - Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents?:contents \"//}\" Space* EOL {code_block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })") Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewLine > { text })") @@ -3677,8 +4014,11 @@ def _Spacechar Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") - Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c {singleline_content(self, c)} | BlockElementParagraph:c {singleline_content(self, c)} | BlankLine:c {singleline_content(self, \"\")})") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "!\"//}\" !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") + Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c {paragraph(self, c.flatten)}") + Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") + Rules[:_CodeBlockElementContents] = rule_info("CodeBlockElementContents", "CodeBlockElementContent+:c { c }") + Rules[:_CodeBlockElementContent] = rule_info("CodeBlockElementContent", "(SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, \"\\n\") } | !\"//}\" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, \"\\n\")] })") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) }") @@ -3710,7 +4050,7 @@ def _Spacechar Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "%literals.LowerAlphabetAscii") Rules[:_Digit] = rule_info("Digit", "%literals.Digit") Rules[:_BOM] = rule_info("BOM", "%literals.BOM") - Rules[:_Newline] = rule_info("Newline", "%literals.Newline") + Rules[:_Newline] = rule_info("Newline", "%literals.Newline:n {newline(self, \"\\n\")}") Rules[:_NonAlphanumeric] = rule_info("NonAlphanumeric", "%literals.NonAlphanumeric") Rules[:_Spacechar] = rule_info("Spacechar", "%literals.Spacechar") # :startdoc: diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 47c8d6ca5..6c21c4cc0 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -460,7 +460,10 @@ def list_body(id, lines) buf end - def source(lines, caption = nil) + def node_source(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = %Q[
      \n] buf << source_header(caption) buf << source_body(caption, lines) @@ -484,7 +487,10 @@ def source_body(id, lines) buf end - def listnum(lines, id, caption) + def node_listnum(node) + id, caption = node.parse_args(:raw, :doc) + lines = node.raw_lines + buf = %Q[
      \n] begin buf << list_header(id, caption) @@ -660,7 +666,10 @@ def image_header(id, caption) buf end - def table(lines, id = nil, caption = nil) + def node_table(node) + id, caption = node.parse_args(:raw, :doc) + lines = node.raw_lines + rows = [] sepidx = nil lines.each_with_index do |line, idx| diff --git a/lib/review/node.rb b/lib/review/node.rb index 2a273e80b..e24ba0a54 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -25,7 +25,7 @@ def to_s_by(meth) end end - def to_json + def to_json(*args) if content.kind_of? String val = '"'+@content.gsub(/\"/,'\\"')+'"' elsif content == nil @@ -92,13 +92,37 @@ def parse_args(*patterns) end end end + end - def raw_lines - self.to_raw.split(/\n/) + class CodeBlockElementNode < Node + + def to_doc + # content_str = super + args = @args.map(&:to_doc) + if @content + content_lines = @content.map(&:to_doc) + else + content_lines = nil + end + @compiler.compile_command(@name, @args, content_lines, self) end + def parse_args(*patterns) + patterns.map.with_index do |pattern, i| + if @args[i] + @args[i].__send__("to_#{pattern}") + else + nil + end + end + end + + def raw_lines + self.content.to_doc.split(/\n/) + end end + class InlineElementNode < Node def to_raw diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 888febbee..2d71ffb65 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -125,11 +125,15 @@ require 'review/node' end def block_required? - @type == :block + @type == :block or @type == :code_block end def block_allowed? - @type == :block or @type == :optional + @type == :block or @type == :code_block or @type == :optional + end + + def code_block? + @type == :code_block end end @@ -139,6 +143,10 @@ require 'review/node' defsyntax(name, (optional ? :optional : :block), argc, esc, &block) end + def self.defcodeblock(name, argc, optional = false, esc = nil, &block) + defsyntax(name, :code_block, argc, esc, &block) + end + def self.defsingle(name, argc, &block) defsyntax name, :line, argc, &block end @@ -175,21 +183,22 @@ require 'review/node' defblock :read, 0 defblock :lead, 0 - defblock :list, 2, nil, [:raw,:doc] - defblock :emlist, 0..1 - defblock :cmd, 0..1 - defblock :table, 0..2 defblock :quote, 0 defblock :image, 2..3, true, [:raw,:doc,:raw] - defblock :source, 0..1 - defblock :listnum, 2 - defblock :emlistnum, 0..1 defblock :bibpaper, 2..3, true defblock :doorquote, 1 defblock :talk, 0 - defblock :texequation, 0 defblock :graph, 1..3 + defcodeblock :emlist, 0..1 + defcodeblock :cmd, 0..1 + defcodeblock :source, 0..1 + defcodeblock :list, 2, nil + defcodeblock :listnum, 2 + defcodeblock :emlistnum, 0..1 + defcodeblock :texequation, 0 + defcodeblock :table, 0..2 + defblock :address, 0 defblock :blockquote, 0 defblock :bpo, 0 @@ -456,6 +465,7 @@ require 'review/node' %% headline = ast HeadlineNode(compiler, level, cmd, label, content) %% paragraph = ast ParagraphNode(compiler, content) %% block_element = ast BlockElementNode(compiler, name, args, content) +%% code_block_element = ast CodeBlockElementNode(compiler, name, args, content) %% inline_element = ast InlineElementNode(compiler, symbol, content) %% inline_element_content = ast InlineElementContentNode(compiler, content) %% text = ast TextNode(compiler, content) @@ -508,6 +518,8 @@ ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) + | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL + ~code_block_element(self, symbol, args, contents) | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL ~block_element(self, symbol, args, contents) | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL ~block_element(self, symbol, args, nil) @@ -546,14 +558,21 @@ BlockElementContent = ( SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c - | BlankLine:c ~newline(self, c) - | BlockElementParagraph:c ~singleline_content(self, c) + | BlankLine:c { c } + | BlockElementParagraph:c { c } ) ## it's like Paragraph, but it's in a block, so do not allow '//}\n' BlockElementParagraph = BlockElementParagraphSub+:c ~paragraph(self, c.flatten) BlockElementParagraphSub = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } +## In CodeBlockElementContents, newline should no be ingored. So we use TextNode instead of NewLineNode. +CodeBlockElementContents = CodeBlockElementContent+:c { c } +CodeBlockElementContent = ( SinglelineComment:c { c } + | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } + | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] } + ) + ## * 箇条書き ## 行頭から… の指定がない diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 44c38ef80..5bc95ed6c 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -318,7 +318,7 @@ def @chapter.image(id) def test_quote actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n") - assert_equal %Q|

      foobar

      \n

      buz

      \n|, actual + assert_equal %Q|

      foobar

      \n

      buz

      \n
      \n|, actual end def test_memo @@ -527,7 +527,7 @@ def @chapter.bibpaper(id) end actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, actual + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n
      \n|, actual end def test_bibpaper_normalized @@ -536,7 +536,7 @@ def @chapter.bibpaper(id) end actual = compile_block("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n|, actual + assert_equal %Q|
      \n[1] sample bib bold\n

      ab

      \n
      \n|, actual end def test_bibpaper_with_anchor @@ -545,7 +545,7 @@ def @chapter.bibpaper(id) end actual = compile_block("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") - assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n|, actual + assert_equal %Q|
      \n[1] sample bib http://example.jp\n

      ab

      \n
      \n|, actual end def column_helper(review) @@ -834,8 +834,7 @@ def test_ul_in_note_and_emlist

      aaaabbb

      -
      -abc
      +
      abc
       ddd
        * A
        * B
      
      From 7445366070419403253272838411c811872d2ce1 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Thu, 12 Feb 2015 20:00:24 +0900
      Subject: [PATCH 142/214] remove split_paragraph and use node_* methods
      
      ---
       lib/review/topbuilder.rb | 27 +++++++++++++++++++--------
       1 file changed, 19 insertions(+), 8 deletions(-)
      
      diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb
      index 104ace55c..20e38288e 100644
      --- a/lib/review/topbuilder.rb
      +++ b/lib/review/topbuilder.rb
      @@ -210,7 +210,7 @@ def dt(line)
       
           def dd(lines)
             buf = ""
      -      split_paragraph(lines).each do |paragraph|
      +      lines.each do |paragraph|
               buf << "\t#{paragraph.gsub(/\n/, '')}\n"
             end
             blank_reset
      @@ -229,7 +229,7 @@ def paragraph(lines)
           def read(lines)
             buf = ""
             buf << "◆→開始:#{@titles["lead"]}←◆\n"
      -      buf << split_paragraph(lines).join("\n") << "\n"
      +      buf << lines.join("\n") << "\n"
             buf << "◆→終了:#{@titles["lead"]}←◆\n"
             blank_reset
             buf << blank
      @@ -277,7 +277,7 @@ def base_block(type, lines, caption = nil)
             buf  << blank
             buf << "◆→開始:#{@titles[type]}←◆\n"
             buf << "■#{caption}\n" unless caption.nil?
      -      buf << lines.join("\n") << "\n"
      +      buf << lines.join("")
             buf << "◆→終了:#{@titles[type]}←◆\n"
             blank_reset
             buf << blank
      @@ -289,18 +289,24 @@ def base_parablock(type, lines, caption = nil)
             buf << blank
             buf << "◆→開始:#{@titles[type]}←◆\n"
             buf << "■#{caption}\n" unless caption.nil?
      -      buf << split_paragraph(lines).join("\n") << "\n"
      +      buf << lines.join("")
             buf << "◆→終了:#{@titles[type]}←◆\n"
             blank_reset
             buf << blank
             buf
           end
       
      -    def emlist(lines, caption = nil)
      +    def node_emlist(node)
      +      caption, = node.parse_args(:doc)
      +      lines = node.raw_lines
      +
             base_block "emlist", lines, caption
           end
       
      -    def emlistnum(lines, caption = nil)
      +    def node_emlistnum(node)
      +      caption, = node.parse_args(:doc)
      +      lines = node.raw_lines
      +
             buf = ""
             buf << blank
             buf << "◆→開始:#{@titles["emlist"]}←◆\n"
      @@ -326,7 +332,10 @@ def listnum_body(lines)
             buf
           end
       
      -    def cmd(lines, caption = nil)
      +    def node_cmd(node)
      +      caption, = node.parse_args(:doc)
      +      lines = node.raw_lines
      +
             base_block "cmd", lines, caption
           end
       
      @@ -376,7 +385,9 @@ def image(lines, id, caption, metric=nil)
             buf
           end
       
      -    def texequation(lines)
      +    def node_texequation(node)
      +      lines = node.raw_lines
      +
             buf = ""
             buf << "◆→開始:#{@titles["texequation"]}←◆\n"
             buf << "#{lines.join("\n")}\n"
      
      From 3ff11b926451e7a21d2560c2103afc2a47aaf5f9 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Thu, 12 Feb 2015 21:49:53 +0900
      Subject: [PATCH 143/214] fix other builders
      
      ---
       lib/review/builder.rb         |  15 ++++-
       lib/review/compiler.rb        |  16 +++---
       lib/review/htmlbuilder.rb     |   4 +-
       lib/review/idgxmlbuilder.rb   |  68 ++++++++++++++---------
       lib/review/inaobuilder.rb     |  22 ++++++--
       lib/review/latexbuilder.rb    |  64 ++++++++++++++-------
       lib/review/markdownbuilder.rb |  17 ++++--
       lib/review/review.kpeg        |  12 ++--
       lib/review/textutils.rb       |  22 --------
       lib/review/topbuilder.rb      | 102 ++++++++++------------------------
       test/test_idgxmlbuilder.rb    |   2 +-
       test/test_textutils.rb        |   3 +
       12 files changed, 175 insertions(+), 172 deletions(-)
      
      diff --git a/lib/review/builder.rb b/lib/review/builder.rb
      index ace938047..4a7646e00 100644
      --- a/lib/review/builder.rb
      +++ b/lib/review/builder.rb
      @@ -86,7 +86,10 @@ def headline_prefix(level)
           end
           private :headline_prefix
       
      -    def list(lines, id, caption)
      +    def node_list(node)
      +      id, caption = node.parse_args(:raw, :doc)
      +      lines = node.raw_lines
      +
             buf = ""
             begin
               buf << list_header(id, caption)
      @@ -97,7 +100,10 @@ def list(lines, id, caption)
             buf
           end
       
      -    def listnum(lines, id, caption)
      +    def node_listnum(node)
      +      id, caption = node.parse_args(:raw, :doc)
      +      lines = node.raw_lines
      +
             buf = ""
             begin
               buf << list_header(id, caption)
      @@ -108,7 +114,10 @@ def listnum(lines, id, caption)
             buf
           end
       
      -    def source(lines, caption)
      +    def node_source(node)
      +      caption, = node.parse_args(:doc)
      +      lines = node.raw_lines
      +
             buf = ""
             buf << source_header(caption)
             buf << source_body(lines)
      diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb
      index 1be8f0b52..51ce23d03 100644
      --- a/lib/review/compiler.rb
      +++ b/lib/review/compiler.rb
      @@ -484,11 +484,11 @@ def block_required?
             end
       
             def block_allowed?
      -        @type == :block or @type == :code_block or @type == :optional
      +        @type == :block or @type == :code_block or @type == :optional or @type == :optional_code_block
             end
       
             def code_block?
      -        @type == :code_block
      +        @type == :code_block or @type == :optional_code_block
             end
           end
       
      @@ -499,7 +499,7 @@ def self.defblock(name, argc, optional = false, esc = nil, &block)
           end
       
           def self.defcodeblock(name, argc, optional = false, esc = nil, &block)
      -      defsyntax(name, :code_block, argc, esc, &block)
      +      defsyntax(name, (optional ? :optional_code_block : :code_block), argc, esc, &block)
           end
       
           def self.defsingle(name, argc, &block)
      @@ -539,7 +539,6 @@ def inline_defined?(name)
           defblock :read, 0
           defblock :lead, 0
           defblock :quote, 0
      -    defblock :image, 2..3, true, [:raw,:doc,:raw]
           defblock :bibpaper, 2..3, true
           defblock :doorquote, 1
           defblock :talk, 0
      @@ -553,6 +552,8 @@ def inline_defined?(name)
           defcodeblock :emlistnum, 0..1
           defcodeblock :texequation, 0
           defcodeblock :table, 0..2
      +    defcodeblock :image, 2..3, true, [:raw,:doc,:raw]
      +    defcodeblock :box, 0..1
       
           defblock :address, 0
           defblock :blockquote, 0
      @@ -560,7 +561,6 @@ def inline_defined?(name)
           defblock :flushright, 0
           defblock :centering, 0
           defblock :note, 0..1
      -    defblock :box, 0..1
           defblock :comment, 0..1, true
       
           defsingle :footnote, 2
      @@ -1495,7 +1495,7 @@ def _ParagraphSub
           return _tmp
         end
       
      -  # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL {code_block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})
      +  # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, r1)} | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL {code_block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})
         def _BlockElement
       
           _save = self.pos
      @@ -1585,7 +1585,7 @@ def _BlockElement
                 break
               end
               _save7 = self.pos
      -        _tmp = begin;  syntax = syntax_descriptor(symbol); syntax.code_block? ; end
      +        _tmp = begin;  syntax = syntax_descriptor(symbol); syntax && syntax.code_block? ; end
               self.pos = _save7
               unless _tmp
                 self.pos = _save5
      @@ -4006,7 +4006,7 @@ def _Spacechar
         Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }")
         Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}")
         Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }")
      -  Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents?:contents \"//}\" Space* EOL {code_block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})")
      +  Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents?:contents \"//}\" Space* EOL {code_block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})")
         Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }")
         Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })")
         Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewLine > { text })")
      diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb
      index 6c21c4cc0..5b9382be3 100644
      --- a/lib/review/htmlbuilder.rb
      +++ b/lib/review/htmlbuilder.rb
      @@ -604,7 +604,9 @@ def talk(lines)
             buf
           end
       
      -    def texequation(lines)
      +    def node_texequation(node)
      +      lines = node.raw_lines
      +
             buf << %Q[
      \n] if @book.config["mathml"] require 'math_ml' diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 14709a6af..ed8d89868 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -23,7 +23,7 @@ class IDGXMLBuilder < Builder [:ttbold, :hint, :maru, :keytop, :labelref, :ref, :pageref, :balloon].each {|e| Compiler.definline(e) } Compiler.defsingle(:dtp, 1) - Compiler.defblock(:insn, 0..1) + Compiler.defcodeblock(:insn, 1) Compiler.defblock(:memo, 0..1) Compiler.defblock(:tip, 0..1) Compiler.defblock(:info, 0..1) @@ -42,14 +42,6 @@ class IDGXMLBuilder < Builder Compiler.defblock(:expert, 0) Compiler.defblock(:rawblock, 0) - def pre_paragraph - '

      ' - end - - def post_paragraph - '

      ' - end - def extname '.xml' end @@ -300,9 +292,10 @@ def paragraph(lines) def read(lines) if @book.config["deprecated-blocklines"].nil? - %Q[#{split_paragraph(lines).join}] + @lf + %Q[#{lines.join}] + @lf else - %Q[

      #{lines.join}

      ] + @lf + str = lines.map{|l| l.sub(/^

      /,"").sub(/<\/p>$/,"")}.join() + %Q[

      #{str}

      ] + @lf end end @@ -371,13 +364,20 @@ def list_body(id, lines) buf end - def emlist(lines, caption = nil) + def node_emlist(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + quotedlist lines, 'emlist', caption end - def emlistnum(lines, caption = nil) + def node_emlistnum(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + _lines = [] lines.each_with_index do |line, i| + line.chomp! ## new parser don't remove LF in table (code block) _lines << detab("" + (i + 1).to_s.rjust(2) + ": " + line) end quotedlist _lines, 'emlistnum', caption @@ -403,7 +403,10 @@ def listnum_body(lines) buf end - def cmd(lines, caption = nil) + def node_cmd(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + quotedlist lines, 'cmd', caption end @@ -432,10 +435,10 @@ def quotedlist(lines, css_class, caption) def quote(lines) if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - "#{blocked_lines.join("")}" + @lf + "#{lines.join("")}" + @lf else - "#{lines.join("\n")}" + @lf + str = lines.map{|l| l.sub(/^

      /,"").sub(/<\/p>$/,"")}.join("\n") + "#{str}" + @lf end end @@ -511,7 +514,9 @@ def image_header(id, caption) end end - def texequation(lines) + def node_texequation(node) + lines = node.raw_lines + buf = "" @texblockequation += 1 buf << %Q[] << @lf @@ -532,6 +537,7 @@ def table(lines, id = nil, caption = nil) rows = [] sepidx = nil lines.each_with_index do |line, idx| + line.chomp! ## new parser don't remove LF in table (code block) if /\A[\=\-]{12}/ =~ line sepidx ||= idx next @@ -915,14 +921,15 @@ def sup_end(level) def flushright(lines) if @book.config["deprecated-blocklines"].nil? - split_paragraph(lines).join.gsub("

      ", "

      ") + @lf + lines.join("").gsub("

      ", "

      ") + @lf else - "

      #{lines.join("\n")}

      " + @lf + str = lines.map{|l| l.sub(/^

      /,"").sub(/<\/p>$/,"")}.join("\n") + "

      #{str}

      " + @lf end end def centering(lines) - split_paragraph(lines).join.gsub("

      ", "

      ") + @lf + lines.join("").gsub("

      ", "

      ") + @lf end def captionblock(type, lines, caption, specialstyle = nil) @@ -931,10 +938,10 @@ def captionblock(type, lines, caption, specialstyle = nil) style = specialstyle.nil? ? "#{type}-title" : specialstyle buf << "#{(caption)}" + @lf unless caption.nil? if @book.config["deprecated-blocklines"].nil? - blocked_lines = split_paragraph(lines) - buf << "#{blocked_lines.join}" << @lf + buf << "#{lines.join}" << @lf else - buf << "#{lines.join("\n")}" << @lf + str = lines.map{|l| l.sub(/^

      /,"").sub(/<\/p>$/,"")}.join("\n") + buf << "#{str}" << @lf end buf end @@ -1034,6 +1041,7 @@ def syntaxblock(type, lines, caption) end no = 1 lines.each do |line| + line.chomp! unless @book.config["listinfo"].nil? buf << %Q[ #{line}"}.join("\n> \n") << "\n" + buf << lines.map{|line| line.chomp!;line.chomp!;"> #{line}"}.join("\n") << "\n" blank_reset buf << "\n" buf @@ -129,7 +129,10 @@ def ol_end buf end - def emlist(lines, caption = nil) + def node_emlist(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = "" buf << blank if caption @@ -223,7 +226,10 @@ def image_ext "jpg" end - def cmd(lines) + def node_cmd(node) + caption, = node.parse_args(:doc) + lines = node.raw_lines + buf = "" buf << "```" << "\n" blank_reset @@ -234,7 +240,10 @@ def cmd(lines) buf end - def table(lines, id = nil, caption = nil) + def node_table(node) + id, caption = node.parse_args(:raw, :doc) + lines = node.raw_lines + buf = "" rows = [] sepidx = nil diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 2d71ffb65..a5298a0bf 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -129,11 +129,11 @@ require 'review/node' end def block_allowed? - @type == :block or @type == :code_block or @type == :optional + @type == :block or @type == :code_block or @type == :optional or @type == :optional_code_block end def code_block? - @type == :code_block + @type == :code_block or @type == :optional_code_block end end @@ -144,7 +144,7 @@ require 'review/node' end def self.defcodeblock(name, argc, optional = false, esc = nil, &block) - defsyntax(name, :code_block, argc, esc, &block) + defsyntax(name, (optional ? :optional_code_block : :code_block), argc, esc, &block) end def self.defsingle(name, argc, &block) @@ -184,7 +184,6 @@ require 'review/node' defblock :read, 0 defblock :lead, 0 defblock :quote, 0 - defblock :image, 2..3, true, [:raw,:doc,:raw] defblock :bibpaper, 2..3, true defblock :doorquote, 1 defblock :talk, 0 @@ -198,6 +197,8 @@ require 'review/node' defcodeblock :emlistnum, 0..1 defcodeblock :texequation, 0 defcodeblock :table, 0..2 + defcodeblock :image, 2..3, true, [:raw,:doc,:raw] + defcodeblock :box, 0..1 defblock :address, 0 defblock :blockquote, 0 @@ -205,7 +206,6 @@ require 'review/node' defblock :flushright, 0 defblock :centering, 0 defblock :note, 0..1 - defblock :box, 0..1 defblock :comment, 0..1, true defsingle :footnote, 2 @@ -518,7 +518,7 @@ ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) - | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL + | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL ~code_block_element(self, symbol, args, contents) | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL ~block_element(self, symbol, args, contents) diff --git a/lib/review/textutils.rb b/lib/review/textutils.rb index 54f4bbb7c..5ee8140a9 100644 --- a/lib/review/textutils.rb +++ b/lib/review/textutils.rb @@ -13,28 +13,6 @@ def detab(str, ts = 8) } end - def split_paragraph(lines) - pre = pre_paragraph - post = post_paragraph - - blocked_lines = [[]] - lines.each {|element| - if element == "" - if blocked_lines.last != [] - blocked_lines << [] - end - else - blocked_lines.last << element - end - } - - if !pre.nil? and !post.nil? - blocked_lines.map!{|i| [pre] + i + [post] } - end - - blocked_lines.map{|l| l.join("")} - end - def convert_inencoding(str, enc) case enc when /^EUC$/i diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb index 20e38288e..109d91bad 100644 --- a/lib/review/topbuilder.rb +++ b/lib/review/topbuilder.rb @@ -22,7 +22,7 @@ class TOPBuilder < Builder } Compiler.defsingle(:dtp, 1) - Compiler.defblock(:insn, 1) + Compiler.defcodeblock(:insn, 1) Compiler.defblock(:memo, 0..1) Compiler.defblock(:tip, 0..1) Compiler.defblock(:info, 0..1) @@ -55,7 +55,6 @@ def builder_init_file @subsection = 0 @subsubsection = 0 @subsubsubsection = 0 - @blank_seen = true @titles = { "emlist" => "インラインリスト", @@ -102,21 +101,6 @@ def builder_init_file end private :builder_init_file - def blank_reset - @blank_seen = false - end - - def blank - seen = @blank_seen - @blank_seen = true - unless seen - "\n" - else - "" - end - end - private :blank - def warn(msg) $stderr.puts "#{@location.filename}:#{@location.lineno}: warning: #{msg}" end @@ -132,7 +116,6 @@ def messages def headline(level, label, caption) buf = "" prefix = "" - buf << blank case level when 1 if @chapter.number.to_s =~ /\A\d+\Z/ @@ -167,44 +150,40 @@ def headline(level, label, caption) end prefix = "" if (level.to_i > @book.config["secnolevel"]) buf << "■H#{level}■#{prefix}#{caption}\n" - blank_reset buf end def ul_begin - blank + "\n" end def ul_item(lines) - blank_reset "●\t#{lines.join}\n" end def ul_end - blank + "\n" end def ol_begin @olitem = 0 - blank + "\n" end def ol_item(lines, num) - blank_reset "#{num}\t#{lines.join}\n" end def ol_end @olitem = nil - blank + "\n" end def dl_begin - blank + "\n" end def dt(line) - blank_reset "★#{line}☆\n" end @@ -213,16 +192,14 @@ def dd(lines) lines.each do |paragraph| buf << "\t#{paragraph.gsub(/\n/, '')}\n" end - blank_reset buf end def dl_end - blank + "\n" end def paragraph(lines) - blank_reset lines.join+"\n" end @@ -231,8 +208,7 @@ def read(lines) buf << "◆→開始:#{@titles["lead"]}←◆\n" buf << lines.join("\n") << "\n" buf << "◆→終了:#{@titles["lead"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -245,20 +221,18 @@ def inline_list(id) else %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap(chapter), @chapter.list(id).number])}] end - end def list_header(id, caption) buf = "" - buf << blank + #buf << "\n" buf << "◆→開始:#{@titles["list"]}←◆\n" if get_chap.nil? buf << %Q[#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << "\n" else buf << %Q[#{I18n.t("list")}#{I18n.t("format_number", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}] << "\n" end - blank_reset - buf << blank + buf << "\n" buf end @@ -268,31 +242,28 @@ def list_body(id, lines) buf << detab(line) << "\n" end buf << "◆→終了:#{@titles["list"]}←◆\n" - blank_reset - buf << blank + buf << "\n" + buf end def base_block(type, lines, caption = nil) buf = "" - buf << blank + buf << "\n" buf << "◆→開始:#{@titles[type]}←◆\n" buf << "■#{caption}\n" unless caption.nil? buf << lines.join("") buf << "◆→終了:#{@titles[type]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end def base_parablock(type, lines, caption = nil) buf = "" - buf << blank buf << "◆→開始:#{@titles[type]}←◆\n" buf << "■#{caption}\n" unless caption.nil? buf << lines.join("") buf << "◆→終了:#{@titles[type]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -308,7 +279,7 @@ def node_emlistnum(node) lines = node.raw_lines buf = "" - buf << blank + #buf << "\n" buf << "◆→開始:#{@titles["emlist"]}←◆\n" buf << "■#{caption}\n" unless caption.nil? _lines = [] @@ -316,8 +287,7 @@ def node_emlistnum(node) buf << (i + 1).to_s.rjust(2) + ": #{line}\n" end buf << "◆→終了:#{@titles["emlist"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -327,8 +297,7 @@ def listnum_body(lines) buf << (i + 1).to_s.rjust(2) + ": #{line}\n" end buf << "◆→終了:#{@titles["list"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -363,15 +332,14 @@ def inline_img(id) def image(lines, id, caption, metric=nil) buf = "" - buf << blank + #buf << "\n" buf << "◆→開始:#{@titles["image"]}←◆\n" if get_chap.nil? buf << "#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" else buf << "#{I18n.t("image")}#{I18n.t("format_number", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" end - blank_reset - buf << blank + buf << "\n" if @chapter.image(id).bound? buf << "◆→#{@chapter.image(id).path}←◆\n" else @@ -380,8 +348,7 @@ def image(lines, id, caption, metric=nil) end end buf << "◆→終了:#{@titles["image"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -392,22 +359,20 @@ def node_texequation(node) buf << "◆→開始:#{@titles["texequation"]}←◆\n" buf << "#{lines.join("\n")}\n" buf << "◆→終了:#{@titles["texequation"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end def table_header(id, caption) buf = "" - buf << blank + buf << "\n" buf << "◆→開始:#{@titles["table"]}←◆\n" if get_chap.nil? buf << "#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" else buf << "#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{caption}\n" end - blank_reset - buf << blank + buf << "\n" buf end @@ -418,7 +383,6 @@ def table_begin(ncols) def tr(rows) buf = "" buf << rows.join("\t") << "\n" - blank_reset buf end @@ -433,8 +397,7 @@ def td(str) def table_end buf = "" buf << "◆→終了:#{@titles["table"]}←◆\n" - blank_reset - buf << blank + buf << "\n" buf end @@ -442,12 +405,10 @@ def comment(lines, comment = nil) lines ||= [] lines.unshift comment unless comment.blank? str = lines.join("") - blank_reset "◆→DTP連絡:#{str}←◆\n" end def footnote(id, str) - blank_reset "【注#{@chapter.footnote(id).number}】#{str}\n" end @@ -564,12 +525,10 @@ def inline_m(str) end def noindent - blank_reset "◆→DTP連絡:次の1行インデントなし←◆\n" end def nonum_begin(level, label, caption) - blank_reset "■H#{level}■#{caption}\n" end @@ -578,18 +537,15 @@ def nonum_end(level) def common_column_begin(type, caption) buf = "" - buf << blank buf << "◆→開始:#{@titles[type]}←◆\n" buf << %Q[■#{caption}\n] - blank_reset buf end def common_column_end(type) buf = "" buf << %Q[◆→終了:#{@titles[type]}←◆\n] - blank_reset - buf << blank + buf << "\n" buf end @@ -741,7 +697,7 @@ def insn(lines, caption = nil) def indepimage(id, caption=nil, metric=nil) buf = "" - buf << blank + buf << "\n" begin buf << "◆→画像 #{@chapter.image(id).path.sub(/\A\.\//, "")} #{metric.join(" ")}←◆\n" rescue @@ -749,8 +705,7 @@ def indepimage(id, caption=nil, metric=nil) buf << "◆→画像 #{id}←◆\n" end buf << "図 #{caption}\n" if caption.present? - blank_reset - buf << blank + buf << "\n" buf end @@ -839,7 +794,6 @@ def inline_pageref(idref) end def circle_begin(level, label, caption) - blank_reset "・\t#{caption}\n" end diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index c7063448b..badfe9012 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -197,7 +197,7 @@ def test_term_deprecated @book.config["deprecated-blocklines"] = true actual = compile_block("//term{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") @book.config["deprecated-blocklines"] = nil - assert_equal %Q|test1\ntest1.5\n\ntest2|, actual + assert_equal %Q|test1test1.5\n\ntest2|, actual end def test_notice diff --git a/test/test_textutils.rb b/test/test_textutils.rb index e8055e107..b6fef2ab9 100644 --- a/test/test_textutils.rb +++ b/test/test_textutils.rb @@ -30,6 +30,7 @@ def test_detab_with_arg assert_equal " abc def", detabed end +=begin def test_split_paragraph_empty_nil ret = @tu_nil.split_paragraph([]) assert_equal ret, [""] @@ -65,4 +66,6 @@ def test_split_paragraph_nil ret = @tu_nil.split_paragraph(["abc","","","def","ghi"]) assert_equal ["abc","defghi"], ret end +=end + end From d11d5d52d0e09f8b7633af81f1a2741e319332d2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Feb 2015 00:37:49 +0900 Subject: [PATCH 144/214] revert node_* methods --- lib/review/builder.rb | 15 +++--------- lib/review/compiler.rb | 46 +++++++++++++++++------------------ lib/review/htmlbuilder.rb | 39 ++++++----------------------- lib/review/idgxmlbuilder.rb | 45 +++++----------------------------- lib/review/inaobuilder.rb | 17 +++---------- lib/review/latexbuilder.rb | 40 ++++++------------------------ lib/review/markdownbuilder.rb | 15 +++--------- lib/review/node.rb | 2 +- lib/review/review.kpeg | 46 +++++++++++++++++------------------ lib/review/topbuilder.rb | 19 +++------------ 10 files changed, 83 insertions(+), 201 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 85905792e..95f31723a 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -86,10 +86,7 @@ def headline_prefix(level) end private :headline_prefix - def node_list(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def list(lines, id, caption = nil, lang = nil) buf = "" begin buf << list_header(id, caption, lang) @@ -100,10 +97,7 @@ def node_list(node) buf end - def node_listnum(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def listnum(lines, id, caption = nil, lang = nil) buf = "" begin buf << list_header(id, caption, lang) @@ -114,10 +108,7 @@ def node_listnum(node) buf end - def node_source(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def source(lines, caption = nil) buf = "" buf << source_header(caption) buf << source_body(lines) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 81a5ae1c0..544811a46 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -502,8 +502,8 @@ def self.defcodeblock(name, argc, optional = false, esc = nil, &block) defsyntax(name, (optional ? :optional_code_block : :code_block), argc, esc, &block) end - def self.defsingle(name, argc, &block) - defsyntax name, :line, argc, &block + def self.defsingle(name, argc, esc = nil, &block) + defsyntax name, :line, argc, esc, &block end def self.defsyntax(name, type, argc, esc = nil, &block) @@ -539,21 +539,21 @@ def inline_defined?(name) defblock :read, 0 defblock :lead, 0 defblock :quote, 0 - defblock :bibpaper, 2..3, true - defblock :doorquote, 1 + defblock :bibpaper, 2..3, true, [:raw, :doc, :doc] + defblock :doorquote, 1, false, [:doc] defblock :talk, 0 - defblock :graph, 1..3 - - defcodeblock :emlist, 0..2 - defcodeblock :cmd, 0..1 - defcodeblock :source, 0..1 - defcodeblock :list, 2..3, nil - defcodeblock :listnum, 2..3 - defcodeblock :emlistnum, 0..2 - defcodeblock :texequation, 0 - defcodeblock :table, 0..2 + defblock :graph, 1..3, false, [:raw, :raw, :doc] + + defcodeblock :emlist, 0..2, false, [:doc, :raw] + defcodeblock :cmd, 0..1, false, [:doc] + defcodeblock :source, 0..1, false, [:doc] + defcodeblock :list, 2..3, false, [:raw, :doc, :raw] + defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] + defcodeblock :emlistnum, 0..2, false, [:doc, :raw] + defcodeblock :texequation, 0, false + defcodeblock :table, 0..2, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] - defcodeblock :box, 0..1 + defcodeblock :box, 0..1, false, [:doc] defblock :address, 0 defblock :blockquote, 0 @@ -563,19 +563,19 @@ def inline_defined?(name) defblock :note, 0..1 defblock :comment, 0..1, true - defsingle :footnote, 2 + defsingle :footnote, 2, [:raw, :doc] defsingle :noindent, 0 defsingle :linebreak, 0 defsingle :pagebreak, 0 - defsingle :indepimage, 1..3 - defsingle :numberlessimage, 1..3 + defsingle :indepimage, 1..3, [:raw, :doc, :raw] + defsingle :numberlessimage, 1..3, [:raw, :doc, :raw] defsingle :hr, 0 defsingle :parasep, 0 - defsingle :label, 1 - defsingle :raw, 1 - defsingle :tsize, 1 - defsingle :include, 1 - defsingle :olnum, 1 + defsingle :label, 1, [:raw] + defsingle :raw, 1, [:raw] + defsingle :tsize, 1, [:raw] + defsingle :include, 1, [:raw] + defsingle :olnum, 1, [:raw] definline :chapref definline :chap diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index a6ee58967..db9b22af2 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -427,10 +427,7 @@ def read(lines) alias_method :lead, :read - def node_list(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def list(lines, id, caption, lang = nil) buf = %Q[

      \n] begin buf << list_header(id, caption, lang) @@ -460,10 +457,7 @@ def list_body(id, lines, lang) buf end - def node_source(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def source(lines, caption = nil) buf = %Q[
      \n] buf << source_header(caption) buf << source_body(caption, lines) @@ -487,10 +481,7 @@ def source_body(id, lines) buf end - def node_listnum(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def listnum(lines, id, caption, lang = nil) buf = %Q[
      \n] begin buf << list_header(id, caption, lang) @@ -512,10 +503,7 @@ def listnum_body(lines, lang) buf end - def node_emlist(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlist(lines, caption = nil, lang = nil) buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -529,10 +517,7 @@ def node_emlist(node) buf end - def node_emlistnum(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlistnum(lines, caption = nil, lang = nil) buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -547,10 +532,7 @@ def node_emlistnum(node) buf end - def node_cmd(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def cmd(lines, caption = nil) buf = %Q[
      \n] if caption.present? buf << %Q(

      #{caption}

      \n) @@ -606,9 +588,7 @@ def talk(lines) buf end - def node_texequation(node) - lines = node.raw_lines - + def texequation(lines) buf << %Q[
      \n] if @book.config["mathml"] require 'math_ml' @@ -670,10 +650,7 @@ def image_header(id, caption) buf end - def node_table(node) - id, caption = node.parse_args(:raw, :doc) - lines = node.raw_lines - + def table(lines, id = nil, caption = nil) rows = [] sepidx = nil lines.each_with_index do |line, idx| diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index a03c89455..1d9eae894 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -364,17 +364,11 @@ def list_body(id, lines, lang) buf end - def node_emlist(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlist(lines, caption = nil, lang = nil) quotedlist lines, 'emlist', caption end - def node_emlistnum(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlistnum(lines, caption = nil, lang = nil) _lines = [] lines.each_with_index do |line, i| line.chomp! ## new parser don't remove LF in table (code block) @@ -403,10 +397,7 @@ def listnum_body(lines, lang) buf end - def node_cmd(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def cmd(lines, caption = nil) quotedlist lines, 'cmd', caption end @@ -514,9 +505,7 @@ def image_header(id, caption) end end - def node_texequation(node) - lines = node.raw_lines - + def texequation(lines) buf = "" @texblockequation += 1 buf << %Q[] << @lf @@ -1057,17 +1046,11 @@ def syntaxblock(type, lines, caption) buf end - def node_insn(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def insn(lines, caption = nil) syntaxblock("insn", lines, caption) end - def node_box(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def box(lines, caption = nil) syntaxblock("box", lines, caption) end @@ -1085,22 +1068,6 @@ def indepimage(id, caption=nil, metric=nil) buf end - def node_indepimage(node) - id, caption, metric = node.parse_args(:raw, :doc, :raw) - buf = "" - metrics = parse_metric("idgxml", metric) - buf << "" << @lf - begin - buf << %Q[] << @lf - rescue - warn %Q[no such image: #{id}] - end - buf << %Q[#{caption}] + @lf if caption.present? - buf << "" << @lf - buf - end - - alias_method :numberlessimage, :indepimage def label(id) diff --git a/lib/review/inaobuilder.rb b/lib/review/inaobuilder.rb index 62ed800c8..758b79e16 100644 --- a/lib/review/inaobuilder.rb +++ b/lib/review/inaobuilder.rb @@ -201,10 +201,7 @@ def listnum_body(lines, lang) buf end - def node_emlist(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def emlist(lines, caption = nil) buf = "" buf << "◆list/◆\n" buf << %Q[●#{caption}\n] unless caption.nil? @@ -226,10 +223,7 @@ def inline_comment(str) end # whiteリスト代用 - def node_cmd(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def cmd(lines, caption = nil) buf = "◆list-white/◆\n" buf << %Q[●#{caption}\n] unless caption.nil? lines.each do |line| @@ -259,7 +253,7 @@ def image(lines, id, caption, metric=nil) buf << @chapter.image(id).path << "\n" else lines.each do |line| - buf << line + buf << line + "\n" end end buf @@ -274,10 +268,7 @@ def inline_table(id) end end - def node_table(node) - id, caption = node.parse_args(:raw, :doc) - lines = node.raw_lines - + def table(lines, id = nil, caption = nil) buf = "" rows = [] sepidx = nil diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index d90a3d16b..bea848ee4 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -227,10 +227,7 @@ def highlight_listings? end private :highlight_listings? - def node_emlist(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlist(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? buf << common_code_block_lst(lines, 'reviewemlistlst', 'title', caption, lang) @@ -242,10 +239,7 @@ def node_emlist(node) buf end - def node_emlistnum(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlistnum(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? buf << common_code_block_lst(lines, 'reviewemlistnumlst', 'title', caption, lang) @@ -258,10 +252,7 @@ def node_emlistnum(node) end ## override Builder#list - def node_list(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def list(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? buf << common_code_block_lst(lines, 'reviewlistlst', 'caption', caption, lang) @@ -280,10 +271,7 @@ def node_list(node) ## override Builder#listnum - def node_listnum(node) - id, caption, lang = node.parse_args(:raw, :doc, :raw) - lines = node.raw_lines - + def listnum(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? buf << common_code_block_lst(lines, 'reviewlistnumlst', 'caption', caption, lang) @@ -300,11 +288,7 @@ def node_listnum(node) buf end - def node_cmd(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - - + def cmd(lines, caption = nil, lang = nil) buf = "" if highlight_listings? buf << common_code_block_lst(lines, 'reviewcmdlst', 'title', caption, lang) @@ -347,10 +331,7 @@ def common_code_block_lst(lines, command, title, caption, lang) buf end - def node_source(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def source(lines, caption = nil) buf = "\n" buf << '\begin{reviewlist}' << "\n" buf << source_header(caption) @@ -469,10 +450,7 @@ def indepimage(id, caption=nil, metric=nil) alias_method :numberlessimage, :indepimage - def node_table(node) - id, caption = node.parse_args(:raw, :doc) - lines = node.raw_lines - + def table(lines, id = nil, caption = nil) buf = "" rows = [] sepidx = nil @@ -592,9 +570,7 @@ def flushright(lines) latex_block 'flushright', lines end - def node_texequation(node) - lines = node.raw_lines - + def texequation(lines) buf = "\n" buf << macro('begin','equation*') << "\n" lines.each do |line| diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index 9c677966d..50d268bbd 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -130,10 +130,7 @@ def ol_end buf end - def node_emlist(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlist(lines, caption = nil, lang = nil) buf = "" buf << blank if caption @@ -228,10 +225,7 @@ def image_ext "jpg" end - def node_cmd(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def cmd(lines, caption = nil) buf = "" buf << "```shell-session" << "\n" blank_reset @@ -242,10 +236,7 @@ def node_cmd(node) buf end - def node_table(node) - id, caption = node.parse_args(:raw, :doc) - lines = node.raw_lines - + def table(lines, id = nil, caption = nil) buf = "" rows = [] sepidx = nil diff --git a/lib/review/node.rb b/lib/review/node.rb index e24ba0a54..27d93df5c 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -100,7 +100,7 @@ def to_doc # content_str = super args = @args.map(&:to_doc) if @content - content_lines = @content.map(&:to_doc) + content_lines = raw_lines else content_lines = nil end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 49c2e3113..fa18a8ff0 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -147,8 +147,8 @@ require 'review/node' defsyntax(name, (optional ? :optional_code_block : :code_block), argc, esc, &block) end - def self.defsingle(name, argc, &block) - defsyntax name, :line, argc, &block + def self.defsingle(name, argc, esc = nil, &block) + defsyntax name, :line, argc, esc, &block end def self.defsyntax(name, type, argc, esc = nil, &block) @@ -184,21 +184,21 @@ require 'review/node' defblock :read, 0 defblock :lead, 0 defblock :quote, 0 - defblock :bibpaper, 2..3, true - defblock :doorquote, 1 + defblock :bibpaper, 2..3, true, [:raw, :doc, :doc] + defblock :doorquote, 1, false, [:doc] defblock :talk, 0 - defblock :graph, 1..3 - - defcodeblock :emlist, 0..2 - defcodeblock :cmd, 0..1 - defcodeblock :source, 0..1 - defcodeblock :list, 2..3, nil - defcodeblock :listnum, 2..3 - defcodeblock :emlistnum, 0..2 - defcodeblock :texequation, 0 - defcodeblock :table, 0..2 + defblock :graph, 1..3, false, [:raw, :raw, :doc] + + defcodeblock :emlist, 0..2, false, [:doc, :raw] + defcodeblock :cmd, 0..1, false, [:doc] + defcodeblock :source, 0..1, false, [:doc] + defcodeblock :list, 2..3, false, [:raw, :doc, :raw] + defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] + defcodeblock :emlistnum, 0..2, false, [:doc, :raw] + defcodeblock :texequation, 0, false + defcodeblock :table, 0..2, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] - defcodeblock :box, 0..1 + defcodeblock :box, 0..1, false, [:doc] defblock :address, 0 defblock :blockquote, 0 @@ -208,19 +208,19 @@ require 'review/node' defblock :note, 0..1 defblock :comment, 0..1, true - defsingle :footnote, 2 + defsingle :footnote, 2, [:raw, :doc] defsingle :noindent, 0 defsingle :linebreak, 0 defsingle :pagebreak, 0 - defsingle :indepimage, 1..3 - defsingle :numberlessimage, 1..3 + defsingle :indepimage, 1..3, [:raw, :doc, :raw] + defsingle :numberlessimage, 1..3, [:raw, :doc, :raw] defsingle :hr, 0 defsingle :parasep, 0 - defsingle :label, 1 - defsingle :raw, 1 - defsingle :tsize, 1 - defsingle :include, 1 - defsingle :olnum, 1 + defsingle :label, 1, [:raw] + defsingle :raw, 1, [:raw] + defsingle :tsize, 1, [:raw] + defsingle :include, 1, [:raw] + defsingle :olnum, 1, [:raw] definline :chapref definline :chap diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb index dadeff77d..21d5f5d4e 100644 --- a/lib/review/topbuilder.rb +++ b/lib/review/topbuilder.rb @@ -267,17 +267,11 @@ def base_parablock(type, lines, caption = nil) buf end - def node_emlist(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlist(lines, caption = nil, lang = nil) base_block "emlist", lines, caption end - def node_emlistnum(node) - caption, lang = node.parse_args(:doc, :raw) - lines = node.raw_lines - + def emlistnum(lines, caption = nil, lang = nil) buf = "" buf << "◆→開始:#{@titles["emlist"]}←◆\n" buf << "■#{caption}\n" unless caption.nil? @@ -300,10 +294,7 @@ def listnum_body(lines, lang) buf end - def node_cmd(node) - caption, = node.parse_args(:doc) - lines = node.raw_lines - + def cmd(lines, caption = nil) base_block "cmd", lines, caption end @@ -351,9 +342,7 @@ def image(lines, id, caption, metric=nil) buf end - def node_texequation(node) - lines = node.raw_lines - + def texequation(lines) buf = "" buf << "◆→開始:#{@titles["texequation"]}←◆\n" buf << "#{lines.join("\n")}\n" From d8db6c514e8dbfcddfe8d36650e8925f8324f2b5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Feb 2015 14:26:48 +0900 Subject: [PATCH 145/214] rename some symbols symbols FooLine should have Newline at end. --- lib/review/compiler.rb | 74 +++++++++++++++++++++--------------------- lib/review/review.kpeg | 31 ++++++++++-------- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 544811a46..8412520f7 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1237,7 +1237,7 @@ def _BlankLine return _tmp end - # SinglelineComment = "#@" < NonNewLine+ > EOL {singleline_comment(self, text)} + # SinglelineComment = "#@" < NonNewline+ > EOL {singleline_comment(self, text)} def _SinglelineComment _save = self.pos @@ -1249,10 +1249,10 @@ def _SinglelineComment end _text_start = self.pos _save1 = self.pos - _tmp = apply(:_NonNewLine) + _tmp = apply(:_NonNewline) if _tmp while true - _tmp = apply(:_NonNewLine) + _tmp = apply(:_NonNewline) break unless _tmp end _tmp = true @@ -1382,18 +1382,18 @@ def _HeadlinePrefix return _tmp end - # Paragraph = ParagraphSub+:c {paragraph(self, c.flatten)} + # Paragraph = ParagraphLine+:c {paragraph(self, c.flatten)} def _Paragraph _save = self.pos while true # sequence _save1 = self.pos _ary = [] - _tmp = apply(:_ParagraphSub) + _tmp = apply(:_ParagraphLine) if _tmp _ary << @result while true - _tmp = apply(:_ParagraphSub) + _tmp = apply(:_ParagraphLine) _ary << @result if _tmp break unless _tmp end @@ -1419,8 +1419,8 @@ def _Paragraph return _tmp end - # ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } - def _ParagraphSub + # ParagraphLine = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } + def _ParagraphLine _save = self.pos while true # sequence @@ -1491,7 +1491,7 @@ def _ParagraphSub break end # end sequence - set_failed_rule :_ParagraphSub unless _tmp + set_failed_rule :_ParagraphLine unless _tmp return _tmp end @@ -1983,7 +1983,7 @@ def _RawBlockBuilderSelectSub return _tmp end - # RawBlockElementArg = !"]" ("\\]" { "]" } | "\\n" { "\n" } | < NonNewLine > { text }) + # RawBlockElementArg = !"]" ("\\]" { "]" } | "\\n" { "\n" } | < NonNewline > { text }) def _RawBlockElementArg _save = self.pos @@ -2039,7 +2039,7 @@ def _RawBlockElementArg _save5 = self.pos while true # sequence _text_start = self.pos - _tmp = apply(:_NonNewLine) + _tmp = apply(:_NonNewline) if _tmp text = get_text(_text_start) end @@ -2070,7 +2070,7 @@ def _RawBlockElementArg return _tmp end - # BracketArg = "[" BracketArgContentInline*:content "]" {bracket_arg(self, content)} + # BracketArg = "[" BracketArgInline*:content "]" {bracket_arg(self, content)} def _BracketArg _save = self.pos @@ -2082,7 +2082,7 @@ def _BracketArg end _ary = [] while true - _tmp = apply(:_BracketArgContentInline) + _tmp = apply(:_BracketArgInline) _ary << @result if _tmp break unless _tmp end @@ -2110,8 +2110,8 @@ def _BracketArg return _tmp end - # BracketArgContentInline = (InlineElement:c { c } | "\\]" {text(self, "]")} | "\\\\" {text(self, "\\")} | < /[^\r\n\]]/ > {text(self, text)}) - def _BracketArgContentInline + # BracketArgInline = (InlineElement:c { c } | "\\]" {text(self, "]")} | "\\\\" {text(self, "\\")} | < /[^\r\n\]]/ > {text(self, text)}) + def _BracketArgInline _save = self.pos while true # choice @@ -2195,7 +2195,7 @@ def _BracketArgContentInline break end # end choice - set_failed_rule :_BracketArgContentInline unless _tmp + set_failed_rule :_BracketArgInline unless _tmp return _tmp end @@ -2368,18 +2368,18 @@ def _BlockElementContent return _tmp end - # BlockElementParagraph = BlockElementParagraphSub+:c {paragraph(self, c.flatten)} + # BlockElementParagraph = BlockElementParagraphLine+:c {paragraph(self, c.flatten)} def _BlockElementParagraph _save = self.pos while true # sequence _save1 = self.pos _ary = [] - _tmp = apply(:_BlockElementParagraphSub) + _tmp = apply(:_BlockElementParagraphLine) if _tmp _ary << @result while true - _tmp = apply(:_BlockElementParagraphSub) + _tmp = apply(:_BlockElementParagraphLine) _ary << @result if _tmp break unless _tmp end @@ -2405,8 +2405,8 @@ def _BlockElementParagraph return _tmp end - # BlockElementParagraphSub = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } - def _BlockElementParagraphSub + # BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } + def _BlockElementParagraphLine _save = self.pos while true # sequence @@ -2486,7 +2486,7 @@ def _BlockElementParagraphSub break end # end sequence - set_failed_rule :_BlockElementParagraphSub unless _tmp + set_failed_rule :_BlockElementParagraphLine unless _tmp return _tmp end @@ -3187,7 +3187,7 @@ def _Inline return _tmp end - # NonInlineElement = !InlineElement < NonNewLine > {text(self, text)} + # NonInlineElement = !InlineElement < NonNewline > {text(self, text)} def _NonInlineElement _save = self.pos @@ -3201,7 +3201,7 @@ def _NonInlineElement break end _text_start = self.pos - _tmp = apply(:_NonNewLine) + _tmp = apply(:_NonNewline) if _tmp text = get_text(_text_start) end @@ -3808,10 +3808,10 @@ def _InlineElementContentText return _tmp end - # NonNewLine = /[^\r\n]/ - def _NonNewLine + # NonNewline = /[^\r\n]/ + def _NonNewline _tmp = scan(/\A(?-mix:[^\r\n])/) - set_failed_rule :_NonNewLine unless _tmp + set_failed_rule :_NonNewline unless _tmp return _tmp end @@ -4001,22 +4001,22 @@ def _Spacechar Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine*:c { c } (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") - Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewLine+ > EOL {singleline_comment(self, text)}") + Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewline+ > EOL {singleline_comment(self, text)}") Rules[:_Headline] = rule_info("Headline", "HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL {headline(self, level, cmd, label, caption)}") Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") - Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphSub+:c {paragraph(self, c.flatten)}") - Rules[:_ParagraphSub] = rule_info("ParagraphSub", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") + Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphLine+:c {paragraph(self, c.flatten)}") + Rules[:_ParagraphLine] = rule_info("ParagraphLine", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents?:contents \"//}\" Space* EOL {code_block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })") - Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewLine > { text })") - Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgContentInline*:content \"]\" {bracket_arg(self, content)}") - Rules[:_BracketArgContentInline] = rule_info("BracketArgContentInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") + Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewline > { text })") + Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgInline*:content \"]\" {bracket_arg(self, content)}") + Rules[:_BracketArgInline] = rule_info("BracketArgInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") - Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphSub+:c {paragraph(self, c.flatten)}") - Rules[:_BlockElementParagraphSub] = rule_info("BlockElementParagraphSub", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") + Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphLine+:c {paragraph(self, c.flatten)}") + Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") Rules[:_CodeBlockElementContents] = rule_info("CodeBlockElementContents", "CodeBlockElementContent+:c { c }") Rules[:_CodeBlockElementContent] = rule_info("CodeBlockElementContent", "(SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, \"\\n\") } | !\"//}\" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, \"\\n\")] })") Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") @@ -4029,7 +4029,7 @@ def _Spacechar Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") - Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewLine > {text(self, text)}") + Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewline > {text(self, text)}") Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" {inline_element(self, symbol,contents)})") Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawBlockBuilderSelect?:builders RawInlineElementContent+:c \"}\" {raw(self, builders,c)}") Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") @@ -4039,7 +4039,7 @@ def _Spacechar Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") - Rules[:_NonNewLine] = rule_info("NonNewLine", "/[^\\r\\n]/") + Rules[:_NonNewline] = rule_info("NonNewline", "/[^\\r\\n]/") Rules[:_Digits] = rule_info("Digits", "Digit+:c { c }") Rules[:_Space] = rule_info("Space", "/[ \\t]/") Rules[:_EOL] = rule_info("EOL", "(Newline | EOF)") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index fa18a8ff0..007340ba2 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -506,15 +506,15 @@ Block = BlankLine*:c { c } BlankLine = Newline -SinglelineComment = ("#@" < NonNewLine+ > EOL) ~singleline_comment(self, text) +SinglelineComment = ("#@" < NonNewline+ > EOL) ~singleline_comment(self, text) Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL ~headline(self, level, cmd, label, caption) HeadlinePrefix = < /={1,5}/ > { text.length } -Paragraph = ParagraphSub+:c ~paragraph(self, c.flatten) +Paragraph = ParagraphLine+:c ~paragraph(self, c.flatten) -ParagraphSub = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } +ParagraphLine = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) @@ -535,17 +535,17 @@ RawBlockBuilderSelectSub = ( < AlphanumericAscii+ >:c1 Space* "," Space* RawBloc RawBlockElementArg = !"]" ( "\\]" { "]" } | "\\n" { "\n" } - | < NonNewLine > { text } + | < NonNewline > { text } ) -BracketArg = "[" BracketArgContentInline*:content "]" ~bracket_arg(self, content) +BracketArg = "[" BracketArgInline*:content "]" ~bracket_arg(self, content) ## XXX '\' (excpet '\]' and '\\' ) => '\' is ??? -BracketArgContentInline = ( InlineElement:c { c } - | "\\]" ~text(self, "]") - | "\\\\" ~text(self, "\\") - | < /[^\r\n\]]/ > ~text(self, text) - ) +BracketArgInline = ( InlineElement:c { c } + | "\\]" ~text(self, "]") + | "\\\\" ~text(self, "\\") + | < /[^\r\n\]]/ > ~text(self, text) + ) BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } @@ -563,8 +563,8 @@ BlockElementContent = ( SinglelineComment:c { c } ) ## it's like Paragraph, but it's in a block, so do not allow '//}\n' -BlockElementParagraph = BlockElementParagraphSub+:c ~paragraph(self, c.flatten) -BlockElementParagraphSub = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } +BlockElementParagraph = BlockElementParagraphLine+:c ~paragraph(self, c.flatten) +BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } ## In CodeBlockElementContents, newline should no be ingored. So we use TextNode instead of NewLineNode. CodeBlockElementContents = CodeBlockElementContent+:c { c } @@ -598,9 +598,12 @@ DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } SinglelineContent = Inline+:c ~singleline_content(self,c) + +# Inline Element and Non Inline Element + Inline = ( InlineElement | NonInlineElement) -NonInlineElement = !InlineElement < NonNewLine > ~text(self, text) +NonInlineElement = !InlineElement < NonNewline > ~text(self, text) InlineElement = ( RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, symbol,contents) @@ -635,7 +638,7 @@ InlineElementContentText = ( "\\}" ~text(self, "}") | !InlineElement < /[^\r\n\\},]/> ~text(self,text) ) -NonNewLine = /[^\r\n]/ +NonNewline = /[^\r\n]/ Digits = Digit+:c { c } From c70b1deefab45dc4e0a323875bd98a3f8d54eed3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 14 Feb 2015 02:29:09 +0900 Subject: [PATCH 146/214] fix some comments --- lib/review/compiler.rb | 8 ++++++-- lib/review/review.kpeg | 12 +++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 8412520f7..5ef2b8a35 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -2272,7 +2272,7 @@ def _BlockElementContents return _tmp end - # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c }) + # BlockElementContent = (SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Dlist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c }) def _BlockElementContent _save = self.pos @@ -2319,6 +2319,10 @@ def _BlockElementContent c = @result break if _tmp self.pos = _save + _tmp = apply(:_Dlist) + c = @result + break if _tmp + self.pos = _save _tmp = apply(:_Olist) c = @result break if _tmp @@ -4014,7 +4018,7 @@ def _Spacechar Rules[:_BracketArgInline] = rule_info("BracketArgInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, \"]\")} | \"\\\\\\\\\" {text(self, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") - Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") + Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Dlist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphLine+:c {paragraph(self, c.flatten)}") Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") Rules[:_CodeBlockElementContents] = rule_info("CodeBlockElementContents", "CodeBlockElementContent+:c { c }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 007340ba2..e97918f27 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -490,10 +490,10 @@ root = Start Start = &. Document:c { @strategy.ast = c } +## a Document is a set of Blocks Document = Block*:c ~document(self, c) -## TODO InlineElement の後に Ulist / Olist / Dlist が来ると先頭行じゃなくてマッチできてしまうかも -## using &. to detect EOF +## ignore leading blank lines Block = BlankLine*:c { c } ( SinglelineComment:c | Headline:c @@ -516,6 +516,7 @@ Paragraph = ParagraphLine+:c ~paragraph(self, c.flatten) ParagraphLine = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } +# There are 3 types of Block Element: raw Block, Code Block, and Normal Block BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, r1) | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL @@ -549,14 +550,15 @@ BracketArgInline = ( InlineElement:c { c } BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } -## contents との差は paragraph を切るか切らないか +## Standard BlockElement has nested blocks. Texts in content of block are parsed as Paragraph. BlockElementContents = BlockElementContent+:c { c } -## 各要素は Newline で終わらなければならない -### SinglelineContentではないような気がするけど改行は落としてあるのでとりあえずこれで +### Headline is prohibited. +### Note: do not allow "//}" at front of Paragraph. BlockElementContent = ( SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c + | Dlist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c } From c1daf7828cf4a9b8696ea657f5b86bc3f5cb3a74 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 03:36:24 +0900 Subject: [PATCH 147/214] fix Olist and Ulist; support Re:VIEW original syntax and markdown-like indented syntax --- lib/review/compiler.rb | 960 +++++++++++++++++++++++++++++++++-------- lib/review/node.rb | 8 + lib/review/review.kpeg | 51 ++- 3 files changed, 830 insertions(+), 189 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 5ef2b8a35..28e0516d8 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -813,6 +813,14 @@ def error(msg) @strategy.error msg end + def check_indent(s) + s.size >= @list_stack.last.size + end + + def check_nested_indent(s) + s.size >= @list_stack.last.size + 2 + end + @@ -1096,7 +1104,7 @@ def _root return _tmp end - # Start = &. Document:c { @strategy.ast = c } + # Start = &. { @list_stack = Array.new } Document:c { @strategy.ast = c } def _Start _save = self.pos @@ -1108,6 +1116,12 @@ def _Start self.pos = _save break end + @result = begin; @list_stack = Array.new ; end + _tmp = true + unless _tmp + self.pos = _save + break + end _tmp = apply(:_Document) c = @result unless _tmp @@ -2614,73 +2628,46 @@ def _CodeBlockElementContent return _tmp end - # Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)} - def _Ulist + # Indent = " " + def _Indent + _tmp = match_string(" ") + set_failed_rule :_Indent unless _tmp + return _tmp + end + + # Bullet = "*" + def _Bullet + _tmp = match_string("*") + set_failed_rule :_Bullet unless _tmp + return _tmp + end + + # Enumerator = < /[0-9]+/ > { num = text } "." { num.to_i } + def _Enumerator _save = self.pos while true # sequence - _save1 = self.pos - _tmp = get_byte - self.pos = _save1 - unless _tmp - self.pos = _save - break + _text_start = self.pos + _tmp = scan(/\A(?-mix:[0-9]+)/) + if _tmp + text = get_text(_text_start) end - @result = begin; @ulist_elem=[] ; end - _tmp = true unless _tmp self.pos = _save break end - _tmp = apply(:_UlistElement) + @result = begin; num = text ; end + _tmp = true unless _tmp self.pos = _save break end - _save2 = self.pos - - _save3 = self.pos - while true # choice - _tmp = apply(:_UlistElement) - break if _tmp - self.pos = _save3 - _tmp = apply(:_UlistContLine) - break if _tmp - self.pos = _save3 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save3 - break - end # end choice - - if _tmp - while true - - _save4 = self.pos - while true # choice - _tmp = apply(:_UlistElement) - break if _tmp - self.pos = _save4 - _tmp = apply(:_UlistContLine) - break if _tmp - self.pos = _save4 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save4 - break - end # end choice - - break unless _tmp - end - _tmp = true - else - self.pos = _save2 - end + _tmp = match_string(".") unless _tmp self.pos = _save break end - @result = begin; ulist(self, @ulist_elem); end + @result = begin; num.to_i ; end _tmp = true unless _tmp self.pos = _save @@ -2688,37 +2675,42 @@ def _Ulist break end # end sequence - set_failed_rule :_Ulist unless _tmp + set_failed_rule :_Enumerator unless _tmp return _tmp end - # UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } - def _UlistElement + # Ulist = Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item { if b.size > 1 then item.level = b.size end } (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))} + def _Ulist _save = self.pos while true # sequence _save1 = self.pos - _tmp = match_string(" ") + _ary = [] + _tmp = apply(:_Indent) if _tmp + _ary << @result while true - _tmp = match_string(" ") + _tmp = apply(:_Indent) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else self.pos = _save1 end + s = @result unless _tmp self.pos = _save break end _save2 = self.pos _ary = [] - _tmp = match_string("*") + _tmp = apply(:_Bullet) if _tmp _ary << @result while true - _tmp = match_string("*") + _tmp = apply(:_Bullet) _ary << @result if _tmp break unless _tmp end @@ -2727,32 +2719,79 @@ def _UlistElement else self.pos = _save2 end - level = @result + b = @result unless _tmp self.pos = _save break end - while true - _tmp = match_string(" ") - break unless _tmp + _save3 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save3 + end + unless _tmp + self.pos = _save + break end + @result = begin; @list_stack.push(s) ; end _tmp = true unless _tmp self.pos = _save break end - _tmp = apply(:_SinglelineContent) - c = @result + _tmp = apply(:_UlistItemBlock) + item = @result unless _tmp self.pos = _save break end - _tmp = apply(:_EOL) + @result = begin; if b.size > 1 then item.level = b.size end ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _ary = [] + while true + + _save5 = self.pos + while true # choice + _tmp = apply(:_UlistItem) + break if _tmp + self.pos = _save5 + _tmp = apply(:_UlistItemMore) + break if _tmp + self.pos = _save5 + _tmp = apply(:_NestedList) + break if _tmp + self.pos = _save5 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + items = @result + unless _tmp + self.pos = _save + break + end + _save6 = self.pos + _tmp = begin; s == @list_stack.pop ; end + self.pos = _save6 unless _tmp self.pos = _save break end - @result = begin; @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) ; end + @result = begin; ulist(self, items.unshift(item)); end _tmp = true unless _tmp self.pos = _save @@ -2760,120 +2799,106 @@ def _UlistElement break end # end sequence - set_failed_rule :_UlistElement unless _tmp + set_failed_rule :_Ulist unless _tmp return _tmp end - # UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } - def _UlistContLine + # Olist = Indent+:s Enumerator:e Space+ { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))} + def _Olist _save = self.pos while true # sequence - _tmp = match_string(" ") - unless _tmp - self.pos = _save - break - end _save1 = self.pos - _tmp = match_string(" ") + _ary = [] + _tmp = apply(:_Indent) if _tmp + _ary << @result while true - _tmp = match_string(" ") + _tmp = apply(:_Indent) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary else self.pos = _save1 end + s = @result unless _tmp self.pos = _save break end - _save2 = self.pos - _tmp = match_string("*") - _tmp = _tmp ? nil : true - self.pos = _save2 + _tmp = apply(:_Enumerator) + e = @result unless _tmp self.pos = _save break end - _tmp = apply(:_SinglelineContent) - c = @result + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end unless _tmp self.pos = _save break end - _tmp = apply(:_EOL) + @result = begin; @list_stack.push(s) ; end + _tmp = true unless _tmp self.pos = _save break end - @result = begin; @ulist_elem[-1].concat(c) ; end - _tmp = true + _tmp = apply(:_OlistItemBlock) + item = @result unless _tmp self.pos = _save + break end - break - end # end sequence - - set_failed_rule :_UlistContLine unless _tmp - return _tmp - end - - # Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c {olist(self, @olist_elem)} - def _Olist - - _save = self.pos - while true # sequence - @result = begin; @olist_elem = [] ; end + @result = begin; item.num = e ; end _tmp = true unless _tmp self.pos = _save break end - _save1 = self.pos _ary = [] + while true - _save2 = self.pos - while true # choice - _tmp = apply(:_OlistElement) - break if _tmp - self.pos = _save2 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save2 - break - end # end choice - - if _tmp - _ary << @result - while true - - _save3 = self.pos - while true # choice - _tmp = apply(:_OlistElement) - break if _tmp - self.pos = _save3 - _tmp = apply(:_SinglelineComment) - break if _tmp - self.pos = _save3 - break - end # end choice + _save4 = self.pos + while true # choice + _tmp = apply(:_OlistItem) + break if _tmp + self.pos = _save4 + _tmp = apply(:_NestedList) + break if _tmp + self.pos = _save4 + break + end # end choice - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 + _ary << @result if _tmp + break unless _tmp end - c = @result + _tmp = true + @result = _ary + items = @result + unless _tmp + self.pos = _save + break + end + _save5 = self.pos + _tmp = begin; s == @list_stack.pop ; end + self.pos = _save5 unless _tmp self.pos = _save break end - @result = begin; olist(self, @olist_elem); end + @result = begin; olist(self, items.unshift(item)); end _tmp = true unless _tmp self.pos = _save @@ -2885,77 +2910,174 @@ def _Olist return _tmp end - # OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } - def _OlistElement + # UlistItemBlock = ListItemFirstLine:c ListItemLine*:d {ulist_element(self, @list_stack.size, d.unshift(c))} + def _UlistItemBlock _save = self.pos while true # sequence - _save1 = self.pos - _tmp = match_string(" ") - if _tmp - while true - _tmp = match_string(" ") - break unless _tmp - end - _tmp = true - else - self.pos = _save1 - end + _tmp = apply(:_ListItemFirstLine) + c = @result unless _tmp self.pos = _save break end - _text_start = self.pos - _save2 = self.pos - _tmp = scan(/\A(?-mix:\d)/) - if _tmp - while true - _tmp = scan(/\A(?-mix:\d)/) - break unless _tmp - end - _tmp = true - else - self.pos = _save2 - end - if _tmp - text = get_text(_text_start) + _ary = [] + while true + _tmp = apply(:_ListItemLine) + _ary << @result if _tmp + break unless _tmp end + _tmp = true + @result = _ary + d = @result unless _tmp self.pos = _save break end - @result = begin; num=text ; end + @result = begin; ulist_element(self, @list_stack.size, d.unshift(c)); end _tmp = true unless _tmp self.pos = _save - break end - _tmp = match_string(".") + break + end # end sequence + + set_failed_rule :_UlistItemBlock unless _tmp + return _tmp + end + + # OlistItemBlock = ListItemFirstLine:c ListItemLine*:d {olist_element(self, 0, d.unshift(c))} + def _OlistItemBlock + + _save = self.pos + while true # sequence + _tmp = apply(:_ListItemFirstLine) + c = @result unless _tmp self.pos = _save break end + _ary = [] while true - _tmp = apply(:_Space) + _tmp = apply(:_ListItemLine) + _ary << @result if _tmp break unless _tmp end _tmp = true + @result = _ary + d = @result unless _tmp self.pos = _save break end - _tmp = apply(:_SinglelineContent) - c = @result + @result = begin; olist_element(self, 0, d.unshift(c)); end + _tmp = true unless _tmp self.pos = _save - break end - _tmp = apply(:_EOL) - unless _tmp - self.pos = _save + break + end # end sequence + + set_failed_rule :_OlistItemBlock unless _tmp + return _tmp + end + + # ListItemFirstLine = SinglelineContent:c Newline { c } + def _ListItemFirstLine + + _save = self.pos + while true # sequence + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_ListItemFirstLine unless _tmp + return _tmp + end + + # ListItemLine = Indent+:s !Bullet !Enumerator !Space SinglelineContent:c &{ check_indent(s) } Newline { c } + def _ListItemLine + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Bullet) + _tmp = _tmp ? nil : true + self.pos = _save2 + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = apply(:_Enumerator) + _tmp = _tmp ? nil : true + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + _save4 = self.pos + _tmp = apply(:_Space) + _tmp = _tmp ? nil : true + self.pos = _save4 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save + break + end + _save5 = self.pos + _tmp = begin; check_indent(s) ; end + self.pos = _save5 + unless _tmp + self.pos = _save break end - @result = begin; @olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) ; end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -2963,7 +3085,477 @@ def _OlistElement break end # end sequence - set_failed_rule :_OlistElement unless _tmp + set_failed_rule :_ListItemLine unless _tmp + return _tmp + end + + # UlistItemMore = Indent+:s Bullet Bullet+:b Space+ &{ check_indent(s) } UlistItemBlock:item { item.level = b.size+1; item } + def _UlistItemMore + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Bullet) + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _ary = [] + _tmp = apply(:_Bullet) + if _tmp + _ary << @result + while true + _tmp = apply(:_Bullet) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save2 + end + b = @result + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save3 + end + unless _tmp + self.pos = _save + break + end + _save4 = self.pos + _tmp = begin; check_indent(s) ; end + self.pos = _save4 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_UlistItemBlock) + item = @result + unless _tmp + self.pos = _save + break + end + @result = begin; item.level = b.size+1; item ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_UlistItemMore unless _tmp + return _tmp + end + + # UlistItem = Indent+:s Bullet Space+ &{ check_indent(s) } UlistItemBlock:item { item } + def _UlistItem + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Bullet) + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = begin; check_indent(s) ; end + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_UlistItemBlock) + item = @result + unless _tmp + self.pos = _save + break + end + @result = begin; item ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_UlistItem unless _tmp + return _tmp + end + + # OlistItem = Indent+:s Enumerator:e Space+ &{ check_indent(s) } OlistItemBlock:item { item.num = e; item } + def _OlistItem + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Enumerator) + e = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = begin; check_indent(s) ; end + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_OlistItemBlock) + item = @result + unless _tmp + self.pos = _save + break + end + @result = begin; item.num = e; item ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_OlistItem unless _tmp + return _tmp + end + + # NestedUlist = Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))} + def _NestedUlist + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Bullet) + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = begin; check_nested_indent(s) ; end + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + @result = begin; @list_stack.push(s) ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_UlistItemBlock) + item = @result + unless _tmp + self.pos = _save + break + end + _ary = [] + while true + + _save5 = self.pos + while true # choice + _tmp = apply(:_UlistItem) + break if _tmp + self.pos = _save5 + _tmp = apply(:_NestedList) + break if _tmp + self.pos = _save5 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + items = @result + unless _tmp + self.pos = _save + break + end + _save6 = self.pos + _tmp = begin; s == @list_stack.pop ; end + self.pos = _save6 + unless _tmp + self.pos = _save + break + end + @result = begin; ulist(self, items.unshift(item)); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_NestedUlist unless _tmp + return _tmp + end + + # NestedOlist = Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))} + def _NestedOlist + + _save = self.pos + while true # sequence + _save1 = self.pos + _ary = [] + _tmp = apply(:_Indent) + if _tmp + _ary << @result + while true + _tmp = apply(:_Indent) + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + s = @result + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_Enumerator) + e = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 + end + unless _tmp + self.pos = _save + break + end + _save3 = self.pos + _tmp = begin; check_nested_indent(s) ; end + self.pos = _save3 + unless _tmp + self.pos = _save + break + end + @result = begin; @list_stack.push(s) ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_OlistItemBlock) + item = @result + unless _tmp + self.pos = _save + break + end + @result = begin; item.num = e ; end + _tmp = true + unless _tmp + self.pos = _save + break + end + _ary = [] + while true + + _save5 = self.pos + while true # choice + _tmp = apply(:_OlistItem) + break if _tmp + self.pos = _save5 + _tmp = apply(:_NestedList) + break if _tmp + self.pos = _save5 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + items = @result + unless _tmp + self.pos = _save + break + end + _save6 = self.pos + _tmp = begin; s == @list_stack.pop ; end + self.pos = _save6 + unless _tmp + self.pos = _save + break + end + @result = begin; olist(self, items.unshift(item)); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_NestedOlist unless _tmp + return _tmp + end + + # NestedList = (NestedUlist | NestedOlist) + def _NestedList + + _save = self.pos + while true # choice + _tmp = apply(:_NestedUlist) + break if _tmp + self.pos = _save + _tmp = apply(:_NestedOlist) + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_NestedList unless _tmp return _tmp end @@ -4001,7 +4593,7 @@ def _Spacechar Rules = {} Rules[:_root] = rule_info("root", "Start") - Rules[:_Start] = rule_info("Start", "&. Document:c { @strategy.ast = c }") + Rules[:_Start] = rule_info("Start", "&. { @list_stack = Array.new } Document:c { @strategy.ast = c }") Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine*:c { c } (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") @@ -4023,11 +4615,21 @@ def _Spacechar Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") Rules[:_CodeBlockElementContents] = rule_info("CodeBlockElementContents", "CodeBlockElementContent+:c { c }") Rules[:_CodeBlockElementContent] = rule_info("CodeBlockElementContent", "(SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, \"\\n\") } | !\"//}\" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, \"\\n\")] })") - Rules[:_Ulist] = rule_info("Ulist", "&. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ {ulist(self, @ulist_elem)}") - Rules[:_UlistElement] = rule_info("UlistElement", "\" \"+ \"*\"+:level \" \"* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) }") - Rules[:_UlistContLine] = rule_info("UlistContLine", "\" \" \" \"+ !\"*\" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) }") - Rules[:_Olist] = rule_info("Olist", "{ @olist_elem = [] } (OlistElement | SinglelineComment)+:c {olist(self, @olist_elem)}") - Rules[:_OlistElement] = rule_info("OlistElement", "\" \"+ < /\\d/+ > { num=text } \".\" Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) }") + Rules[:_Indent] = rule_info("Indent", "\" \"") + Rules[:_Bullet] = rule_info("Bullet", "\"*\"") + Rules[:_Enumerator] = rule_info("Enumerator", "< /[0-9]+/ > { num = text } \".\" { num.to_i }") + Rules[:_Ulist] = rule_info("Ulist", "Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item { if b.size > 1 then item.level = b.size end } (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))}") + Rules[:_Olist] = rule_info("Olist", "Indent+:s Enumerator:e Space+ { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") + Rules[:_UlistItemBlock] = rule_info("UlistItemBlock", "ListItemFirstLine:c ListItemLine*:d {ulist_element(self, @list_stack.size, d.unshift(c))}") + Rules[:_OlistItemBlock] = rule_info("OlistItemBlock", "ListItemFirstLine:c ListItemLine*:d {olist_element(self, 0, d.unshift(c))}") + Rules[:_ListItemFirstLine] = rule_info("ListItemFirstLine", "SinglelineContent:c Newline { c }") + Rules[:_ListItemLine] = rule_info("ListItemLine", "Indent+:s !Bullet !Enumerator !Space SinglelineContent:c &{ check_indent(s) } Newline { c }") + Rules[:_UlistItemMore] = rule_info("UlistItemMore", "Indent+:s Bullet Bullet+:b Space+ &{ check_indent(s) } UlistItemBlock:item { item.level = b.size+1; item }") + Rules[:_UlistItem] = rule_info("UlistItem", "Indent+:s Bullet Space+ &{ check_indent(s) } UlistItemBlock:item { item }") + Rules[:_OlistItem] = rule_info("OlistItem", "Indent+:s Enumerator:e Space+ &{ check_indent(s) } OlistItemBlock:item { item.num = e; item }") + Rules[:_NestedUlist] = rule_info("NestedUlist", "Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))}") + Rules[:_NestedOlist] = rule_info("NestedOlist", "Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") + Rules[:_NestedList] = rule_info("NestedList", "(NestedUlist | NestedOlist)") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") diff --git a/lib/review/node.rb b/lib/review/node.rb index 27d93df5c..5f2738e20 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -194,6 +194,10 @@ def to_doc end class UlistElementNode < Node + def level=(level) + @level = level + end + def to_doc @content.map(&:to_doc).join("") end @@ -210,6 +214,10 @@ def to_doc end class OlistElementNode < Node + def num=(num) + @num = num + end + def to_doc @content.map(&:to_doc).join("") end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index e97918f27..f1deb49bd 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -458,6 +458,14 @@ require 'review/node' @strategy.error msg end + def check_indent(s) + s.size >= @list_stack.last.size + end + + def check_nested_indent(s) + s.size >= @list_stack.last.size + 2 + end + } @@ -488,7 +496,7 @@ require 'review/node' root = Start -Start = &. Document:c { @strategy.ast = c } +Start = &. { @list_stack = Array.new } Document:c { @strategy.ast = c } ## a Document is a set of Blocks Document = Block*:c ~document(self, c) @@ -575,20 +583,43 @@ CodeBlockElementContent = ( SinglelineComment:c { c } | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] } ) -## * 箇条書き -## 行頭から… の指定がない +Indent = " " +Bullet = "*" +Enumerator = < /[0-9]+/ > { num = text } "." { num.to_i } + + +Ulist = Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item + { if b.size > 1 then item.level = b.size end } + (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } + ~ulist(self, items.unshift(item)) + +Olist = Indent+:s Enumerator:e Space+ { @list_stack.push(s) } OlistItemBlock:item + { item.num = e } + (OlistItem | NestedList)*:items &{ s == @list_stack.pop } + ~olist(self, items.unshift(item)) + +UlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~ulist_element(self, @list_stack.size, d.unshift(c)) +OlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~olist_element(self, 0, d.unshift(c)) + +ListItemFirstLine = SinglelineContent:c Newline { c } +ListItemLine = Indent+:s !Bullet !Enumerator !Space SinglelineContent:c &{ check_indent(s) } Newline { c } + +UlistItemMore = Indent+:s Bullet Bullet+:b Space+ &{ check_indent(s) } UlistItemBlock:item { item.level = b.size+1; item } -Ulist = &. { @ulist_elem=[] } UlistElement (UlistElement | UlistContLine | SinglelineComment)+ ~ulist(self, @ulist_elem) +UlistItem = Indent+:s Bullet Space+ &{ check_indent(s) } UlistItemBlock:item { item } +OlistItem = Indent+:s Enumerator:e Space+ &{ check_indent(s) } OlistItemBlock:item { item.num = e; item } -UlistElement = " "+ "*"+:level " "* SinglelineContent:c EOL { @ulist_elem << ::ReVIEW::UlistElementNode.new(self, level.size, [c]) } -UlistContLine = " " " "+ !"*" SinglelineContent:c EOL { @ulist_elem[-1].concat(c) } +NestedUlist = Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item + (UlistItem | NestedList)*:items &{ s == @list_stack.pop } + ~ulist(self, items.unshift(item)) +NestedOlist = Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item + { item.num = e } + (OlistItem | NestedList)*:items &{ s == @list_stack.pop } + ~olist(self, items.unshift(item)) -## 1. 番号付き箇条書き -## 行頭から… の指定がない +NestedList = (NestedUlist | NestedOlist) -Olist = { @olist_elem = [] } (OlistElement | SinglelineComment)+:c ~olist(self, @olist_elem) -OlistElement = " "+ < /\d/+ > { num=text } "." Space* SinglelineContent:c EOL {@olist_elem << ReVIEW::OlistElementNode.new(self, num.to_i, [c]) } ## : 用語リスト ## 行頭から… の指定がない From 3831f04c583dc654fbc60ff31e4287bba1323f7b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 03:41:29 +0900 Subject: [PATCH 148/214] add comment --- lib/review/compiler.rb | 38 +++++++++++++++++++------------------- lib/review/review.kpeg | 10 +++++++--- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 28e0516d8..c6f040d37 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -3320,6 +3320,24 @@ def _OlistItem return _tmp end + # NestedList = (NestedUlist | NestedOlist) + def _NestedList + + _save = self.pos + while true # choice + _tmp = apply(:_NestedUlist) + break if _tmp + self.pos = _save + _tmp = apply(:_NestedOlist) + break if _tmp + self.pos = _save + break + end # end choice + + set_failed_rule :_NestedList unless _tmp + return _tmp + end + # NestedUlist = Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))} def _NestedUlist @@ -3541,24 +3559,6 @@ def _NestedOlist return _tmp end - # NestedList = (NestedUlist | NestedOlist) - def _NestedList - - _save = self.pos - while true # choice - _tmp = apply(:_NestedUlist) - break if _tmp - self.pos = _save - _tmp = apply(:_NestedOlist) - break if _tmp - self.pos = _save - break - end # end choice - - set_failed_rule :_NestedList unless _tmp - return _tmp - end - # Dlist = (DlistElement | SinglelineComment)+:content {dlist(self, content)} def _Dlist @@ -4627,9 +4627,9 @@ def _Spacechar Rules[:_UlistItemMore] = rule_info("UlistItemMore", "Indent+:s Bullet Bullet+:b Space+ &{ check_indent(s) } UlistItemBlock:item { item.level = b.size+1; item }") Rules[:_UlistItem] = rule_info("UlistItem", "Indent+:s Bullet Space+ &{ check_indent(s) } UlistItemBlock:item { item }") Rules[:_OlistItem] = rule_info("OlistItem", "Indent+:s Enumerator:e Space+ &{ check_indent(s) } OlistItemBlock:item { item.num = e; item }") + Rules[:_NestedList] = rule_info("NestedList", "(NestedUlist | NestedOlist)") Rules[:_NestedUlist] = rule_info("NestedUlist", "Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))}") Rules[:_NestedOlist] = rule_info("NestedOlist", "Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") - Rules[:_NestedList] = rule_info("NestedList", "(NestedUlist | NestedOlist)") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index f1deb49bd..af0971a69 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -583,11 +583,12 @@ CodeBlockElementContent = ( SinglelineComment:c { c } | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] } ) +## Ulist and Olist + Indent = " " Bullet = "*" Enumerator = < /[0-9]+/ > { num = text } "." { num.to_i } - Ulist = Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item { if b.size > 1 then item.level = b.size end } (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } @@ -609,6 +610,11 @@ UlistItemMore = Indent+:s Bullet Bullet+:b Space+ &{ check_indent(s) } UlistItem UlistItem = Indent+:s Bullet Space+ &{ check_indent(s) } UlistItemBlock:item { item } OlistItem = Indent+:s Enumerator:e Space+ &{ check_indent(s) } OlistItemBlock:item { item.num = e; item } +## NestedList is markdown-like indented syntax. +## You can write nested Ulist and Olist with this syntax. + +NestedList = (NestedUlist | NestedOlist) + NestedUlist = Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } ~ulist(self, items.unshift(item)) @@ -617,8 +623,6 @@ NestedOlist = Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_ (OlistItem | NestedList)*:items &{ s == @list_stack.pop } ~olist(self, items.unshift(item)) -NestedList = (NestedUlist | NestedOlist) - ## : 用語リスト From 4ad4e03f44ea884c633233bdeccdeaf7ad2848b6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 11:02:07 +0900 Subject: [PATCH 149/214] fix dlist; use symbols instead of literals --- lib/review/compiler.rb | 41 ++++++++++++++++++++++++++--------------- lib/review/review.kpeg | 9 +++------ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index c6f040d37..acea840f6 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -3618,13 +3618,13 @@ def _Dlist return _tmp end - # DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)} + # DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)} def _DlistElement _save = self.pos while true # sequence while true - _tmp = match_string(" ") + _tmp = apply(:_Indent) break unless _tmp end _tmp = true @@ -3637,16 +3637,17 @@ def _DlistElement self.pos = _save break end - _tmp = match_string(" ") - unless _tmp - self.pos = _save - break - end - while true - _tmp = apply(:_Space) - break unless _tmp + _save2 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save2 end - _tmp = true unless _tmp self.pos = _save break @@ -3694,12 +3695,22 @@ def _DlistElement return _tmp end - # DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } + # DlistElementContent = Space+ SinglelineContent:c Newline:n { c } def _DlistElementContent _save = self.pos while true # sequence - _tmp = scan(/\A(?-mix:[ \t]+)/) + _save1 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end unless _tmp self.pos = _save break @@ -4631,8 +4642,8 @@ def _Spacechar Rules[:_NestedUlist] = rule_info("NestedUlist", "Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))}") Rules[:_NestedOlist] = rule_info("NestedOlist", "Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") - Rules[:_DlistElement] = rule_info("DlistElement", "\" \"* \":\" \" \" Space* SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") - Rules[:_DlistElementContent] = rule_info("DlistElementContent", "/[ \\t]+/ SinglelineContent:c Newline:n { c }") + Rules[:_DlistElement] = rule_info("DlistElement", "Indent* \":\" Space+ SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") + Rules[:_DlistElementContent] = rule_info("DlistElementContent", "Space+ SinglelineContent:c Newline:n { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewline > {text(self, text)}") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index af0971a69..4f4afdad7 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -623,15 +623,12 @@ NestedOlist = Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_ (OlistItem | NestedList)*:items &{ s == @list_stack.pop } ~olist(self, items.unshift(item)) - - -## : 用語リスト - ## 行頭から… の指定がない +# Dlist Dlist = (DlistElement | SinglelineComment)+:content ~dlist(self, content) -DlistElement = " "* ":" " " Space* SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, text, content) +DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, text, content) -DlistElementContent = /[ \t]+/ SinglelineContent:c Newline:n { c } +DlistElementContent = Space+ SinglelineContent:c Newline:n { c } SinglelineContent = Inline+:c ~singleline_content(self,c) From ad658e416b43c17418eb499a5f37b3ef1662718d Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 11:07:14 +0900 Subject: [PATCH 150/214] remove split_paragraph --- test/test_textutils.rb | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/test/test_textutils.rb b/test/test_textutils.rb index b6fef2ab9..c34f1a17e 100644 --- a/test/test_textutils.rb +++ b/test/test_textutils.rb @@ -29,43 +29,4 @@ def test_detab_with_arg detabed = detab("\tabc\tdef", 4) assert_equal " abc def", detabed end - -=begin - def test_split_paragraph_empty_nil - ret = @tu_nil.split_paragraph([]) - assert_equal ret, [""] - end - - def test_split_paragraph_empty_p - ret = @tu_p.split_paragraph([]) - assert_equal ret, ["

      "] - end - - def test_split_paragraph_p - ret = @tu_p.split_paragraph(["abc"]) - assert_equal ["

      abc

      "], ret - ret = @tu_p.split_paragraph(["abc","def"]) - assert_equal ["

      abcdef

      "], ret - ret = @tu_p.split_paragraph(["abc","","def"]) - assert_equal ["

      abc

      ","

      def

      "], ret - ret = @tu_p.split_paragraph(["abc","","","def"]) - assert_equal ["

      abc

      ","

      def

      "], ret - ret = @tu_p.split_paragraph(["abc","","","def","ghi"]) - assert_equal ["

      abc

      ","

      defghi

      "], ret - end - - def test_split_paragraph_nil - ret = @tu_nil.split_paragraph(["abc"]) - assert_equal ["abc"], ret - ret = @tu_nil.split_paragraph(["abc","def"]) - assert_equal ["abcdef"], ret - ret = @tu_nil.split_paragraph(["abc","","def"]) - assert_equal ["abc","def"], ret - ret = @tu_nil.split_paragraph(["abc","","","def"]) - assert_equal ["abc","def"], ret - ret = @tu_nil.split_paragraph(["abc","","","def","ghi"]) - assert_equal ["abc","defghi"], ret - end -=end - end From d028d0f5047d29b8e5bec4c211e49363f90dab71 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 11:08:09 +0900 Subject: [PATCH 151/214] remove comments --- test/test_book_chapter.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_book_chapter.rb b/test/test_book_chapter.rb index b9d84534a..cb80967d8 100644 --- a/test/test_book_chapter.rb +++ b/test/test_book_chapter.rb @@ -16,12 +16,6 @@ def setup @sjis_str = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8" @jis_str = "\x1b\x24\x42\x24\x22\x24\x24\x24\x26\x24\x28\x24\x2a\x1b\x28\x42" end -=begin - @utf8_str = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a" # "あいうえお" - @eucjp_str = "\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa" - @sjis_str = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8" - @jis_str = "\x1b\x24\x42\x24\x22\x24\x24\x24\x26\x24\x28\x24\x2a\x1b\x28\x42" -=end end def test_initialize From 719c0b614237183dfeaffc1e3445df3b8f0fa217 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 11:09:40 +0900 Subject: [PATCH 152/214] remove tests for obsoleted methods print/puts --- test/test_builder.rb | 53 -------------------------------------------- 1 file changed, 53 deletions(-) diff --git a/test/test_builder.rb b/test/test_builder.rb index 728731227..436789817 100644 --- a/test/test_builder.rb +++ b/test/test_builder.rb @@ -44,59 +44,6 @@ def test_result assert_equal '', b.result end -=begin - def test_print_and_puts - b = Builder.new - assert_raises(NoMethodError) do # XXX: OK? - b.print "" - end - assert_raises(NoMethodError) do # XXX: OK? - b.puts "" - end - - if "".respond_to?(:encode) - utf8_str = "あいうえお" - eucjp_str = "あいうえお".encode("EUC-JP") - sjis_str = "あいうえお".encode("Shift_JIS") - jis_str = "あいうえお".encode("ISO-2022-JP") - else - utf8_str = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a" # "あいうえお" - eucjp_str = "\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa" - sjis_str = "\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8" - jis_str = "\x1b\x24\x42\x24\x22\x24\x24\x24\x26\x24\x28\x24\x2a\x1b\x28\x42" - end - - [ - ['EUC', eucjp_str], - ['SJIS', sjis_str], -# ['JIS', jis_str], -# ['jis', jis_str], -# ['jIs', jis_str], - ['XYZ', utf8_str], - ].each do |enc, expect| - params = {"outencoding" => enc} - - [ - [:print, utf8_str, expect], - [:puts, utf8_str, "#{expect}\n"], - [:print, "#{utf8_str}\n", "#{expect}\n"], - [:puts, "#{utf8_str}\n", "#{expect}\n"], - ].each do |m, instr, expstr| - b = Builder.new - chapter = ReVIEW::Book::Chapter.new(ReVIEW::Book::Base.load, nil, '-', nil) - b.bind(nil, chapter, nil) - chapter.book.config = params - b.__send__(m, instr) - if "".respond_to?(:encode) - assert_equal expstr.encode("UTF-8"), b.result - else - assert_equal expstr, b.result - end - end - end - end -=end - def test_not_implemented_methods ex = NoMethodError # XXX: OK? [ From 5f381c0980a0ac5499823556295850c523a3dad9 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 11:30:40 +0900 Subject: [PATCH 153/214] fix handling newline --- lib/review/compiler.rb | 85 ++++++++++++++---------------------------- lib/review/review.kpeg | 14 +++---- 2 files changed, 33 insertions(+), 66 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index acea840f6..6851381cd 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1140,11 +1140,21 @@ def _Start return _tmp end - # Document = Block*:c {document(self, c)} + # Document = BOM? Block*:c {document(self, c)} def _Document _save = self.pos while true # sequence + _save1 = self.pos + _tmp = apply(:_BOM) + unless _tmp + _tmp = true + self.pos = _save1 + end + unless _tmp + self.pos = _save + break + end _ary = [] while true _tmp = apply(:_Block) @@ -2423,7 +2433,7 @@ def _BlockElementParagraph return _tmp end - # BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } + # BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } def _BlockElementParagraphLine _save = self.pos @@ -2491,12 +2501,11 @@ def _BlockElementParagraphLine break end _tmp = apply(:_Newline) - n = @result unless _tmp self.pos = _save break end - @result = begin; [c, n] ; end + @result = begin; c ; end _tmp = true unless _tmp self.pos = _save @@ -2545,7 +2554,7 @@ def _CodeBlockElementContents return _tmp end - # CodeBlockElementContent = (SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] }) + # CodeBlockElementContent = (SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } | !"//}" SinglelineContent:c Newline { [c, ::ReVIEW::TextNode.new(self, "\n")] }) def _CodeBlockElementContent _save = self.pos @@ -2606,7 +2615,6 @@ def _CodeBlockElementContent break end _tmp = apply(:_Newline) - n = @result unless _tmp self.pos = _save3 break @@ -2628,13 +2636,6 @@ def _CodeBlockElementContent return _tmp end - # Indent = " " - def _Indent - _tmp = match_string(" ") - set_failed_rule :_Indent unless _tmp - return _tmp - end - # Bullet = "*" def _Bullet _tmp = match_string("*") @@ -3695,7 +3696,7 @@ def _DlistElement return _tmp end - # DlistElementContent = Space+ SinglelineContent:c Newline:n { c } + # DlistElementContent = Space+ SinglelineContent:c Newline { c } def _DlistElementContent _save = self.pos @@ -3722,7 +3723,6 @@ def _DlistElementContent break end _tmp = apply(:_Newline) - n = @result unless _tmp self.pos = _save break @@ -4422,43 +4422,6 @@ def _NonNewline return _tmp end - # Digits = Digit+:c { c } - def _Digits - - _save = self.pos - while true # sequence - _save1 = self.pos - _ary = [] - _tmp = apply(:_Digit) - if _tmp - _ary << @result - while true - _tmp = apply(:_Digit) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 - end - c = @result - unless _tmp - self.pos = _save - break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save - end - break - end # end sequence - - set_failed_rule :_Digits unless _tmp - return _tmp - end - # Space = /[ \t]/ def _Space _tmp = scan(/\A(?-mix:[ \t])/) @@ -4466,6 +4429,13 @@ def _Space return _tmp end + # Indent = " " + def _Indent + _tmp = match_string(" ") + set_failed_rule :_Indent unless _tmp + return _tmp + end + # EOL = (Newline | EOF) def _EOL @@ -4605,7 +4575,7 @@ def _Spacechar Rules = {} Rules[:_root] = rule_info("root", "Start") Rules[:_Start] = rule_info("Start", "&. { @list_stack = Array.new } Document:c { @strategy.ast = c }") - Rules[:_Document] = rule_info("Document", "Block*:c {document(self, c)}") + Rules[:_Document] = rule_info("Document", "BOM? Block*:c {document(self, c)}") Rules[:_Block] = rule_info("Block", "BlankLine*:c { c } (SinglelineComment:c | Headline:c | BlockElement:c | Ulist:c | Olist:c | Dlist:c | Paragraph:c) { c }") Rules[:_BlankLine] = rule_info("BlankLine", "Newline") Rules[:_SinglelineComment] = rule_info("SinglelineComment", "\"\#@\" < NonNewline+ > EOL {singleline_comment(self, text)}") @@ -4623,10 +4593,9 @@ def _Spacechar Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Dlist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphLine+:c {paragraph(self, c.flatten)}") - Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] }") + Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") Rules[:_CodeBlockElementContents] = rule_info("CodeBlockElementContents", "CodeBlockElementContent+:c { c }") - Rules[:_CodeBlockElementContent] = rule_info("CodeBlockElementContent", "(SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, \"\\n\") } | !\"//}\" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, \"\\n\")] })") - Rules[:_Indent] = rule_info("Indent", "\" \"") + Rules[:_CodeBlockElementContent] = rule_info("CodeBlockElementContent", "(SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, \"\\n\") } | !\"//}\" SinglelineContent:c Newline { [c, ::ReVIEW::TextNode.new(self, \"\\n\")] })") Rules[:_Bullet] = rule_info("Bullet", "\"*\"") Rules[:_Enumerator] = rule_info("Enumerator", "< /[0-9]+/ > { num = text } \".\" { num.to_i }") Rules[:_Ulist] = rule_info("Ulist", "Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item { if b.size > 1 then item.level = b.size end } (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } {ulist(self, items.unshift(item))}") @@ -4643,7 +4612,7 @@ def _Spacechar Rules[:_NestedOlist] = rule_info("NestedOlist", "Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "Indent* \":\" Space+ SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") - Rules[:_DlistElementContent] = rule_info("DlistElementContent", "Space+ SinglelineContent:c Newline:n { c }") + Rules[:_DlistElementContent] = rule_info("DlistElementContent", "Space+ SinglelineContent:c Newline { c }") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewline > {text(self, text)}") @@ -4657,8 +4626,8 @@ def _Spacechar Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") Rules[:_NonNewline] = rule_info("NonNewline", "/[^\\r\\n]/") - Rules[:_Digits] = rule_info("Digits", "Digit+:c { c }") Rules[:_Space] = rule_info("Space", "/[ \\t]/") + Rules[:_Indent] = rule_info("Indent", "\" \"") Rules[:_EOL] = rule_info("EOL", "(Newline | EOF)") Rules[:_EOF] = rule_info("EOF", "!.") Rules[:_ElementName] = rule_info("ElementName", "< LowerAlphabetAscii+ > { text }") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 4f4afdad7..0ee6560be 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -499,7 +499,7 @@ root = Start Start = &. { @list_stack = Array.new } Document:c { @strategy.ast = c } ## a Document is a set of Blocks -Document = Block*:c ~document(self, c) +Document = BOM? Block*:c ~document(self, c) ## ignore leading blank lines Block = BlankLine*:c { c } @@ -574,18 +574,17 @@ BlockElementContent = ( SinglelineComment:c { c } ## it's like Paragraph, but it's in a block, so do not allow '//}\n' BlockElementParagraph = BlockElementParagraphLine+:c ~paragraph(self, c.flatten) -BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline:n { [c, n] } +BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } ## In CodeBlockElementContents, newline should no be ingored. So we use TextNode instead of NewLineNode. CodeBlockElementContents = CodeBlockElementContent+:c { c } CodeBlockElementContent = ( SinglelineComment:c { c } | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } - | !"//}" SinglelineContent:c Newline:n { [c, ::ReVIEW::TextNode.new(self, "\n")] } + | !"//}" SinglelineContent:c Newline { [c, ::ReVIEW::TextNode.new(self, "\n")] } ) ## Ulist and Olist -Indent = " " Bullet = "*" Enumerator = < /[0-9]+/ > { num = text } "." { num.to_i } @@ -628,7 +627,7 @@ Dlist = (DlistElement | SinglelineComment)+:content ~dlist(self, content) DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, text, content) -DlistElementContent = Space+ SinglelineContent:c Newline:n { c } +DlistElementContent = Space+ SinglelineContent:c Newline { c } SinglelineContent = Inline+:c ~singleline_content(self,c) @@ -674,11 +673,10 @@ InlineElementContentText = ( "\\}" ~text(self, "}") NonNewline = /[^\r\n]/ -Digits = Digit+:c { c } - -## Space = /[  \t]/ Space = /[ \t]/ +Indent = " " + EOL = (Newline|EOF) EOF = !. From 5e3684c0784466c391628d5fe71e71bfa44d5fce Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 15 Feb 2015 13:42:05 +0900 Subject: [PATCH 154/214] add QuotedInlineText --- lib/review/compiler.rb | 249 ++++++++++++++++++++++++++++++++++++++--- lib/review/review.kpeg | 13 ++- 2 files changed, 245 insertions(+), 17 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 6851381cd..a190079ff 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -4096,7 +4096,7 @@ def _InlineElementContents return _tmp end - # InlineElementContentsSub = !"}" (InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] }) + # InlineElementContentsSub = !"}" (Space* InlineElementContent:c1 Space* "," InlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] }) def _InlineElementContentsSub _save = self.pos @@ -4115,12 +4115,6 @@ def _InlineElementContentsSub _save3 = self.pos while true # sequence - _tmp = apply(:_InlineElementContent) - c1 = @result - unless _tmp - self.pos = _save3 - break - end while true _tmp = apply(:_Space) break unless _tmp @@ -4130,7 +4124,8 @@ def _InlineElementContentsSub self.pos = _save3 break end - _tmp = match_string(",") + _tmp = apply(:_InlineElementContent) + c1 = @result unless _tmp self.pos = _save3 break @@ -4144,6 +4139,11 @@ def _InlineElementContentsSub self.pos = _save3 break end + _tmp = match_string(",") + unless _tmp + self.pos = _save3 + break + end _tmp = apply(:_InlineElementContentsSub) c2 = @result unless _tmp @@ -4163,12 +4163,30 @@ def _InlineElementContentsSub _save6 = self.pos while true # sequence + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save6 + break + end _tmp = apply(:_InlineElementContent) c1 = @result unless _tmp self.pos = _save6 break end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save6 + break + end @result = begin; [c1] ; end _tmp = true unless _tmp @@ -4229,7 +4247,7 @@ def _InlineElementContent return _tmp end - # InlineElementContentSub = (InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)}) + # InlineElementContentSub = (InlineElement:c { c } | !InlineElement QuotedInlineText:content {inline_element_content(self, content)} | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)}) def _InlineElementContentSub _save = self.pos @@ -4264,7 +4282,34 @@ def _InlineElementContentSub self.pos = _save2 break end - _save4 = self.pos + _tmp = apply(:_QuotedInlineText) + content = @result + unless _tmp + self.pos = _save2 + break + end + @result = begin; inline_element_content(self, content); end + _tmp = true + unless _tmp + self.pos = _save2 + end + break + end # end sequence + + break if _tmp + self.pos = _save + + _save4 = self.pos + while true # sequence + _save5 = self.pos + _tmp = apply(:_InlineElement) + _tmp = _tmp ? nil : true + self.pos = _save5 + unless _tmp + self.pos = _save4 + break + end + _save6 = self.pos _ary = [] _tmp = apply(:_InlineElementContentText) if _tmp @@ -4277,17 +4322,17 @@ def _InlineElementContentSub _tmp = true @result = _ary else - self.pos = _save4 + self.pos = _save6 end content = @result unless _tmp - self.pos = _save2 + self.pos = _save4 break end @result = begin; inline_element_content(self, content); end _tmp = true unless _tmp - self.pos = _save2 + self.pos = _save4 end break end # end sequence @@ -4301,6 +4346,179 @@ def _InlineElementContentSub return _tmp end + # QuotedInlineText = "\"" ("\\\"" { "\"" } | "\\\\" { "\\" } | < /[^"\r\n\\]/ > { text })+:str "\"" {text(self, str.join(""))} + def _QuotedInlineText + + _save = self.pos + while true # sequence + _tmp = match_string("\"") + unless _tmp + self.pos = _save + break + end + _save1 = self.pos + _ary = [] + + _save2 = self.pos + while true # choice + + _save3 = self.pos + while true # sequence + _tmp = match_string("\\\"") + unless _tmp + self.pos = _save3 + break + end + @result = begin; "\"" ; end + _tmp = true + unless _tmp + self.pos = _save3 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save4 = self.pos + while true # sequence + _tmp = match_string("\\\\") + unless _tmp + self.pos = _save4 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save4 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + + _save5 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^"\r\n\\])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save5 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + + break if _tmp + self.pos = _save2 + break + end # end choice + + if _tmp + _ary << @result + while true + + _save6 = self.pos + while true # choice + + _save7 = self.pos + while true # sequence + _tmp = match_string("\\\"") + unless _tmp + self.pos = _save7 + break + end + @result = begin; "\"" ; end + _tmp = true + unless _tmp + self.pos = _save7 + end + break + end # end sequence + + break if _tmp + self.pos = _save6 + + _save8 = self.pos + while true # sequence + _tmp = match_string("\\\\") + unless _tmp + self.pos = _save8 + break + end + @result = begin; "\\" ; end + _tmp = true + unless _tmp + self.pos = _save8 + end + break + end # end sequence + + break if _tmp + self.pos = _save6 + + _save9 = self.pos + while true # sequence + _text_start = self.pos + _tmp = scan(/\A(?-mix:[^"\r\n\\])/) + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save9 + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save9 + end + break + end # end sequence + + break if _tmp + self.pos = _save6 + break + end # end choice + + _ary << @result if _tmp + break unless _tmp + end + _tmp = true + @result = _ary + else + self.pos = _save1 + end + str = @result + unless _tmp + self.pos = _save + break + end + _tmp = match_string("\"") + unless _tmp + self.pos = _save + break + end + @result = begin; text(self, str.join("")); end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_QuotedInlineText unless _tmp + return _tmp + end + # InlineElementContentText = ("\\}" {text(self, "}")} | "\\," {text(self, ",")} | "\\\\" {text(self, "\\" )} | "\\" {text(self, "\\" )} | !InlineElement < /[^\r\n\\},]/ > {text(self,text)}) def _InlineElementContentText @@ -4621,9 +4839,10 @@ def _Spacechar Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ > { text }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") - Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (InlineElementContent:c1 Space* \",\" Space* InlineElementContentsSub:c2 { [c1]+c2 } | InlineElementContent:c1 { [c1] })") + Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (Space* InlineElementContent:c1 Space* \",\" InlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] })") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") - Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") + Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement QuotedInlineText:content {inline_element_content(self, content)} | !InlineElement InlineElementContentText+:content {inline_element_content(self, content)})") + Rules[:_QuotedInlineText] = rule_info("QuotedInlineText", "\"\\\"\" (\"\\\\\\\"\" { \"\\\"\" } | \"\\\\\\\\\" { \"\\\\\" } | < /[^\"\\r\\n\\\\]/ > { text })+:str \"\\\"\" {text(self, str.join(\"\"))}") Rules[:_InlineElementContentText] = rule_info("InlineElementContentText", "(\"\\\\}\" {text(self, \"}\")} | \"\\\\,\" {text(self, \",\")} | \"\\\\\\\\\" {text(self, \"\\\\\" )} | \"\\\\\" {text(self, \"\\\\\" )} | !InlineElement < /[^\\r\\n\\\\},]/ > {text(self,text)})") Rules[:_NonNewline] = rule_info("NonNewline", "/[^\\r\\n]/") Rules[:_Space] = rule_info("Space", "/[ \\t]/") diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 0ee6560be..8419c7eb2 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -652,17 +652,26 @@ InlineElementSymbol = < AlphanumericAscii+ > { text } InlineElementContents = !"}" InlineElementContentsSub:c { c } -InlineElementContentsSub = !"}" ( InlineElementContent:c1 Space* "," Space* InlineElementContentsSub:c2 { [c1]+c2 } - | InlineElementContent:c1 { [c1] } +InlineElementContentsSub = !"}" ( Space* InlineElementContent:c1 Space* "," InlineElementContentsSub:c2 { [c1]+c2 } + | Space* InlineElementContent:c1 Space* { [c1] } ) InlineElementContent = InlineElementContentSub+:d { d } InlineElementContentSub = ( InlineElement:c { c } + | !InlineElement QuotedInlineText:content ~inline_element_content(self, content) | !InlineElement InlineElementContentText+:content ~inline_element_content(self, content) ) +## Quoted Inline Text is "..." +## In Quoted Inline Text, "\\" is "\", "\'" is "'", other characters are kept as is. +QuotedInlineText = "\"" + ( "\\\"" { "\"" } + | "\\\\" { "\\" } + | < /[^"\r\n\\]/ > { text } + )+:str "\"" ~text(self, str.join("")) + ## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? InlineElementContentText = ( "\\}" ~text(self, "}") | "\\," ~text(self, ",") From ec92a7424dbf05c05325a1e88c0a28f32d9d77fb Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 3 May 2015 14:54:21 +0900 Subject: [PATCH 155/214] remove kpeg from Gemfile; added already in review.gemspec --- Gemfile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Gemfile b/Gemfile index 95b8b1a1b..20fec56ca 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,3 @@ source 'https://rubygems.org' gemspec gem "json" - -group :development do - gem "kpeg" -end \ No newline at end of file From eef76af460ef8243da6f28bc1b9f4129905d6388 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 3 May 2015 15:22:56 +0900 Subject: [PATCH 156/214] suport SinglelineComment in DListElement --- lib/review/review.kpeg | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 8419c7eb2..11467e0b2 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -627,7 +627,9 @@ Dlist = (DlistElement | SinglelineComment)+:content ~dlist(self, content) DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, text, content) -DlistElementContent = Space+ SinglelineContent:c Newline { c } +DlistElementContent = (SinglelineComment:c { c } + |Space+ SinglelineContent:c Newline { c } + ) SinglelineContent = Inline+:c ~singleline_content(self,c) From 8b1617860f1dd457978c2dfbda1f298ddf7ed1e3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 3 May 2015 15:25:08 +0900 Subject: [PATCH 157/214] fix build error --- lib/review/compiler.rb | 87 +++++++++++++++++++---------- lib/review/compiler/literals_1_8.rb | 6 +- lib/review/htmlbuilder.rb | 1 + 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index a190079ff..2c2f65f73 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -3696,44 +3696,71 @@ def _DlistElement return _tmp end - # DlistElementContent = Space+ SinglelineContent:c Newline { c } + # DlistElementContent = (SinglelineComment:c { c } | Space+ SinglelineContent:c Newline { c }) def _DlistElementContent _save = self.pos - while true # sequence + while true # choice + _save1 = self.pos - _tmp = apply(:_Space) - if _tmp - while true - _tmp = apply(:_Space) - break unless _tmp + while true # sequence + _tmp = apply(:_SinglelineComment) + c = @result + unless _tmp + self.pos = _save1 + break end + @result = begin; c ; end _tmp = true - else - self.pos = _save1 - end - unless _tmp - self.pos = _save - break - end - _tmp = apply(:_SinglelineContent) - c = @result - unless _tmp - self.pos = _save + unless _tmp + self.pos = _save1 + end break - end - _tmp = apply(:_Newline) - unless _tmp - self.pos = _save + end # end sequence + + break if _tmp + self.pos = _save + + _save2 = self.pos + while true # sequence + _save3 = self.pos + _tmp = apply(:_Space) + if _tmp + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + else + self.pos = _save3 + end + unless _tmp + self.pos = _save2 + break + end + _tmp = apply(:_SinglelineContent) + c = @result + unless _tmp + self.pos = _save2 + break + end + _tmp = apply(:_Newline) + unless _tmp + self.pos = _save2 + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save2 + end break - end - @result = begin; c ; end - _tmp = true - unless _tmp - self.pos = _save - end + end # end sequence + + break if _tmp + self.pos = _save break - end # end sequence + end # end choice set_failed_rule :_DlistElementContent unless _tmp return _tmp @@ -4830,7 +4857,7 @@ def _Spacechar Rules[:_NestedOlist] = rule_info("NestedOlist", "Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } {olist(self, items.unshift(item))}") Rules[:_Dlist] = rule_info("Dlist", "(DlistElement | SinglelineComment)+:content {dlist(self, content)}") Rules[:_DlistElement] = rule_info("DlistElement", "Indent* \":\" Space+ SinglelineContent:text Newline DlistElementContent+:content {dlist_element(self, text, content)}") - Rules[:_DlistElementContent] = rule_info("DlistElementContent", "Space+ SinglelineContent:c Newline { c }") + Rules[:_DlistElementContent] = rule_info("DlistElementContent", "(SinglelineComment:c { c } | Space+ SinglelineContent:c Newline { c })") Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self,c)}") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewline > {text(self, text)}") diff --git a/lib/review/compiler/literals_1_8.rb b/lib/review/compiler/literals_1_8.rb index 22654fc32..ab2e3fe49 100644 --- a/lib/review/compiler/literals_1_8.rb +++ b/lib/review/compiler/literals_1_8.rb @@ -365,7 +365,7 @@ def setup_foreign_grammar; end # Alphanumeric = /[0-9A-Za-z\200-\377]/n def _Alphanumeric - _tmp = scan(/\A(?-mix:[0-9A-Za-z\200-\377])/n) + _tmp = scan(/\A(?-mix:[0-9A-Za-z\200-\377])/) set_failed_rule :_Alphanumeric unless _tmp return _tmp end @@ -393,7 +393,7 @@ def _Digit # BOM = "" def _BOM - _tmp = match_string("\303\257\302\273\302\277") + _tmp = match_string("\u{ef}\u{bb}\u{bf}") set_failed_rule :_BOM unless _tmp return _tmp end @@ -407,7 +407,7 @@ def _Newline # NonAlphanumeric = /[\000-\057\072-\100\133-\140\173-\177]/n def _NonAlphanumeric - _tmp = scan(/\A(?-mix:[\000-\057\072-\100\133-\140\173-\177])/n) + _tmp = scan(/\A(?-mix:[\000-\057\072-\100\133-\140\173-\177])/) set_failed_rule :_NonAlphanumeric unless _tmp return _tmp end diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index c18fc123e..3397713ac 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -496,6 +496,7 @@ def listnum(lines, id, caption, lang = nil) end def listnum_body(lines, lang) + buf = "" body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang buf << highlight(:body => body, :lexer => lexer, :format => 'html', From a32a984d91514b35441faac74d886a749509012f Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 3 May 2015 16:04:57 +0900 Subject: [PATCH 158/214] escape caption in @ --- lib/review/latexbuilder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 52f73fdf9..1e6780005 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -813,9 +813,9 @@ def inline_hd_chap(chap, id) end if @book.config["chapterlink"] anchor = n.gsub(/\./, "-") - macro('reviewsecref', str, sec_label(anchor)) + macro('reviewsecref', escape(str), sec_label(anchor)) else - str + escape(str) end end From 928ba63df253bed9f8e6b4b3449d7a94fdf3b424 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 3 May 2015 16:06:16 +0900 Subject: [PATCH 159/214] fix test: remove unused \n --- test/test_htmlbuilder.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 48b09b3c8..545ee8d47 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -59,7 +59,7 @@ def on_APPENDIX? end end actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      1.1 this is test.

      \n|, actual + assert_equal %Q|

      1.1 this is test.

      \n|, actual end def test_headline_level1_postdef_roman @@ -81,7 +81,7 @@ def on_APPENDIX? end end actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      I.1 this is test.

      \n|, actual + assert_equal %Q|

      I.1 this is test.

      \n|, actual end def test_headline_level1_postdef_alpha @@ -103,7 +103,7 @@ def on_APPENDIX? end end actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      A.1 this is test.

      \n|, actual + assert_equal %Q|

      A.1 this is test.

      \n|, actual end def test_headline_level1_without_secno @@ -124,18 +124,18 @@ def test_headline_level1_with_inlinetag def test_headline_level2 actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      1.1 this is test.

      \n|, actual + assert_equal %Q|

      1.1 this is test.

      \n|, actual end def test_headline_level3 actual = compile_block("==={test} this is test.\n") - assert_equal %Q|\n

      this is test.

      \n|, actual + assert_equal %Q|

      this is test.

      \n|, actual end def test_headline_level3_with_secno @book.config["secnolevel"] = 3 actual = compile_block("==={test} this is test.\n") - assert_equal %Q|\n

      1.0.1 this is test.

      \n|, actual + assert_equal %Q|

      1.0.1 this is test.

      \n|, actual end def test_label @@ -480,7 +480,7 @@ def @chapter.list(id) @book.config["pygments"] = true actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") - assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>\n
      \n
      \n|, actual + assert_equal %Q|
      \n

      リスト1.1: this is test<&>_

      \n
      test1\ntest1.5\n\ntest<i>2</i>
      \n
      \n|, actual end def test_list_pygments_lang @@ -500,7 +500,7 @@ def @chapter.list(id) %Q|
      def foo(a1, a2=:test)\n| +
                        %Q|  (1..3).times{\|i\| a.include?(:foo)}\n| +
                        %Q|  return true\n| +
      -                 %Q|end\n| +
      +                 %Q|end| +
                        %Q|
      \n| + %Q|
      \n|, actual end @@ -518,7 +518,7 @@ def @chapter.list(id) @book.config["pygments"] = true actual = compile_block("//list[samplelist][this is @{test}<&>_][]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") - assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n
      \n
      \n", actual + assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend
      \n
      \n", actual end def test_listnum_pygments_lang @@ -534,7 +534,7 @@ def @chapter.list(id) @book.config["pygments"] = true actual = compile_block("//listnum[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") - assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      1 def foo(a1, a2=:test)\n2   (1..3).times{|i| a.include?(:foo)}\n3   return true\n4 end\n
      \n
      \n", actual + assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      1 def foo(a1, a2=:test)\n2   (1..3).times{|i| a.include?(:foo)}\n3   return true\n4 end\n
      ", actual end def test_listnum_pygments_lang_without_lang @@ -552,7 +552,7 @@ def @chapter.list(id) @book.config["highlight"]["lang"] = "ruby" actual = compile_block("//listnum[samplelist][this is @{test}<&>_]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") - assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      1 def foo(a1, a2=:test)\n2   (1..3).times{|i| a.include?(:foo)}\n3   return true\n4 end\n
      \n
      \n", actual + assert_equal "
      \n

      リスト1.1: this is test<&>_

      \n
      1 def foo(a1, a2=:test)\n2   (1..3).times{|i| a.include?(:foo)}\n3   return true\n4 end\n
      ", actual end @@ -570,7 +570,7 @@ def test_emlist_pygments_lang end @book.config["pygments"] = true actual = compile_block("//emlist[][sql]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n") - assert_equal "
      \n
      SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n
      \n
      \n", actual + assert_equal "
      \n
      SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
      \n
      \n", actual end def test_emlist_caption @@ -602,7 +602,7 @@ def test_cmd_pygments end @book.config["pygments"] = true actual = compile_block("//cmd{\nlineA\nlineB\n//}\n") - assert_equal "
      \n
      lineA\nlineB\n
      \n
      \n", actual + assert_equal "
      \n
      lineA\nlineB
      \n
      \n", actual end def test_cmd_caption @@ -698,7 +698,6 @@ def test_column_2

      test

      inside column

      -

      next level

      EOS @@ -734,7 +733,6 @@ def test_column_ref

      test

      inside column

      -

      next level

      this is コラム「test」.

      EOS From d6f82da7324925230dc1e2ca06ebd4e68fd20f98 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 4 May 2015 01:14:09 +0900 Subject: [PATCH 160/214] JSON format of AST should be similar as that of review.js * fix names of attributes * add position of Node (line, column, pos) --- lib/review/node.rb | 24 ++++++-- lib/review/review.kpeg | 135 +++++++++++++++++++++++------------------ 2 files changed, 95 insertions(+), 64 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index 5f2738e20..ed2600123 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -27,7 +27,7 @@ def to_s_by(meth) def to_json(*args) if content.kind_of? String - val = '"'+@content.gsub(/\"/,'\\"')+'"' + val = '"'+@content.gsub(/\"/,'\\"').gsub(/\n/,'\\n')+'"' elsif content == nil val = "null" elsif !content.kind_of? Array @@ -35,7 +35,10 @@ def to_json(*args) else val = "["+@content.map(&:to_json).join(",")+"]" end - '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '","c":' + val + '}' + '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," + + '"childNodes":' + val + + '}' end def inspect @@ -54,10 +57,11 @@ def to_doc end def to_json - '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + %Q|"cmd":"#{@cmd.to_json}",|+ %Q|"label":"#{@label.to_json}",|+ - '"c":' + @content.to_json + '}' + "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," + + '"childNodes":' + @content.to_json + '}' end end @@ -136,9 +140,10 @@ def to_doc end def to_json - '{"t":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + %Q|"symbol":"#{@symbol}",| + - '"c":[' + @content.map(&:to_json).join(",") + ']}' + "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," + + '"childNodes":[' + @content.map(&:to_json).join(",") + ']}' end end @@ -156,6 +161,13 @@ def to_doc content_str = super @compiler.compile_text(content_str) end + + def to_json(*args) + val = '"'+@content.gsub(/\"/,'\\"').gsub(/\n/,'\\n')+'"' + '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," + + '"text":' + val + '}' + end end class NewLineNode < Node diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 11467e0b2..949f06bd5 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -2,6 +2,14 @@ %% { class Error; end + class Position + attr_accessor :pos, :line, :col + def initialize(compiler) + @pos = compiler.pos + @line = compiler.current_line + @col = compiler.current_column + end + end require 'review/location' require 'review/extentions' @@ -18,6 +26,8 @@ require 'review/node' ## redifine Compiler.new def initialize(strategy) @strategy = strategy + @current_column = nil + @chapter = nil end attr_accessor :strategy @@ -38,6 +48,11 @@ require 'review/node' def convert_ast ast = @strategy.ast convert_column(ast) + if $DEBUG + File.open("review-dump.json","w") do |f| + f.write(ast.to_json) + end + end @strategy.output << ast.to_doc end @@ -57,7 +72,7 @@ require 'review/node' if elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc == "column" flush_column(new_content) @current_content = [] - @current_column = ReVIEW::ColumnNode.new(elem.compiler, elem.level, + @current_column = ReVIEW::ColumnNode.new(elem.compiler, elem.position, elem.level, elem.label, elem.content, @current_content) next elsif elem.kind_of?(ReVIEW::HeadlineNode) && elem.cmd && elem.cmd.to_doc =~ %r|^/| @@ -466,31 +481,35 @@ require 'review/node' s.size >= @list_stack.last.size + 2 end + def position + Position.new(self) + end + } %% ast-location = ::ReVIEW -%% headline = ast HeadlineNode(compiler, level, cmd, label, content) -%% paragraph = ast ParagraphNode(compiler, content) -%% block_element = ast BlockElementNode(compiler, name, args, content) -%% code_block_element = ast CodeBlockElementNode(compiler, name, args, content) -%% inline_element = ast InlineElementNode(compiler, symbol, content) -%% inline_element_content = ast InlineElementContentNode(compiler, content) -%% text = ast TextNode(compiler, content) -%% raw = ast RawNode(compiler, builder, content) -%% brace = ast BraceNode(compiler, content) -%% singleline_content = ast SinglelineContentNode(compiler, content) -%% singleline_comment = ast SinglelineCommentNode(compiler, content) -%% ulist = ast UlistNode(compiler, content) -%% ulist_element = ast UlistElementNode(compiler, level, content) -%% olist = ast OlistNode(compiler, content) -%% olist_element = ast OlistElementNode(compiler, num, content) -%% dlist = ast DlistNode(compiler, content) -%% dlist_element = ast DlistElementNode(compiler, text, content) -%% bracket_arg = ast BracketArgNode(compiler, content) -%% document = ast DocumentNode(compiler, content) -%% column = ast ColumnNode(compiler, level, label, caption, content) -%% newline = ast NewLineNode(compiler, content) +%% headline = ast HeadlineNode(compiler, position, level, cmd, label, content) +%% paragraph = ast ParagraphNode(compiler, position, content) +%% block_element = ast BlockElementNode(compiler, position, name, args, content) +%% code_block_element = ast CodeBlockElementNode(compiler, position, name, args, content) +%% inline_element = ast InlineElementNode(compiler, position, symbol, content) +%% inline_element_content = ast InlineElementContentNode(compiler, position, content) +%% text = ast TextNode(compiler, position, content) +%% raw = ast RawNode(compiler, builder, position, content) +%% brace = ast BraceNode(compiler, position, content) +%% singleline_content = ast SinglelineContentNode(compiler, position, content) +%% singleline_comment = ast SinglelineCommentNode(compiler, position, content) +%% ulist = ast UlistNode(compiler, position, content) +%% ulist_element = ast UlistElementNode(compiler, position, level, content) +%% olist = ast OlistNode(compiler, position, content) +%% olist_element = ast OlistElementNode(compiler, position, num, content) +%% dlist = ast DlistNode(compiler, position, content) +%% dlist_element = ast DlistElementNode(compiler, position, text, content) +%% bracket_arg = ast BracketArgNode(compiler, position, content) +%% document = ast DocumentNode(compiler, position, content) +%% column = ast ColumnNode(compiler, position, level, label, caption, content) +%% newline = ast NewLineNode(compiler, position, content) # %% dummy @@ -499,7 +518,7 @@ root = Start Start = &. { @list_stack = Array.new } Document:c { @strategy.ast = c } ## a Document is a set of Blocks -Document = BOM? Block*:c ~document(self, c) +Document = BOM? Block*:c ~document(self, position, c) ## ignore leading blank lines Block = BlankLine*:c { c } @@ -514,24 +533,24 @@ Block = BlankLine*:c { c } BlankLine = Newline -SinglelineComment = ("#@" < NonNewline+ > EOL) ~singleline_comment(self, text) +SinglelineComment = ("#@" < NonNewline+ > EOL) ~singleline_comment(self, position, text) -Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL ~headline(self, level, cmd, label, caption) +Headline = HeadlinePrefix:level BracketArg?:cmd BraceArg?:label Space* SinglelineContent?:caption EOL ~headline(self, position, level, cmd, label, caption) HeadlinePrefix = < /={1,5}/ > { text.length } -Paragraph = ParagraphLine+:c ~paragraph(self, c.flatten) +Paragraph = ParagraphLine+:c ~paragraph(self, position, c.flatten) ParagraphLine = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } # There are 3 types of Block Element: raw Block, Code Block, and Normal Block BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL - ~raw(self, b, r1) + ~raw(self, position, b, r1) | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL - ~code_block_element(self, symbol, args, contents) + ~code_block_element(self, position, symbol, args, contents) | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL - ~block_element(self, symbol, args, contents) - | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL ~block_element(self, symbol, args, nil) + ~block_element(self, position, symbol, args, contents) + | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL ~block_element(self, position, symbol, args, nil) ) RawBlockBuilderSelect = "|" Space* RawBlockBuilderSelectSub:c Space* "|" { c } @@ -547,13 +566,13 @@ RawBlockElementArg = !"]" ( "\\]" { "]" } | < NonNewline > { text } ) -BracketArg = "[" BracketArgInline*:content "]" ~bracket_arg(self, content) +BracketArg = "[" BracketArgInline*:content "]" ~bracket_arg(self, position, content) ## XXX '\' (excpet '\]' and '\\' ) => '\' is ??? BracketArgInline = ( InlineElement:c { c } - | "\\]" ~text(self, "]") - | "\\\\" ~text(self, "\\") - | < /[^\r\n\]]/ > ~text(self, text) + | "\\]" ~text(self, position, "]") + | "\\\\" ~text(self, position, "\\") + | < /[^\r\n\]]/ > ~text(self, position, text) ) BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } @@ -573,14 +592,14 @@ BlockElementContent = ( SinglelineComment:c { c } ) ## it's like Paragraph, but it's in a block, so do not allow '//}\n' -BlockElementParagraph = BlockElementParagraphLine+:c ~paragraph(self, c.flatten) +BlockElementParagraph = BlockElementParagraphLine+:c ~paragraph(self, position, c.flatten) BlockElementParagraphLine = !"//}" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c } ## In CodeBlockElementContents, newline should no be ingored. So we use TextNode instead of NewLineNode. CodeBlockElementContents = CodeBlockElementContent+:c { c } CodeBlockElementContent = ( SinglelineComment:c { c } - | BlankLine:c { ::ReVIEW::TextNode.new(self, "\n") } - | !"//}" SinglelineContent:c Newline { [c, ::ReVIEW::TextNode.new(self, "\n")] } + | BlankLine:c { ::ReVIEW::TextNode.new(self, position, "\n") } + | !"//}" SinglelineContent:c Newline { [c, ::ReVIEW::TextNode.new(self, position, "\n")] } ) ## Ulist and Olist @@ -591,15 +610,15 @@ Enumerator = < /[0-9]+/ > { num = text } "." { num.to_i } Ulist = Indent+:s Bullet+:b Space+ { @list_stack.push(s) } UlistItemBlock:item { if b.size > 1 then item.level = b.size end } (UlistItem | UlistItemMore | NestedList)*:items &{ s == @list_stack.pop } - ~ulist(self, items.unshift(item)) + ~ulist(self, position, items.unshift(item)) Olist = Indent+:s Enumerator:e Space+ { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } - ~olist(self, items.unshift(item)) + ~olist(self, position, items.unshift(item)) -UlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~ulist_element(self, @list_stack.size, d.unshift(c)) -OlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~olist_element(self, 0, d.unshift(c)) +UlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~ulist_element(self, position, @list_stack.size, d.unshift(c)) +OlistItemBlock = ListItemFirstLine:c ListItemLine*:d ~olist_element(self, position, 0, d.unshift(c)) ListItemFirstLine = SinglelineContent:c Newline { c } ListItemLine = Indent+:s !Bullet !Enumerator !Space SinglelineContent:c &{ check_indent(s) } Newline { c } @@ -616,35 +635,35 @@ NestedList = (NestedUlist | NestedOlist) NestedUlist = Indent+:s Bullet Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } UlistItemBlock:item (UlistItem | NestedList)*:items &{ s == @list_stack.pop } - ~ulist(self, items.unshift(item)) + ~ulist(self, position, items.unshift(item)) NestedOlist = Indent+:s Enumerator:e Space+ &{ check_nested_indent(s) } { @list_stack.push(s) } OlistItemBlock:item { item.num = e } (OlistItem | NestedList)*:items &{ s == @list_stack.pop } - ~olist(self, items.unshift(item)) + ~olist(self, position, items.unshift(item)) # Dlist -Dlist = (DlistElement | SinglelineComment)+:content ~dlist(self, content) +Dlist = (DlistElement | SinglelineComment)+:content ~dlist(self, position, content) -DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, text, content) +DlistElement = Indent* ":" Space+ SinglelineContent:text Newline DlistElementContent+:content ~dlist_element(self, position, text, content) DlistElementContent = (SinglelineComment:c { c } |Space+ SinglelineContent:c Newline { c } ) -SinglelineContent = Inline+:c ~singleline_content(self,c) +SinglelineContent = Inline+:c ~singleline_content(self, position, c) # Inline Element and Non Inline Element Inline = ( InlineElement | NonInlineElement) -NonInlineElement = !InlineElement < NonNewline > ~text(self, text) +NonInlineElement = !InlineElement < NonNewline > ~text(self, position, text) InlineElement = ( RawInlineElement:c { c } - | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, symbol,contents) + | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, position, symbol,contents) ) -RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" ~raw(self, builders,c) +RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" ~raw(self, position, builders,c) RawInlineElementContent = ( "\\}" { "}" } | < /[^\r\n\}]/ > { text } @@ -662,8 +681,8 @@ InlineElementContentsSub = !"}" ( Space* InlineElementContent:c1 Space* "," Inli InlineElementContent = InlineElementContentSub+:d { d } InlineElementContentSub = ( InlineElement:c { c } - | !InlineElement QuotedInlineText:content ~inline_element_content(self, content) - | !InlineElement InlineElementContentText+:content ~inline_element_content(self, content) + | !InlineElement QuotedInlineText:content ~inline_element_content(self, position, content) + | !InlineElement InlineElementContentText+:content ~inline_element_content(self, position, content) ) ## Quoted Inline Text is "..." @@ -672,14 +691,14 @@ QuotedInlineText = "\"" ( "\\\"" { "\"" } | "\\\\" { "\\" } | < /[^"\r\n\\]/ > { text } - )+:str "\"" ~text(self, str.join("")) + )+:str "\"" ~text(self, position, str.join("")) ## XXX '\' (excpet '\}' and '\,' and '\\' ) => '\' is OK? -InlineElementContentText = ( "\\}" ~text(self, "}") - | "\\," ~text(self, ",") - | "\\\\" ~text(self, "\\" ) - | "\\" ~text(self, "\\" ) - | !InlineElement < /[^\r\n\\},]/> ~text(self,text) +InlineElementContentText = ( "\\}" ~text(self, position, "}") + | "\\," ~text(self, position, ",") + | "\\\\" ~text(self, position, "\\" ) + | "\\" ~text(self, position, "\\" ) + | !InlineElement < /[^\r\n\\},]/> ~text(self, position, text) ) NonNewline = /[^\r\n]/ @@ -700,6 +719,6 @@ AlphanumericAscii = %literals.AlphanumericAscii LowerAlphabetAscii = %literals.LowerAlphabetAscii Digit = %literals.Digit BOM = %literals.BOM -Newline = %literals.Newline:n ~newline(self, "\n") +Newline = %literals.Newline:n ~newline(self, position, "\n") NonAlphanumeric = %literals.NonAlphanumeric Spacechar = %literals.Spacechar From 4ac03485f3fad9a9a0b508ff2b3377efaa6fa1bf Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 4 May 2015 01:16:41 +0900 Subject: [PATCH 161/214] avoid warning --- lib/review/inaobuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/inaobuilder.rb b/lib/review/inaobuilder.rb index 758b79e16..b4e1461d3 100644 --- a/lib/review/inaobuilder.rb +++ b/lib/review/inaobuilder.rb @@ -195,7 +195,7 @@ def list_body(id, lines, lang) def listnum_body(lines, lang) buf = "" lines.each_with_index do |line, i| - buf << detab((i+1).to_s.rjust(2) + " " +line) << "\n" + buf << detab((i+1).to_s.rjust(2) + " " + line) << "\n" end buf << "◆/list◆\n" buf From ae30fcef7b70a7f543abb32a268152f1706f624a Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 31 May 2015 18:34:05 +0900 Subject: [PATCH 162/214] 2.0.0.beta1 --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index 96cce9118..929eb5f5c 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "1.5.0" + VERSION = "2.0.0.beta1" end From a868e43129d0d2f8302ed05087843939b2424e86 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 3 Oct 2015 02:12:41 +0900 Subject: [PATCH 163/214] remove compile_inline() --- lib/review/builder.rb | 4 ++-- test/test_builder.rb | 20 -------------------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 258d23a43..5f6b7ed24 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -209,7 +209,7 @@ def inline_chap(id) end def inline_title(id) - compile_inline @book.chapter_index.title(id) + @book.chapter_index.title(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") @@ -407,7 +407,7 @@ def image_ext end def inline_include(file_name) - compile_inline File.open(file_name).read + File.open(file_name).read end def include(file_name) diff --git a/test/test_builder.rb b/test/test_builder.rb index da65ea6b2..56a6b5d1e 100644 --- a/test/test_builder.rb +++ b/test/test_builder.rb @@ -60,26 +60,6 @@ def test_not_implemented_methods end end - def test_compile_inline - text = "abc" - assert_equal [:text, text], @b.compile_inline(text) - end - - def test_inline_ruby - def @b.compile_ruby(base,ruby) - [base,ruby] - end - str = @b.inline_ruby("foo,bar") - assert_equal str, ["foo","bar"] - str = @b.inline_ruby("foo\\,\\,,\\,bar,buz") - assert_equal str, ["foo,,",",bar,buz"] - end - - def test_compile_inline_backslash - text = "abc\\d\\#a" - assert_equal [:text, text], @b.compile_inline(text) - end - class XBuilder < Builder def list_header(id, caption) end From 40ebb2be8a57a9631b8e574c26b7cfcca0b8de3b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 17 Oct 2015 22:15:10 +0900 Subject: [PATCH 164/214] remove compile_inline --- lib/review/htmlbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 84ca98c15..b37f3b626 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -96,7 +96,7 @@ def result # default XHTML header/footer @error_messages = error_messages @warning_messages = warning_messages - @title = strip_html(compile_inline(@chapter.title)) + @title = strip_html(@chapter.title) @body = @output.string @language = @book.config['language'] @stylesheets = @book.config["stylesheet"] From 033b57036458a5c2abb922f9d153e497e6920516 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 17 Oct 2015 23:27:57 +0900 Subject: [PATCH 165/214] fix rubocop warnings --- .rubocop.yml | 4 ++++ lib/review/builder.rb | 1 - lib/review/compiler.rb | 10 ++++++---- lib/review/compiler/literals_1_8.rb | 1 + lib/review/extentions/array.rb | 2 +- lib/review/htmlbuilder.rb | 8 ++++---- lib/review/idgxmlbuilder.rb | 6 ++++-- lib/review/latexbuilder.rb | 2 +- lib/review/location.rb | 2 +- lib/review/markdownbuilder.rb | 2 +- lib/review/node.rb | 6 +++--- lib/review/review.kpeg | 10 ++++++---- test/test_compiler.rb | 4 ++-- 13 files changed, 34 insertions(+), 24 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f944ff7a4..f0e692392 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -41,3 +41,7 @@ Style/OpMethod: # Use snake_case for variable names. Style/VariableName: Enabled: true + +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 1500 diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 22f69968f..941d926f0 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -72,7 +72,6 @@ def print(*s) def puts(*s) raise NotImplementedError, "XXX: `puts` method is obsoleted. Do not use it." - @output.print(*s) end def target_name diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 9404364e6..5313a1c8b 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1,4 +1,4 @@ -class ReVIEW::Compiler +class ReVIEW::Compiler # rubocop:disable all # :stopdoc: # This is distinct from setup_parser so that a standalone parser @@ -360,12 +360,13 @@ class Error; end class Position attr_accessor :pos, :line, :col def initialize(compiler) - @pos = compiler.pos + @pos = compiler.pos @line = compiler.current_line - @col = compiler.current_column + @col = compiler.current_column end end +# rubocop:disable all require 'review/location' require 'review/extentions' require 'review/preprocessor' @@ -699,7 +700,8 @@ def compile_ulist(content) buf0 = "" level = 0 content.each do |element| - current_level, buf = element.level, element.to_doc + current_level = element.level + buf = element.to_doc if level == current_level buf0 << @strategy.ul_item_end # body diff --git a/lib/review/compiler/literals_1_8.rb b/lib/review/compiler/literals_1_8.rb index ab2e3fe49..a4c6c928b 100644 --- a/lib/review/compiler/literals_1_8.rb +++ b/lib/review/compiler/literals_1_8.rb @@ -1,4 +1,5 @@ $KCODE='u' ## for Ruby 1.8 :-( +# rubocop:disable all ## # Provides Literals appropriate for your ruby version. diff --git a/lib/review/extentions/array.rb b/lib/review/extentions/array.rb index d83e1627f..446bae0a8 100644 --- a/lib/review/extentions/array.rb +++ b/lib/review/extentions/array.rb @@ -18,7 +18,7 @@ def map(&block) if !block_given? return to_enum :map else - collect(&block) ## XXX same as original + collect(&block) ## XXX same as original end end end diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index b37f3b626..f6ac536f7 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -353,7 +353,7 @@ def ol_begin if @ol_num num = @ol_num @ol_num = nil - "
        \n" ## it's OK in HTML5, but not OK in XHTML1.1 + "
          \n" ## it's OK in HTML5, but not OK in XHTML1.1 else "
            \n" end @@ -392,7 +392,7 @@ def paragraph(lines) end end - def parasep() + def parasep "
            \n" end @@ -587,10 +587,10 @@ def texequation(lines) require 'math_ml' require 'math_ml/symbol/character_reference' p = MathML::LaTeX::Parser.new(:symbol=>MathML::Symbol::CharacterReference) - buf << p.parse(unescape_html(lines.join("\n")), true) << "\n + buf << p.parse(unescape_html(lines.join("\n")), true) << "\n" else buf << '
            '
            -        buf << "#{lines.join("\n")}\n"
            +        buf << lines.join("\n") << "\n"
                     buf << "
            \n" end buf << "
      \n" diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index b4d8610cb..e77f61440 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -661,8 +661,10 @@ def compile_ruby(base, ruby) def compile_kw(word, alt) '' + if alt - then ("#{word}(#{alt.strip})") - else (word) + then + ("#{word}(#{alt.strip})") + else + (word) end + '' + %Q[] + diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 59c6cc366..6acd41eb8 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -144,7 +144,7 @@ def box(lines, caption = nil) end buf << '\begin{reviewbox}' << "\n" lines.each do |line| - buf << detab(line) << "\n" + buf << detab(line) << "\n" end< buf << '\end{reviewbox}' << "\n" end diff --git a/lib/review/location.rb b/lib/review/location.rb index 777612260..662aa3df7 100644 --- a/lib/review/location.rb +++ b/lib/review/location.rb @@ -19,6 +19,6 @@ def string end end - alias to_s string + alias_method :to_s, :string end end diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index 6aef9a818..d16f1771b 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -216,7 +216,7 @@ def indepimage(id, caption="", metric=nil) end def pagebreak - buf = "" + buf = "" buf << "{pagebreak}" << "\n" buf end diff --git a/lib/review/node.rb b/lib/review/node.rb index ed2600123..75eb61c03 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -15,7 +15,7 @@ def to_doc def to_s_by(meth) if content.kind_of? String @content - elsif content == nil + elsif content.nil? nil elsif !content.kind_of? Array @content.__send__(meth) @@ -28,7 +28,7 @@ def to_s_by(meth) def to_json(*args) if content.kind_of? String val = '"'+@content.gsub(/\"/,'\\"').gsub(/\n/,'\\n')+'"' - elsif content == nil + elsif content.nil? val = "null" elsif !content.kind_of? Array val = @content.to_json @@ -131,7 +131,7 @@ class InlineElementNode < Node def to_raw content_str = super - "@<#{@symbol.to_s}>{#{content_str}}" + "@<#{@symbol}>{#{content_str}}" end def to_doc diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 84718fa9a..97de9f48a 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -1,16 +1,17 @@ -%% name = ReVIEW::Compiler +%% name = ReVIEW::Compiler # rubocop:disable all %% { class Error; end class Position attr_accessor :pos, :line, :col def initialize(compiler) - @pos = compiler.pos + @pos = compiler.pos @line = compiler.current_line - @col = compiler.current_column + @col = compiler.current_column end end +# rubocop:disable all require 'review/location' require 'review/extentions' require 'review/preprocessor' @@ -344,7 +345,8 @@ require 'review/node' buf0 = "" level = 0 content.each do |element| - current_level, buf = element.level, element.to_doc + current_level = element.level + buf = element.to_doc if level == current_level buf0 << @strategy.ul_item_end # body diff --git a/test/test_compiler.rb b/test/test_compiler.rb index 94db7c2f5..01a6b1a07 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -12,11 +12,11 @@ class CompilerTest < Test::Unit::TestCase def setup @builder = HTMLBuilder.new() @param = { - "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder + "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder "inencoding" => "UTF-8", "outencoding" => "UTF-8", "subdirmode" => nil, - "stylesheet" => nil, # for HTMLBuilder + "stylesheet" => nil, # for HTMLBuilder } ReVIEW.book.param = @param @compiler = ReVIEW::Compiler.new(@builder) From a87451d8a1e17d35c05b624cceda5695c2126daf Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 17 Oct 2015 23:35:30 +0900 Subject: [PATCH 166/214] not support Ruby 1.8 --- Rakefile | 2 - lib/review/compiler.rb | 9 +- lib/review/compiler/literals_1_8.kpeg | 19 -- lib/review/compiler/literals_1_8.rb | 433 -------------------------- lib/review/review.kpeg | 9 +- 5 files changed, 6 insertions(+), 466 deletions(-) delete mode 100644 lib/review/compiler/literals_1_8.kpeg delete mode 100644 lib/review/compiler/literals_1_8.rb diff --git a/Rakefile b/Rakefile index 958eec325..b59ff17c5 100644 --- a/Rakefile +++ b/Rakefile @@ -15,9 +15,7 @@ task :default => [:test] task :kpeg do FileUtils.rm_f "lib/review/compiler.rb" - FileUtils.rm_f "lib/review/compiler/literals_1_8.rb" FileUtils.rm_f "lib/review/compiler/literals_1_9.rb" - sh "kpeg -s lib/review/compiler/literals_1_8.kpeg -o lib/review/compiler/literals_1_8.rb" sh "kpeg -s lib/review/compiler/literals_1_9.kpeg -o lib/review/compiler/literals_1_9.rb" sh "kpeg -s lib/review/review.kpeg -o lib/review/compiler.rb" end diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 5313a1c8b..ab6d008be 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1,4 +1,4 @@ -class ReVIEW::Compiler # rubocop:disable all +class ReVIEW::Compiler # :stopdoc: # This is distinct from setup_parser so that a standalone parser @@ -373,11 +373,8 @@ def initialize(compiler) require 'review/exception' require 'review/node' require 'lineinput' - if RUBY_VERSION > '1.9' then - require 'review/compiler/literals_1_9' - else - require 'review/compiler/literals_1_8' - end + require 'review/compiler/literals_1_9' + # require 'review/compiler/literals_1_8' ## redifine Compiler.new def initialize(strategy) diff --git a/lib/review/compiler/literals_1_8.kpeg b/lib/review/compiler/literals_1_8.kpeg deleted file mode 100644 index e431d9168..000000000 --- a/lib/review/compiler/literals_1_8.kpeg +++ /dev/null @@ -1,19 +0,0 @@ -%% name = ReVIEW::Compiler::Literals - -%% header { -$KCODE='u' ## for Ruby 1.8 :-( - -## -# Provides Literals appropriate for your ruby version. -#-- -# This set of literals is for Ruby 1.8 regular expressions. -} - -Alphanumeric = /[0-9A-Za-z\200-\377]/n -AlphanumericAscii = /[A-Za-z0-9]/ -LowerAlphabetAscii = /[a-z]/ -Digit = /[0-9]/ -BOM = "\357\273\277" -Newline = /\n|\r\n?/ -NonAlphanumeric = /[\000-\057\072-\100\133-\140\173-\177]/n -Spacechar = / |\t/ diff --git a/lib/review/compiler/literals_1_8.rb b/lib/review/compiler/literals_1_8.rb deleted file mode 100644 index a4c6c928b..000000000 --- a/lib/review/compiler/literals_1_8.rb +++ /dev/null @@ -1,433 +0,0 @@ -$KCODE='u' ## for Ruby 1.8 :-( -# rubocop:disable all - -## -# Provides Literals appropriate for your ruby version. -#-- -# This set of literals is for Ruby 1.8 regular expressions. -class ReVIEW::Compiler::Literals - # :stopdoc: - - # This is distinct from setup_parser so that a standalone parser - # can redefine #initialize and still have access to the proper - # parser setup code. - def initialize(str, debug=false) - setup_parser(str, debug) - end - - - - # Prepares for parsing +str+. If you define a custom initialize you must - # call this method before #parse - def setup_parser(str, debug=false) - set_string str, 0 - @memoizations = Hash.new { |h,k| h[k] = {} } - @result = nil - @failed_rule = nil - @failing_rule_offset = -1 - - setup_foreign_grammar - end - - attr_reader :string - attr_reader :failing_rule_offset - attr_accessor :result, :pos - - def current_column(target=pos) - if c = string.rindex("\n", target-1) - return target - c - 1 - end - - target + 1 - end - - def current_line(target=pos) - cur_offset = 0 - cur_line = 0 - - string.each_line do |line| - cur_line += 1 - cur_offset += line.size - return cur_line if cur_offset >= target - end - - -1 - end - - def lines - lines = [] - string.each_line { |l| lines << l } - lines - end - - - - def get_text(start) - @string[start..@pos-1] - end - - # Sets the string and current parsing position for the parser. - def set_string string, pos - @string = string - @string_size = string ? string.size : 0 - @pos = pos - end - - def show_pos - width = 10 - if @pos < width - "#{@pos} (\"#{@string[0,@pos]}\" @ \"#{@string[@pos,width]}\")" - else - "#{@pos} (\"... #{@string[@pos - width, width]}\" @ \"#{@string[@pos,width]}\")" - end - end - - def failure_info - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - "line #{l}, column #{c}: failed rule '#{info.name}' = '#{info.rendered}'" - else - "line #{l}, column #{c}: failed rule '#{@failed_rule}'" - end - end - - def failure_caret - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - line = lines[l-1] - "#{line}\n#{' ' * (c - 1)}^" - end - - def failure_character - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - lines[l-1][c-1, 1] - end - - def failure_oneline - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - char = lines[l-1][c-1, 1] - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - "@#{l}:#{c} failed rule '#{info.name}', got '#{char}'" - else - "@#{l}:#{c} failed rule '#{@failed_rule}', got '#{char}'" - end - end - - class ParseError < RuntimeError - end - - def raise_error - raise ParseError, failure_oneline - end - - def show_error(io=STDOUT) - error_pos = @failing_rule_offset - line_no = current_line(error_pos) - col_no = current_column(error_pos) - - io.puts "On line #{line_no}, column #{col_no}:" - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - io.puts "Failed to match '#{info.rendered}' (rule '#{info.name}')" - else - io.puts "Failed to match rule '#{@failed_rule}'" - end - - io.puts "Got: #{string[error_pos,1].inspect}" - line = lines[line_no-1] - io.puts "=> #{line}" - io.print(" " * (col_no + 3)) - io.puts "^" - end - - def set_failed_rule(name) - if @pos > @failing_rule_offset - @failed_rule = name - @failing_rule_offset = @pos - end - end - - attr_reader :failed_rule - - def match_string(str) - len = str.size - if @string[pos,len] == str - @pos += len - return str - end - - return nil - end - - def scan(reg) - if m = reg.match(@string[@pos..-1]) - width = m.end(0) - @pos += width - return true - end - - return nil - end - - if "".respond_to? :ord - def get_byte - if @pos >= @string_size - return nil - end - - s = @string[@pos].ord - @pos += 1 - s - end - else - def get_byte - if @pos >= @string_size - return nil - end - - s = @string[@pos] - @pos += 1 - s - end - end - - def parse(rule=nil) - # We invoke the rules indirectly via apply - # instead of by just calling them as methods because - # if the rules use left recursion, apply needs to - # manage that. - - if !rule - apply(:_root) - else - method = rule.gsub("-","_hyphen_") - apply :"_#{method}" - end - end - - class MemoEntry - def initialize(ans, pos) - @ans = ans - @pos = pos - @result = nil - @set = false - @left_rec = false - end - - attr_reader :ans, :pos, :result, :set - attr_accessor :left_rec - - def move!(ans, pos, result) - @ans = ans - @pos = pos - @result = result - @set = true - @left_rec = false - end - end - - def external_invoke(other, rule, *args) - old_pos = @pos - old_string = @string - - set_string other.string, other.pos - - begin - if val = __send__(rule, *args) - other.pos = @pos - other.result = @result - else - other.set_failed_rule "#{self.class}##{rule}" - end - val - ensure - set_string old_string, old_pos - end - end - - def apply_with_args(rule, *args) - memo_key = [rule, args] - if m = @memoizations[memo_key][@pos] - @pos = m.pos - if !m.set - m.left_rec = true - return nil - end - - @result = m.result - - return m.ans - else - m = MemoEntry.new(nil, @pos) - @memoizations[memo_key][@pos] = m - start_pos = @pos - - ans = __send__ rule, *args - - lr = m.left_rec - - m.move! ans, @pos, @result - - # Don't bother trying to grow the left recursion - # if it's failing straight away (thus there is no seed) - if ans and lr - return grow_lr(rule, args, start_pos, m) - else - return ans - end - - return ans - end - end - - def apply(rule) - if m = @memoizations[rule][@pos] - @pos = m.pos - if !m.set - m.left_rec = true - return nil - end - - @result = m.result - - return m.ans - else - m = MemoEntry.new(nil, @pos) - @memoizations[rule][@pos] = m - start_pos = @pos - - ans = __send__ rule - - lr = m.left_rec - - m.move! ans, @pos, @result - - # Don't bother trying to grow the left recursion - # if it's failing straight away (thus there is no seed) - if ans and lr - return grow_lr(rule, nil, start_pos, m) - else - return ans - end - - return ans - end - end - - def grow_lr(rule, args, start_pos, m) - while true - @pos = start_pos - @result = m.result - - if args - ans = __send__ rule, *args - else - ans = __send__ rule - end - return nil unless ans - - break if @pos <= m.pos - - m.move! ans, @pos, @result - end - - @result = m.result - @pos = m.pos - return m.ans - end - - class RuleInfo - def initialize(name, rendered) - @name = name - @rendered = rendered - end - - attr_reader :name, :rendered - end - - def self.rule_info(name, rendered) - RuleInfo.new(name, rendered) - end - - - # :startdoc: - # :stopdoc: - def setup_foreign_grammar; end - - # Alphanumeric = /[0-9A-Za-z\200-\377]/n - def _Alphanumeric - _tmp = scan(/\A(?-mix:[0-9A-Za-z\200-\377])/) - set_failed_rule :_Alphanumeric unless _tmp - return _tmp - end - - # AlphanumericAscii = /[A-Za-z0-9]/ - def _AlphanumericAscii - _tmp = scan(/\A(?-mix:[A-Za-z0-9])/) - set_failed_rule :_AlphanumericAscii unless _tmp - return _tmp - end - - # LowerAlphabetAscii = /[a-z]/ - def _LowerAlphabetAscii - _tmp = scan(/\A(?-mix:[a-z])/) - set_failed_rule :_LowerAlphabetAscii unless _tmp - return _tmp - end - - # Digit = /[0-9]/ - def _Digit - _tmp = scan(/\A(?-mix:[0-9])/) - set_failed_rule :_Digit unless _tmp - return _tmp - end - - # BOM = "" - def _BOM - _tmp = match_string("\u{ef}\u{bb}\u{bf}") - set_failed_rule :_BOM unless _tmp - return _tmp - end - - # Newline = /\n|\r\n?/ - def _Newline - _tmp = scan(/\A(?-mix:\n|\r\n?)/) - set_failed_rule :_Newline unless _tmp - return _tmp - end - - # NonAlphanumeric = /[\000-\057\072-\100\133-\140\173-\177]/n - def _NonAlphanumeric - _tmp = scan(/\A(?-mix:[\000-\057\072-\100\133-\140\173-\177])/) - set_failed_rule :_NonAlphanumeric unless _tmp - return _tmp - end - - # Spacechar = / |\t/ - def _Spacechar - _tmp = scan(/\A(?-mix: |\t)/) - set_failed_rule :_Spacechar unless _tmp - return _tmp - end - - Rules = {} - Rules[:_Alphanumeric] = rule_info("Alphanumeric", "/[0-9A-Za-z\\200-\\377]/n") - Rules[:_AlphanumericAscii] = rule_info("AlphanumericAscii", "/[A-Za-z0-9]/") - Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "/[a-z]/") - Rules[:_Digit] = rule_info("Digit", "/[0-9]/") - Rules[:_BOM] = rule_info("BOM", "\"\"") - Rules[:_Newline] = rule_info("Newline", "/\\n|\\r\\n?/") - Rules[:_NonAlphanumeric] = rule_info("NonAlphanumeric", "/[\\000-\\057\\072-\\100\\133-\\140\\173-\\177]/n") - Rules[:_Spacechar] = rule_info("Spacechar", "/ |\\t/") - # :startdoc: -end diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 97de9f48a..a53947352 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -1,4 +1,4 @@ -%% name = ReVIEW::Compiler # rubocop:disable all +%% name = ReVIEW::Compiler %% { class Error; end @@ -18,11 +18,8 @@ require 'review/preprocessor' require 'review/exception' require 'review/node' require 'lineinput' - if RUBY_VERSION > '1.9' then - require 'review/compiler/literals_1_9' - else - require 'review/compiler/literals_1_8' - end + require 'review/compiler/literals_1_9' + # require 'review/compiler/literals_1_8' ## redifine Compiler.new def initialize(strategy) From 6353b7eeb9b4b77e194a5290f6208f3bea5e7a82 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 17 Oct 2015 23:36:07 +0900 Subject: [PATCH 167/214] generated ReVIEW::Compiler class is over 4000 lines... :( --- .rubocop.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index f0e692392..a24041267 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,4 +44,4 @@ Style/VariableName: # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 1500 + Max: 4500 From 5dad64ab01fe3e582ffda886f26f6399e1aad8b8 Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 29 Oct 2015 18:23:59 +0900 Subject: [PATCH 168/214] update version --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index 929eb5f5c..b99154fe2 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "2.0.0.beta1" + VERSION = "2.0.0.beta2" end From 2d72c0f3319bfe6862fe536952aa2da53ce28f99 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Oct 2015 20:15:51 +0900 Subject: [PATCH 169/214] update gemspec for review-peg --- review.gemspec | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/review.gemspec b/review.gemspec index 9fc9525c5..ba785f77d 100644 --- a/review.gemspec +++ b/review.gemspec @@ -3,17 +3,16 @@ $:.push File.expand_path("../lib", __FILE__) require "review/version" Gem::Specification.new do |gem| - gem.name = "review" + gem.name = "review-peg" gem.version = ReVIEW::VERSION gem.platform = Gem::Platform::RUBY gem.license = "LGPL" gem.authors = ["kmuto", "takahashim"] gem.email = "kmuto@debian.org" gem.homepage = "http://github.com/kmuto/review" - gem.summary = "Re:VIEW: a easy-to-use digital publishing system" + gem.summary = "Re:VIEW peg: a easy-to-use digital publishing system (PEG version)" gem.description = "Re:VIEW is a digital publishing system for books and ebooks. It supports InDesign, EPUB and LaTeX." gem.required_rubygems_version = Gem::Requirement.new(">= 0") if gem.respond_to? :required_rubygems_version= - gem.date = "2015-10-29" gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") From e54be64471252eda870bc48648cfe618a6f44e96 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 30 Oct 2015 20:15:34 +0900 Subject: [PATCH 170/214] update version for review-peg --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index b99154fe2..cd148068f 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "2.0.0.beta2" + VERSION = "0.1.0" end From 51f895a35643e827ed3dbf2c1f83f517193358c1 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 31 Oct 2015 18:55:43 +0900 Subject: [PATCH 171/214] add postfix "-peg" --- ...eview-catalog-converter => review-catalog-converter-peg} | 0 bin/{review-check => review-check-peg} | 0 bin/{review-checkdep => review-checkdep-peg} | 0 bin/{review-compile => review-compile-peg} | 0 ...{review-epubmaker-legacy => review-epubmaker-legacy-peg} | 0 bin/{review-epubmaker => review-epubmaker-peg} | 0 bin/{review-index => review-index-peg} | 0 bin/{review-init => review-init-peg} | 0 bin/{review-pdfmaker => review-pdfmaker-peg} | 0 bin/{review-preproc => review-preproc-peg} | 0 bin/{review-validate => review-validate-peg} | 0 bin/{review-vol => review-vol-peg} | 0 lib/review/compiler.rb | 2 +- lib/review/epubmaker.rb | 2 +- lib/review/latexbuilder.rb | 6 ++++-- lib/review/pdfmaker.rb | 2 +- lib/review/review.kpeg | 2 +- test/sample-book/src/Rakefile | 5 +++-- 18 files changed, 11 insertions(+), 8 deletions(-) rename bin/{review-catalog-converter => review-catalog-converter-peg} (100%) rename bin/{review-check => review-check-peg} (100%) rename bin/{review-checkdep => review-checkdep-peg} (100%) rename bin/{review-compile => review-compile-peg} (100%) rename bin/{review-epubmaker-legacy => review-epubmaker-legacy-peg} (100%) rename bin/{review-epubmaker => review-epubmaker-peg} (100%) rename bin/{review-index => review-index-peg} (100%) rename bin/{review-init => review-init-peg} (100%) rename bin/{review-pdfmaker => review-pdfmaker-peg} (100%) rename bin/{review-preproc => review-preproc-peg} (100%) rename bin/{review-validate => review-validate-peg} (100%) rename bin/{review-vol => review-vol-peg} (100%) diff --git a/bin/review-catalog-converter b/bin/review-catalog-converter-peg similarity index 100% rename from bin/review-catalog-converter rename to bin/review-catalog-converter-peg diff --git a/bin/review-check b/bin/review-check-peg similarity index 100% rename from bin/review-check rename to bin/review-check-peg diff --git a/bin/review-checkdep b/bin/review-checkdep-peg similarity index 100% rename from bin/review-checkdep rename to bin/review-checkdep-peg diff --git a/bin/review-compile b/bin/review-compile-peg similarity index 100% rename from bin/review-compile rename to bin/review-compile-peg diff --git a/bin/review-epubmaker-legacy b/bin/review-epubmaker-legacy-peg similarity index 100% rename from bin/review-epubmaker-legacy rename to bin/review-epubmaker-legacy-peg diff --git a/bin/review-epubmaker b/bin/review-epubmaker-peg similarity index 100% rename from bin/review-epubmaker rename to bin/review-epubmaker-peg diff --git a/bin/review-index b/bin/review-index-peg similarity index 100% rename from bin/review-index rename to bin/review-index-peg diff --git a/bin/review-init b/bin/review-init-peg similarity index 100% rename from bin/review-init rename to bin/review-init-peg diff --git a/bin/review-pdfmaker b/bin/review-pdfmaker-peg similarity index 100% rename from bin/review-pdfmaker rename to bin/review-pdfmaker-peg diff --git a/bin/review-preproc b/bin/review-preproc-peg similarity index 100% rename from bin/review-preproc rename to bin/review-preproc-peg diff --git a/bin/review-validate b/bin/review-validate-peg similarity index 100% rename from bin/review-validate rename to bin/review-validate-peg diff --git a/bin/review-vol b/bin/review-vol-peg similarity index 100% rename from bin/review-vol rename to bin/review-vol-peg diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index ab6d008be..121d3367d 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -560,7 +560,7 @@ def inline_defined?(name) defcodeblock :emlist, 0..2, false, [:doc, :raw] defcodeblock :cmd, 0..1, false, [:doc] defcodeblock :source, 0..2, false, [:doc, :raw] - defcodeblock :list, 2..3, false, [:raw, :doc, :raw] + defcodeblock :list, 2..4, false, [:doc, :raw, :raw, :raw] defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index a6e65a1af..b4c9b70b9 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -291,7 +291,7 @@ def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil) end ENV["REVIEWFNAME"] = filename - system("#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=html --level=#{level} --htmlversion=#{@params["htmlversion"]} --epubversion=#{@params["epubversion"]} #{stylesheet} #{@params["params"]} #{filename} > \"#{basetmpdir}/#{htmlfile}\"") + system("#{ReVIEW::MakerHelper.bindir}/review-compile-peg --yaml=#{yamlfile} --target=html --level=#{level} --htmlversion=#{@params["htmlversion"]} --epubversion=#{@params["epubversion"]} #{stylesheet} #{@params["params"]} #{filename} > \"#{basetmpdir}/#{htmlfile}\"") write_info_body(basetmpdir, id, htmlfile, ispart, chaptype) end diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 6acd41eb8..2b24adc72 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -685,7 +685,8 @@ def inline_title(id) # FIXME: use TeX native label/ref. - def inline_list(id) + def node_inline_list(node) + id = node[0].to_raw chapter, id = extract_chapter_id(id) macro('reviewlistref', "#{chapter.number}.#{chapter.list(id).number}") end @@ -707,7 +708,8 @@ def footnote(id, content) end end - def inline_fn(id) + def node_inline_fn(node) + id = node[0].to_raw if @book.config["footnotetext"] macro("footnotemark[#{@chapter.footnote(id).number}]", "") else diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index 6b153740d..98bcfd7b9 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -193,7 +193,7 @@ def generate_pdf(yamlfile) def output_chaps(filename, yamlfile) $stderr.puts "compiling #{filename}.tex" - cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=latex --level=#{@config["secnolevel"]} --toclevel=#{@config["toclevel"]} #{@config["params"]} #{filename}.re > #{@path}/#{filename}.tex" + cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile-peg --yaml=#{yamlfile} --target=latex --level=#{@config["secnolevel"]} --toclevel=#{@config["toclevel"]} #{@config["params"]} #{filename}.re > #{@path}/#{filename}.tex" if system cmd # OK else diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index a53947352..a69dcaced 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -205,7 +205,7 @@ require 'review/node' defcodeblock :emlist, 0..2, false, [:doc, :raw] defcodeblock :cmd, 0..1, false, [:doc] defcodeblock :source, 0..2, false, [:doc, :raw] - defcodeblock :list, 2..3, false, [:raw, :doc, :raw] + defcodeblock :list, 2..4, false, [:doc, :raw, :raw, :raw] defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false diff --git a/test/sample-book/src/Rakefile b/test/sample-book/src/Rakefile index 11d4fc099..4b6f62c3d 100644 --- a/test/sample-book/src/Rakefile +++ b/test/sample-book/src/Rakefile @@ -5,16 +5,17 @@ BOOK = "book" BOOK_PDF = BOOK+".pdf" BOOK_EPUB = BOOK+".epub" CONFIG_FILE = "config.yml" +REVIEW_COMPILE = "review-compile-peg" def build(mode, chapter) - sh "review-compile --target=#{mode} --footnotetext --stylesheet=style.css #{chapter} > tmp" + sh "#{REVIEW_COMPILE} --target=#{mode} --footnotetext --stylesheet=style.css #{chapter} > tmp" mode_ext = {"html" => "html", "latex" => "tex", "idgxml" => "xml", "inao" => "inao"} FileUtils.mv "tmp", chapter.gsub(/re\z/, mode_ext[mode]) end def build_all(mode) - sh "review-compile --all --target=#{mode} --footnotetext --stylesheet=style.css" + sh "#{REVIEW_COMPILE} --all --target=#{mode} --footnotetext --stylesheet=style.css" end task :default => :html_all From af7537894c2766670443f665571c63a22f9db670 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 02:03:01 +0900 Subject: [PATCH 172/214] remove rescue to debug easily some rescues hide detail of errors :( --- bin/review-compile-peg | 12 ++++++------ lib/review/review.kpeg | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bin/review-compile-peg b/bin/review-compile-peg index bdc9d6d69..431476713 100755 --- a/bin/review-compile-peg +++ b/bin/review-compile-peg @@ -136,7 +136,7 @@ def _main exit 1 end - begin +## begin config["builder"] = target ReVIEW::I18n.setup(config["language"]) @@ -177,11 +177,11 @@ def _main else raise "must not happen: #{mode}" end - rescue ReVIEW::ApplicationError => err - raise if $DEBUG - error err.message - exit 1 - end +# rescue ReVIEW::ApplicationError => err +# raise if $DEBUG +# error err.message +# exit 1 +# end end def error(msg) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index a69dcaced..408bac44d 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -447,8 +447,8 @@ require 'review/node' else @strategy.__send__("inline_#{op}", *(args.map(&:to_doc))) end - rescue => err - error err.message +# rescue => err +# error err.message end def compile_paragraph(buf) From 55ec89de84f4e8519f7b5ff3c70c37259b0acf11 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 02:05:22 +0900 Subject: [PATCH 173/214] inline args should not be escaped --- lib/review/builder.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 941d926f0..b291ff71e 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -193,42 +193,48 @@ def adjust_n_cols(rows) # @compiler.text(s) # end - def inline_chapref(id) + def node_inline_chapref(nodelist) + id = nodelist[0].to_raw @book.chapter_index.display_string(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def inline_chap(id) + def node_inline_chap(nodelist) + id = nodelist[0].to_raw @book.chapter_index.number(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def inline_title(id) + def node_inline_title(nodelist) + id = nodelist[0].to_raw @book.chapter_index.title(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def inline_list(id) + def node_inline_list(nodelist) + id = nodelist[0].to_raw "#{I18n.t("list")}#{@chapter.list(id).number}" rescue KeyError error "unknown list: #{id}" nofunc_text("[UnknownList:#{id}]") end - def inline_img(id) + def node_inline_img(nodelist) + id = nodelist[0].to_raw "#{I18n.t("image")}#{@chapter.image(id).number}" rescue KeyError error "unknown image: #{id}" nofunc_text("[UnknownImage:#{id}]") end - def inline_imgref(id) + def node_inline_imgref(nodelist) + id = nodelist[0].to_raw img = inline_img(id) if @chapter.image(id).caption @@ -238,14 +244,16 @@ def inline_imgref(id) end end - def inline_table(id) + def node_inline_table(nodelist) + id = nodelist[0].to_raw "#{I18n.t("table")}#{@chapter.table(id).number}" rescue KeyError error "unknown table: #{id}" nofunc_text("[UnknownTable:#{id}]") end - def inline_fn(id) + def node_inline_fn(nodelist) + id = nodelist[0].to_raw @chapter.footnote(id).content rescue KeyError error "unknown footnote: #{id}" @@ -293,7 +301,8 @@ def node_inline_hd(nodelist) return inline_hd_chap(@chapter, id) end - def inline_column(id) + def node_inline_column(nodelist) + id = nodelist[0].to_raw @chapter.column(id).caption rescue error "unknown column: #{id}" From a5857a37b8d77bc225ced7defd75894f8478496c Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 02:05:55 +0900 Subject: [PATCH 174/214] fix escaping arguments --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 408bac44d..f7afcc35c 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -205,7 +205,7 @@ require 'review/node' defcodeblock :emlist, 0..2, false, [:doc, :raw] defcodeblock :cmd, 0..1, false, [:doc] defcodeblock :source, 0..2, false, [:doc, :raw] - defcodeblock :list, 2..4, false, [:doc, :raw, :raw, :raw] + defcodeblock :list, 2..4, false, [:raw, :doc, :raw, :raw] defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false From c43eb129287dd815eef195df447557f7a6b4607a Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 02:06:36 +0900 Subject: [PATCH 175/214] update compiler --- lib/review/compiler.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 121d3367d..395f0c282 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -560,7 +560,7 @@ def inline_defined?(name) defcodeblock :emlist, 0..2, false, [:doc, :raw] defcodeblock :cmd, 0..1, false, [:doc] defcodeblock :source, 0..2, false, [:doc, :raw] - defcodeblock :list, 2..4, false, [:doc, :raw, :raw, :raw] + defcodeblock :list, 2..4, false, [:raw, :doc, :raw, :raw] defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false @@ -802,8 +802,8 @@ def compile_inline(op, args) else @strategy.__send__("inline_#{op}", *(args.map(&:to_doc))) end - rescue => err - error err.message +# rescue => err +# error err.message end def compile_paragraph(buf) From 9efc04c58870e9d9cc25be76081246bff0cc0dc6 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 02:07:39 +0900 Subject: [PATCH 176/214] using part is not error --- lib/review/book/index.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/review/book/index.rb b/lib/review/book/index.rb index e41253a32..ee129c978 100644 --- a/lib/review/book/index.rb +++ b/lib/review/book/index.rb @@ -91,10 +91,14 @@ def item_type end def number(id) - chapter = @index.fetch(id) - chapter.format_number + chapter = @index.fetch(id, nil) + if chapter + return chapter.format_number + else + return "#{I18n.t("part", chapter.number)}" + end rescue # part - "#{I18n.t("part", chapter.number)}" + raise "invalid number error: id:#{id}, capter:#{chapter}" end def title(id) From a4ea065925b329d30a7ef872ea647c66f0e8cad7 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 15:57:52 +0900 Subject: [PATCH 177/214] Revert "inline args should not be escaped" This reverts commit 55ec89de84f4e8519f7b5ff3c70c37259b0acf11. --- lib/review/builder.rb | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index b291ff71e..941d926f0 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -193,48 +193,42 @@ def adjust_n_cols(rows) # @compiler.text(s) # end - def node_inline_chapref(nodelist) - id = nodelist[0].to_raw + def inline_chapref(id) @book.chapter_index.display_string(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def node_inline_chap(nodelist) - id = nodelist[0].to_raw + def inline_chap(id) @book.chapter_index.number(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def node_inline_title(nodelist) - id = nodelist[0].to_raw + def inline_title(id) @book.chapter_index.title(id) rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end - def node_inline_list(nodelist) - id = nodelist[0].to_raw + def inline_list(id) "#{I18n.t("list")}#{@chapter.list(id).number}" rescue KeyError error "unknown list: #{id}" nofunc_text("[UnknownList:#{id}]") end - def node_inline_img(nodelist) - id = nodelist[0].to_raw + def inline_img(id) "#{I18n.t("image")}#{@chapter.image(id).number}" rescue KeyError error "unknown image: #{id}" nofunc_text("[UnknownImage:#{id}]") end - def node_inline_imgref(nodelist) - id = nodelist[0].to_raw + def inline_imgref(id) img = inline_img(id) if @chapter.image(id).caption @@ -244,16 +238,14 @@ def node_inline_imgref(nodelist) end end - def node_inline_table(nodelist) - id = nodelist[0].to_raw + def inline_table(id) "#{I18n.t("table")}#{@chapter.table(id).number}" rescue KeyError error "unknown table: #{id}" nofunc_text("[UnknownTable:#{id}]") end - def node_inline_fn(nodelist) - id = nodelist[0].to_raw + def inline_fn(id) @chapter.footnote(id).content rescue KeyError error "unknown footnote: #{id}" @@ -301,8 +293,7 @@ def node_inline_hd(nodelist) return inline_hd_chap(@chapter, id) end - def node_inline_column(nodelist) - id = nodelist[0].to_raw + def inline_column(id) @chapter.column(id).caption rescue error "unknown column: #{id}" From f366b63ce18678045167a837b17601957a879dad Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:03:42 +0900 Subject: [PATCH 178/214] add postfix "-peg" --- test/test_catalog_converter_cmd.rb | 2 +- test/test_epubmaker_cmd.rb | 2 +- test/test_pdfmaker_cmd.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_catalog_converter_cmd.rb b/test/test_catalog_converter_cmd.rb index ab240ad7d..ed65f91e7 100644 --- a/test/test_catalog_converter_cmd.rb +++ b/test/test_catalog_converter_cmd.rb @@ -4,7 +4,7 @@ require 'yaml' require 'rbconfig' -load File.expand_path('../bin/review-catalog-converter', File.dirname(__FILE__)) +load File.expand_path('../bin/review-catalog-converter-peg', File.dirname(__FILE__)) class CatalogConverterCmdTest < Test::Unit::TestCase def test_parse_chaps diff --git a/test/test_epubmaker_cmd.rb b/test/test_epubmaker_cmd.rb index 2e1fcafc8..4ddf1db45 100644 --- a/test/test_epubmaker_cmd.rb +++ b/test/test_epubmaker_cmd.rb @@ -6,7 +6,7 @@ require 'yaml' require 'rbconfig' -REVIEW_EPUBMAKER = File.expand_path('../bin/review-epubmaker', File.dirname(__FILE__)) +REVIEW_EPUBMAKER = File.expand_path('../bin/review-epubmaker-peg', File.dirname(__FILE__)) class EPUBMakerCmdTest < Test::Unit::TestCase def setup diff --git a/test/test_pdfmaker_cmd.rb b/test/test_pdfmaker_cmd.rb index db6ec8e39..9b16b6a01 100644 --- a/test/test_pdfmaker_cmd.rb +++ b/test/test_pdfmaker_cmd.rb @@ -6,7 +6,7 @@ require 'yaml' require 'rbconfig' -REVIEW_PDFMAKER = File.expand_path('../bin/review-pdfmaker', File.dirname(__FILE__)) +REVIEW_PDFMAKER = File.expand_path('../bin/review-pdfmaker-peg', File.dirname(__FILE__)) class PDFMakerCmdTest < Test::Unit::TestCase def setup From 68d791a12415fb4ce324f5cdcec9c84db0677368 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:06:14 +0900 Subject: [PATCH 179/214] fix lost argument --- lib/review/review.kpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index f7afcc35c..d322633ff 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -209,7 +209,7 @@ require 'review/node' defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false - defcodeblock :table, 0..2, [:raw, :doc] + defcodeblock :table, 0..2, false, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] defcodeblock :box, 0..1, false, [:doc] From 34a156c77ad6089ae73527c8dcdf1c9a4c254b96 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:11:08 +0900 Subject: [PATCH 180/214] add ComplexInlineElementNode --- lib/review/node.rb | 21 +++++++++++++++++++++ lib/review/review.kpeg | 34 ++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/review/node.rb b/lib/review/node.rb index 75eb61c03..87cb89517 100644 --- a/lib/review/node.rb +++ b/lib/review/node.rb @@ -128,7 +128,25 @@ def raw_lines class InlineElementNode < Node + def to_raw + content_str = super + "@<#{@symbol}>{#{content_str}}" + end + + def to_doc + #content_str = super + @compiler.compile_inline(@symbol, @content) + end + def to_json + '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' + + %Q|"symbol":"#{@symbol}",| + + "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," + + '"childNodes":[' + @content.map(&:to_json).join(",") + ']}' + end + end + + class ComplexInlineElementNode < Node def to_raw content_str = super "@<#{@symbol}>{#{content_str}}" @@ -150,6 +168,9 @@ def to_json class InlineElementContentNode < Node end + class ComplexInlineElementContentNode < Node + end + class TextNode < Node def to_raw diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index d322633ff..1eefe8319 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -185,13 +185,18 @@ require 'review/node' end INLINE = {} + COMPLEX_INLINE = {} def self.definline(name) INLINE[name] = InlineSyntaxElement.new(name) end + def self.defcomplexinline(name) + COMPLEX_INLINE[name] = InlineSyntaxElement.new(name) + end + def inline_defined?(name) - INLINE.key?(name.to_sym) + INLINE.key?(name.to_sym) || COMPLEX_INLINE.key?(name.to_sym) end defblock :read, 0 @@ -244,8 +249,6 @@ require 'review/node' definline :list definline :table definline :fn - definline :kw - definline :ruby definline :bou definline :ami definline :b @@ -253,7 +256,6 @@ require 'review/node' definline :code definline :bib definline :hd - definline :href definline :recipe definline :column @@ -287,6 +289,9 @@ require 'review/node' definline :comment definline :include + defcomplexinline :kw + defcomplexinline :ruby + defcomplexinline :href def compile_column(level, label, caption, content) buf = "" @@ -480,6 +485,14 @@ require 'review/node' s.size >= @list_stack.last.size + 2 end + def check_inline_element_symbol(name) + INLINE.key?(name.to_sym) + end + + def check_complex_inline_element_symbol(name) + COMPLEX_INLINE.key?(name.to_sym) + end + def position Position.new(self) end @@ -494,6 +507,8 @@ require 'review/node' %% code_block_element = ast CodeBlockElementNode(compiler, position, name, args, content) %% inline_element = ast InlineElementNode(compiler, position, symbol, content) %% inline_element_content = ast InlineElementContentNode(compiler, position, content) +%% complex_inline_element = ast ComplexInlineElementNode(compiler, position, symbol, content) +%% complex_inline_element_content = ast ComplexInlineElementContentNode(compiler, position, content) %% text = ast TextNode(compiler, position, content) %% raw = ast RawNode(compiler, builder, position, content) %% brace = ast BraceNode(compiler, position, content) @@ -660,6 +675,7 @@ NonInlineElement = !InlineElement < NonNewline > ~text(self, position, text) InlineElement = ( RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" ~inline_element(self, position, symbol,contents) + | !RawInlineElement "@<" ComplexInlineElementSymbol:symbol ">" "{" ComplexInlineElementContents?:contents "}" ~complex_inline_element(self, position, symbol,contents) ) RawInlineElement = "@{" RawBlockBuilderSelect?:builders RawInlineElementContent+:c "}" ~raw(self, builders, position, c) @@ -668,11 +684,17 @@ RawInlineElementContent = ( "\\}" { "}" } | < /[^\r\n\}]/ > { text } ) -InlineElementSymbol = < AlphanumericAscii+ > { text } +InlineElementSymbol = < AlphanumericAscii+ >:s &{ check_inline_element_symbol(text) } { text } InlineElementContents = !"}" InlineElementContentsSub:c { c } -InlineElementContentsSub = !"}" ( Space* InlineElementContent:c1 Space* "," InlineElementContentsSub:c2 { [c1]+c2 } +InlineElementContentsSub = !"}" Space* InlineElementContent:c1 Space* { [c1] } + +ComplexInlineElementSymbol = < AlphanumericAscii+ > &{ check_complex_inline_element_symbol(text) } { text } + +ComplexInlineElementContents = !"}" ComplexInlineElementContentsSub:c { c } + +ComplexInlineElementContentsSub = !"}" ( Space* InlineElementContent:c1 Space* "," ComplexInlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] } ) From 58a2a93ed4b057b2732c130bdf23dec331adaadd Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:12:41 +0900 Subject: [PATCH 181/214] IDGXMLBuilder: remote escape_html escaping should be done in Compiler, not in Builder. --- lib/review/idgxmlbuilder.rb | 38 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index e77f61440..b5d5b0561 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -676,15 +676,15 @@ def compile_kw(word, alt) end def compile_href(url, label) - %Q[#{label.nil? ? escape_html(url) : escape_html(label)}] + %Q[#{label.nil? ? url : label}] end def inline_sup(str) - %Q[#{escape_html(str)}] + %Q[#{str}] end def inline_sub(str) - %Q[#{escape_html(str)}] + %Q[#{str}] end def inline_raw(str) @@ -693,9 +693,9 @@ def inline_raw(str) def inline_hint(str) if @book.config["nolf"].nil? - %Q[\n#{escape_html(str)}] + %Q[\n#{str}] else - %Q[#{escape_html(str)}] + %Q[#{str}] end end @@ -720,23 +720,23 @@ def inline_maru(str) end def inline_idx(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_hidx(str) - %Q() + %Q() end def inline_ami(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_i(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_b(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_tt(str) @@ -750,11 +750,11 @@ def inline_ttb(str) alias_method :inline_ttbold, :inline_ttb def inline_tti(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_u(str) - %Q(#{escape_html(str)}) + %Q(#{str}) end def inline_icon(id) @@ -767,11 +767,11 @@ def inline_icon(id) end def inline_bou(str) - %Q[#{escape_html(str)}] + %Q[#{str}] end def inline_keytop(str) - %Q[#{escape_html(str)}] + %Q[#{str}] end def inline_labelref(idref) @@ -785,7 +785,7 @@ def inline_pageref(idref) end def inline_balloon(str) - %Q[#{escape_html(str).gsub(/@maru\[(\d+)\]/) {|m| inline_maru($1)}}] + %Q[#{str.gsub(/@maru\[(\d+)\]/) {|m| inline_maru($1)}}] end def inline_uchar(str) @@ -794,7 +794,7 @@ def inline_uchar(str) def inline_m(str) @texinlineequation += 1 - %Q[
      #{escape_html(str)}
      ] + %Q[
      #{str}
      ] end def noindent @@ -812,7 +812,7 @@ def pagebreak end def nonum_begin(level, label, caption) - %Q[#{caption}] + @lf + %Q[#{caption}] + @lf end def nonum_end(level) @@ -1098,7 +1098,7 @@ def inline_dtp(str) end def inline_code(str) - %Q[#{escape_html(str)}] + %Q[#{str}] end def inline_br(str) @@ -1217,7 +1217,7 @@ def inline_hd_chap(chap, id) def inline_recipe(id) # FIXME - %Q([XXX]「#{escape_html(id)}」 p.XX) + %Q([XXX]「#{id}」 p.XX) end def nofunc_text(str) From 300635897e1367b6c92204e2d3226b245be0b0f8 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:13:36 +0900 Subject: [PATCH 182/214] regenerate Compiler --- lib/review/compiler.rb | 260 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 247 insertions(+), 13 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 395f0c282..f3169a1e1 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -540,13 +540,18 @@ def initialize(name) end INLINE = {} + COMPLEX_INLINE = {} def self.definline(name) INLINE[name] = InlineSyntaxElement.new(name) end + def self.defcomplexinline(name) + COMPLEX_INLINE[name] = InlineSyntaxElement.new(name) + end + def inline_defined?(name) - INLINE.key?(name.to_sym) + INLINE.key?(name.to_sym) || COMPLEX_INLINE.key?(name.to_sym) end defblock :read, 0 @@ -564,7 +569,7 @@ def inline_defined?(name) defcodeblock :listnum, 2..3, false, [:raw, :doc, :raw] defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false - defcodeblock :table, 0..2, [:raw, :doc] + defcodeblock :table, 0..2, false, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] defcodeblock :box, 0..1, false, [:doc] @@ -599,8 +604,6 @@ def inline_defined?(name) definline :list definline :table definline :fn - definline :kw - definline :ruby definline :bou definline :ami definline :b @@ -608,7 +611,6 @@ def inline_defined?(name) definline :code definline :bib definline :hd - definline :href definline :recipe definline :column @@ -642,6 +644,9 @@ def inline_defined?(name) definline :comment definline :include + defcomplexinline :kw + defcomplexinline :ruby + defcomplexinline :href def compile_column(level, label, caption, content) buf = "" @@ -835,6 +840,14 @@ def check_nested_indent(s) s.size >= @list_stack.last.size + 2 end + def check_inline_element_symbol(name) + INLINE.key?(name.to_sym) + end + + def check_complex_inline_element_symbol(name) + COMPLEX_INLINE.key?(name.to_sym) + end + def position Position.new(self) end @@ -910,6 +923,28 @@ def initialize(compiler, position, level, label, caption, content) attr_reader :caption attr_reader :content end + class ComplexInlineElementNode < Node + def initialize(compiler, position, symbol, content) + @compiler = compiler + @position = position + @symbol = symbol + @content = content + end + attr_reader :compiler + attr_reader :position + attr_reader :symbol + attr_reader :content + end + class ComplexInlineElementContentNode < Node + def initialize(compiler, position, content) + @compiler = compiler + @position = position + @content = content + end + attr_reader :compiler + attr_reader :position + attr_reader :content + end class DlistNode < Node def initialize(compiler, position, content) @compiler = compiler @@ -1103,6 +1138,12 @@ def code_block_element(compiler, position, name, args, content) def column(compiler, position, level, label, caption, content) ::ReVIEW::ColumnNode.new(compiler, position, level, label, caption, content) end + def complex_inline_element(compiler, position, symbol, content) + ::ReVIEW::ComplexInlineElementNode.new(compiler, position, symbol, content) + end + def complex_inline_element_content(compiler, position, content) + ::ReVIEW::ComplexInlineElementContentNode.new(compiler, position, content) + end def dlist(compiler, position, content) ::ReVIEW::DlistNode.new(compiler, position, content) end @@ -3915,7 +3956,7 @@ def _NonInlineElement return _tmp end - # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" {inline_element(self, position, symbol,contents)}) + # InlineElement = (RawInlineElement:c { c } | !RawInlineElement "@<" InlineElementSymbol:symbol ">" "{" InlineElementContents?:contents "}" {inline_element(self, position, symbol,contents)} | !RawInlineElement "@<" ComplexInlineElementSymbol:symbol ">" "{" ComplexInlineElementContents?:contents "}" {complex_inline_element(self, position, symbol,contents)}) def _InlineElement _save = self.pos @@ -3996,6 +4037,65 @@ def _InlineElement break end # end sequence + break if _tmp + self.pos = _save + + _save5 = self.pos + while true # sequence + _save6 = self.pos + _tmp = apply(:_RawInlineElement) + _tmp = _tmp ? nil : true + self.pos = _save6 + unless _tmp + self.pos = _save5 + break + end + _tmp = match_string("@<") + unless _tmp + self.pos = _save5 + break + end + _tmp = apply(:_ComplexInlineElementSymbol) + symbol = @result + unless _tmp + self.pos = _save5 + break + end + _tmp = match_string(">") + unless _tmp + self.pos = _save5 + break + end + _tmp = match_string("{") + unless _tmp + self.pos = _save5 + break + end + _save7 = self.pos + _tmp = apply(:_ComplexInlineElementContents) + @result = nil unless _tmp + unless _tmp + _tmp = true + self.pos = _save7 + end + contents = @result + unless _tmp + self.pos = _save5 + break + end + _tmp = match_string("}") + unless _tmp + self.pos = _save5 + break + end + @result = begin; complex_inline_element(self, position, symbol,contents); end + _tmp = true + unless _tmp + self.pos = _save5 + end + break + end # end sequence + break if _tmp self.pos = _save break @@ -4116,7 +4216,7 @@ def _RawInlineElementContent return _tmp end - # InlineElementSymbol = < AlphanumericAscii+ > { text } + # InlineElementSymbol = < AlphanumericAscii+ >:s &{ check_inline_element_symbol(text) } { text } def _InlineElementSymbol _save = self.pos @@ -4136,6 +4236,14 @@ def _InlineElementSymbol if _tmp text = get_text(_text_start) end + s = @result + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = begin; check_inline_element_symbol(text) ; end + self.pos = _save2 unless _tmp self.pos = _save break @@ -4183,9 +4291,132 @@ def _InlineElementContents return _tmp end - # InlineElementContentsSub = !"}" (Space* InlineElementContent:c1 Space* "," InlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] }) + # InlineElementContentsSub = !"}" Space* InlineElementContent:c1 Space* { [c1] } def _InlineElementContentsSub + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_InlineElementContent) + c1 = @result + unless _tmp + self.pos = _save + break + end + while true + _tmp = apply(:_Space) + break unless _tmp + end + _tmp = true + unless _tmp + self.pos = _save + break + end + @result = begin; [c1] ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_InlineElementContentsSub unless _tmp + return _tmp + end + + # ComplexInlineElementSymbol = < AlphanumericAscii+ > &{ check_complex_inline_element_symbol(text) } { text } + def _ComplexInlineElementSymbol + + _save = self.pos + while true # sequence + _text_start = self.pos + _save1 = self.pos + _tmp = apply(:_AlphanumericAscii) + if _tmp + while true + _tmp = apply(:_AlphanumericAscii) + break unless _tmp + end + _tmp = true + else + self.pos = _save1 + end + if _tmp + text = get_text(_text_start) + end + unless _tmp + self.pos = _save + break + end + _save2 = self.pos + _tmp = begin; check_complex_inline_element_symbol(text) ; end + self.pos = _save2 + unless _tmp + self.pos = _save + break + end + @result = begin; text ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_ComplexInlineElementSymbol unless _tmp + return _tmp + end + + # ComplexInlineElementContents = !"}" ComplexInlineElementContentsSub:c { c } + def _ComplexInlineElementContents + + _save = self.pos + while true # sequence + _save1 = self.pos + _tmp = match_string("}") + _tmp = _tmp ? nil : true + self.pos = _save1 + unless _tmp + self.pos = _save + break + end + _tmp = apply(:_ComplexInlineElementContentsSub) + c = @result + unless _tmp + self.pos = _save + break + end + @result = begin; c ; end + _tmp = true + unless _tmp + self.pos = _save + end + break + end # end sequence + + set_failed_rule :_ComplexInlineElementContents unless _tmp + return _tmp + end + + # ComplexInlineElementContentsSub = !"}" (Space* InlineElementContent:c1 Space* "," ComplexInlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] }) + def _ComplexInlineElementContentsSub + _save = self.pos while true # sequence _save1 = self.pos @@ -4231,7 +4462,7 @@ def _InlineElementContentsSub self.pos = _save3 break end - _tmp = apply(:_InlineElementContentsSub) + _tmp = apply(:_ComplexInlineElementContentsSub) c2 = @result unless _tmp self.pos = _save3 @@ -4293,7 +4524,7 @@ def _InlineElementContentsSub break end # end sequence - set_failed_rule :_InlineElementContentsSub unless _tmp + set_failed_rule :_ComplexInlineElementContentsSub unless _tmp return _tmp end @@ -4921,12 +5152,15 @@ def _Spacechar Rules[:_SinglelineContent] = rule_info("SinglelineContent", "Inline+:c {singleline_content(self, position, c)}") Rules[:_Inline] = rule_info("Inline", "(InlineElement | NonInlineElement)") Rules[:_NonInlineElement] = rule_info("NonInlineElement", "!InlineElement < NonNewline > {text(self, position, text)}") - Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" {inline_element(self, position, symbol,contents)})") + Rules[:_InlineElement] = rule_info("InlineElement", "(RawInlineElement:c { c } | !RawInlineElement \"@<\" InlineElementSymbol:symbol \">\" \"{\" InlineElementContents?:contents \"}\" {inline_element(self, position, symbol,contents)} | !RawInlineElement \"@<\" ComplexInlineElementSymbol:symbol \">\" \"{\" ComplexInlineElementContents?:contents \"}\" {complex_inline_element(self, position, symbol,contents)})") Rules[:_RawInlineElement] = rule_info("RawInlineElement", "\"@{\" RawBlockBuilderSelect?:builders RawInlineElementContent+:c \"}\" {raw(self, builders, position, c)}") Rules[:_RawInlineElementContent] = rule_info("RawInlineElementContent", "(\"\\\\}\" { \"}\" } | < /[^\\r\\n\\}]/ > { text })") - Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ > { text }") + Rules[:_InlineElementSymbol] = rule_info("InlineElementSymbol", "< AlphanumericAscii+ >:s &{ check_inline_element_symbol(text) } { text }") Rules[:_InlineElementContents] = rule_info("InlineElementContents", "!\"}\" InlineElementContentsSub:c { c }") - Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" (Space* InlineElementContent:c1 Space* \",\" InlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] })") + Rules[:_InlineElementContentsSub] = rule_info("InlineElementContentsSub", "!\"}\" Space* InlineElementContent:c1 Space* { [c1] }") + Rules[:_ComplexInlineElementSymbol] = rule_info("ComplexInlineElementSymbol", "< AlphanumericAscii+ > &{ check_complex_inline_element_symbol(text) } { text }") + Rules[:_ComplexInlineElementContents] = rule_info("ComplexInlineElementContents", "!\"}\" ComplexInlineElementContentsSub:c { c }") + Rules[:_ComplexInlineElementContentsSub] = rule_info("ComplexInlineElementContentsSub", "!\"}\" (Space* InlineElementContent:c1 Space* \",\" ComplexInlineElementContentsSub:c2 { [c1]+c2 } | Space* InlineElementContent:c1 Space* { [c1] })") Rules[:_InlineElementContent] = rule_info("InlineElementContent", "InlineElementContentSub+:d { d }") Rules[:_InlineElementContentSub] = rule_info("InlineElementContentSub", "(InlineElement:c { c } | !InlineElement QuotedInlineText:content {inline_element_content(self, position, content)} | !InlineElement InlineElementContentText+:content {inline_element_content(self, position, content)})") Rules[:_QuotedInlineText] = rule_info("QuotedInlineText", "\"\\\"\" (\"\\\\\\\"\" { \"\\\"\" } | \"\\\\\\\\\" { \"\\\\\" } | < /[^\"\\r\\n\\\\]/ > { text })+:str \"\\\"\" {text(self, position, str.join(\"\"))}") From 4bf93f2ee8ca254fcb35c42419450b6305cf362c Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 1 Nov 2015 16:21:06 +0900 Subject: [PATCH 183/214] fix test; add definline directive --- test/test_compiler.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_compiler.rb b/test/test_compiler.rb index 01a6b1a07..cd31cf379 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -84,6 +84,7 @@ def test_compile_inline_backslash def @compiler.inline_dummy(*args) return args end + Compiler.definline :dummy args = compile_inline("@{abc\\d\\#a}", false) assert_equal "abc\\d\\#a", args.content[0].content.to_doc end From 2a5512875b5ef799750e6da1c1bae8f6ea087911 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 4 Nov 2015 15:25:50 +0900 Subject: [PATCH 184/214] HTMLBuilder: fix texequation --- lib/review/htmlbuilder.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index f6ac536f7..1a1143465 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -581,13 +581,14 @@ def talk(lines) buf end - def texequation(lines) + def node_texequation(node) + buf = "" buf << %Q[
      \n] if @book.config["mathml"] require 'math_ml' require 'math_ml/symbol/character_reference' p = MathML::LaTeX::Parser.new(:symbol=>MathML::Symbol::CharacterReference) - buf << p.parse(unescape_html(lines.join("\n")), true) << "\n" + buf << p.parse(node.to_raw, true).to_s << "\n" else buf << '
      '
               buf << lines.join("\n") << "\n"
      
      From 5168412842bd708174af5a4f954b810252662e6d Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Wed, 4 Nov 2015 15:26:09 +0900
      Subject: [PATCH 185/214] bump up
      
      ---
       lib/review/version.rb | 2 +-
       1 file changed, 1 insertion(+), 1 deletion(-)
      
      diff --git a/lib/review/version.rb b/lib/review/version.rb
      index cd148068f..175d5c34c 100644
      --- a/lib/review/version.rb
      +++ b/lib/review/version.rb
      @@ -1,3 +1,3 @@
       module ReVIEW
      -  VERSION = "0.1.0"
      +  VERSION = "0.1.1"
       end
      
      From fbb98da4f57f87a7cfed650fc5ebadad1193ebcc Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Wed, 4 Nov 2015 15:28:37 +0900
      Subject: [PATCH 186/214] fix gem description
      
      ---
       review.gemspec | 2 +-
       1 file changed, 1 insertion(+), 1 deletion(-)
      
      diff --git a/review.gemspec b/review.gemspec
      index ba785f77d..57ff27118 100644
      --- a/review.gemspec
      +++ b/review.gemspec
      @@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
         gem.email       = "kmuto@debian.org"
         gem.homepage    = "http://github.com/kmuto/review"
         gem.summary     = "Re:VIEW peg: a easy-to-use digital publishing system (PEG version)"
      -  gem.description = "Re:VIEW is a digital publishing system for books and ebooks. It supports InDesign, EPUB and LaTeX."
      +  gem.description = "Re:VIEW is a digital publishing system for books and ebooks. It supports InDesign, EPUB and LaTeX.  This gem is experimental version with PEG grammar"
         gem.required_rubygems_version = Gem::Requirement.new(">= 0") if gem.respond_to? :required_rubygems_version=
       
         gem.files         = `git ls-files`.split("\n")
      
      From 3d4a22ef980702ba2c7964d100b5aa6a97fa4a00 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Tue, 10 Nov 2015 03:31:32 +0900
      Subject: [PATCH 187/214] fix invalid JSON generation
      
      ---
       lib/review/node.rb | 2 +-
       1 file changed, 1 insertion(+), 1 deletion(-)
      
      diff --git a/lib/review/node.rb b/lib/review/node.rb
      index 87cb89517..2d9fe6dcd 100644
      --- a/lib/review/node.rb
      +++ b/lib/review/node.rb
      @@ -142,7 +142,7 @@ def to_json
             '{"ruleName":"' + self.class.to_s.sub(/ReVIEW::/,"").sub(/Node$/,"") + '",' +
               %Q|"symbol":"#{@symbol}",| +
               "\"offset\":#{position.pos},\"line\":#{position.line},\"column\":#{position.col}," +
      -        '"childNodes":[' + @content.map(&:to_json).join(",") + ']}'
      +        (@concat ? '"childNodes":[' + @content.map(&:to_json).join(",") + ']' : '"childNodes":[]') + '}'
           end
         end
       
      
      From 6b3156a7f919a1fc4959bd5bab5c3fa5f4bab447 Mon Sep 17 00:00:00 2001
      From: takahashim 
      Date: Tue, 10 Nov 2015 03:33:29 +0900
      Subject: [PATCH 188/214] LATEXBuilder: //table should use AST
      
      fix `@
      {}` in table --- lib/review/latexbuilder.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 2b24adc72..21359d9b9 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -459,7 +459,13 @@ def indepimage(id, caption=nil, metric=nil) alias_method :numberlessimage, :indepimage - def table(lines, id = nil, caption = nil) + def node_table(node) + id = node.args[0].to_doc + caption = node.args[1].to_doc + lines = [] + node.content.each do |line| + lines << line.to_doc + end buf = "" rows = [] sepidx = nil @@ -549,7 +555,7 @@ def td(s) def tr(rows) buf = "" - buf << rows.join(' & ') << "\n" + buf << rows.join(' & ') buf << ' \\\\ \hline' << "\n" buf end From 34082a4ccc224a0b797532e82480fd372eda8fce Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 10 Nov 2015 03:35:39 +0900 Subject: [PATCH 189/214] bump up --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index 175d5c34c..213a720be 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "0.1.1" + VERSION = "0.1.2" end From 78ed9a56fb28fbb1946e18381f3d1e0370f33185 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 10 Nov 2015 09:33:04 +0900 Subject: [PATCH 190/214] LATEXBuilder: id should not be escaped --- lib/review/latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 21359d9b9..2c847dbd1 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -460,7 +460,7 @@ def indepimage(id, caption=nil, metric=nil) alias_method :numberlessimage, :indepimage def node_table(node) - id = node.args[0].to_doc + id = node.args[0].to_raw caption = node.args[1].to_doc lines = [] node.content.each do |line| From b0549fde277fc1b7b20c66ea6e3f111066334ffa Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 10 Nov 2015 09:33:40 +0900 Subject: [PATCH 191/214] bump up --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index 213a720be..adb3ec43f 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "0.1.2" + VERSION = "0.1.3" end From 267ffb7c82296e22bcfc0707e9fe0cccfad9fb9b Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 12 Nov 2015 15:54:25 +0900 Subject: [PATCH 192/214] fix @ and @ to unescape id --- lib/review/latexbuilder.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 2c847dbd1..7242a6d2e 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -697,12 +697,14 @@ def node_inline_list(node) macro('reviewlistref', "#{chapter.number}.#{chapter.list(id).number}") end - def inline_table(id) + def node_inline_table(node) + id = node[0].to_raw chapter, id = extract_chapter_id(id) macro('reviewtableref', "#{chapter.number}.#{chapter.table(id).number}", table_label(id, chapter)) end - def inline_img(id) + def node_inline_img(node) + id = node[0].to_raw chapter, id = extract_chapter_id(id) macro('reviewimageref', "#{chapter.number}.#{chapter.image(id).number}", image_label(id, chapter)) end From b68f1d8737367aac9d0d7b74a7224316b1ca1d7c Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Nov 2015 00:54:21 +0900 Subject: [PATCH 193/214] perfomance tuning * fix kpeg * include literal_1_9 --- lib/review/compiler.rb | 95 ++++++++++++----------------- lib/review/compiler/literals_1_9.rb | 33 ++++++---- lib/review/review.kpeg | 21 ++++--- 3 files changed, 69 insertions(+), 80 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index f3169a1e1..22cbd5e93 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -1,3 +1,4 @@ +# coding: UTF-8 class ReVIEW::Compiler # :stopdoc: @@ -35,22 +36,16 @@ def current_column(target=pos) end def current_line(target=pos) - cur_offset = 0 - cur_line = 0 - - string.each_line do |line| - cur_line += 1 - cur_offset += line.size - return cur_line if cur_offset >= target - end + offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} + if offset + return offset[0] + end -1 end def lines - lines = [] - string.each_line { |l| lines << l } - lines + @lines_memo end @@ -61,9 +56,22 @@ def get_text(start) # Sets the string and current parsing position for the parser. def set_string string, pos - @string = string - @string_size = string ? string.size : 0 + @string = string.freeze @pos = pos + @sizes_memo = [] + if string + @string_size = string.size + @lines_memo = string.lines + cur_offset = cur_line = 0 + @lines_memo.each do |line| + cur_line += 1 + cur_offset += line.size + @sizes_memo << [cur_line, cur_offset] + end + else + @string_size = 0 + @lines_memo = [] + end end def show_pos @@ -373,7 +381,7 @@ def initialize(compiler) require 'review/exception' require 'review/node' require 'lineinput' - require 'review/compiler/literals_1_9' + # require 'review/compiler/literals_1_9' # require 'review/compiler/literals_1_8' ## redifine Compiler.new @@ -1194,9 +1202,7 @@ def ulist_element(compiler, position, level, content) end end include ::ReVIEWConstruction - def setup_foreign_grammar - @_grammar_literals = ReVIEW::Compiler::Literals.new(nil) - end + def setup_foreign_grammar; end # root = Start def _root @@ -5036,48 +5042,40 @@ def _ElementName return _tmp end - # Alphanumeric = %literals.Alphanumeric - def _Alphanumeric - _tmp = @_grammar_literals.external_invoke(self, :_Alphanumeric) - set_failed_rule :_Alphanumeric unless _tmp - return _tmp - end - - # AlphanumericAscii = %literals.AlphanumericAscii + # AlphanumericAscii = /[A-Za-z0-9]/ def _AlphanumericAscii - _tmp = @_grammar_literals.external_invoke(self, :_AlphanumericAscii) + _tmp = scan(/\A(?-mix:[A-Za-z0-9])/) set_failed_rule :_AlphanumericAscii unless _tmp return _tmp end - # LowerAlphabetAscii = %literals.LowerAlphabetAscii + # LowerAlphabetAscii = /[a-z]/ def _LowerAlphabetAscii - _tmp = @_grammar_literals.external_invoke(self, :_LowerAlphabetAscii) + _tmp = scan(/\A(?-mix:[a-z])/) set_failed_rule :_LowerAlphabetAscii unless _tmp return _tmp end - # Digit = %literals.Digit + # Digit = /[0-9]/ def _Digit - _tmp = @_grammar_literals.external_invoke(self, :_Digit) + _tmp = scan(/\A(?-mix:[0-9])/) set_failed_rule :_Digit unless _tmp return _tmp end - # BOM = %literals.BOM + # BOM = "uFEFF" def _BOM - _tmp = @_grammar_literals.external_invoke(self, :_BOM) + _tmp = match_string("uFEFF") set_failed_rule :_BOM unless _tmp return _tmp end - # Newline = %literals.Newline:n {newline(self, position, "\n")} + # Newline = /\n|\r\n?|\p{Zl}|\p{Zp}/ {newline(self, position, "\n")} def _Newline _save = self.pos while true # sequence - _tmp = @_grammar_literals.external_invoke(self, :_Newline) - n = @result + _tmp = scan(/\A(?-mix:\n|\r\n?|\p{Zl}|\p{Zp})/) unless _tmp self.pos = _save break @@ -5094,20 +5092,6 @@ def _Newline return _tmp end - # NonAlphanumeric = %literals.NonAlphanumeric - def _NonAlphanumeric - _tmp = @_grammar_literals.external_invoke(self, :_NonAlphanumeric) - set_failed_rule :_NonAlphanumeric unless _tmp - return _tmp - end - - # Spacechar = %literals.Spacechar - def _Spacechar - _tmp = @_grammar_literals.external_invoke(self, :_Spacechar) - set_failed_rule :_Spacechar unless _tmp - return _tmp - end - Rules = {} Rules[:_root] = rule_info("root", "Start") Rules[:_Start] = rule_info("Start", "&. { @list_stack = Array.new } Document:c { @strategy.ast = c }") @@ -5171,13 +5155,10 @@ def _Spacechar Rules[:_EOL] = rule_info("EOL", "(Newline | EOF)") Rules[:_EOF] = rule_info("EOF", "!.") Rules[:_ElementName] = rule_info("ElementName", "< LowerAlphabetAscii+ > { text }") - Rules[:_Alphanumeric] = rule_info("Alphanumeric", "%literals.Alphanumeric") - Rules[:_AlphanumericAscii] = rule_info("AlphanumericAscii", "%literals.AlphanumericAscii") - Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "%literals.LowerAlphabetAscii") - Rules[:_Digit] = rule_info("Digit", "%literals.Digit") - Rules[:_BOM] = rule_info("BOM", "%literals.BOM") - Rules[:_Newline] = rule_info("Newline", "%literals.Newline:n {newline(self, position, \"\\n\")}") - Rules[:_NonAlphanumeric] = rule_info("NonAlphanumeric", "%literals.NonAlphanumeric") - Rules[:_Spacechar] = rule_info("Spacechar", "%literals.Spacechar") + Rules[:_AlphanumericAscii] = rule_info("AlphanumericAscii", "/[A-Za-z0-9]/") + Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "/[a-z]/") + Rules[:_Digit] = rule_info("Digit", "/[0-9]/") + Rules[:_BOM] = rule_info("BOM", "\"uFEFF\"") + Rules[:_Newline] = rule_info("Newline", "/\\n|\\r\\n?|\\p{Zl}|\\p{Zp}/ {newline(self, position, \"\\n\")}") # :startdoc: end diff --git a/lib/review/compiler/literals_1_9.rb b/lib/review/compiler/literals_1_9.rb index 1a68c0889..e6adf680f 100644 --- a/lib/review/compiler/literals_1_9.rb +++ b/lib/review/compiler/literals_1_9.rb @@ -44,22 +44,16 @@ def current_column(target=pos) end def current_line(target=pos) - cur_offset = 0 - cur_line = 0 - - string.each_line do |line| - cur_line += 1 - cur_offset += line.size - return cur_line if cur_offset >= target - end + offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} + if offset + return offset[0] + end -1 end def lines - lines = [] - string.each_line { |l| lines << l } - lines + @lines_memo end @@ -70,9 +64,22 @@ def get_text(start) # Sets the string and current parsing position for the parser. def set_string string, pos - @string = string - @string_size = string ? string.size : 0 + @string = string.freeze @pos = pos + @sizes_memo = [] + if string + @string_size = string.size + @lines_memo = string.lines + cur_offset = cur_line = 0 + @lines_memo.each do |line| + cur_line += 1 + cur_offset += line.size + @sizes_memo << [cur_line, cur_offset] + end + else + @string_size = 0 + @lines_memo = [] + end end def show_pos diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 1eefe8319..657c1bd7f 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -1,5 +1,10 @@ %% name = ReVIEW::Compiler +%% header { +# coding: UTF-8 + +} + %% { class Error; end class Position @@ -18,7 +23,7 @@ require 'review/preprocessor' require 'review/exception' require 'review/node' require 'lineinput' - require 'review/compiler/literals_1_9' + # require 'review/compiler/literals_1_9' # require 'review/compiler/literals_1_8' ## redifine Compiler.new @@ -734,12 +739,8 @@ EOF = !. ElementName = < LowerAlphabetAscii+ > { text } -%literals = ReVIEW::Compiler::Literals -Alphanumeric = %literals.Alphanumeric -AlphanumericAscii = %literals.AlphanumericAscii -LowerAlphabetAscii = %literals.LowerAlphabetAscii -Digit = %literals.Digit -BOM = %literals.BOM -Newline = %literals.Newline:n ~newline(self, position, "\n") -NonAlphanumeric = %literals.NonAlphanumeric -Spacechar = %literals.Spacechar +AlphanumericAscii = /[A-Za-z0-9]/ +LowerAlphabetAscii = /[a-z]/ +Digit = /[0-9]/ +BOM = "\uFEFF" +Newline = /\n|\r\n?|\p{Zl}|\p{Zp}/ ~newline(self, position, "\n") From 97fbd5fd21d69c1a291e186ca1ac46a6706a2d24 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Nov 2015 01:00:51 +0900 Subject: [PATCH 194/214] fix other inlines * use node_* methods * id should not be escaped --- lib/review/latexbuilder.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 7242a6d2e..69761d131 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -654,8 +654,9 @@ def noindent '\noindent' end - def inline_chapref(id) - title = super + def node_inline_chapref(node) + id = node[0].to_raw + title = @book.chapter_index.display_string(id) if @book.config["chapterlink"] "\\hyperref[chap:#{id}]{#{title}}" else @@ -666,7 +667,8 @@ def inline_chapref(id) nofunc_text("[UnknownChapter:#{id}]") end - def inline_chap(id) + def node_inline_chap(node) + id = node[0].to_raw if @book.config["chapterlink"] "\\hyperref[chap:#{id}]{#{@book.chapter_index.number(id)}}" else @@ -677,7 +679,8 @@ def inline_chap(id) nofunc_text("[UnknownChapter:#{id}]") end - def inline_title(id) + def node_inline_title(node) + id = node[0].to_raw title = super if @book.config["chapterlink"] "\\hyperref[chap:#{id}]{#{title}}" @@ -814,7 +817,8 @@ def inline_ttb(str) macro('texttt', macro('textbf', str)) end - def inline_bib(id) + def node_inline_bib(node) + id = node[0].to_raw macro('reviewbibref', "[#{@chapter.bibpaper(id).number}]", bib_label(id)) end @@ -833,7 +837,8 @@ def inline_hd_chap(chap, id) end end - def inline_column(id) + def node_inline_column(node) + id = node[0].to_raw macro('reviewcolumnref', "#{@chapter.column(id).caption}", column_label(id)) end @@ -865,7 +870,8 @@ def inline_ami(str) macro('reviewami', str) end - def inline_icon(id) + def node_inline_icon(node) + id = node[0].to_raw macro('includegraphics', @chapter.image(id).path) end From c2fa9ee31994ba3f04f3ea76c242e45f6a14c910 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Nov 2015 01:18:06 +0900 Subject: [PATCH 195/214] remove compiler/literals_1_9.* --- Rakefile | 2 - lib/review/compiler/literals_1_9.kpeg | 22 -- lib/review/compiler/literals_1_9.rb | 442 -------------------------- 3 files changed, 466 deletions(-) delete mode 100644 lib/review/compiler/literals_1_9.kpeg delete mode 100644 lib/review/compiler/literals_1_9.rb diff --git a/Rakefile b/Rakefile index b59ff17c5..d11970603 100644 --- a/Rakefile +++ b/Rakefile @@ -15,8 +15,6 @@ task :default => [:test] task :kpeg do FileUtils.rm_f "lib/review/compiler.rb" - FileUtils.rm_f "lib/review/compiler/literals_1_9.rb" - sh "kpeg -s lib/review/compiler/literals_1_9.kpeg -o lib/review/compiler/literals_1_9.rb" sh "kpeg -s lib/review/review.kpeg -o lib/review/compiler.rb" end diff --git a/lib/review/compiler/literals_1_9.kpeg b/lib/review/compiler/literals_1_9.kpeg deleted file mode 100644 index bf9d80c8b..000000000 --- a/lib/review/compiler/literals_1_9.kpeg +++ /dev/null @@ -1,22 +0,0 @@ -%% name = ReVIEW::Compiler::Literals - -%% header { -# coding: UTF-8 - -## -#-- -# This set of literals is for Ruby 1.9 regular expressions and gives full -# unicode support. -# -# Unlike peg-markdown, this set of literals recognizes Unicode alphanumeric -# characters, newlines and spaces. -} - -Alphanumeric = /\p{Word}/ -AlphanumericAscii = /[A-Za-z0-9]/ -LowerAlphabetAscii = /[a-z]/ -Digit = /[0-9]/ -BOM = "\uFEFF" -Newline = /\n|\r\n?|\p{Zl}|\p{Zp}/ -NonAlphanumeric = /\p{^Word}/ -Spacechar = /\t|\p{Zs}/ diff --git a/lib/review/compiler/literals_1_9.rb b/lib/review/compiler/literals_1_9.rb deleted file mode 100644 index e6adf680f..000000000 --- a/lib/review/compiler/literals_1_9.rb +++ /dev/null @@ -1,442 +0,0 @@ -# coding: UTF-8 - -## -#-- -# This set of literals is for Ruby 1.9 regular expressions and gives full -# unicode support. -# -# Unlike peg-markdown, this set of literals recognizes Unicode alphanumeric -# characters, newlines and spaces. -class ReVIEW::Compiler::Literals - # :stopdoc: - - # This is distinct from setup_parser so that a standalone parser - # can redefine #initialize and still have access to the proper - # parser setup code. - def initialize(str, debug=false) - setup_parser(str, debug) - end - - - - # Prepares for parsing +str+. If you define a custom initialize you must - # call this method before #parse - def setup_parser(str, debug=false) - set_string str, 0 - @memoizations = Hash.new { |h,k| h[k] = {} } - @result = nil - @failed_rule = nil - @failing_rule_offset = -1 - - setup_foreign_grammar - end - - attr_reader :string - attr_reader :failing_rule_offset - attr_accessor :result, :pos - - def current_column(target=pos) - if c = string.rindex("\n", target-1) - return target - c - 1 - end - - target + 1 - end - - def current_line(target=pos) - offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} - if offset - return offset[0] - end - - -1 - end - - def lines - @lines_memo - end - - - - def get_text(start) - @string[start..@pos-1] - end - - # Sets the string and current parsing position for the parser. - def set_string string, pos - @string = string.freeze - @pos = pos - @sizes_memo = [] - if string - @string_size = string.size - @lines_memo = string.lines - cur_offset = cur_line = 0 - @lines_memo.each do |line| - cur_line += 1 - cur_offset += line.size - @sizes_memo << [cur_line, cur_offset] - end - else - @string_size = 0 - @lines_memo = [] - end - end - - def show_pos - width = 10 - if @pos < width - "#{@pos} (\"#{@string[0,@pos]}\" @ \"#{@string[@pos,width]}\")" - else - "#{@pos} (\"... #{@string[@pos - width, width]}\" @ \"#{@string[@pos,width]}\")" - end - end - - def failure_info - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - "line #{l}, column #{c}: failed rule '#{info.name}' = '#{info.rendered}'" - else - "line #{l}, column #{c}: failed rule '#{@failed_rule}'" - end - end - - def failure_caret - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - line = lines[l-1] - "#{line}\n#{' ' * (c - 1)}^" - end - - def failure_character - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - lines[l-1][c-1, 1] - end - - def failure_oneline - l = current_line @failing_rule_offset - c = current_column @failing_rule_offset - - char = lines[l-1][c-1, 1] - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - "@#{l}:#{c} failed rule '#{info.name}', got '#{char}'" - else - "@#{l}:#{c} failed rule '#{@failed_rule}', got '#{char}'" - end - end - - class ParseError < RuntimeError - end - - def raise_error - raise ParseError, failure_oneline - end - - def show_error(io=STDOUT) - error_pos = @failing_rule_offset - line_no = current_line(error_pos) - col_no = current_column(error_pos) - - io.puts "On line #{line_no}, column #{col_no}:" - - if @failed_rule.kind_of? Symbol - info = self.class::Rules[@failed_rule] - io.puts "Failed to match '#{info.rendered}' (rule '#{info.name}')" - else - io.puts "Failed to match rule '#{@failed_rule}'" - end - - io.puts "Got: #{string[error_pos,1].inspect}" - line = lines[line_no-1] - io.puts "=> #{line}" - io.print(" " * (col_no + 3)) - io.puts "^" - end - - def set_failed_rule(name) - if @pos > @failing_rule_offset - @failed_rule = name - @failing_rule_offset = @pos - end - end - - attr_reader :failed_rule - - def match_string(str) - len = str.size - if @string[pos,len] == str - @pos += len - return str - end - - return nil - end - - def scan(reg) - if m = reg.match(@string[@pos..-1]) - width = m.end(0) - @pos += width - return true - end - - return nil - end - - if "".respond_to? :ord - def get_byte - if @pos >= @string_size - return nil - end - - s = @string[@pos].ord - @pos += 1 - s - end - else - def get_byte - if @pos >= @string_size - return nil - end - - s = @string[@pos] - @pos += 1 - s - end - end - - def parse(rule=nil) - # We invoke the rules indirectly via apply - # instead of by just calling them as methods because - # if the rules use left recursion, apply needs to - # manage that. - - if !rule - apply(:_root) - else - method = rule.gsub("-","_hyphen_") - apply :"_#{method}" - end - end - - class MemoEntry - def initialize(ans, pos) - @ans = ans - @pos = pos - @result = nil - @set = false - @left_rec = false - end - - attr_reader :ans, :pos, :result, :set - attr_accessor :left_rec - - def move!(ans, pos, result) - @ans = ans - @pos = pos - @result = result - @set = true - @left_rec = false - end - end - - def external_invoke(other, rule, *args) - old_pos = @pos - old_string = @string - - set_string other.string, other.pos - - begin - if val = __send__(rule, *args) - other.pos = @pos - other.result = @result - else - other.set_failed_rule "#{self.class}##{rule}" - end - val - ensure - set_string old_string, old_pos - end - end - - def apply_with_args(rule, *args) - memo_key = [rule, args] - if m = @memoizations[memo_key][@pos] - @pos = m.pos - if !m.set - m.left_rec = true - return nil - end - - @result = m.result - - return m.ans - else - m = MemoEntry.new(nil, @pos) - @memoizations[memo_key][@pos] = m - start_pos = @pos - - ans = __send__ rule, *args - - lr = m.left_rec - - m.move! ans, @pos, @result - - # Don't bother trying to grow the left recursion - # if it's failing straight away (thus there is no seed) - if ans and lr - return grow_lr(rule, args, start_pos, m) - else - return ans - end - - return ans - end - end - - def apply(rule) - if m = @memoizations[rule][@pos] - @pos = m.pos - if !m.set - m.left_rec = true - return nil - end - - @result = m.result - - return m.ans - else - m = MemoEntry.new(nil, @pos) - @memoizations[rule][@pos] = m - start_pos = @pos - - ans = __send__ rule - - lr = m.left_rec - - m.move! ans, @pos, @result - - # Don't bother trying to grow the left recursion - # if it's failing straight away (thus there is no seed) - if ans and lr - return grow_lr(rule, nil, start_pos, m) - else - return ans - end - - return ans - end - end - - def grow_lr(rule, args, start_pos, m) - while true - @pos = start_pos - @result = m.result - - if args - ans = __send__ rule, *args - else - ans = __send__ rule - end - return nil unless ans - - break if @pos <= m.pos - - m.move! ans, @pos, @result - end - - @result = m.result - @pos = m.pos - return m.ans - end - - class RuleInfo - def initialize(name, rendered) - @name = name - @rendered = rendered - end - - attr_reader :name, :rendered - end - - def self.rule_info(name, rendered) - RuleInfo.new(name, rendered) - end - - - # :startdoc: - # :stopdoc: - def setup_foreign_grammar; end - - # Alphanumeric = /\p{Word}/ - def _Alphanumeric - _tmp = scan(/\A(?-mix:\p{Word})/) - set_failed_rule :_Alphanumeric unless _tmp - return _tmp - end - - # AlphanumericAscii = /[A-Za-z0-9]/ - def _AlphanumericAscii - _tmp = scan(/\A(?-mix:[A-Za-z0-9])/) - set_failed_rule :_AlphanumericAscii unless _tmp - return _tmp - end - - # LowerAlphabetAscii = /[a-z]/ - def _LowerAlphabetAscii - _tmp = scan(/\A(?-mix:[a-z])/) - set_failed_rule :_LowerAlphabetAscii unless _tmp - return _tmp - end - - # Digit = /[0-9]/ - def _Digit - _tmp = scan(/\A(?-mix:[0-9])/) - set_failed_rule :_Digit unless _tmp - return _tmp - end - - # BOM = "uFEFF" - def _BOM - _tmp = match_string("uFEFF") - set_failed_rule :_BOM unless _tmp - return _tmp - end - - # Newline = /\n|\r\n?|\p{Zl}|\p{Zp}/ - def _Newline - _tmp = scan(/\A(?-mix:\n|\r\n?|\p{Zl}|\p{Zp})/) - set_failed_rule :_Newline unless _tmp - return _tmp - end - - # NonAlphanumeric = /\p{^Word}/ - def _NonAlphanumeric - _tmp = scan(/\A(?-mix:\p{^Word})/) - set_failed_rule :_NonAlphanumeric unless _tmp - return _tmp - end - - # Spacechar = /\t|\p{Zs}/ - def _Spacechar - _tmp = scan(/\A(?-mix:\t|\p{Zs})/) - set_failed_rule :_Spacechar unless _tmp - return _tmp - end - - Rules = {} - Rules[:_Alphanumeric] = rule_info("Alphanumeric", "/\\p{Word}/") - Rules[:_AlphanumericAscii] = rule_info("AlphanumericAscii", "/[A-Za-z0-9]/") - Rules[:_LowerAlphabetAscii] = rule_info("LowerAlphabetAscii", "/[a-z]/") - Rules[:_Digit] = rule_info("Digit", "/[0-9]/") - Rules[:_BOM] = rule_info("BOM", "\"uFEFF\"") - Rules[:_Newline] = rule_info("Newline", "/\\n|\\r\\n?|\\p{Zl}|\\p{Zp}/") - Rules[:_NonAlphanumeric] = rule_info("NonAlphanumeric", "/\\p{^Word}/") - Rules[:_Spacechar] = rule_info("Spacechar", "/\\t|\\p{Zs}/") - # :startdoc: -end From c6e4c1ac38c1610c9ed45a61a16c17c451e73ce5 Mon Sep 17 00:00:00 2001 From: takahashim Date: Fri, 13 Nov 2015 01:26:46 +0900 Subject: [PATCH 196/214] bump up --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index adb3ec43f..401d1c25b 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "0.1.3" + VERSION = "0.2.0" end From 885525195d7ff2b05945c20ebfe52c2679f78e46 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 23 Nov 2015 23:31:58 +0900 Subject: [PATCH 197/214] fix @; use node_inline_imgref instead of inline_imgref --- lib/review/builder.rb | 10 ++++++++-- lib/review/idgxmlbuilder.rb | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 941d926f0..674a89f12 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -228,8 +228,14 @@ def inline_img(id) nofunc_text("[UnknownImage:#{id}]") end - def inline_imgref(id) - img = inline_img(id) + def node_inline_img(node) + id = node[0].to_raw + inline_img(id) + end + + def node_inline_imgref(node) + id = node[0].to_raw + img = node_inline_img(node) if @chapter.image(id).caption "#{img}#{I18n.t('image_quote', @chapter.image(id).caption)}" diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index b5d5b0561..50833ff5d 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -442,7 +442,8 @@ def inline_table(id) end end - def inline_img(id) + def node_inline_img(node) + id = node[0].to_raw chapter, id = extract_chapter_id(id) if get_chap(chapter).nil? "#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [chapter.image(id).number])}" @@ -451,10 +452,11 @@ def inline_img(id) end end - def inline_imgref(id) + def node_inline_imgref(node) + id = node[0].to_raw chapter, id = extract_chapter_id(id) if chapter.image(id).caption.blank? - inline_img(id) + node_inline_img(node) else if get_chap(chapter).nil? "#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [chapter.image(id).number])}#{I18n.t('image_quote', chapter.image(id).caption)}" From d32fafd10e84e5a1ecffdfa6756edb6f4ed3b77f Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 23 Nov 2015 23:12:38 +0900 Subject: [PATCH 198/214] do not use super in node_* cf. #463 --- lib/review/latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 69761d131..5dddf7185 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -681,7 +681,7 @@ def node_inline_chap(node) def node_inline_title(node) id = node[0].to_raw - title = super + title = @book.chapter_index.title(id) if @book.config["chapterlink"] "\\hyperref[chap:#{id}]{#{title}}" else From 2b39bc5b92ab675e9a892909797fe25b422bd422 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 23 Nov 2015 23:42:07 +0900 Subject: [PATCH 199/214] fix indent --- lib/review/compiler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 22cbd5e93..1a3b040f4 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -39,7 +39,7 @@ def current_line(target=pos) offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} if offset return offset[0] - end + end -1 end From f612bf075b496118be0d1cad9ee2eb1e3d97a3b8 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 00:02:23 +0900 Subject: [PATCH 200/214] support Ruby 1.9 add workaround when missing Array#bsearch --- lib/review/compiler.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 1a3b040f4..14adf7696 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -36,9 +36,17 @@ def current_column(target=pos) end def current_line(target=pos) - offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} - if offset - return offset[0] + if @sizes_memo.respond_to?(:bsearch) + offset = @sizes_memo.bsearch{|line, cur_offset| cur_offset >= target} + if offset + return offset[0] + end + else + @sizes_memo.each do |line, cur_offset| + if cur_offset >= target + return cur_offset + end + end end -1 From 2ff543a547f265484276cf63511a5d2f79c584d0 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 00:18:54 +0900 Subject: [PATCH 201/214] add "\n" before caption --- lib/review/latexbuilder.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 5dddf7185..caee98a85 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -258,6 +258,7 @@ def list(lines, id, caption = nil, lang = nil) buf << common_code_block_lst(lines, 'reviewlistlst', 'caption', caption, lang) else begin + buf << "\n" buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" rescue KeyError error "no such list: #{id}" @@ -277,6 +278,7 @@ def listnum(lines, id, caption = nil, lang = nil) buf << common_code_block_lst(lines, 'reviewlistnumlst', 'caption', caption, lang) else begin + buf << "\n" buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" rescue KeyError error "no such list: #{id}" From a7961663a626e6ebdaac455a9564364b0c01745f Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 00:25:27 +0900 Subject: [PATCH 202/214] fix test --- test/test_latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 19c8601f9..a332f784f 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -279,7 +279,7 @@ def test_emlist_with_tab4 def test_listnum actual = compile_block("//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n") - assert_equal %Q|\\reviewlistcaption{リスト1.1: ruby}\n\\reviewlistcaption{ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual + assert_equal %Q|\n\\reviewlistcaption{リスト1.1: ruby}\n\\reviewlistcaption{ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual end def test_listnum_lst From eee67f5e2db06e516886557059cf47e61891e32b Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 00:57:59 +0900 Subject: [PATCH 203/214] fix caption of //list, //listnum --- lib/review/latexbuilder.rb | 49 ++++++++++++++++++-------------------- test/test_latexbuilder.rb | 2 +- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index caee98a85..787336804 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -230,9 +230,9 @@ def highlight_listings? def emlist(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewemlistlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewemlistlst', 'title', caption, lang) else - buf << common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx| detab(line) + "\n" end end @@ -242,9 +242,9 @@ def emlist(lines, caption = nil, lang = nil) def emlistnum(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewemlistnumlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang) else - buf << common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx| detab((idx+1).to_s.rjust(2)+": " + line) + "\n" end end @@ -255,15 +255,9 @@ def emlistnum(lines, caption = nil, lang = nil) def list(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewlistlst', 'caption', caption, lang) + buf << common_code_block_lst(id, lines, 'reviewlistlst', 'caption', caption, lang) else - begin - buf << "\n" - buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" - rescue KeyError - error "no such list: #{id}" - end - buf << common_code_block(lines, 'reviewlist', nil, lang) do |line, idx| + buf << common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx| detab(line) + "\n" end end @@ -275,15 +269,9 @@ def list(lines, id, caption = nil, lang = nil) def listnum(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewlistnumlst', 'caption', caption, lang) + buf << common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang) else - begin - buf << "\n" - buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" - rescue KeyError - error "no such list: #{id}" - end - buf << common_code_block(lines, 'reviewlist', caption, lang) do |line, idx| + buf << common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx| detab((idx+1).to_s.rjust(2)+": " + line) + "\n" end end @@ -293,20 +281,29 @@ def listnum(lines, id, caption = nil, lang = nil) def cmd(lines, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewcmdlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewcmdlst', 'title', caption, lang) else buf << "\n" - buf << common_code_block(lines, 'reviewcmd', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewcmd', caption, lang) do |line, idx| detab(line) + "\n" end end buf end - def common_code_block(lines, command, caption, lang) + def common_code_block(id, lines, command, caption, lang) buf = "" if caption - buf << macro(command + 'caption', "#{caption}") + "\n" + if command =~ /emlist/ || command =~ /cmd/ + buf << macro(command + 'caption', "#{caption}") + "\n" + else + begin + buf << "\n" + buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" + rescue KeyError + error "no such list: #{id}" + end + end end body = "" lines.each_with_index do |line, idx| @@ -318,7 +315,7 @@ def common_code_block(lines, command, caption, lang) buf end - def common_code_block_lst(lines, command, title, caption, lang) + def common_code_block_lst(id, lines, command, title, caption, lang) buf = "" caption_str = (caption || "") if title == "title" && caption_str == "" @@ -340,7 +337,7 @@ def common_code_block_lst(lines, command, title, caption, lang) def source(lines, caption = nil, lang = nil) if highlight_listings? - common_code_block_lst(lines, 'reviewlistlst', 'title', caption, lang) + common_code_block_lst(nil, lines, 'reviewlistlst', 'title', caption, lang) else buf = "\n" buf << '\begin{reviewlist}' << "\n" diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index a332f784f..015ec84e7 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -279,7 +279,7 @@ def test_emlist_with_tab4 def test_listnum actual = compile_block("//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n") - assert_equal %Q|\n\\reviewlistcaption{リスト1.1: ruby}\n\\reviewlistcaption{ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual + assert_equal %Q|\n\\reviewlistcaption{リスト1.1: ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual end def test_listnum_lst From 08d5cf24e1ad3f5b301a2947fff9d4a4a0f8e918 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 01:16:47 +0900 Subject: [PATCH 204/214] bump up --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index 401d1c25b..b3256aca6 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "0.2.0" + VERSION = "0.2.1" end From b254d151bf80b50d877a8bea1902c5c99180d91f Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 07:39:19 +0900 Subject: [PATCH 205/214] do not escape content of footnote --- lib/review/latexbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 787336804..53a85b414 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -723,7 +723,7 @@ def node_inline_fn(node) if @book.config["footnotetext"] macro("footnotemark[#{@chapter.footnote(id).number}]", "") else - macro('footnote', escape(@chapter.footnote(id).content.strip)) + macro('footnote', @chapter.footnote(id).content.strip) end end From aff7574b7e07a36c77bf0d8c1d8ec5a89540d768 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 08:20:51 +0900 Subject: [PATCH 206/214] bump up --- lib/review/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/version.rb b/lib/review/version.rb index b3256aca6..1e3d952f3 100644 --- a/lib/review/version.rb +++ b/lib/review/version.rb @@ -1,3 +1,3 @@ module ReVIEW - VERSION = "0.2.1" + VERSION = "0.2.2" end From bb47f4f5c202260cca00d863332ad8d65fb3eb71 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 25 Jan 2016 00:37:40 +0900 Subject: [PATCH 207/214] fix rubocop --- lib/review/htmlbuilder.rb | 48 ++++++++++++++++++------------------- lib/review/idgxmlbuilder.rb | 14 +++++------ lib/review/latexbuilder.rb | 2 +- test/test_helper.rb | 10 -------- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 3cedae534..4c017ca4a 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -618,7 +618,7 @@ def result_metric(array) def image_image(id, caption, metric) metrics = parse_metric("html", metric) buf = %Q[
      \n] - buf << %Q[#{escape_html(caption)}\n] + buf << %Q[#{caption}\n] buf << image_header(id, caption) buf << %Q[
      \n] buf @@ -745,7 +745,7 @@ def indepimage(id, caption="", metric=nil) caption = "" if caption.nil? buf = %Q[
      \n] begin - buf << %Q[#{escape_html(caption)}\n] + buf << %Q[#{caption}\n] rescue buf << %Q[
      missing image: #{id}
      \n] end @@ -791,7 +791,7 @@ def noindent end def inline_labelref(idref) - %Q[「#{I18n.t("label_marker")}#{(idref)}」] + %Q[「#{I18n.t("label_marker")}#{idref}」] end alias_method :inline_ref, :inline_labelref @@ -843,7 +843,7 @@ def compile_ruby(base, ruby) if @book.htmlversion == 5 %Q[#{base}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] else - %Q[#{(base)}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] + %Q[#{base}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] end end @@ -865,26 +865,26 @@ def inline_b(str) end def inline_ami(str) - %Q(#{(str)}) + %Q(#{str}) end def inline_bou(str) - %Q(#{(str)}) + %Q(#{str}) end def inline_tti(str) if @book.htmlversion == 5 - %Q(#{(str)}) + %Q(#{str}) else - %Q(#{(str)}) + %Q(#{str}) end end def inline_ttb(str) if @book.htmlversion == 5 - %Q(#{(str)}) + %Q(#{str}) else - %Q(#{(str)}) + %Q(#{str}) end end @@ -894,14 +894,14 @@ def inline_dtp(str) def inline_code(str) if @book.htmlversion == 5 - %Q(#{(str)}) + %Q(#{str}) else - %Q(#{(str)}) + %Q(#{str}) end end def inline_idx(str) - %Q(#{(str)}) + %Q(#{str}) end def inline_hidx(str) @@ -920,7 +920,7 @@ def inline_m(str) :symbol => MathML::Symbol::CharacterReference) %Q[#{parser.parse(str, nil)}] else - %Q[#{(str)}] + %Q[#{str}] end end @@ -942,7 +942,7 @@ def bibpaper_header(id, caption) buf = %Q() buf << "[#{@chapter.bibpaper(id).number}]" buf << %Q() - buf << " #{(caption)}" << "\n" + buf << " #{caption}" << "\n" end def bibpaper_bibpaper(id, caption, lines) @@ -976,9 +976,9 @@ def column_label(id) def inline_column_chap(chapter, id) if @book.config["chapterlink"] - %Q(#{I18n.t("column", (chapter.column(id).caption))}) + %Q(#{I18n.t("column", chapter.column(id).caption)}) else - I18n.t("column", (chapter.column(id).caption)) + I18n.t("column", chapter.column(id).caption) end end @@ -1031,7 +1031,7 @@ def inline_img(id) end def inline_asis(str, tag) - %Q(<#{tag}>#{(str)}) + %Q(<#{tag}>#{str}) end def inline_abbr(str) @@ -1088,9 +1088,9 @@ def inline_sup(str) def inline_tt(str) if @book.htmlversion == 5 - %Q(#{(str)}) + %Q(#{str}) else - %Q(#{(str)}) + %Q(#{str}) end end @@ -1103,11 +1103,11 @@ def inline_ins(str) end def inline_u(str) - %Q(#{(str)}) + %Q(#{str}) end def inline_recipe(str) - %Q(「#{(str)}」) + %Q(「#{str}」) end def inline_icon(id) @@ -1124,7 +1124,7 @@ def inline_uchar(str) def inline_comment(str) if @book.config["draft"] - %Q(#{(str)}) + %Q(#{str}) else %Q() end @@ -1139,7 +1139,7 @@ def nofunc_text(str) end def compile_href(url, label) - %Q(#{label.nil? ? (url) : (label)}) + %Q(#{label.nil? ? url : label}) end def flushright(lines) diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 3e2fd1c25..6397a9e74 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -661,14 +661,14 @@ def compile_kw(word, alt) '' + if alt then - ("#{word}(#{alt.strip})") + "#{word}(#{alt.strip})" else - (word) + word end + '' + %Q[] + if alt - alt.split(/\s*,\s*/).collect! {|e| %Q[] }.join + alt.split(/\s*,\s*/).collect! {|e| %Q[] }.join else "" end @@ -774,13 +774,13 @@ def inline_keytop(str) end def inline_labelref(idref) - %Q[「#{I18n.t("label_marker")}#{(idref)}」] # FIXME:節名とタイトルも込みで要出力 + %Q[「#{I18n.t("label_marker")}#{idref}」] # FIXME:節名とタイトルも込みで要出力 end alias_method :inline_ref, :inline_labelref def inline_pageref(idref) - %Q[●●] # ページ番号を参照 + %Q[●●] # ページ番号を参照 end def inline_balloon(str) @@ -829,7 +829,7 @@ def common_column_begin(type, caption) @column += 1 a_id = %Q[id="column-#{@column}"] buf << "<#{type}column #{a_id}>" - buf << %Q[#{compile_inline(caption)}] << @lf + buf << %Q[#{caption}] << @lf buf end @@ -926,7 +926,7 @@ def captionblock(type, lines, caption, specialstyle = nil) buf = "" buf << "<#{type}>" style = specialstyle.nil? ? "#{type}-title" : specialstyle - buf << "#{(caption)}" + @lf unless caption.nil? + buf << "#{caption}" + @lf unless caption.nil? if @book.config["deprecated-blocklines"].nil? buf << "#{lines.join}" << @lf else diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 53a85b414..7368de486 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -520,7 +520,7 @@ def table_begin(ncols) buf << macro('begin', 'reviewtable', @latex_tsize) << "\n" elsif @tsize cellwidth = @tsize.split(/\s*,\s*/) - buf << macro('begin', 'reviewtable', '|'+(cellwidth.collect{|i| "p{#{i}mm}"}.join('|'))+'|') << "\n" + buf << macro('begin', 'reviewtable', '|'+cellwidth.collect{|i| "p{#{i}mm}"}.join('|')+'|') << "\n" else buf << macro('begin', 'reviewtable', (['|'] * (ncols + 1)).join('l')) << "\n" end diff --git a/test/test_helper.rb b/test/test_helper.rb index adfe6d828..7feaa645f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,16 +11,6 @@ def compile_document(str, force_to_s=true) end end - def compile_block(str, force_to_s=true) - @compiler.setup_parser(str) - @compiler.parse("Document") - if force_to_s - @compiler.result.to_doc - else - @compiler.result - end - end - def compile_inline(str, force_to_s=true) @compiler.setup_parser(str) @compiler.parse("Paragraph") From 0cd6254b99fb44dbe2626537257dedfcfdf8d610 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 7 Feb 2016 14:56:37 +0900 Subject: [PATCH 208/214] fix test --- test/test_htmlbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 84cd2c360..16c7a2359 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -119,7 +119,7 @@ def on_APPENDIX? assert_equal %Q|

      付録A this is test.

      \n|, actual actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      A.1 this is test.

      \n|, actual + assert_equal %Q|

      A.1 this is test.

      \n|, actual end end end From d9b56d0512ddc5d8c1ee247a90037bb68f4f4c92 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 7 Feb 2016 15:26:45 +0900 Subject: [PATCH 209/214] TOCParser: remove compile_inline --- lib/review/tocparser.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/review/tocparser.rb b/lib/review/tocparser.rb index 131e41785..73ca34af8 100644 --- a/lib/review/tocparser.rb +++ b/lib/review/tocparser.rb @@ -105,8 +105,11 @@ def compile_label(line) dummy_book = ReVIEW::Book::Base.load dummy_chapter = ReVIEW::Book::Chapter.new(dummy_book, 1, '-', nil, StringIO.new) dummy_loc = Location.new("", StringIO.new) - b.bind(ReVIEW::Compiler.new(b), dummy_chapter, dummy_loc) - b.compile_inline(line) + c = ReVIEW::Compiler.new(b) + b.bind(c, dummy_chapter, dummy_loc) + c.setup_parser(line) + c.parse("Paragraph") + c.result.to_doc end def error!(filename, lineno, msg) From a428dd486ba5b993387b009d0975215a2431e2f4 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 20 Mar 2016 13:54:32 +0900 Subject: [PATCH 210/214] fix tests --- test/test_htmlbuilder.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index ace36568e..e9259341c 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -1151,7 +1151,9 @@ def test_inline_hd builder = ReVIEW::HTMLBuilder.new comp = ReVIEW::Compiler.new(builder) builder.bind(comp, chap2, nil) - hd = builder.inline_hd("ch1|test1-1") + comp.setup_parser("@{ch1|test1-1}") + comp.parse("Paragraph") + hd = comp.result.to_doc assert_equal "「1.1 test1-1」", hd end @@ -1167,7 +1169,9 @@ def test_inline_hd_for_part builder = ReVIEW::HTMLBuilder.new comp = ReVIEW::Compiler.new(builder) builder.bind(comp, chap2, nil) - hd = builder.inline_hd("part1|part1-1") + comp.setup_parser("@{part1|part1-1}") + comp.parse("Paragraph") + hd = comp.result.to_doc assert_equal "「1.1 part1-1」", hd end end From baf80417a8add324e94d104fd490eb0545017fa7 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 14 Nov 2016 00:01:33 -0600 Subject: [PATCH 211/214] merge master --- lib/lineinput.rb | 10 ++++ lib/review/compiler.rb | 100 ++++++++++++++++------------------ lib/review/htmlbuilder.rb | 37 +++++++------ lib/review/idgxmlbuilder.rb | 1 + lib/review/latexbuilder.rb | 20 +++++-- lib/review/markdownbuilder.rb | 4 +- lib/review/md2inaobuilder.rb | 34 +++++++----- lib/review/review.kpeg | 17 +++++- lib/review/tocparser.rb | 2 +- test/test_compiler.rb | 5 +- test/test_htmlbuilder.rb | 21 ++----- test/test_location.rb | 22 +++++--- test/test_markdownbuilder.rb | 9 +-- test/test_md2inaobuilder.rb | 11 ++-- 14 files changed, 161 insertions(+), 132 deletions(-) diff --git a/lib/lineinput.rb b/lib/lineinput.rb index 0b5db2209..80da99f3e 100644 --- a/lib/lineinput.rb +++ b/lib/lineinput.rb @@ -48,6 +48,16 @@ def ungets(line) line end + def peek + line = gets() + ungets line if line + line + end + + def next? + peek() ? true : false + end + def each while line = gets() yield line diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 14adf7696..fd0f19f28 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -586,6 +586,7 @@ def inline_defined?(name) defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false defcodeblock :table, 0..2, false, [:raw, :doc] + defcodeblock :imgtable, 0..2, false, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] defcodeblock :box, 0..1, false, [:doc] @@ -595,6 +596,14 @@ def inline_defined?(name) defblock :flushright, 0 defblock :centering, 0 defblock :note, 0..1 + defblock :memo, 0..1 + defblock :info, 0..1 + defblock :important, 0..1 + defblock :caution, 0..1 + defblock :notice, 0..1 + defblock :warning, 0..1 + defblock :tip, 0..1 + defblock :box, 0..1 defblock :comment, 0..1, true defsingle :footnote, 2, [:raw, :doc] @@ -629,6 +638,7 @@ def inline_defined?(name) definline :hd definline :recipe definline :column + definline :tcy definline :abbr definline :acronym @@ -659,6 +669,7 @@ def inline_defined?(name) definline :hidx definline :comment definline :include + definline :tcy defcomplexinline :kw defcomplexinline :ruby @@ -1634,7 +1645,7 @@ def _ParagraphLine return _tmp end - # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, position, r1)} | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL {code_block_element(self, position, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL {block_element(self, position, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, position, symbol, args, nil)}) + # BlockElement = ("//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL {raw(self, b, position, r1)} | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents:contents "//}" Space* EOL {code_block_element(self, position, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents:contents "//}" Space* EOL {block_element(self, position, symbol, args, contents)} | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, position, symbol, args, nil)}) def _BlockElement _save = self.pos @@ -1762,13 +1773,7 @@ def _BlockElement self.pos = _save5 break end - _save10 = self.pos _tmp = apply(:_CodeBlockElementContents) - @result = nil unless _tmp - unless _tmp - _tmp = true - self.pos = _save10 - end contents = @result unless _tmp self.pos = _save5 @@ -1804,25 +1809,25 @@ def _BlockElement break if _tmp self.pos = _save - _save12 = self.pos + _save11 = self.pos while true # sequence - _save13 = self.pos + _save12 = self.pos _tmp = match_string("//raw") _tmp = _tmp ? nil : true - self.pos = _save13 + self.pos = _save12 unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = match_string("//") unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = apply(:_ElementName) symbol = @result unless _tmp - self.pos = _save12 + self.pos = _save11 break end _ary = [] @@ -1835,12 +1840,12 @@ def _BlockElement @result = _ary args = @result unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = match_string("{") unless _tmp - self.pos = _save12 + self.pos = _save11 break end while true @@ -1849,29 +1854,23 @@ def _BlockElement end _tmp = true unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = apply(:_Newline) unless _tmp - self.pos = _save12 + self.pos = _save11 break end - _save16 = self.pos _tmp = apply(:_BlockElementContents) - @result = nil unless _tmp - unless _tmp - _tmp = true - self.pos = _save16 - end contents = @result unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = match_string("//}") unless _tmp - self.pos = _save12 + self.pos = _save11 break end while true @@ -1880,18 +1879,18 @@ def _BlockElement end _tmp = true unless _tmp - self.pos = _save12 + self.pos = _save11 break end _tmp = apply(:_EOL) unless _tmp - self.pos = _save12 + self.pos = _save11 break end @result = begin; block_element(self, position, symbol, args, contents); end _tmp = true unless _tmp - self.pos = _save12 + self.pos = _save11 end break end # end sequence @@ -1899,25 +1898,25 @@ def _BlockElement break if _tmp self.pos = _save - _save18 = self.pos + _save16 = self.pos while true # sequence - _save19 = self.pos + _save17 = self.pos _tmp = match_string("//raw") _tmp = _tmp ? nil : true - self.pos = _save19 + self.pos = _save17 unless _tmp - self.pos = _save18 + self.pos = _save16 break end _tmp = match_string("//") unless _tmp - self.pos = _save18 + self.pos = _save16 break end _tmp = apply(:_ElementName) symbol = @result unless _tmp - self.pos = _save18 + self.pos = _save16 break end _ary = [] @@ -1930,7 +1929,7 @@ def _BlockElement @result = _ary args = @result unless _tmp - self.pos = _save18 + self.pos = _save16 break end while true @@ -1939,18 +1938,18 @@ def _BlockElement end _tmp = true unless _tmp - self.pos = _save18 + self.pos = _save16 break end _tmp = apply(:_EOL) unless _tmp - self.pos = _save18 + self.pos = _save16 break end @result = begin; block_element(self, position, symbol, args, nil); end _tmp = true unless _tmp - self.pos = _save18 + self.pos = _save16 end break end # end sequence @@ -2374,26 +2373,19 @@ def _BraceArg return _tmp end - # BlockElementContents = BlockElementContent+:c { c } + # BlockElementContents = BlockElementContent*:c { c } def _BlockElementContents _save = self.pos while true # sequence - _save1 = self.pos _ary = [] - _tmp = apply(:_BlockElementContent) - if _tmp - _ary << @result - while true - _tmp = apply(:_BlockElementContent) - _ary << @result if _tmp - break unless _tmp - end - _tmp = true - @result = _ary - else - self.pos = _save1 + while true + _tmp = apply(:_BlockElementContent) + _ary << @result if _tmp + break unless _tmp end + _tmp = true + @result = _ary c = @result unless _tmp self.pos = _save @@ -5111,14 +5103,14 @@ def _Newline Rules[:_HeadlinePrefix] = rule_info("HeadlinePrefix", "< /={1,5}/ > { text.length }") Rules[:_Paragraph] = rule_info("Paragraph", "ParagraphLine+:c {paragraph(self, position, c.flatten)}") Rules[:_ParagraphLine] = rule_info("ParagraphLine", "!Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") - Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, position, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents?:contents \"//}\" Space* EOL {code_block_element(self, position, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents?:contents \"//}\" Space* EOL {block_element(self, position, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, position, symbol, args, nil)})") + Rules[:_BlockElement] = rule_info("BlockElement", "(\"//raw[\" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 \"]\" Space* EOL {raw(self, b, position, r1)} | !\"//raw\" \"//\" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args \"{\" Space* Newline CodeBlockElementContents:contents \"//}\" Space* EOL {code_block_element(self, position, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args \"{\" Space* Newline BlockElementContents:contents \"//}\" Space* EOL {block_element(self, position, symbol, args, contents)} | !\"//raw\" \"//\" ElementName:symbol BracketArg*:args Space* EOL {block_element(self, position, symbol, args, nil)})") Rules[:_RawBlockBuilderSelect] = rule_info("RawBlockBuilderSelect", "\"|\" Space* RawBlockBuilderSelectSub:c Space* \"|\" { c }") Rules[:_RawBlockBuilderSelectSub] = rule_info("RawBlockBuilderSelectSub", "(< AlphanumericAscii+ >:c1 Space* \",\" Space* RawBlockBuilderSelectSub:c2 { [text] + c2 } | < AlphanumericAscii+ >:c1 { [text] })") Rules[:_RawBlockElementArg] = rule_info("RawBlockElementArg", "!\"]\" (\"\\\\]\" { \"]\" } | \"\\\\n\" { \"\\n\" } | < NonNewline > { text })") Rules[:_BracketArg] = rule_info("BracketArg", "\"[\" BracketArgInline*:content \"]\" {bracket_arg(self, position, content)}") Rules[:_BracketArgInline] = rule_info("BracketArgInline", "(InlineElement:c { c } | \"\\\\]\" {text(self, position, \"]\")} | \"\\\\\\\\\" {text(self, position, \"\\\\\")} | < /[^\\r\\n\\]]/ > {text(self, position, text)})") Rules[:_BraceArg] = rule_info("BraceArg", "\"{\" < /([^\\r\\n}\\\\]|\\\\[^\\r\\n])*/ > \"}\" { text }") - Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent+:c { c }") + Rules[:_BlockElementContents] = rule_info("BlockElementContents", "BlockElementContent*:c { c }") Rules[:_BlockElementContent] = rule_info("BlockElementContent", "(SinglelineComment:c { c } | BlockElement:c { c } | Ulist:c | Dlist:c | Olist:c | BlankLine:c { c } | BlockElementParagraph:c { c })") Rules[:_BlockElementParagraph] = rule_info("BlockElementParagraph", "BlockElementParagraphLine+:c {paragraph(self, position, c.flatten)}") Rules[:_BlockElementParagraphLine] = rule_info("BlockElementParagraphLine", "!\"//}\" !BlankLine !SinglelineComment !BlockElement !Ulist !Olist !Dlist SinglelineContent:c Newline { c }") diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 13efed15a..50b2eee96 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -190,32 +190,36 @@ def nonum_end(level) end def notoc_begin(level, label, caption) + buf = "" @nonum_counter += 1 - puts '' if level > 1 + buf << "\n" if level > 1 unless caption.empty? if label.nil? id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}") - puts %Q[#{compile_inline(caption)}] + buf << %Q[#{compile_inline(caption)}] else - puts %Q[#{compile_inline(caption)}] + buf << %Q[#{compile_inline(caption)}] end end + buf end def notoc_end(level) end def nodisp_begin(level, label, caption) + buf = "" @nonum_counter += 1 - puts '' if level > 1 + buf << "\n" if level > 1 unless caption.empty? if label.nil? id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}") - puts %Q[] + buf << %Q[] else - puts %Q[] + buf << %Q[] end end + buf end def nodisp_end(level) @@ -740,27 +744,28 @@ def table_end end def imgtable(lines, id, caption = nil, metric = nil) + buf = "" if !@chapter.image(id).bound? warn "image not bound: #{id}" - image_dummy id, caption, lines - return + return image_dummy(id, caption, lines) end - puts %Q[
      ] + buf << %Q[
      \n] begin - table_header id, caption unless caption.nil? + buf << table_header(id, caption) unless caption.nil? rescue KeyError error "no such table: #{id}" end - imgtable_image(id, caption, metric) + buf << imgtable_image(id, caption, metric) - puts %Q[
      ] + buf << %Q[
      \n] + buf end def imgtable_image(id, caption, metric) metrics = parse_metric("html", metric) - puts %Q[#{escape_html(compile_inline(caption))}] + %Q[#{escape_html(compile_inline(caption))}\n] end def comment(lines, comment = nil) @@ -786,7 +791,7 @@ def footnote(id, str) def indepimage(id, caption="", metric=nil) metrics = parse_metric("html", metric) caption = "" if caption.nil? - buf = %Q[
      ] + buf = %Q[
      ] + "\n" begin buf << %Q[#{caption}\n] rescue @@ -1189,9 +1194,9 @@ def nofunc_text(str) def compile_href(url, label) if @book.config["externallink"] - %Q(#{label.nil? ? escape_html(url) : escape_html(label)}) + %Q(#{label.nil? ? url : label}) else - label.nil? ? escape_html(url) : I18n.t('external_link', [escape_html(label), escape_html(url)]) + label.nil? ? url : I18n.t('external_link', [label, url]) end end diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 8fd84a317..919c9e68f 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -1081,6 +1081,7 @@ def label(id) def tsize(str) @tsize = str + nil end def dtp(str) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index f1910de14..d820a6401 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -134,7 +134,7 @@ def column_begin(level, label, caption) def column_end(level) buf = "" - buf << "\\end{reviewcolumn}\n\n" + buf << "\\end{reviewcolumn}\n" buf end @@ -142,9 +142,12 @@ def captionblock(type, lines, caption) buf = "" buf << "\\begin{reviewminicolumn}\n" unless caption.nil? - buf << "\\reviewminicolumntitle{#{caption}}" + buf << "\\reviewminicolumntitle{#{caption}}\n" end + if lines[0] =~ /\A\n/ + lines[0].sub!(/\A\n/, "") + end buf << lines.join("") buf << "\\end{reviewminicolumn}\n" @@ -306,6 +309,7 @@ def cmd(lines, caption = nil, lang = nil) end def common_code_block(id, lines, command, caption, lang) + buf = "" if caption if command =~ /emlist/ || command =~ /cmd/ buf << macro(command + 'caption', "#{caption}") + "\n" @@ -479,8 +483,10 @@ def indepimage(id, caption=nil, metric=nil) alias_method :numberlessimage, :indepimage def node_table(node) - id = node.args[0].to_raw - caption = node.args[1].to_doc + id_node = node.args[0] + id = id_node ? id_node.to_raw : "" + caption_node = node.args[1] + caption = caption_node ? caption_node.to_doc : "" lines = [] node.content.each do |line| lines << line.to_doc @@ -530,7 +536,9 @@ def table_header(id, caption) buf << '\begin{table}[h]' << "\n" buf << macro('reviewtablecaption', caption) << "\n" end - buf << macro('label', table_label(id)) << "\n" + if id.present? + buf << macro('label', table_label(id)) << "\n" + end buf end @@ -586,7 +594,6 @@ def table_end buf << '\end{table}' << "\n" end @table_caption = nil - buf << "\n" buf end @@ -612,6 +619,7 @@ def imgtable(lines, id, caption = nil, metric = nil) buf << '\end{table}' + "\n" if @table_caption @table_caption = nil + buf end def imgtable_image(id, caption, metric) diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index 6aed8d998..fa258eb17 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -356,9 +356,9 @@ def nofunc_text(str) def compile_ruby(base, ruby) if @book.htmlversion == 5 - %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{escape_html(ruby)}#{I18n.t("ruby_postfix")}] + %Q[#{base}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] else - %Q[#{escape_html(base)}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] + %Q[#{base}#{I18n.t("ruby_prefix")}#{ruby}#{I18n.t("ruby_postfix")}] end end diff --git a/lib/review/md2inaobuilder.rb b/lib/review/md2inaobuilder.rb index 4dc290abf..6eec427fa 100644 --- a/lib/review/md2inaobuilder.rb +++ b/lib/review/md2inaobuilder.rb @@ -9,48 +9,54 @@ module ReVIEW class MD2INAOBuilder < MARKDOWNBuilder def paragraph(lines) - puts " " + lines.join - puts "\n" + ## XXX fix; do not use fullwidth space + buf = " " + lines.join << "\n" + blank_reset + buf << "\n" + buf end def list_header(id, caption, lang) lang ||= "" - puts "```#{lang}" - print %Q[●リスト#{@chapter.list(id).number}::#{compile_inline(caption)}\n\n] + buf = "```#{lang}\n" + buf << %Q[●リスト#{@chapter.list(id).number}::#{compile_inline(caption)}\n\n] + buf end def cmd(lines) # WEB+DB では使っていないらしいけど - puts "!!! cmd" + buf = "!!! cmd\n" lines.each do |line| - puts detab(line) + buf << detab(line) + "\n" end - puts "" + buf << "\n" + buf end def dl_begin - puts '
      ' + "
      \n" end def dt(line) - puts "
      #{line}
      " + "
      #{line}
      \n" end def dd(lines) - puts "
      #{lines.join}
      " + "
      #{lines.join}
      \n" end def dl_end - puts '
      ' + "
      \n" end def comment(lines, comment = nil) lines ||= [] lines.unshift comment unless comment.blank? str = lines.join("\n") - puts '' - puts str - puts '' + buf = '' + "\n" + buf << str + "\n" + buf << '' + "\n" + buf end def compile_ruby(base, ruby) diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 657c1bd7f..4a30cd75f 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -220,6 +220,7 @@ require 'review/node' defcodeblock :emlistnum, 0..2, false, [:doc, :raw] defcodeblock :texequation, 0, false defcodeblock :table, 0..2, false, [:raw, :doc] + defcodeblock :imgtable, 0..2, false, [:raw, :doc] defcodeblock :image, 2..3, true, [:raw,:doc,:raw] defcodeblock :box, 0..1, false, [:doc] @@ -229,6 +230,14 @@ require 'review/node' defblock :flushright, 0 defblock :centering, 0 defblock :note, 0..1 + defblock :memo, 0..1 + defblock :info, 0..1 + defblock :important, 0..1 + defblock :caution, 0..1 + defblock :notice, 0..1 + defblock :warning, 0..1 + defblock :tip, 0..1 + defblock :box, 0..1 defblock :comment, 0..1, true defsingle :footnote, 2, [:raw, :doc] @@ -263,6 +272,7 @@ require 'review/node' definline :hd definline :recipe definline :column + definline :tcy definline :abbr definline :acronym @@ -293,6 +303,7 @@ require 'review/node' definline :hidx definline :comment definline :include + definline :tcy defcomplexinline :kw defcomplexinline :ruby @@ -565,9 +576,9 @@ ParagraphLine = !Headline !SinglelineComment !BlockElement !Ulist !Olist !Dlist # There are 3 types of Block Element: raw Block, Code Block, and Normal Block BlockElement = ( "//raw[" RawBlockBuilderSelect?:b RawBlockElementArg*:r1 "]" Space* EOL ~raw(self, b, position, r1) - | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents?:contents "//}" Space* EOL + | !"//raw" "//" ElementName:symbol &{ syntax = syntax_descriptor(symbol); syntax && syntax.code_block? } BracketArg*:args "{" Space* Newline CodeBlockElementContents:contents "//}" Space* EOL ~code_block_element(self, position, symbol, args, contents) - | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents?:contents "//}" Space* EOL + | !"//raw" "//" ElementName:symbol BracketArg*:args "{" Space* Newline BlockElementContents:contents "//}" Space* EOL ~block_element(self, position, symbol, args, contents) | !"//raw" "//" ElementName:symbol BracketArg*:args Space* EOL ~block_element(self, position, symbol, args, nil) ) @@ -597,7 +608,7 @@ BracketArgInline = ( InlineElement:c { c } BraceArg = "{" < /([^\r\n}\\]|\\[^\r\n])*/ > "}" { text } ## Standard BlockElement has nested blocks. Texts in content of block are parsed as Paragraph. -BlockElementContents = BlockElementContent+:c { c } +BlockElementContents = BlockElementContent*:c { c } ### Headline is prohibited. ### Note: do not allow "//}" at front of Paragraph. diff --git a/lib/review/tocparser.rb b/lib/review/tocparser.rb index 10b5f6d6e..685533f36 100644 --- a/lib/review/tocparser.rb +++ b/lib/review/tocparser.rb @@ -108,8 +108,8 @@ def compile_label(line) b = ReVIEW::TEXTBuilder.new dummy_book = ReVIEW::Book::Base.load dummy_chapter = ReVIEW::Book::Chapter.new(dummy_book, 1, '-', nil, StringIO.new) - dummy_loc = Location.new("", StringIO.new) c = ReVIEW::Compiler.new(b) + dummy_loc = Location.new("", c) b.bind(c, dummy_chapter, dummy_loc) c.setup_parser(line) c.parse("Paragraph") diff --git a/test/test_compiler.rb b/test/test_compiler.rb index cd31cf379..7c84409f3 100644 --- a/test/test_compiler.rb +++ b/test/test_compiler.rb @@ -18,9 +18,10 @@ def setup "subdirmode" => nil, "stylesheet" => nil, # for HTMLBuilder } - ReVIEW.book.param = @param + @book = Book::Base.new(nil) + @book.config = @param @compiler = ReVIEW::Compiler.new(@builder) - @chapter = Book::Chapter.new(Book::Base.new(nil), 1, '-', nil, StringIO.new) + @chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new) location = Location.new(nil, nil) @builder.bind(@compiler, @chapter, location) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 89141103a..d159a4bc6 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -55,7 +55,7 @@ def on_APPENDIX? end end actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      1.1 this is test.

      \n|, actual + assert_equal %Q|

      A.1 this is test.

      \n|, actual end def test_headline_postdef_roman @@ -71,22 +71,11 @@ def on_APPENDIX? end actual = compile_block("=={test} this is test.\n") - assert_equal %Q|\n

      I.1 this is test.

      \n|, actual + assert_equal %Q|

      I.1 this is test.

      \n|, actual end end end - def test_headline_level2_postdef_roman - @chapter.book.config["appendix_format"] = "roman" - @chapter.instance_eval do - def on_APPENDIX? - true - end - end - actual = compile_block("=={test} this is test.\n") - assert_equal %Q|

      I.1 this is test.

      \n|, actual - end - def test_headline_postdef_alpha Dir.mktmpdir do |dir| Dir.chdir(dir) do @@ -656,7 +645,7 @@ def test_emlist_pygments_lang @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//emlist[][sql]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n") - assert_equal "
      \n
      SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
      \n
      \n", actual + assert_equal "
      \n
      SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
      \n
      \n", actual end def test_emlist_caption @@ -1185,12 +1174,12 @@ def test_href_nest def test_inline_tti_nest actual = compile_inline("test @{aa@{inline test}bb} test2") - assert_equal %Q|test aainline testbb test2|, actual + assert_equal %Q|test aainline testbb test2|, actual end def test_inline_ttib_nest actual = compile_inline("test @{aa@{inline @{te}st}bb} test2") - assert_equal %Q|test aainline testbb test2|, actual + assert_equal %Q|test aainline testbb test2|, actual end diff --git a/test/test_location.rb b/test/test_location.rb index 6cd5911b4..0fcdcdf88 100644 --- a/test/test_location.rb +++ b/test/test_location.rb @@ -1,26 +1,30 @@ require 'test_helper' +require 'review/location' +require 'review/book' +require 'review/textbuilder' require 'review/compiler' class LocationTest < Test::Unit::TestCase def setup + @builder = ReVIEW::TEXTBuilder.new + dummy_book = ReVIEW::Book::Base.load + dummy_chapter = ReVIEW::Book::Chapter.new(dummy_book, 1, '-', nil, StringIO.new("a\nb\nc\n")) + @compiler = ReVIEW::Compiler.new(@builder) + @location = ReVIEW::Location.new("foo", @compiler) + @builder.bind(@compiler, dummy_chapter, @location) + @compiler.setup_parser(dummy_chapter.content) end def test_lineno - f = StringIO.new("a\nb\nc\n") - location = ReVIEW::Location.new("foo", f) - assert_equal 0, location.lineno - f.gets - assert_equal 1, location.lineno + assert_equal 1, @location.lineno end def test_string - location = ReVIEW::Location.new("foo", StringIO.new("a\nb\nc\n")) - assert_equal "foo:0", location.string + assert_equal "foo:1", @location.string end def test_to_s - location = ReVIEW::Location.new("foo", StringIO.new("a\nb\nc\n")) - assert_equal "foo:0", "#{location}" + assert_equal "foo:1", "#{@location}" end def test_to_s_nil diff --git a/test/test_markdownbuilder.rb b/test/test_markdownbuilder.rb index ba06a2ebc..03d95718b 100644 --- a/test/test_markdownbuilder.rb +++ b/test/test_markdownbuilder.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'test_helper' -require 'review/compiler' require 'review/book' +require 'review/compiler' require 'review/markdownbuilder' require 'review/i18n' @@ -10,6 +10,7 @@ class MARKDOWNBuilderTest < Test::Unit::TestCase include ReVIEW def setup + ReVIEW::I18n.setup @builder = MARKDOWNBuilder.new() @config = { "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder @@ -140,9 +141,9 @@ def test_table assert_equal "|testA|testB|\n|:--|:--|\n|contentA|contentB|\n\n", actual end - def test_ruby - actual = compile_block("@{謳,うた}い文句") - assert_equal "うたい文句\n\n", actual + def test_inline_ruby + actual = compile_inline("@{謳,うた}い文句") + assert_equal "うたい文句", actual end end diff --git a/test/test_md2inaobuilder.rb b/test/test_md2inaobuilder.rb index 1ee2b3cf5..1f72a292f 100644 --- a/test/test_md2inaobuilder.rb +++ b/test/test_md2inaobuilder.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'test_helper' -require 'review/compiler' require 'review/book' +require 'review/compiler' require 'review/md2inaobuilder' require 'review/i18n' @@ -10,6 +10,7 @@ class MD2INAOBuilderTest < Test::Unit::TestCase include ReVIEW def setup + ReVIEW::I18n.setup @builder = MD2INAOBuilder.new() @config = { "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder @@ -25,7 +26,7 @@ def setup end def test_paragraph - actual = compile_block("Hello, world!") + actual = compile_block("Hello, world!\n") assert_equal " Hello, world!\n\n", actual end @@ -59,16 +60,16 @@ def test_list def test_comment actual = compile_block("//comment{\nHello, world!\n//}\n") - assert_equal "\nHello, world!\n\n", actual + assert_equal "\n Hello, world!\n\n\n\n", actual end def test_ruby_mono - actual = compile_block("@{謳,うた}い文句") + actual = compile_block("@{謳,うた}い文句\n") assert_equal " 謳(うた)い文句\n\n", actual end def test_ruby_group - actual = compile_block("@{欠伸,あくび}が出る") + actual = compile_block("@{欠伸,あくび}が出る\n") assert_equal " 欠伸(あくび)が出る\n\n", actual end From a514b1d24e8695bc61c1a35509981785f76c5d2b Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 16 Nov 2016 09:52:55 +0900 Subject: [PATCH 212/214] delete duplicated definition --- lib/review/compiler.rb | 1 - lib/review/review.kpeg | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index fd0f19f28..67f2d5dea 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -603,7 +603,6 @@ def inline_defined?(name) defblock :notice, 0..1 defblock :warning, 0..1 defblock :tip, 0..1 - defblock :box, 0..1 defblock :comment, 0..1, true defsingle :footnote, 2, [:raw, :doc] diff --git a/lib/review/review.kpeg b/lib/review/review.kpeg index 4a30cd75f..701825f01 100644 --- a/lib/review/review.kpeg +++ b/lib/review/review.kpeg @@ -237,7 +237,6 @@ require 'review/node' defblock :notice, 0..1 defblock :warning, 0..1 defblock :tip, 0..1 - defblock :box, 0..1 defblock :comment, 0..1, true defsingle :footnote, 2, [:raw, :doc] From ffe89eb73f37428a87db8c435c988610589b786c Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 16 Nov 2016 09:53:57 +0900 Subject: [PATCH 213/214] review/compiler does not define module ReVIEW --- test/test_idgxmlbuilder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index 009dbabfe..b65ee863b 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -1,8 +1,8 @@ # encoding: utf-8 require 'test_helper' -require 'review/compiler' require 'review/book' +require 'review/compiler' require 'review/idgxmlbuilder' require 'review/i18n' From 0822bdd7fd9053a8213ae4675d8775c05574462d Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 16 Nov 2016 09:54:11 +0900 Subject: [PATCH 214/214] suppress rubocop --- .rubocop.yml | 1 + lib/review/latexbuilder.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a4c41a3e7..931b5ef09 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,7 @@ AllCops: Exclude: - lib/lineinput.rb - lib/uuid.rb + - lib/review/compiler.rb DisplayCopNames: true inherit_from: .rubocop_todo.yml diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 2e597fa1d..1e8d9ceab 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -103,7 +103,7 @@ def notoc_end(level) end def nodisp_begin(level, label, caption) - buf = "\n" + buf = "\n" buf << macro('clearpage') if @output.pos == 0 buf << macro('addcontentsline', 'toc', HEADLINE[level], caption) # FIXME: headings @@ -145,7 +145,7 @@ def captionblock(type, lines, caption) buf << "\\reviewminicolumntitle{#{caption}}\n" end - if lines[0] =~ /\A\n/ + if lines[0].start_with?("\n") lines[0].sub!(/\A\n/, "") end buf << lines.join("")