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.4.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

.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



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
127
128
129
130
# File 'lib/philiprehberger/truncate.rb', line 96

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



71
72
73
74
75
76
77
78
# File 'lib/philiprehberger/truncate.rb', line 71

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/philiprehberger/truncate.rb', line 80

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