Class: OpenStax::Content::Fragment::Embedded

Inherits:
Html show all
Defined in:
lib/openstax/content/fragment/embedded.rb

Direct Known Subclasses

Interactive, Video

Constant Summary collapse

TITLE_CSS =

Used to get the title

'[data-type="title"]'
LABEL_ATTRIBUTE =

Used to get the title if there are no title nodes

'[data-label]'
TAGGED_URL_CSS =

CSS to find embedded content urls

'iframe.os-embed, a.os-embed, .os-embed iframe, .os-embed a'
UNTAGGED_URL_CSS =
'iframe, a'
CLASS_ATTRIBUTES =
[ :iframe_classes, :iframe_title, :default_width, :default_height ]

Instance Attribute Summary collapse

Attributes inherited from Html

#to_html

Attributes inherited from OpenStax::Content::Fragment

#labels, #node_id, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Html

#append, #has_css?, #html?, #instance_variables, #node, #transform_links!

Methods inherited from OpenStax::Content::Fragment

#html?

Constructor Details

#initialize(node:, title: nil, labels: nil) ⇒ Embedded

Returns a new instance of Embedded.



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
# File 'lib/openstax/content/fragment/embedded.rb', line 35

def initialize(node:, title: nil, labels: nil)
  super

  @title ||= begin
    title_nodes = @node.css(TITLE_CSS)
    titles = title_nodes.empty? ? @node.css(LABEL_ATTRIBUTE).map do |label|
      label.attr('data-label')
    end : title_nodes.map { |node| node.content.strip }
    titles.uniq.join('; ')
  end

  url_node = @node.at_css(TAGGED_URL_CSS) || @node.css(UNTAGGED_URL_CSS).last

  @width = url_node&.[]('width') || default_width
  @height = url_node&.[]('height') || default_height
  @url = url_node&.[]('src') || url_node&.[]('href')

  if url_node&.name == 'iframe'
    node_classes = url_node['class'].to_s.split(' ') + iframe_classes
    url_node['class'] = node_classes.uniq.join(' ')
    url_node['title'] ||= iframe_title
    # To always force the default iframe size, change ||= to =
    url_node['width'] ||= default_width
    url_node['height'] ||= default_height
  end

  @to_html = @node.to_html
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



33
34
35
# File 'lib/openstax/content/fragment/embedded.rb', line 33

def height
  @height
end

#urlObject (readonly)

Returns the value of attribute url.



33
34
35
# File 'lib/openstax/content/fragment/embedded.rb', line 33

def url
  @url
end

#widthObject (readonly)

Returns the value of attribute width.



33
34
35
# File 'lib/openstax/content/fragment/embedded.rb', line 33

def width
  @width
end

Class Method Details

.inherited(subclass) ⇒ Object



19
20
21
22
23
# File 'lib/openstax/content/fragment/embedded.rb', line 19

def inherited(subclass)
  CLASS_ATTRIBUTES.each do |class_attribute|
    subclass.send "#{class_attribute}=", send(class_attribute)
  end
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/openstax/content/fragment/embedded.rb', line 64

def blank?
  url.nil? || url.empty?
end