Class: Metanorma::Html::Drops::SourcecodeDrop

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

Instance Attribute Summary collapse

Attributes inherited from BlockElementDrop

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

Class Method Summary collapse

Methods inherited from BlockElementDrop

#initialize

Constructor Details

This class inherits a constructor from Metanorma::Html::Drops::BlockElementDrop

Instance Attribute Details

#code_htmlObject (readonly)

Returns the value of attribute code_html.



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

def code_html
  @code_html
end

#langObject (readonly)

Returns the value of attribute lang.



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

def lang
  @lang
end

#name_htmlObject (readonly)

Returns the value of attribute name_html.



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

def name_html
  @name_html
end

Class Method Details

.from_model(sc, renderer:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/metanorma/html/drops/sourcecode_drop.rb', line 9

def self.from_model(sc, renderer:)
  id = renderer.safe_attr(sc, :id)
  lang = renderer.safe_attr(sc, :lang)

  name_html = if sc.name
                renderer.capture_output { renderer.render_inline_element(sc.name) }
              end

  code_text = if sc.body&.content
                sc.body.content
              elsif sc.content
                sc.content
              else
                ""
              end
  raw_text = code_text.gsub("&lt;", "<").gsub("&gt;", ">").gsub("&amp;", "&").gsub("&quot;", "\"")

  new(
    id: id,
    lang: lang,
    name_html: name_html,
    code_html: renderer.escape_html(raw_text),
    css_class: "sourcecode",
  )
end