class OpenStax::Content::Fragment::Html
Attributes
to_html[R]
Public Class Methods
new(node:, title: nil, labels: nil)
click to toggle source
Calls superclass method
OpenStax::Content::Fragment::new
# 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
Public Instance Methods
append(new_node)
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 45 def append(new_node) (node.at_css('body') || node.root) << new_node @to_html = node.to_html end
as_json(*args)
click to toggle source
Calls superclass method
# File lib/openstax/content/fragment/html.rb, line 15 def as_json(*args) # Don't attempt to serialize @node (it would fail) super.except('node') end
blank?()
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 20 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)
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 41 def has_css?(css, custom_css) !node.at_css(css, custom_css).nil? end
html?()
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 33 def html? !blank? end
node()
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 37 def node @node ||= Nokogiri::HTML.fragment to_html end
transform_links!()
click to toggle source
# File lib/openstax/content/fragment/html.rb, line 51 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