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, #has_key?, item_class, #item_type

Constructor Details

#initialize(items, chap) ⇒ HeadlineIndex

Returns a new instance of HeadlineIndex.



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

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



310
311
312
# File 'lib/review/book/index.rb', line 310

def items
  @items
end

Class Method Details

.parse(src, chap) ⇒ Object



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
352
353
# File 'lib/review/book/index.rb', line 312

def HeadlineIndex.parse(src, chap)
  items = []
  indexs = []
  headlines = []
  inside_column = false
  src.each do |line|
    if m = HEADLINE_PATTERN.match(line)
      next if m[1].size > 10 # Ignore too deep index
      index = m[1].size - 2

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

      if 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{|i| indexs[i] = 0 if indexs[i].nil?}
        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
    end
  end
  new(items, chap)
end

Instance Method Details

#number(id) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/review/book/index.rb', line 365

def number(id)
  n = @chap.number
  if @chap.on_APPENDIX? && @chap.number > 0 && @chap.number < 28
    type = @chap.book.config["appendix_format"].blank? ? "arabic" : @chap.book.config["appendix_format"].downcase.strip
    n = case type
        when "roman"
          ROMAN[@chap.number]
        when "alphabet", "alpha"
          ALPHA[@chap.number]
        else
          # nil, "arabic", etc...
          "#{@chap.number}"
        end
  end
  return ([n] + self[id].number).join(".")
end