Sha256: 6d281d0908f1a13387d1f2a42e999a92c1089753ec3a48e2d9416df65b2cfae8

Contents?: true

Size: 907 Bytes

Versions: 3

Compression:

Stored size: 907 Bytes

Contents

gem 'sanitize'; require 'sanitize'
gem 'nokogiri'; require 'nokogiri'

module HtmlUtils

	# Truncates HTML text. Breaks on word boundaries and closes tags.
	# valid values for aLevel are :NO_TAGS, :RESTRICTED, :BASIC and :RELAXED
	def self.word_safe_truncate(aHtmlText,aMaxLength,aSuffix='...',aLevel=:BASIC)
		result = StringUtils.word_safe_truncate(aHtmlText,aMaxLength)
		level = case aLevel
			when :NO_TAGS		
				nil
			when :RESTRICTED
				Sanitize::Config::RESTRICTED
			when :BASIC
				Sanitize::Config::BASIC
			when :RELAXED
				Sanitize::Config::RELAXED
			else
				Sanitize::Config::BASIC
		end
		result = level ? Sanitize.clean(result,level) : Sanitize.clean(result)
		result += ' '+aSuffix
		result
	end
	
	def self.html_to_plain(aHtml)
		return '' if !aHtml
		aHtml = StringUtils.simplify_whitespace(aHtml)
		aHtml = Sanitize.clean(aHtml)
		StringUtils.simplify_whitespace(aHtml)
	end

end
	

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
buzzcore-0.5.1 lib/buzzcore/extra/html_truncate.rb
buzzcore-0.5.0 lib/buzzcore/extra/html_truncate.rb
buzzcore-0.4.3 lib/buzzcore/extra/html_truncate.rb