lib/card/content/truncate.rb in card-1.96.8 vs lib/card/content/truncate.rb in card-1.97.0

- old
+ new

@@ -4,10 +4,11 @@ module Truncate ELLIPSES_HTML = '<span class="closed-content-ellipses">...</span>'.freeze def smart_truncate input, words=25 return if input.nil? + truncated, wordstring = truncate input, words # nuke partial tags at end of snippet wordstring.gsub!(/(<[^\>]+)$/, "") wordstring = close_tags wordstring wordstring += ELLIPSES_HTML if truncated @@ -16,11 +17,11 @@ end def truncate input, words wordlist = input.to_s.split l = words.to_i - 1 - l = 0 if l < 0 + l = 0 if l.negative? truncating = wordlist.length > l wordstring = truncating ? wordlist[0..l].join(" ") : input.to_s [truncating, wordstring] end @@ -46,14 +47,16 @@ tags.unshift(t[0]) end # match tags with self closing and mark them as closed wordstring.scan(%r{\<([^\>\s/]+)[^\>]*?/\>}).each do |t| next unless (x = tags.index(t[0])) + tags.slice!(x) end # match close tags wordstring.scan(%r{\</([^\>\s/]+)[^\>]*?\>}).each do |t| next unless (x = tags.rindex(t[0])) + tags.slice!(x) end tags end end