Class: OpenStax::Content::Fragment::Html

Inherits:
OpenStax::Content::Fragment show all
Defined in:
lib/openstax/content/fragment/html.rb

Direct Known Subclasses

Embedded, Reading

Instance Attribute Summary collapse

Attributes inherited from OpenStax::Content::Fragment

#labels, #node_id, #title

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Html.



8
9
10
11
12
13
# File 'lib/openstax/content/fragment/html.rb', line 8

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

  @node = Nokogiri::HTML.fragment node.to_html
  @to_html = @node.to_html
end

Instance Attribute Details

#to_htmlObject (readonly)

Returns the value of attribute to_html.



6
7
8
# File 'lib/openstax/content/fragment/html.rb', line 6

def to_html
  @to_html
end

Instance Method Details

#append(new_node) ⇒ Object



47
48
49
50
51
# File 'lib/openstax/content/fragment/html.rb', line 47

def append(new_node)
  (node.at_css('body') || node.root) << new_node

  @to_html = node.to_html
end

#blank?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openstax/content/fragment/html.rb', line 22

def blank?
  return @blank unless @blank.nil?

  @blank = if to_html.nil? || to_html.strip.empty?
    true
  else
    node_without_title = node.dup
    node_without_title.css('[data-type="document-title"]').remove
    text = node_without_title.text
    text.nil? || text.strip.empty?
  end
end

#has_css?(css, custom_css) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/openstax/content/fragment/html.rb', line 43

def has_css?(css, custom_css)
  !node.at_css(css, custom_css).nil?
end

#html?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/openstax/content/fragment/html.rb', line 35

def html?
  !blank?
end

#instance_variablesObject

Serialization methods use #instance_variables to iterate through and dump all instance variables Nokogiri classes are not serializable, so we do not want to dump the @node variable Instead, we recreate it by parsing the HTML again if needed



18
19
20
# File 'lib/openstax/content/fragment/html.rb', line 18

def instance_variables
  super - [ :@node ]
end

#nodeObject



39
40
41
# File 'lib/openstax/content/fragment/html.rb', line 39

def node
  @node ||= Nokogiri::HTML.fragment to_html
end

#transform_links!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/openstax/content/fragment/html.rb', line 53

def transform_links!
  node.css('[href]').each do |link|
    href = link.attributes['href']
    uri = Addressable::URI.parse(href.value) rescue nil

    # Modify only fragment-only links
    next if uri.nil? || uri.absolute? || !uri.path.empty?

    # Abort if there is no target or it contains double quotes
    # or it's still present in this fragment
    target = uri.fragment
    next if target.nil? || target.empty? || target.include?('"') ||
            node.at_css("[id=\"#{target}\"], [name=\"#{target}\"]")

    # Change the link to point to the reference view
    href.value = "#{@reference_view_url}##{target}"
  end unless @reference_view_url.nil?

  @to_html = node.to_html
end