class ActsAsContentHighlightable::HtmlNodeParser

Attributes

content[RW]
parsed[RW]

Public Class Methods

new(content) click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 3
def initialize(content)
  @content = content
  @parsed = Nokogiri::HTML.parse(@content)
end

Public Instance Methods

assign_unique_node_identifiers(attribute_name) click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 10
def assign_unique_node_identifiers(attribute_name)
  @running_unique_identifiers = Array.new
  apply_unique_identifiers_to_children(@parsed, attribute_name)
  return self
end
body_content() click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 16
def body_content
  return @parsed.css('body').inner_html
end
body_text() click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 20
def body_text
  return @parsed.text
end

Private Instance Methods

apply_unique_identifiers_to_children(node, attribute_name) click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 26
def apply_unique_identifiers_to_children(node, attribute_name)
  node.children.each do |node|
    if node[attribute_name].blank?
      unique_id = get_unique_key(@running_unique_identifiers)
      node[attribute_name] = unique_id
    end
    @running_unique_identifiers.push(node[attribute_name])
    node = apply_unique_identifiers_to_children(node, attribute_name)
  end
  return node
end
get_unique_key(existing_array) click to toggle source
# File lib/acts_as_content_highlightable/html_node_parser.rb, line 38
def get_unique_key(existing_array)
  unique_id = SecureRandom.hex(4)
  max_iter = 100
  iter = 0
  while(existing_array.include? unique_id and iter < max_iter)
    unique_id = (iter > max_iter/2) ? SecureRandom.hex(5) : SecureRandom.hex(4)
    iter += 1
    end
  return unique_id
end