class ReVIEW::EPUBMaker::ReVIEWHeaderListener

Listener class to scan HTML and get heading information

The heading information this listener will retrieve is as follows:

Public Class Methods

new(headlines) click to toggle source
# File lib/review/epubmaker/reviewheaderlistener.rb, line 21
def initialize(headlines)
  @level = nil
  @content = ''
  @headlines = headlines
end

Public Instance Methods

tag_end(name) click to toggle source
# File lib/review/epubmaker/reviewheaderlistener.rb, line 43
def tag_end(name)
  if name =~ /\Ah\d+/
    if @id.present?
      @headlines.push({ 'level' => @level,
                        'id' => @id,
                        'title' => @content,
                        'notoc' => @notoc })
    end
    @content = ''
    @level = nil
    @id = nil
    @notoc = nil
  end

  true
end
tag_start(name, attrs) click to toggle source
# File lib/review/epubmaker/reviewheaderlistener.rb, line 27
def tag_start(name, attrs)
  if name =~ /\Ah(\d+)/
    raise "#{name}, #{attrs}" if @level.present?

    @level = $1.to_i
    @id = attrs['id'] if attrs['id'].present?
    @notoc = attrs['notoc'] if attrs['notoc'].present?
  elsif @level.present? # if in <hN> tag
    if name == 'img' && attrs['alt'].present?
      @content << attrs['alt']
    elsif name == 'a' && attrs['id'].present?
      @id = attrs['id']
    end
  end
end
text(text) click to toggle source
# File lib/review/epubmaker/reviewheaderlistener.rb, line 60
def text(text)
  if @level.present?
    @content << text.tr("\t", ' ')
  end
end