Class: Repubmark::Elems::Figure

Inherits:
Base
  • Object
show all
Defined in:
lib/repubmark/elems/figure.rb

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#absolute_url, #config, #count_words, #html_class, #own_url, parent?, parents, #relative_url, #to_summary_plain, #word_count

Constructor Details

#initialize(parent, name, alt) ⇒ Figure

Returns a new instance of Figure.



8
9
10
11
12
13
14
15
16
17
# File 'lib/repubmark/elems/figure.rb', line 8

def initialize(parent, name, alt)
  super parent

  alt = String(alt).strip.split.join(' ').freeze
  raise 'Empty alt text' if alt.empty?

  @name = String(name).strip.freeze
  @alt = alt
  @caption = Caption.new self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/repubmark/elems/figure.rb', line 50

def method_missing(method_name, ...)
  if @caption.respond_to? method_name
    @caption.public_send(method_name, ...)
  else
    super
  end
end

Instance Method Details

#caption_htmlObject (private)



64
65
66
67
68
69
70
# File 'lib/repubmark/elems/figure.rb', line 64

def caption_html
  if @caption.empty?
    "#{CGI.escape_html(@alt)}\n"
  else
    @caption.to_html
  end
end

#respond_to_missing?(method_name, _include_private) ⇒ Boolean

Builder methods #

Returns:

  • (Boolean)


46
47
48
# File 'lib/repubmark/elems/figure.rb', line 46

def respond_to_missing?(method_name, _include_private)
  @caption.respond_to?(method_name) || super
end

#srcObject (private)



60
61
62
# File 'lib/repubmark/elems/figure.rb', line 60

def src
  @src ||= own_url "#{config[:images_prefix]}/#@name"
end

#to_gemtextObject



36
37
38
39
40
# File 'lib/repubmark/elems/figure.rb', line 36

def to_gemtext
  caption = @caption.to_gemtext.to_s.strip
  caption = @alt if caption.empty?
  "=> #{src} #{caption}\n".freeze
end

#to_htmlObject

Basic methods #



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/repubmark/elems/figure.rb', line 23

def to_html
  [
    "<div#{html_class(:figure_wrap)}>\n",
    "<figure#{html_class(:figure_self)}>\n",
    "<img src=\"#{src}\" alt=\"#{CGI.escape_html(@alt)}\"/>\n",
    "<figcaption>\n",
    caption_html,
    "</figcaption>\n",
    "</figure>\n",
    "</div>\n",
  ].join.freeze
end