Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use generate_indexes() to generate chapter_index and other indexes in… #1552

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ def htmlversion
end
end

def create_chapter_index
chapter_index = ChapterIndex.new
each_chapter do |chap|
chapter_index.add_item(Index::Item.new(chap.id, chap.number, chap))
end
parts.each do |prt|
if prt.id.present?
chapter_index.add_item(Index::Item.new(prt.id, prt.number, prt))
end
end
chapter_index
end

def generate_indexes
self.each_chapter(&:generate_indexes)
self.parts.map(&:generate_indexes)
@chapter_index = create_chapter_index
end

def parts
@parts ||= read_parts
end
Expand Down Expand Up @@ -136,15 +155,7 @@ def each_chapter_r(&block)

def chapter_index
return @chapter_index if @chapter_index
@chapter_index = ChapterIndex.new
each_chapter do |chap|
@chapter_index.add_item(Index::Item.new(chap.id, chap.number, chap))
end
parts.each do |prt|
if prt.id.present?
@chapter_index.add_item(Index::Item.new(prt.id, prt.number, prt))
end
end
@chapter_index = create_chapter_index
@chapter_index
end

Expand Down
99 changes: 25 additions & 74 deletions lib/review/book/book_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,41 @@ class BookUnit
include TextUtils
attr_reader :book
attr_reader :path
attr_reader :lines
attr_accessor :content

def initialize(book, number, name = nil)
@book = book
@number = number
@name = name
attr_reader :list_index, :table_index, :equation_index, :footnote_index,
:numberless_image_index, :image_index, :icon_index, :indepimage_index,
:headline_index, :column_index

def initialize
if @content
@lines = content.lines
end
end

def generate_indexes
return unless content

@lines = content.lines
@list_index = ListIndex.parse(lines)
@table_index = TableIndex.parse(lines)
@equation_index = EquationIndex.parse(lines)
@footnote_index = FootnoteIndex.parse(lines)
@headline_index = HeadlineIndex.parse(lines, self)
@column_index = ColumnIndex.parse(lines)
end

def dirname
return nil unless @path
File.dirname(@path)
@path && File.dirname(@path)
end

def basename
return nil unless @path
File.basename(@path)
@path && File.basename(@path)
end

def name
return nil unless @name
File.basename(@name, '.*')
@name && File.basename(@name, '.*')
end

alias_method :id, :name
Expand All @@ -61,108 +75,46 @@ def volume
@volume ||= Volume.count_file(path)
end

def lines
# FIXME: we cannot duplicate Enumerator on ruby 1.9 HEAD
(@lines ||= content.lines.to_a).dup
end

def list(id)
list_index[id]
end

def list_index
@list_index ||= ListIndex.parse(lines)
@list_index
end

def table(id)
table_index[id]
end

def table_index
@table_index ||= TableIndex.parse(lines)
@table_index
end

def equation(id)
equation_index[id]
end

def equation_index
@equation_index ||= EquationIndex.parse(lines)
@equation_index
end

def footnote(id)
footnote_index[id]
end

def footnote_index
@footnote_index ||= FootnoteIndex.parse(lines)
@footnote_index
end

def image(id)
return image_index[id] if image_index.key?(id)
return icon_index[id] if icon_index.key?(id)
return numberless_image_index[id] if numberless_image_index.key?(id)
indepimage_index[id]
end

def numberless_image_index
@numberless_image_index ||=
NumberlessImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
end

def image_index
@image_index ||= ImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
@image_index
end

def icon_index
@icon_index ||= IconIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
@icon_index
end

def indepimage_index
@indepimage_index ||=
IndepImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
end

def bibpaper(id)
bibpaper_index[id]
end

def bibpaper_index
raise FileNotFound, "no such bib file: #{@book.bib_file}" unless @book.bib_exist?
@bibpaper_index ||= BibpaperIndex.parse(@book.read_bib.lines.to_a)
@bibpaper_index
end

def headline(caption)
headline_index[caption]
end

def headline_index
@headline_index ||= HeadlineIndex.parse(lines, self)
end

def column(id)
column_index[id]
end

def column_index
@column_index ||= ColumnIndex.parse(lines)
end

def next_chapter
book.next_chapter(self)
end
Expand All @@ -172,8 +124,7 @@ def prev_chapter
end

def image_bound?(item_id)
item = self.image(item_id)
item.path
image(item_id).path
end
end
end
Expand Down
40 changes: 28 additions & 12 deletions lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def self.mkchap_ifexist(book, name, number = nil)
end

def initialize(book, number, name, path, io = nil)
super(book, number, name)
@book = book
@number = number
@name = name
@path = path
@io = io
@title = nil
Expand All @@ -49,17 +51,31 @@ def initialize(book, number, name, path, io = nil)
@content = File.read(@path, mode: 'rt:BOM|utf-8')
@number = nil if %w[nonum nodisp notoc].include?(find_first_header_option)
end
@list_index = nil
@table_index = nil
@equation_index = nil
@footnote_index = nil
@image_index = nil
@icon_index = nil
@numberless_image_index = nil
@indepimage_index = nil
@headline_index = nil
@column_index = nil
@volume = nil

super()
end

def generate_indexes
super

return unless content
@numberless_image_index =
NumberlessImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
@image_index = ImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
@icon_index = IconIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
@indepimage_index =
IndepImageIndex.parse(lines, id,
@book.imagedir,
@book.image_types, @book.config['builder'])
if @book.bib_exist?
@bibpaper_index = BibpaperIndex.parse(@book.read_bib.lines.to_a)
end
end

def find_first_header_option
Expand Down
11 changes: 8 additions & 3 deletions lib/review/book/part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,27 @@ def self.mkpart(chaps)
# if Part is dummy, `number` is nil.
#
def initialize(book, number, chapters, name = '', io = nil)
super(book, number, name)
@book = book
@number = number
@name = name
@chapters = chapters
@path = name
@content = ''
if io
@content = io.read
elsif @path.present? && File.exist?(File.join(@book.config['contentdir'], @path))
@content = File.read(File.join(@book.config['contentdir'], @path), mode: 'rt:BOM|utf-8')
@name = File.basename(@name, '.re')
@name = File.basename(name, '.re')
else
@content = ''
end
if file?
@title = nil
else
@title = name
end
@volume = nil

super()
end

attr_reader :number
Expand Down
4 changes: 4 additions & 0 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def bind(compiler, chapter, location)
if @chapter.present?
@book = @chapter.book
end
@chapter.generate_indexes
if @book
@book.generate_indexes
end
@tabwidth = nil
@tsize = nil
if @book && @book.config
Expand Down
4 changes: 3 additions & 1 deletion test/test_book_chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_lines

book = Book::Base.new
ch = Book::Chapter.new(book, nil, nil, tf.path)
ch.generate_indexes
assert_equal lines, ch.lines

lines = ["1\n", "2\n", '3']
Expand All @@ -78,6 +79,7 @@ def test_lines
tf2.close

ch = Book::Chapter.new(book, nil, nil, tf1.path, tf2.path)
ch.generate_indexes
assert_equal lines, ch.lines # XXX: OK?
end

Expand Down Expand Up @@ -262,7 +264,7 @@ def do_test_index(content, _klass, _list_method, ref_method, opts = {})
o.print content
end
ch = Book::Chapter.new(book, 1, 'chapter', path)

ch.generate_indexes
assert ch.__send__(ref_method, 'abc')
assert ch.__send__(ref_method, 'def')
assert_raises ReVIEW::KeyError do
Expand Down
Loading