Class: ReVIEW::Book::HeadlineIndex

Inherits:
Index show all
Defined in:
lib/review/book/index.rb

Defined Under Namespace

Classes: Item

Constant Summary collapse

HEADLINE_PATTERN =
/\A(=+)(?:\[(.+?)\])?(?:\{(.+?)\})?(.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

#[], #each, item_class, #item_type, #key?

Constructor Details

#initialize(items, chap) ⇒ HeadlineIndex



323
324
325
326
327
328
329
330
331
# File 'lib/review/book/index.rb', line 323

def initialize(items, chap)
  @items = items
  @chap = chap
  @index = {}
  items.each do |i|
    @logger.warn "warning: duplicate ID: #{i.id}" if @index[i.id]
    @index[i.id] = i
  end
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



275
276
277
# File 'lib/review/book/index.rb', line 275

def items
  @items
end

Class Method Details

.parse(src, chap) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/review/book/index.rb', line 277

def self.parse(src, chap)
  items = []
  indexs = []
  headlines = []
  inside_column = false
  inside_block = nil
  src.each do |line|
    if line =~ %r{\A//[a-z]+.*\{\Z}
      inside_block = true
      next
    elsif line =~ %r{\A//\}}
      inside_block = nil
      next
    elsif inside_block
      next
    end

    m = HEADLINE_PATTERN.match(line)
    next if m.nil? || m[1].size > 10 # Ignore too deep index
    next if m[4].strip.empty? # no title
    index = m[1].size - 2

    # column
    if m[2] == 'column'
      inside_column = true
      next
    elsif m[2] == '/column'
      inside_column = false
      next
    end
    inside_column = false if indexs.blank? || index <= indexs[-1]
    next if inside_column

    next unless index >= 0
    if indexs.size > (index + 1)
      indexs = indexs.take(index + 1)
      headlines = headlines.take(index + 1)
    end
    (0..index).each { |i| indexs[i] = 0 if indexs[i].nil? } if indexs[index].nil?
    indexs[index] += 1
    headlines[index] = m[3].present? ? m[3].strip : m[4].strip
    items.push Item.new(headlines.join('|'), indexs.dup, m[4].strip)
  end
  new(items, chap)
end

Instance Method Details

#number(id) ⇒ Object



333
334
335
336
337
# File 'lib/review/book/index.rb', line 333

def number(id)
  n = @chap.number
  n = @chap.format_number(false) if @chap.on_appendix? && @chap.number > 0 && @chap.number < 28
  ([n] + self[id].number).join('.')
end