Sha256: 81ab0bf3f9a25806f1f72559a87078091837ab38640fee9bc1dbafe4a4fa4f5b
Contents?: true
Size: 1.79 KB
Versions: 2
Compression:
Stored size: 1.79 KB
Contents
module TruncateHtml class HtmlTruncator UNPAIRED_TAGS = %w(br hr img) def initialize(original_html) @original_html = original_html end def truncate(options = {}) options[:length] ||= 100 options[:omission] ||= '...' @chars_remaining = options[:length] @open_tags, result = [], [] html_tokens.each do |str| if @chars_remaining > 0 if html_tag?(str) if open_tag?(str) @open_tags << str else open_tags = remove_latest_open_tag(str) end else @chars_remaining -= str.length end result << str else result[-1] += options[:omission] unless options[:omission].nil? @open_tags.reverse_each do |open_tag| result << matching_close_tag(open_tag) end break end end result.join('') end private ############################# def html_tokens @original_html.scan(/<\/?[^>]+>|[\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+/).map do |t| t.gsub( #remove newline characters /\n/,'' ).gsub( #clean out extra consecutive whitespace /\s+/, ' ' ) end end def html_tag?(string) string =~ /<\/?[^>]+>/ ? true : false end def open_tag?(html_tag) html_tag =~ /<(?!(?:#{UNPAIRED_TAGS.join('|')}|\/))[^>]+>/i ? true : false end def remove_latest_open_tag(close_tag) (0...@open_tags.length).to_a.reverse.each do |i| if matching_close_tag(@open_tags[i]) == close_tag @open_tags.delete_at(i) break end end end def matching_close_tag(open_tag) open_tag.gsub(/<(\w+)\s?.*>/, '</\1>').strip end end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
hgimenez-truncate_html-0.1.1 | lib/truncate_html/html_truncator.rb |
truncate_html-0.1.1 | lib/truncate_html/html_truncator.rb |