Sha256: 3f42d858ae32c478d8857c4f5c75746ec52f01639080dd7cd0193521c0908ad0

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

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

module Spirit

  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 {RenderError} 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 RenderError, 'Not really a block image.'
        end
      end

    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spirit-0.2 lib/spirit/render/templates/image.rb
spirit-0.1.0.pre.2 lib/spirit/render/templates/image.rb
spirit-0.1.0.pre.1 lib/spirit/render/templates/image.rb
spirit-0.1.0.pre lib/spirit/render/templates/image.rb