From 10be0f5a5d851c9e3f1fd7a213501b652e467527 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 2 Apr 2014 11:33:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/initializers/string_ext.rb | 42 ------------------------------- 1 file changed, 42 deletions(-) delete mode 100644 config/initializers/string_ext.rb diff --git a/config/initializers/string_ext.rb b/config/initializers/string_ext.rb deleted file mode 100644 index 7b0ef2f..0000000 --- a/config/initializers/string_ext.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'rexml/parsers/pullparser' - -class String - def truncate_html(len = 30, at_end = nil) - p = REXML::Parsers::PullParser.new(self) - tags = [] - new_len = len - results = '' - while p.has_next? && new_len > 0 - p_e = p.pull - case p_e.event_type - when :start_element - tags.push p_e[0] - results << "<#{tags.last}#{attrs_to_s(p_e[1])}>" - when :end_element - results << "" - when :text - results << p_e[0][0..new_len] - new_len -= p_e[0].length - else - results << "" - end - end - if at_end - results << "..." - end - tags.reverse.each do |tag| - results << "" - end - results - end - - private - - def attrs_to_s(attrs) - if attrs.empty? - '' - else - ' ' + attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ') - end - end -end