Class: JsDuck::Inline::Img

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/inline/img.rb

Overview

Implementation of inline tag @img

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Img

Returns a new instance of Img.



18
19
20
21
22
# File 'lib/jsduck/inline/img.rb', line 18

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

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

Instance Attribute Details

#doc_contextObject

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



16
17
18
# File 'lib/jsduck/inline/img.rb', line 16

def doc_context
  @doc_context
end

#imagesObject

Instance of Img::Dir or Img::DirSet that’s used for looking up image information.



12
13
14
# File 'lib/jsduck/inline/img.rb', line 12

def images
  @images
end

Instance Method Details

#apply_tpl(url, alt_text) ⇒ Object

applies the image template



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsduck/inline/img.rb', line 38

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

#replace(input) ⇒ Object

Takes StringScanner instance.

Looks for inline tag at the current scan pointer position, when found, moves scan pointer forward and performs the apporpriate replacement.



29
30
31
32
33
34
35
# File 'lib/jsduck/inline/img.rb', line 29

def replace(input)
  if input.check(@re)
    input.scan(@re).sub(@re) { apply_tpl($1, $2) }
  else
    false
  end
end