Sha256: a3e6747592d4d32179319dfa461c647a5c5e5512c06c93f5e851e03b311152e3

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

# ~*~ encoding: utf-8 ~*~
require 'nokogiri'

module Aladdin

  module Render

    # Renders a block image with a figure number.
    class Image < Template

      # <img ...>
      IMAGE_TAG = 'img'

      # Name of template file for rendering block images
      TEMPLATE = 'img.haml'

      class << self

        # Parses the given text for a block image.
        def parse(text)
          Image.new text
        end

      end

      # Creates a new image.
      def initialize(html)
        @html = html
        parse_or_raise
      end

      def render(locals={})
        super locals.merge(img: @html, caption: @node['alt'])
      end

      private

      # Parses the given HTML, or raise {ParseError} if it is invalid.
      def parse_or_raise
        frag = Nokogiri::HTML::DocumentFragment.parse(@html)
        if 1 == frag.children.count and
          node = frag.children.first and
          node.is_a? Nokogiri::XML::Element and
          node.name == IMAGE_TAG
          @node = node
        else raise ParseError.new 'Not really a block image.'
        end
      end

    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aladdin-0.0.8 lib/aladdin/render/templates/image.rb
aladdin-0.0.7 lib/aladdin/render/templates/image.rb
aladdin-0.0.6 lib/aladdin/render/templates/image.rb
aladdin-0.0.5 lib/aladdin/render/templates/image.rb
aladdin-0.0.4 lib/aladdin/render/image.rb
aladdin-0.0.3 lib/aladdin/render/image.rb