Skip to content

Commit

Permalink
Merge pull request #1731 from kmuto/check-crossref
Browse files Browse the repository at this point in the history
check unreferred footnote/endnote
  • Loading branch information
takahashim authored Sep 6, 2021
2 parents 0b886b7 + b007cbe commit 9b0cb19
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 deletions.
80 changes: 63 additions & 17 deletions lib/review/index_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def bind(compiler, chapter, location)
def builder_init_file
super
@headline_stack = []
@crossref = {
footnote: {},
endnote: {}
}

@list_index = ReVIEW::Book::ListIndex.new
@table_index = ReVIEW::Book::TableIndex.new
Expand All @@ -75,6 +79,14 @@ def builder_init_file
private :builder_init_file

def result
%i[footnote endnote].each do |name|
@crossref[name].each_pair do |k, v|
if v == 0
warn "#{@chapter.basename}: #{name} ID #{k} is not referred."
end
end
end

nil
end

Expand Down Expand Up @@ -249,87 +261,111 @@ def read(_lines)

alias_method :lead, :read

def list(_lines, id, _caption, _lang = nil)
def list(lines, id, caption, _lang = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @list_index.size + 1)
@list_index.add_item(item)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def source(_lines, _caption = nil, _lang = nil)
def source(lines, caption = nil, _lang = nil)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def listnum(_lines, id, _caption, _lang = nil)
def listnum(lines, id, caption, _lang = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @list_index.size + 1)
@list_index.add_item(item)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def emlist(lines, caption = nil, lang = nil)
def emlist(lines, caption = nil, _lang = nil)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def emlistnum(lines, caption = nil, lang = nil)
def emlistnum(lines, caption = nil, _lang = nil)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def cmd(lines, caption = nil)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def quote(lines)
lines.each { |line| compile_inline(line) }
end

def image(_lines, id, caption, _metric = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @image_index.size + 1, caption)
@image_index.add_item(item)
compile_inline(caption)
end

def table(_lines, id = nil, caption = nil)
def table(lines, id = nil, caption = nil)
check_id(id)
if id
item = ReVIEW::Book::Index::Item.new(id, @table_index.size + 1, caption)
@table_index.add_item(item)
end
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def emtable(_lines, _caption = nil)
def emtable(_lines, caption = nil)
# item = ReVIEW::Book::TableIndex::Item.new(id, @table_index.size + 1)
# @table_index << item
compile_inline(caption)
end

def comment(lines, comment = nil)
end

def imgtable(_lines, id, _caption = nil, _metric = nil)
def imgtable(_lines, id, caption = nil, _metric = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @table_index.size + 1)
@table_index.add_item(item)

## to find image path
item = ReVIEW::Book::Index::Item.new(id, @indepimage_index.size + 1)
@indepimage_index.add_item(item)
compile_inline(caption)
end

def footnote(id, str)
check_id(id)
@crossref[:footnote][id] ||= 0
item = ReVIEW::Book::Index::Item.new(id, @footnote_index.size + 1, str)
@footnote_index.add_item(item)
compile_inline(str)
end

def endnote(id, str)
check_id(id)
@crossref[:endnote][id] ||= 0
item = ReVIEW::Book::Index::Item.new(id, @endnote_index.size + 1, str)
@endnote_index.add_item(item)
compile_inline(str)
end

def indepimage(_lines, id, _caption = '', _metric = nil)
def indepimage(_lines, id, caption = '', _metric = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @indepimage_index.size + 1)
@indepimage_index.add_item(item)
compile_inline(caption)
end

def numberlessimage(_lines, id, _caption = '', _metric = nil)
def numberlessimage(_lines, id, caption = '', _metric = nil)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @indepimage_index.size + 1)
@indepimage_index.add_item(item)
compile_inline(caption)
end

def hr
Expand All @@ -342,10 +378,12 @@ def label(id)
def blankline
end

def flushright(_lines)
def flushright(lines)
lines.each { |line| compile_inline(line) }
end

def centering(lines)
lines.each { |line| compile_inline(line) }
end

def olnum(_num)
Expand All @@ -354,7 +392,8 @@ def olnum(_num)
def pagebreak
end

def bpo(_lines)
def bpo(lines)
lines.each { |line| compile_inline(line) }
end

def noindent
Expand Down Expand Up @@ -399,11 +438,13 @@ def inline_eq(_id)
''
end

def inline_fn(_id)
def inline_fn(id)
@crossref[:footnote][id] = @crossref[:footnote][id] ? @crossref[:footnote][id] + 1 : 1
''
end

def inline_endnote(_id)
def inline_endnote(id)
@crossref[:endnote][id] = @crossref[:endnote][id] ? @crossref[:endnote][id] + 1 : 1
''
end

