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 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
parse(src, chap)
click to toggle source
# 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
Public Instance Methods
number(id)
click to toggle source
# 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