Class: ReVIEW::Book::HeadlineIndex

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

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

Constructor Details

#initialize(items, chap) ⇒ HeadlineIndex

Returns a new instance of HeadlineIndex.



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

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



257
258
259
# File 'lib/review/book/index.rb', line 257

def items
  @items
end

Class Method Details

.parse(src, chap) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
322
# File 'lib/review/book/index.rb', line 259

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

Instance Method Details

#number(id) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/review/book/index.rb', line 337

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