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.5.0'

Class Method Summary collapse

Class Method Details

.batch(strings, length, omission: DEFAULT_OMISSION, position: :end) ⇒ Object



17
18
19
# File 'lib/philiprehberger/truncate.rb', line 17

def batch(strings, length, omission: DEFAULT_OMISSION, position: :end)
  strings.map { |s| chars(s, length, omission: omission, position: position) }
end

.bytes(text, byte_count, omission: DEFAULT_OMISSION, position: :end) ⇒ String

Truncate by byte length, preserving UTF-8 boundaries so no partial multi-byte codepoint is emitted. Useful for database column limits (‘VARCHAR(255)` is bytes under MySQL `utf8mb4`), HTTP header budgets, and message-size caps. The omission string’s byte length is included in the budget.

Parameters:

  • text (String)

    the input string

  • byte_count (Integer)

    maximum byte length of the result

  • omission (String) (defaults to: DEFAULT_OMISSION)

    suffix appended when truncated (default ‘’…‘`)

  • position (Symbol) (defaults to: :end)

    ‘:end`, `:start`, or `:middle`

Returns:

  • (String)

    truncated string, at or under ‘byte_count` bytes



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/philiprehberger/truncate.rb', line 82

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

  bytes_in = text.bytesize
  return text if bytes_in <= byte_count

  omission_bytes = omission.bytesize
  return safe_byte_slice(omission, byte_count) if byte_count <= omission_bytes

  case position
  when :start
    bytes_start(text, byte_count, omission, omission_bytes)
  when :middle
    bytes_middle(text, byte_count, omission, omission_bytes)
  else
    bytes_end(text, byte_count, omission, omission_bytes)
  end
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/philiprehberger/truncate.rb', line 39

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/philiprehberger/truncate.rb', line 126

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



101
102
103
104
105
106
107
108
# File 'lib/philiprehberger/truncate.rb', line 101

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/philiprehberger/truncate.rb', line 53

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/philiprehberger/truncate.rb', line 110

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/philiprehberger/truncate.rb', line 21

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