From d6eb070112ff26fd7dcebeb820efdf1b38f8ae11 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 29 May 2021 22:32:45 +0900 Subject: [PATCH 1/4] move @doc_status into compiler builder should not have (global) status --- lib/review/builder.rb | 23 +++++++++------------ lib/review/compiler.rb | 37 ++++++++++++++++++++++++++-------- lib/review/htmlbuilder.rb | 6 +----- lib/review/idgxmlbuilder.rb | 19 +++++------------ lib/review/latexbuilder.rb | 28 ++++++++++++------------- lib/review/markdownbuilder.rb | 5 +---- lib/review/plaintextbuilder.rb | 16 ++------------- lib/review/rstbuilder.rb | 5 +---- lib/review/topbuilder.rb | 5 +---- test/test_htmlbuilder.rb | 3 --- test/test_idgxmlbuilder.rb | 3 --- test/test_latexbuilder.rb | 3 --- test/test_plaintextbuilder.rb | 4 ---- test/test_topbuilder.rb | 4 ---- 14 files changed, 63 insertions(+), 98 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 36ad5f2dd..b2524f928 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -32,14 +32,12 @@ def post_paragraph nil end - attr_accessor :doc_status attr_reader :location def initialize(strict = false, *_args, img_math: nil) @strict = strict @output = nil @logger = ReVIEW.logger - @doc_status = {} @dictionary = {} @img_math = img_math end @@ -88,7 +86,6 @@ def bind(compiler, chapter, location) def builder_init_file @sec_counter = SecCounter.new(5, @chapter) - @doc_status = {} end private :builder_init_file @@ -552,32 +549,30 @@ def captionblock(_type, _lines, _caption, _specialstyle = nil) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}(lines, caption = nil) - check_nested_minicolumn captionblock("#{name}", lines, caption) end def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' if caption puts compile_inline(caption) end end def #{name}_end - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 17 + ), __FILE__, __LINE__ - 13 end - def check_nested_minicolumn - if @doc_status[:minicolumn] - app_error "#{@location}: nested mini-column is not allowed" - end + def in_minicolumn? + @compiler.in_minicolumn? end - def in_minicolumn? - @doc_status[:minicolumn] + def in_column? + @compiler.in_minicolumn? + end + + def in_dt? + @compiler.in_dt? end def minicolumn_block_name?(name) diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 753669176..0025803b6 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -28,6 +28,8 @@ def initialize(builder) ## to decide escaping/non-escaping for text @command_name_stack = [] + @doc_status = nil + @logger = ReVIEW.logger @ignore_errors = builder.is_a?(ReVIEW::IndexBuilder) @@ -52,6 +54,7 @@ def non_escaped_commands def compile(chap) @chapter = chap + @doc_status = {} do_compile if @compile_errors raise ApplicationError, "#{location.filename} cannot be compiled." @@ -269,6 +272,18 @@ def inline_defined?(name) definline :w definline :wb + def in_minicolumn? + @doc_status[:minicolumn] + end + + def in_column? + @doc_status[:column] + end + + def in_dt? + @doc_status[:dt] + end + private def do_compile @@ -362,6 +377,7 @@ def compile_minicolumn_begin(name, caption = nil) return end @minicolumn_name = name + @doc_status[:minicolumn] = name @builder.__send__(mid, caption) end @@ -375,6 +391,8 @@ def compile_minicolumn_end mid = "#{name}_end" @builder.__send__(mid) + + @doc_status[:minicolumn] = nil @minicolumn_name = nil end @@ -443,12 +461,19 @@ def open_tagged_section(tag, level, label, caption) return end @tagged_section.push([tag, level]) + if tag == 'column' + @doc_status[:column] = true + end @builder.__send__(mid, level, label, caption) end def close_tagged_section(tag, level) mid = "#{tag}_end" if @builder.respond_to?(mid) + if tag == 'column' + @doc_status[:column] = nil + end + @builder.__send__(mid, level) else error "builder does not support block op: #{mid}", location: location @@ -523,9 +548,9 @@ def compile_dlist(f) @builder.dl_begin while /\A\s*:/ =~ f.peek # defer compile_inline to handle footnotes - @builder.doc_status[:dt] = true + @doc_status[:dt] = true @builder.dt(text(f.gets.sub(/\A\s*:/, '').strip)) - @builder.doc_status[:dt] = nil + @doc_status[:dt] = nil desc = [] f.until_match(/\A(\S|\s*:|\s+\d+\.\s|\s+\*\s)/) do |line| desc << text(line.strip) @@ -553,9 +578,9 @@ def read_command(f) ignore_inline = @non_parsed_commands.include?(name) @command_name_stack.push(name) args = parse_args(line.sub(%r{\A//[a-z]+}, '').rstrip.chomp('{'), name) - @builder.doc_status[name] = true + @doc_status[name] = true lines = block_open?(line) ? read_block(f, ignore_inline) : nil - @builder.doc_status[name] = nil + @doc_status[name] = nil [name, args, lines] end @@ -698,10 +723,6 @@ def compile_inline(str) @builder.nofunc_text(str) end - def in_minicolumn? - @builder.in_minicolumn? - end - def minicolumn_block_name?(name) @builder.minicolumn_block_name?(name) end diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 5dc8b4385..440e3d7a5 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -246,7 +246,6 @@ def sup_end(_level) end def captionblock(type, lines, caption) - check_nested_minicolumn puts %Q(
) if caption.present? puts %Q(

#{compile_inline(caption)}

) @@ -334,8 +333,6 @@ def note(lines, caption = nil) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' puts %Q(
) if caption.present? puts %Q(

\#{compile_inline(caption)}

) @@ -344,9 +341,8 @@ def #{name}_begin(caption = nil) def #{name}_end puts '
' - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 14 + ), __FILE__, __LINE__ - 11 end def ul_begin diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 2638b073a..364686360 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -65,6 +65,7 @@ def builder_init_file print %Q(\n) print %Q(<#{@rootelement} xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">) @secttags = @book.config['structuredxml'] + @minicolumn_with_caption = nil end private :builder_init_file @@ -955,22 +956,18 @@ def captionblock(type, lines, caption, specialstyle = nil) end def note(lines, caption = nil) - check_nested_minicolumn captionblock('note', lines, caption) end def memo(lines, caption = nil) - check_nested_minicolumn captionblock('memo', lines, caption) end def tip(lines, caption = nil) - check_nested_minicolumn captionblock('tip', lines, caption) end def info(lines, caption = nil) - check_nested_minicolumn captionblock('info', lines, caption) end @@ -983,7 +980,6 @@ def best(lines, caption = nil) end def important(lines, caption = nil) - check_nested_minicolumn captionblock('important', lines, caption) end @@ -992,12 +988,10 @@ def security(lines, caption = nil) end def caution(lines, caption = nil) - check_nested_minicolumn captionblock('caution', lines, caption) end def warning(lines, caption = nil) - check_nested_minicolumn captionblock('warning', lines, caption) end @@ -1010,7 +1004,6 @@ def link(lines, caption = nil) end def notice(lines, caption = nil) - check_nested_minicolumn if caption captionblock('notice-t', lines, caption, 'notice-title') else @@ -1049,12 +1042,10 @@ def expert(lines) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn if '#{name}' == 'notice' && caption.present? - @doc_status[:minicolumn] = '#{name}-t' + @minicolumn_with_caption = true print "<#{name}-t>" else - @doc_status[:minicolumn] = '#{name}' print "<#{name}>" end if caption.present? @@ -1063,14 +1054,14 @@ def #{name}_begin(caption = nil) end def #{name}_end - if '#{name}' == 'notice' && @doc_status[:minicolumn] == 'notice-t' + if '#{name}' == 'notice' && @minicolumn_with_caption print "" + @minicolumn_with_caption = nil else print "" end - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 23 + ), __FILE__, __LINE__ - 21 end def syntaxblock(type, lines, caption) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 59999437f..556ab7cc8 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -195,7 +195,6 @@ def nodisp_end(level) def column_begin(level, label, caption) blank - @doc_status[:column] = true target = nil if label @@ -224,17 +223,16 @@ def column_begin(level, label, caption) def column_end(_level) puts '\\end{reviewcolumn}' blank - @doc_status[:column] = nil end def common_block_begin(type, caption = nil) - check_nested_minicolumn if @book.config.check_version('2', exception: false) - type = 'minicolumn' + command_name = 'reviewminicolumn' + else + command_name = "review#{type}" end - @doc_status[:minicolumn] = type - print "\\begin{review#{type}}" + print "\\begin{#{command_name}}" @doc_status[:caption] = true if @book.config.check_version('2', exception: false) @@ -253,11 +251,12 @@ def common_block_begin(type, caption = nil) def common_block_end(type) if @book.config.check_version('2', exception: false) - type = 'minicolumn' + command_name = 'reviewminicolumn' + else + command_name = "review#{type}" end - puts "\\end{review#{type}}" - @doc_status[:minicolumn] = nil + puts "\\end{#{command_name}}" end CAPTION_TITLES.each do |name| @@ -273,12 +272,13 @@ def #{name}_end end def captionblock(type, lines, caption) - check_nested_minicolumn if @book.config.check_version('2', exception: false) - type = 'minicolumn' + command_name = 'reviewminicolumn' + else + command_name = "review#{type}" end - print "\\begin{review#{type}}" + print "\\begin{#{command_name}}" @doc_status[:caption] = true if @book.config.check_version('2', exception: false) @@ -1130,7 +1130,7 @@ def inline_eq(id) def footnote(id, content) if @book.config['footnotetext'] || @foottext[id] - if @doc_status[:column] + if in_column? warn "//footnote[#{id}] is in the column block. It is recommended to move out of the column block.", location: location end puts macro("footnotetext[#{@chapter.footnote(id).number}]", compile_inline(content.strip)) @@ -1140,7 +1140,7 @@ def footnote(id, content) def inline_fn(id) if @book.config['footnotetext'] macro("footnotemark[#{@chapter.footnote(id).number}]", '') - elsif @doc_status[:caption] || @doc_status[:table] || @doc_status[:column] || @doc_status[:dt] + elsif @doc_status[:caption] || @doc_status[:table] || in_column? || in_dt? @foottext[id] = @chapter.footnote(id).number macro('protect\\footnotemark', '') else diff --git a/lib/review/markdownbuilder.rb b/lib/review/markdownbuilder.rb index eea3c3fbc..325acbf25 100644 --- a/lib/review/markdownbuilder.rb +++ b/lib/review/markdownbuilder.rb @@ -162,8 +162,6 @@ def captionblock(type, lines, caption, _specialstyle = nil) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' puts %Q(
) if caption.present? puts %Q(

\#{compile_inline(caption)}

) @@ -172,9 +170,8 @@ def #{name}_begin(caption = nil) def #{name}_end puts '
' - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 14 + ), __FILE__, __LINE__ - 11 end def hr diff --git a/lib/review/plaintextbuilder.rb b/lib/review/plaintextbuilder.rb index d6dc50890..6b321bfb0 100644 --- a/lib/review/plaintextbuilder.rb +++ b/lib/review/plaintextbuilder.rb @@ -19,7 +19,7 @@ class PLAINTEXTBuilder < Builder Compiler.defblock(:insn, 1) Compiler.defblock(:planning, 0..1) - Compiler.defblock(:best, 0..1) + Compiler.defminicolumn(:best, 0..1) Compiler.defblock(:securty, 0..1) Compiler.defblock(:point, 0..1) Compiler.defblock(:reference, 0) @@ -520,22 +520,18 @@ def centering(lines) end def note(lines, caption = nil) - check_nested_minicolumn base_parablock('note', lines, caption) end def memo(lines, caption = nil) - check_nested_minicolumn base_parablock('memo', lines, caption) end def tip(lines, caption = nil) - check_nested_minicolumn base_parablock('tip', lines, caption) end def info(lines, caption = nil) - check_nested_minicolumn base_parablock('info', lines, caption) end @@ -544,12 +540,10 @@ def planning(lines, caption = nil) end def best(lines, caption = nil) - check_nested_minicolumn base_parablock('best', lines, caption) end def important(lines, caption = nil) - check_nested_minicolumn base_parablock('important', lines, caption) end @@ -558,7 +552,6 @@ def security(lines, caption = nil) end def caution(lines, caption = nil) - check_nested_minicolumn base_parablock('caution', lines, caption) end @@ -571,7 +564,6 @@ def link(lines, caption = nil) end def notice(lines, caption = nil) - check_nested_minicolumn base_parablock('notice', lines, caption) end @@ -600,7 +592,6 @@ def insn(lines, caption = nil) end def warning(lines, caption = nil) - check_nested_minicolumn base_parablock('warning', lines, caption) end @@ -609,8 +600,6 @@ def warning(lines, caption = nil) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' blank if caption.present? puts compile_inline(caption) @@ -619,9 +608,8 @@ def #{name}_begin(caption = nil) def #{name}_end blank - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 14 + ), __FILE__, __LINE__ - 11 end def indepimage(_lines, _id, caption = nil, _metric = nil) diff --git a/lib/review/rstbuilder.rb b/lib/review/rstbuilder.rb index 460f7ab78..2934fa08b 100644 --- a/lib/review/rstbuilder.rb +++ b/lib/review/rstbuilder.rb @@ -586,8 +586,6 @@ def centering(lines) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' puts ".. #{name}::" blank puts " " + compile_inline(caption).to_s unless caption.nil? @@ -596,9 +594,8 @@ def #{name}_begin(caption = nil) def #{name}_end blank - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 14 + ), __FILE__, __LINE__ - 11 end def note(lines, caption = nil) diff --git a/lib/review/topbuilder.rb b/lib/review/topbuilder.rb index 00ea5efd0..1f2f80076 100644 --- a/lib/review/topbuilder.rb +++ b/lib/review/topbuilder.rb @@ -465,16 +465,13 @@ def common_block_end(type, _level) CAPTION_TITLES.each do |name| class_eval %Q( def #{name}_begin(caption = nil) - check_nested_minicolumn - @doc_status[:minicolumn] = '#{name}' common_block_begin('#{name}', nil, nil, caption) end def #{name}_end common_block_end('#{name}', nil) - @doc_status[:minicolumn] = nil end - ), __FILE__, __LINE__ - 11 + ), __FILE__, __LINE__ - 8 end def indepimage(_lines, id, caption = nil, metric = nil) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 01ac7c52a..11754c655 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -2729,7 +2729,6 @@ def test_minicolumn_blocks def test_minicolumn_blocks_nest_error1 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -2745,7 +2744,6 @@ def test_minicolumn_blocks_nest_error1 def test_minicolumn_blocks_nest_error2 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -2762,7 +2760,6 @@ def test_minicolumn_blocks_nest_error2 def test_minicolumn_blocks_nest_error3 %w[memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index 0f872ac70..490d63815 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -429,7 +429,6 @@ def test_minicolumn_blocks def test_minicolumn_blocks_nest_error1 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -445,7 +444,6 @@ def test_minicolumn_blocks_nest_error1 def test_minicolumn_blocks_nest_error2 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -462,7 +460,6 @@ def test_minicolumn_blocks_nest_error2 def test_minicolumn_blocks_nest_error3 %w[memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index c6b8da72f..332ee20b4 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -2245,7 +2245,6 @@ def test_minicolumn_blocks def test_minicolumn_blocks_nest_error1 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -2261,7 +2260,6 @@ def test_minicolumn_blocks_nest_error1 def test_minicolumn_blocks_nest_error2 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -2278,7 +2276,6 @@ def test_minicolumn_blocks_nest_error2 def test_minicolumn_blocks_nest_error3 %w[memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ diff --git a/test/test_plaintextbuilder.rb b/test/test_plaintextbuilder.rb index aaa489ed4..1afa21887 100644 --- a/test/test_plaintextbuilder.rb +++ b/test/test_plaintextbuilder.rb @@ -592,7 +592,6 @@ def test_major_blocks def test_minicolumn_blocks %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}[#{type}1]{ @@ -684,7 +683,6 @@ def test_minicolumn_blocks def test_minicolumn_blocks_nest_error1 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -700,7 +698,6 @@ def test_minicolumn_blocks_nest_error1 def test_minicolumn_blocks_nest_error2 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -717,7 +714,6 @@ def test_minicolumn_blocks_nest_error2 def test_minicolumn_blocks_nest_error3 %w[memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ diff --git a/test/test_topbuilder.rb b/test/test_topbuilder.rb index 0af665b7b..47fe3a879 100644 --- a/test/test_topbuilder.rb +++ b/test/test_topbuilder.rb @@ -755,7 +755,6 @@ def test_minicolumn_blocks } %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}[#{type}1]{ @@ -872,7 +871,6 @@ def test_minicolumn_blocks def test_minicolumn_blocks_nest_error1 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -888,7 +886,6 @@ def test_minicolumn_blocks_nest_error1 def test_minicolumn_blocks_nest_error2 %w[note memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ @@ -905,7 +902,6 @@ def test_minicolumn_blocks_nest_error2 def test_minicolumn_blocks_nest_error3 %w[memo tip info warning important caution notice].each do |type| - @builder.doc_status.clear src = <<-EOS //#{type}{ From 59b8a51c5ea58b9ef27c522928055bdfe6fc3b1b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 29 May 2021 23:18:55 +0900 Subject: [PATCH 2/4] fix caption --- lib/review/latexbuilder.rb | 79 +++++++++++++++----------------------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 556ab7cc8..17b076029 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -143,9 +143,7 @@ def headline(level, label, caption) prefix = '*' end blank unless @output.pos == 0 - @doc_status[:caption] = true - puts macro(headline_name + prefix, compile_inline(caption)) - @doc_status[:caption] = nil + puts macro(headline_name + prefix, compile_caption_text(caption)) if prefix == '*' && level <= @book.config['toclevel'].to_i puts "\\addcontentsline{toc}{#{headline_name}}{#{compile_inline(caption)}}" end @@ -161,9 +159,7 @@ def headline(level, label, caption) def nonum_begin(level, _label, caption) blank unless @output.pos == 0 - @doc_status[:caption] = true - puts macro(HEADLINE[level] + '*', compile_inline(caption)) - @doc_status[:caption] = nil + puts macro(HEADLINE[level] + '*', compile_caption_text(caption)) puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption)) end @@ -172,9 +168,7 @@ def nonum_end(level) def notoc_begin(level, _label, caption) blank unless @output.pos == 0 - @doc_status[:caption] = true - puts macro(HEADLINE[level] + '*', compile_inline(caption)) - @doc_status[:caption] = nil + puts macro(HEADLINE[level] + '*', compile_caption_text(caption)) end def notoc_end(level) @@ -203,17 +197,15 @@ def column_begin(level, label, caption) target = "\\hypertarget{#{column_label(caption)}}{}" end - @doc_status[:caption] = true if @book.config.check_version('2', exception: false) puts '\\begin{reviewcolumn}' puts target - puts macro('reviewcolumnhead', nil, compile_inline(caption)) + puts macro('reviewcolumnhead', nil, compile_caption_text(caption)) else # ver.3 print '\\begin{reviewcolumn}' - puts "[#{compile_inline(caption)}#{target}]" + puts "[#{compile_caption_text(caption)}#{target}]" end - @doc_status[:caption] = nil if level <= @book.config['toclevel'].to_i puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}" @@ -234,19 +226,17 @@ def common_block_begin(type, caption = nil) print "\\begin{#{command_name}}" - @doc_status[:caption] = true if @book.config.check_version('2', exception: false) puts if caption.present? - puts "\\reviewminicolumntitle{#{compile_inline(caption)}}" + puts "\\reviewminicolumntitle{#{compile_caption_text(caption)}}" end else if caption.present? - print "[#{compile_inline(caption)}]" + print "[#{compile_caption_text(caption)}]" end puts end - @doc_status[:caption] = nil end def common_block_end(type) @@ -280,20 +270,18 @@ def captionblock(type, lines, caption) print "\\begin{#{command_name}}" - @doc_status[:caption] = true if @book.config.check_version('2', exception: false) puts if caption.present? - puts "\\reviewminicolumntitle{#{compile_inline(caption)}}" + puts "\\reviewminicolumntitle{#{compile_caption_text(caption)}}" end else if caption.present? - print "[#{compile_inline(caption)}]" + print "[#{compile_caption_text(caption)}]" end puts end - @doc_status[:caption] = nil blocked_lines = split_paragraph(lines) puts blocked_lines.join("\n\n") @@ -464,27 +452,25 @@ def cmd(lines, caption = nil, lang = nil) end def common_code_block(id, lines, command, caption, _lang) - @doc_status[:caption] = true captionstr = nil unless @book.config.check_version('2', exception: false) puts '\\begin{reviewlistblock}' end if caption.present? if command =~ /emlist/ || command =~ /cmd/ || command =~ /source/ - captionstr = macro(command + 'caption', compile_inline(caption)) + captionstr = macro(command + 'caption', compile_caption_text(caption)) else begin if get_chap.nil? - captionstr = macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header_without_chapter', [@chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}") + captionstr = macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header_without_chapter', [@chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_caption_text(caption)}") else - captionstr = macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header', [get_chap, @chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}") + captionstr = macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header', [get_chap, @chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_caption_text(caption)}") end rescue KeyError app_error "no such list: #{id}" end end end - @doc_status[:caption] = nil if caption_top?('list') && captionstr puts captionstr @@ -577,14 +563,12 @@ def result_metric(array) def image_image(id, caption = '', metric = nil) captionstr = nil - @doc_status[:caption] = true if @book.config.check_version('2', exception: false) - captionstr = macro('caption', compile_inline(caption)) + "\n" + captionstr = macro('caption', compile_caption_text(caption)) + "\n" else - captionstr = macro('reviewimagecaption', compile_inline(caption)) + "\n" + captionstr = macro('reviewimagecaption', compile_caption_text(caption)) + "\n" end captionstr << macro('label', image_label(id)) - @doc_status[:caption] = nil metrics = parse_metric('latex', metric) # image is always bound here @@ -621,13 +605,11 @@ def image_dummy(id, caption, lines) puts detab(line.rstrip) end puts macro('label', image_label(id)) - @doc_status[:caption] = true if @book.config.check_version('2', exception: false) - puts macro('caption', compile_inline(caption)) if caption.present? + puts macro('caption', compile_caption_text(caption)) if caption.present? elsif caption.present? - puts macro('reviewimagecaption', compile_inline(caption)) + puts macro('reviewimagecaption', compile_caption_text(caption)) end - @doc_status[:caption] = nil puts '\end{reviewdummyimage}' end @@ -676,10 +658,8 @@ def indepimage(lines, id, caption = nil, metric = nil) captionstr = nil if caption.present? - @doc_status[:caption] = true captionstr = macro('reviewindepimagecaption', - %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)})) - @doc_status[:caption] = nil + %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_caption_text(caption)})) end if @chapter.image(id).path @@ -782,15 +762,11 @@ def table_rows(sepidx, rows) def table_header(id, caption) if id.nil? if caption.present? - @doc_status[:caption] = true - puts macro('reviewtablecaption*', compile_inline(caption)) - @doc_status[:caption] = nil + puts macro('reviewtablecaption*', compile_caption_text(caption)) end else if caption.present? - @doc_status[:caption] = true - puts macro('reviewtablecaption', compile_inline(caption)) - @doc_status[:caption] = nil + puts macro('reviewtablecaption', compile_caption_text(caption)) end puts macro('label', table_label(id)) end @@ -907,9 +883,7 @@ def imgtable(lines, id, caption = nil, metric = nil) begin if caption.present? puts "\\begin{table}[h]%%#{id}" - @doc_status[:caption] = true - captionstr = macro('reviewimgtablecaption', compile_inline(caption)) - @doc_status[:caption] = nil + captionstr = macro('reviewimgtablecaption', compile_caption_text(caption)) if caption_top?('table') puts captionstr end @@ -1140,7 +1114,7 @@ def footnote(id, content) def inline_fn(id) if @book.config['footnotetext'] macro("footnotemark[#{@chapter.footnote(id).number}]", '') - elsif @doc_status[:caption] || @doc_status[:table] || in_column? || in_dt? + elsif in_caption? || in_column? || in_dt? @foottext[id] = @chapter.footnote(id).number macro('protect\\footnotemark', '') else @@ -1420,5 +1394,16 @@ def image_ext def olnum(num) @ol_num = num.to_i end + + def in_caption? + @in_caption + end + + def compile_caption_text(caption) + @in_caption = true + caption_text = @compiler.text(caption) + @in_caption = nil + return caption_text + end end end From 378be47ab5cc4cf30407a12ec45085b8a7591033 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 30 May 2021 12:28:04 +0900 Subject: [PATCH 3/4] left @doc_status for compatibility --- lib/review/builder.rb | 3 +++ lib/review/compiler.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index b2524f928..8f36f0300 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -86,6 +86,9 @@ def bind(compiler, chapter, location) def builder_init_file @sec_counter = SecCounter.new(5, @chapter) + if @compiler.doc_status + @doc_status = @compiler.doc_status ## for compatibility + end end private :builder_init_file diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 0025803b6..a479f4820 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -37,7 +37,7 @@ def initialize(builder) @compile_errors = nil end - attr_reader :builder, :previous_list_type + attr_reader :builder, :previous_list_type, :doc_status def strategy error 'Compiler#strategy is obsoleted. Use Compiler#builder.' From 50c928261815307700b77e4a9cf02f40f1bf7a7e Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 30 May 2021 12:37:14 +0900 Subject: [PATCH 4/4] fix tests --- test/test_builder.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/test_builder.rb b/test/test_builder.rb index a6ebe7380..3df07c7ce 100644 --- a/test/test_builder.rb +++ b/test/test_builder.rb @@ -4,6 +4,10 @@ require 'review/book' class MockCompiler + def doc_status + {} + end + def text(s) [:text, s] end @@ -26,7 +30,7 @@ def test_bind b = Builder.new chap = ReVIEW::Book::Chapter.new(ReVIEW::Book::Base.new, nil, '-', nil) assert_nothing_raised do - b.bind(nil, chap, nil) + b.bind(MockCompiler.new, chap, nil) end end @@ -38,7 +42,7 @@ def test_result b = Builder.new chapter = ReVIEW::Book::Chapter.new(ReVIEW::Book::Base.new, nil, '-', nil) - b.bind(nil, chapter, nil) + b.bind(MockCompiler.new, chapter, nil) assert_equal '', b.result end @@ -83,7 +87,7 @@ def test_compile_inline_backslash def test_inline_missing_ref b = Builder.new chapter = ReVIEW::Book::Chapter.new(ReVIEW::Book::Base.new, 1, 'chap1', nil, StringIO.new) - b.bind(nil, chapter, nil) + b.bind(MockCompiler.new, chapter, nil) e = assert_raises(ReVIEW::ApplicationError) { b.inline_list('unknown|list1') } assert_equal 'unknown list: unknown|list1', e.message e = assert_raises(ReVIEW::ApplicationError) { b.inline_table('unknown|table1') }