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

Returns a new instance of HeadlineIndex.



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/review/book/index.rb', line 353

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



295
296
297
# File 'lib/review/book/index.rb', line 295

def items
  @items
end

Class Method Details

.parse(src, chap) ⇒ Object



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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/review/book/index.rb', line 297

def self.parse(src, chap)
  items = []
  indexs = []
  headlines = []
  inside_column = false
  inside_block = nil
  column_level = -1
  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)
    if m.nil? || m[1].size > 10 # Ignore too deep index
      next
    end
    index = m[1].size - 2

    # column
    if m[2] == 'column'
      inside_column = true
      column_level = index
      next
    elsif m[2] == '/column'
      inside_column = false
      next
    end
    if indexs.blank? || index <= column_level
      inside_column = false
    end
    next if inside_column
    next if m[4].strip.empty? # no title

    next unless index >= 0
    if indexs.size > (index + 1)
      indexs = indexs.take(index + 1)
      headlines = headlines.take(index + 1)
    end
    if indexs[index].nil?
      (0..index).each do |i|
        indexs[i] ||= 0
      end
    end
    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



366
367
368
369
370
371
372
373
# File 'lib/review/book/index.rb', line 366

def number(id)
  n = @chap.number
  # XXX: remove magic number (move to lib/review/book/chapter.rb)
  if @chap.on_appendix? && @chap.number > 0 && @chap.number < 28
    n = @chap.format_number(false)
  end
  ([n] + self[id].number).join('.')
end