class ReVIEW::Book::HeadlineIndex

Constants

HEADLINE_PATTERN
Item

Attributes

items[R]

Public Class Methods

new(items, chap) click to toggle source
# File lib/review/book/index.rb, line 362
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
parse(src, chap) click to toggle source
# 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)
      unless %w[nonum notoc nodisp].include?(m[2])
        indexs = indexs.take(index + 1)
      end
      headlines = headlines.take(index + 1)
    end
    if indexs[index].nil?
      (0..index).each do |i|
        indexs[i] ||= 0
      end
    end

    if %w[nonum notoc nodisp].include?(m[2])
      headlines[index] = m[3].present? ? m[3].strip : m[4].strip
      items.push Item.new(headlines.join('|'), nil, m[4].strip)
    else
      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
  new(items, chap)
end

Public Instance Methods

number(id) click to toggle source
# File lib/review/book/index.rb, line 375
def number(id)
  unless self[id].number
    # when notoc
    return ''
  end
  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