Module: Philiprehberger::Truncate

Defined in:
lib/philiprehberger/truncate.rb,
lib/philiprehberger/truncate/version.rb

Constant Summary collapse

DEFAULT_OMISSION =
'...'
OPENING_TAG =
/<(\w+)(?:\s[^>]*)?>/
CLOSING_TAG =
%r{</(\w+)>}
SELF_CLOSING_TAG =
%r{<\w+(?:\s[^>]*)?\s*/>}
TAG_PATTERN =
%r{</?[^>]+>}
SENTENCE_BOUNDARY =
/(?<=[.!?])\s+/
VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.chars(text, count, omission: DEFAULT_OMISSION, position: :end) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/philiprehberger/truncate.rb', line 35

def chars(text, count, omission: DEFAULT_OMISSION, position: :end)
  return '' if text.empty?
  return text if text.length <= count

  case position
  when :start
    chars_start(text, count, omission)
  when :middle
    chars_middle(text, count, omission)
  else
    chars_end(text, count, omission)
  end
end

.html(html, char_count, omission: DEFAULT_OMISSION) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/philiprehberger/truncate.rb', line 92

def html(html, char_count, omission: DEFAULT_OMISSION)
  return '' if html.empty?

  open_tags = []
  visible_chars = 0
  result = +''
  scanner = StringScanner.new(html)

  while !scanner.eos? && visible_chars < char_count
    if scanner.scan(SELF_CLOSING_TAG)
      result << scanner.matched
    elsif scanner.scan(CLOSING_TAG)
      tag_name = scanner[1]
      open_tags.delete_at(open_tags.rindex(tag_name)) if open_tags.include?(tag_name)
      result << scanner.matched
    elsif scanner.scan(OPENING_TAG)
      open_tags.push(scanner[1])
      result << scanner.matched
    elsif scanner.scan(TAG_PATTERN)
      result << scanner.matched
    else
      char = scanner.getch
      visible_chars += 1
      result << char
    end
  end

  if scanner.eos?
    html
  else
    result << omission
    open_tags.reverse_each { |tag| result << "</#{tag}>" }
    result
  end
end

.lines(text, count, omission: DEFAULT_OMISSION) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/philiprehberger/truncate.rb', line 67

def lines(text, count, omission: DEFAULT_OMISSION)
  return '' if text.empty?

  all_lines = text.split("\n", -1)
  return text if all_lines.length <= count

  all_lines.first(count).join("\n") + omission
end

.sentences(text, count, omission: DEFAULT_OMISSION, position: :end) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/philiprehberger/truncate.rb', line 49

def sentences(text, count, omission: DEFAULT_OMISSION, position: :end)
  return '' if text.empty?

  parts = text.split(SENTENCE_BOUNDARY)
  return text if parts.length <= count

  case position
  when :start
    omission + parts.last(count).join(' ')
  when :middle
    half = count / 2
    tail = count - half
    parts.first(half).join(' ') + omission + parts.last(tail).join(' ')
  else
    parts.first(count).join(' ') + omission
  end
end

.strip_html(html, length, omission: DEFAULT_OMISSION) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/philiprehberger/truncate.rb', line 76

def strip_html(html, length, omission: DEFAULT_OMISSION)
  return '' if html.empty?

  plain = html.gsub(/<[^>]+>/, ' ')
  plain = plain.gsub('&amp;', '&')
               .gsub('&lt;', '<')
               .gsub('&gt;', '>')
               .gsub('&quot;', '"')
               .gsub('&apos;', "'")
               .gsub(/&#x([0-9a-fA-F]+);/) { ::Regexp.last_match(1).to_i(16).chr(Encoding::UTF_8) }
               .gsub(/&#([0-9]+);/) { ::Regexp.last_match(1).to_i.chr(Encoding::UTF_8) }
  plain = plain.gsub(/\s+/, ' ').strip

  chars(plain, length, omission: omission)
end

.words(text, count, omission: DEFAULT_OMISSION, position: :end) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/philiprehberger/truncate.rb', line 17

def words(text, count, omission: DEFAULT_OMISSION, position: :end)
  return '' if text.empty?

  parts = text.split(/\s+/)
  return text if parts.length <= count

  case position
  when :start
    omission + parts.last(count).join(' ')
  when :middle
    half = count / 2
    tail = count - half
    parts.first(half).join(' ') + omission + parts.last(tail).join(' ')
  else
    parts.first(count).join(' ') + omission
  end
end