Sha256: 6e3713875288614ebd4040cce96b2b354fd82d625c571e9d00bdcbd55b4e90d0

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

Contents

require 'jsduck/util/html'
require 'jsduck/logger'
require 'pp'

module JsDuck
  module Inline

    # Implementation of inline tag {@img}
    class Img
      # Instance of Img::Dir or Img::DirSet that's used for looking up
      # image information.
      attr_accessor :images

      # Sets up instance to work in context of particular doc object.
      # Used for error reporting.
      attr_accessor :doc_context

      def initialize(opts={})
        @tpl = opts[:img_tpl] || '<img src="%u" alt="%a" width="%w" height="%h"/>'

        @re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m
      end

      # Takes StringScanner instance.
      #
      # Looks for inline tag at the current scan pointer position, when
      # found, moves scan pointer forward and performs the apporpriate
      # replacement.
      def replace(input)
        if input.check(@re)
          input.scan(@re).sub(@re) { apply_tpl($1, $2) }
        else
          false
        end
      end

      # applies the image template
      def apply_tpl(url, alt_text)
        img = @images.get(url)
        if !img
          Logger.warn(:image, "Image #{url} not found.", @doc_context)
          img = {}
        end

        @tpl.gsub(/(%\w)/) do
          case $1
          when '%u'
            img[:relative_path]
          when '%a'
            Util::HTML.escape(alt_text||"")
          when '%w'
            img[:width]
          when '%h'
            img[:height]
          else
            $1
          end
        end
      end
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
jsduck-5.3.4 lib/jsduck/inline/img.rb
jsduck-5.3.3 lib/jsduck/inline/img.rb
jsduck-5.3.2 lib/jsduck/inline/img.rb
jsduck-5.3.1 lib/jsduck/inline/img.rb
jsduck-5.3.0 lib/jsduck/inline/img.rb
jsduck-5.2.0 lib/jsduck/inline/img.rb
jsduck-5.1.0 lib/jsduck/inline/img.rb
jsduck-5.0.1 lib/jsduck/inline/img.rb
jsduck-5.0.0 lib/jsduck/inline/img.rb
jsduck-5.0.0.beta5 lib/jsduck/inline/img.rb
jsduck-5.0.0.beta4 lib/jsduck/inline/img.rb
jsduck-5.0.0.beta3 lib/jsduck/inline/img.rb
jsduck-5.0.0.beta2 lib/jsduck/inline/img.rb