Sha256: a1ab6adc0f961ff6ad2a6d05a79d957a68842becacfb33b32e55ab45fda4cb29

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Infoboxer
  class Parser
    module Image
      include Tree

      def image
        @context.skip(re.file_namespace) or
          @context.fail!("Something went wrong: it's not image?")

        path = @context.scan_until(/\||\]\]/)
        attrs = @context.matched == '|' ? image_attrs : {}
        Tree::Image.new(path, attrs)
      end

      def image_attrs
        nodes = []

        loop do
          nodes << long_inline(/\||\]\]/)
          break if @context.matched == ']]'
        end

        nodes.map(&method(:image_attr))
             .inject(&:merge)
             .reject { |_k, v| v.nil? || v.empty? }
      end

      def image_attr(nodes)
        # it's caption, and can have inline markup!
        return {caption: ImageCaption.new(nodes)} unless nodes.count == 1 && nodes.first.is_a?(Text)

        case (str = nodes.first.text)
        when /^(thumb)(?:nail)?$/, /^(frame)(?:d)?$/
          {type: Regexp.last_match(1)}
        when 'frameless'
          {type: str}
        when 'border'
          {border: str}
        when /^(baseline|middle|sub|super|text-top|text-bottom|top|bottom)$/
          {alignment: str}
        when /^(\d*)(?:x(\d+))?px$/
          {width: Regexp.last_match(1), height: Regexp.last_match(2)}
        when /^link=(.*)$/i
          {link: Regexp.last_match(1)}
        when /^alt=(.*)$/i
          {alt: Regexp.last_match(1)}
        else # text-only caption
          {caption: ImageCaption.new(nodes)}
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
infoboxer-0.3.3 lib/infoboxer/parser/image.rb
infoboxer-0.3.2 lib/infoboxer/parser/image.rb
infoboxer-0.3.1 lib/infoboxer/parser/image.rb