Module: HtmlAwareTruncation

Defined in:
lib/html_aware_truncation.rb,
lib/html_aware_truncation/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.truncate_html(str, length: HtmlAwareTruncation.default_length, omission: HtmlAwareTruncation.default_omission, separator: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/html_aware_truncation.rb', line 12

def truncate_html(str,
                  length: HtmlAwareTruncation.default_length,
                  omission: HtmlAwareTruncation.default_omission,
                  separator: nil)

  HtmlAwareTruncation.truncate_nokogiri_node(
    Nokogiri::HTML::DocumentFragment.parse(str),
    length: length,
    omission: omission,
    separator: separator
  ).to_html
end

.truncate_nokogiri_node(node, length: HtmlAwareTruncation.default_length, omission: HtmlAwareTruncation.default_omission, separator: nil) ⇒ Object

HTML-aware truncation of a Nokogiri::HTML::DocumentFragment, perhaps one you created with ‘Nokogiri::HTML::DocumentFragment.parse(str)` Returns a TODO. (may mutate input?)

See also truncate_string, which will take and return a string, parsing for you for convenience.



32
33
34
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
63
64
65
66
67
68
69
# File 'lib/html_aware_truncation.rb', line 32

def truncate_nokogiri_node(node,
                               length: HtmlAwareTruncation.default_length,
                               omission: HtmlAwareTruncation.default_omission,
                               separator: nil)
  if node.kind_of?(::Nokogiri::XML::Text)
    if node.content.length > length
      allowable_endpoint = [0, length - omission.length].max
      if separator
        allowable_endpoint = (node.content.rindex(separator, allowable_endpoint) || allowable_endpoint)
      end

      ::Nokogiri::XML::Text.new(node.content.slice(0, allowable_endpoint) + omission, node.parent)
    else
      node.dup
    end
  else # DocumentFragment or Element
    return node if node.inner_text.length <= length

    truncated_node = node.dup
    truncated_node.children.remove
    remaining_length = length

    node.children.each do |child|
      if remaining_length == 0
        truncated_node.add_child ::Nokogiri::XML::Text.new(omission, truncated_node)
        break
      elsif remaining_length < 0
        break
      end
      truncated_node.add_child HtmlAwareTruncation.truncate_nokogiri_node(child, length: remaining_length, omission: omission, separator: separator)
      # can end up less than 0 if the child was truncated to fit, that's
      # fine:
      remaining_length = remaining_length - child.inner_text.length

    end
    truncated_node
  end
end