Sha256: 9b8e723a7472db31481e3d9186090a2e051600a28cbf02c0a1dba54d715604b9

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

require 'cgi'
require 'jsduck/logger'

module JsDuck

  # Implementation of inline tag {@img}
  class InlineImg
    # Base path to prefix images from {@img} tags.
    # Defaults to no prefix.
    attr_accessor :base_path

    # This will hold list of all image paths gathered from {@img} tags.
    attr_accessor :images

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

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

      @base_path = nil
      @images = []
    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)
      @images << url
      @tpl.gsub(/(%\w)/) do
        case $1
        when '%u'
          @base_path ? (@base_path + "/" + url) : url
        when '%a'
          CGI.escapeHTML(alt_text||"")
        else
          $1
        end
      end
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jsduck-3.10.0 lib/jsduck/inline_img.rb
jsduck-3.9.1 lib/jsduck/inline_img.rb
jsduck-3.9.0 lib/jsduck/inline_img.rb
jsduck-3.9.0.pre lib/jsduck/inline_img.rb
jsduck-3.8.4 lib/jsduck/inline_img.rb
jsduck-3.8.3 lib/jsduck/inline_img.rb
jsduck-3.8.2 lib/jsduck/inline_img.rb
jsduck-3.8.1 lib/jsduck/inline_img.rb
jsduck-3.8.0 lib/jsduck/inline_img.rb