Class: JsDuck::Inline::Img
- Inherits:
-
Object
- Object
- JsDuck::Inline::Img
- Defined in:
- lib/jsduck/inline/img.rb
Overview
Implementation of inline tag @img
Instance Attribute Summary collapse
-
#doc_context ⇒ Object
Sets up instance to work in context of particular doc object.
-
#images ⇒ Object
Instance of Img::Dir or Img::DirSet that’s used for looking up image information.
Instance Method Summary collapse
-
#apply_tpl(url, alt_text) ⇒ Object
applies the image template.
-
#initialize(opts = {}) ⇒ Img
constructor
A new instance of Img.
-
#replace(input) ⇒ Object
Takes StringScanner instance.
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_context ⇒ Object
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 |
#images ⇒ Object
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 |