Class: Metanorma::Html::Drops::FigureDrop

Inherits:
BlockElementDrop show all
Defined in:
lib/metanorma/html/drops/figure_drop.rb

Instance Attribute Summary collapse

Attributes inherited from BlockElementDrop

#content_html, #css_class, #id, #label_html, #type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, image_html: nil, caption_html: nil, key_html: nil, sub_figures_html: nil, css_class: nil) ⇒ FigureDrop

Returns a new instance of FigureDrop.



9
10
11
12
13
14
15
16
17
# File 'lib/metanorma/html/drops/figure_drop.rb', line 9

def initialize(id: nil, image_html: nil, caption_html: nil, key_html: nil,
               sub_figures_html: nil, css_class: nil)
  @id = id
  @image_html = image_html
  @caption_html = caption_html
  @key_html = key_html
  @sub_figures_html = sub_figures_html
  @css_class = css_class
end

Instance Attribute Details

#caption_htmlObject (readonly)

Returns the value of attribute caption_html.



7
8
9
# File 'lib/metanorma/html/drops/figure_drop.rb', line 7

def caption_html
  @caption_html
end

#image_htmlObject (readonly)

Returns the value of attribute image_html.



7
8
9
# File 'lib/metanorma/html/drops/figure_drop.rb', line 7

def image_html
  @image_html
end

#key_htmlObject (readonly)

Returns the value of attribute key_html.



7
8
9
# File 'lib/metanorma/html/drops/figure_drop.rb', line 7

def key_html
  @key_html
end

#sub_figures_htmlObject (readonly)

Returns the value of attribute sub_figures_html.



7
8
9
# File 'lib/metanorma/html/drops/figure_drop.rb', line 7

def sub_figures_html
  @sub_figures_html
end

Class Method Details

.from_model(figure, renderer:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/metanorma/html/drops/figure_drop.rb', line 19

def self.from_model(figure, renderer:)
  id = renderer.safe_attr(figure, :id)
  fig_name = renderer.safe_attr(figure,
                                :fmt_name) || renderer.safe_attr(
                                  figure, :name
                                )
  if id && fig_name
    renderer.register_figure_entry(id: id,
                                   text: renderer.extract_plain_text(fig_name))
  end

  image_html = if figure.image
                 renderer.render_image(figure.image)
               elsif renderer.safe_attr(figure, :source)
                 src = renderer.safe_attr(figure, :source)
                 renderer.render_liquid("_image.html.liquid",
                                        "attrs" => %( src="#{renderer.escape_html(src)}"))
               end

  caption_html = if fig_name || renderer.safe_attr(figure, :name)
                   el = renderer.safe_attr(figure,
                                           :fmt_name) || figure.name
                   renderer.render_inline_element(el)
                 end

  sub_figures_parts = []
  figure.figure&.each do |sub|
    sub_figures_parts << (renderer.render_figure(sub) || "")
  end
  sub_figures_html = sub_figures_parts.join

  key_parts = []
  renderer.safe_attr(figure, :note)&.each do |n|
    key_parts << (renderer.render_note(n) || "")
  end
  renderer.safe_attr(figure, :dl)&.then do |dl|
    key_parts << (renderer.render_definition_list(dl) || "")
  end
  key_html = key_parts.join

  new(
    id: id,
    image_html: image_html,
    caption_html: caption_html,
    key_html: key_html,
    sub_figures_html: sub_figures_html,
    css_class: "figure",
  )
end