Sha256: fdacd5dd266c0f1dc9e575093454957e4300c34a57c2bdf5df17f88128926aa2

Contents?: true

Size: 955 Bytes

Versions: 5

Compression:

Stored size: 955 Bytes

Contents

module Boilerpipe::SAX::TagActions
  # Special TagAction for the <FONT> tag, which keeps track of
  # the absolute and relative font size.
  class Font
    FONT_SIZE = /([\+\-]?)([0-9])/

    def start(handler, name, attrs)
      m = FONT_SIZE.match attrs['size']
      if m
        rel = m[1]
        val = m[2].to_i # absolute
        size = rel.empty? ? val : relative(handler.font_size_stack, rel, val)
        handler.font_size_stack << size
      else
        handler.font_size_stack << nil
      end
      false
    end

    def end_tag(handler, name)
      handler.font_size_stack.pop
      false
    end

    def changes_tag_level?
      false
    end

    def relative(font_size_stack, rel, val)
      prev_size = font_size_stack.reverse_each.find { |s| !s.nil? }
      prev_size = 3 if prev_size.nil?

      size = if rel == '+'
               prev_size + val
             else
               prev_size - val
             end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
boilerpipe-ruby-0.5.0 lib/boilerpipe/sax/tag_actions/font.rb
boilerpipe-ruby-0.4.4 lib/boilerpipe/sax/tag_actions/font.rb
boilerpipe-ruby-0.4.3 lib/boilerpipe/sax/tag_actions/font.rb
boilerpipe-ruby-0.4.2 lib/boilerpipe/sax/tag_actions/font.rb
boilerpipe-ruby-0.4.1 lib/boilerpipe/sax/tag_actions/font.rb