Expand Down Expand Up @@ -479,10 +520,12 @@ def text(_str)
''
end

def bibpaper(_lines, id, caption)
def bibpaper(lines, id, caption)
check_id(id)
item = ReVIEW::Book::Index::Item.new(id, @bibpaper_index.size + 1, caption)
@bibpaper_index.add_item(item)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
end

def inline_hd(_id)
Expand Down Expand Up @@ -617,12 +660,13 @@ def error(msg = nil)
# ignore in indexing
end

def texequation(_lines, id = nil, _caption = '')
def texequation(_lines, id = nil, caption = '')
check_id(id)
if id
item = ReVIEW::Book::Index::Item.new(id, @equation_index.size + 1)
@equation_index.add_item(item)
end
compile_inline(caption)
end

def get_chap(_chapter = nil)
Expand All @@ -633,7 +677,9 @@ def extract_chapter_id(_chap_ref)
''
end

def captionblock(_type, _lines, _caption, _specialstyle = nil)
def captionblock(_type, lines, caption, _specialstyle = nil)
compile_inline(caption)
lines.each { |line| compile_inline(line) }
''
end

Expand Down
3 changes: 3 additions & 0 deletions test/test_book_chapter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'book_test_helper'

class ChapterTest < Test::Unit::TestCase
include BookTestHelper

Expand Down Expand Up @@ -174,6 +175,7 @@ def test_table_index

def test_footnote_index
content = <<E
@<fn>{abc}@<fn>{def}@<fn>{xyz}
//footnote[abc][textabc...]
//footnote[def][textdef...]
//footnote[xyz][textxyz...]
Expand All @@ -191,6 +193,7 @@ def test_footnote_index

def test_endnote_index
content = <<E
@<fn>{abc}@<fn>{def}@<fn>{xyz}@<endnote>{abc}@<endnote>{def}@<endnote>{xyz}
//footnote[abc][textabc...]
//footnote[def][textdef...]
//footnote[xyz][textxyz...]
Expand Down
23 changes: 18 additions & 5 deletions test/test_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def setup
@builder = TOPBuilder.new
@config = ReVIEW::Configure.create(config: { 'secnolevel' => 2, 'language' => 'ja' })
@book = Book::Base.new(config: @config)
@log_io = StringIO.new
ReVIEW.logger = ReVIEW::Logger.new(@log_io)
@compiler = ReVIEW::Compiler.new(@builder)
@chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new)
location = Location.new(nil, nil)
Expand All @@ -21,16 +23,19 @@ def setup
end

def test_footnote_index
compile_block("//footnote[foo][bar]\n")
compile_block("@<fn>{foo}\n//footnote[foo][bar]\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
assert_equal 'foo', item.id
assert_equal 'bar', item.content

compile_block("//footnote[foo][bar]\n")
assert_match(/ID foo is not referred/, @log_io.string)
end

def test_footnote_index_with_escape
compile_block('//footnote[foo][bar[\]buz]' + "\n")
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar[\]buz]' + "\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
Expand All @@ -39,7 +44,7 @@ def test_footnote_index_with_escape
end

def test_footnote_index_with_escape2
compile_block('//footnote[foo][bar\\a\\$buz]' + "\n")
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar\\a\\$buz]' + "\n")
fn = @chapter.footnote_index
items = fn.to_a
item = items[0]
Expand All @@ -48,7 +53,7 @@ def test_footnote_index_with_escape2
end

def test_footnote_index_key?
compile_block('//footnote[foo][bar]' + "\n")
compile_block("@<fn>{foo}\n" + '//footnote[foo][bar]' + "\n")
fn = @chapter.footnote_index
assert_equal true, fn.key?('foo')

Expand All @@ -59,7 +64,7 @@ def test_footnote_index_key?
end

def test_endnote_index
compile_block("//endnote[foo][bar]\n//printendnotes\n")
compile_block("@<endnote>{foo}\n//endnote[foo][bar]\n//printendnotes\n")
endnote = @chapter.endnote_index
items = endnote.to_a
item = items[0]
Expand All @@ -69,6 +74,14 @@ def test_endnote_index
# rubocop:disable Style/PreferredHashMethods
assert_equal true, endnote.has_key?('foo')
# rubocop:enable Style/PreferredHashMethods

e = assert_raises(ReVIEW::ApplicationError) do
compile_block("@<endnote>{foo}\n//endnote[foo][bar]\n")
end
assert_equal '//endnote is found but //printendnotes is not found.', e.message

compile_block("//endnote[foo][bar]\n//printendnotes\n")
assert_match(/ID foo is not referred/, @log_io.string)
end

def test_headline_index
Expand Down

0 comments on commit 9b0cb19

Please sign in to comment.