Module: HtmlUtils
- Defined in:
- lib/buzzcore/html_utils.rb,
lib/buzzcore/extra/html_truncate.rb
Constant Summary collapse
- INLINE_BLOCK_SAFE_STYLE =
browser-safe inline block
"display: -moz-inline-box; display: inline-block; zoom: 1; *display:inline"
- RIGID_CRR_BLOCK_STYLE =
see fixed_frame_image
"margin: 0; padding: 0; position: relative; top: 0pt; left: 0pt; display: table-cell; vertical-align: middle; overflow: hidden"
- IMAGE_CRD_STYLE =
"display: block; margin: auto; vertical-align: middle;"
Class Method Summary collapse
- .fixed_frame_image(aImgTag, aFrameW, aFrameH, aImgW = nil, aImgH = nil) ⇒ Object
- .gen_seo_title(*aValues) ⇒ Object
- .html_to_plain(aHtml) ⇒ Object
- .plain_chars(aHtmlText, aMaxLength) ⇒ Object
- .plain_words(aHtmlText, aWords) ⇒ Object
- .url_name_from(*aValues) ⇒ Object
-
.word_safe_truncate(aHtmlText, aMaxLength, aSuffix = '...', aLevel = :BASIC) ⇒ Object
Truncates HTML text.
Class Method Details
.fixed_frame_image(aImgTag, aFrameW, aFrameH, aImgW = nil, aImgH = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/buzzcore/html_utils.rb', line 14 def self.fixed_frame_image(aImgTag,aFrameW,aFrameH,aImgW=nil,aImgH=nil) style = RIGID_CRR_BLOCK_STYLE+";width: #{aFrameW}px; height: #{aFrameH}px" result = "<div style=\"#{INLINE_BLOCK_SAFE_STYLE}\"><div style=\"#{style}\">" aImgTag = XmlUtils.quick_remove_att(aImgTag,'width') if aImgW aImgTag = XmlUtils.quick_remove_att(aImgTag,'height') if aImgH style = XmlUtils.quick_att_from_tag(aImgTag,'style').to_s style += IMAGE_CRD_STYLE style += ";width: #{aImgW}px; height: #{aImgH}px" aImgTag = XmlUtils.quick_set_att(aImgTag,'style',style) result << aImgTag result << "</div></div>" result end |
.gen_seo_title(*aValues) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/buzzcore/html_utils.rb', line 28 def self.gen_seo_title(*aValues) aValues = [aValues] unless aValues.is_a? Array aValues = aValues.map {|f| f.to_s.strip } aValues.delete_if {|v| v.empty? } aValues.join(' ').gsub(/ {2,}/,' ') end |
.html_to_plain(aHtml) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/buzzcore/extra/html_truncate.rb', line 27 def self.html_to_plain(aHtml) return '' if !aHtml aHtml = StringUtils.simplify_whitespace(aHtml) aHtml = Sanitize.clean(aHtml) StringUtils.simplify_whitespace(aHtml) end |
.plain_chars(aHtmlText, aMaxLength) ⇒ Object
39 40 41 42 43 |
# File 'lib/buzzcore/extra/html_truncate.rb', line 39 def self.plain_chars(aHtmlText,aMaxLength) result = CGI.unescapeHTML(Sanitize.clean(aHtmlText)) result = StringUtils.simplify_whitespace(result) StringUtils.word_safe_truncate(result,aMaxLength) end |
.plain_words(aHtmlText, aWords) ⇒ Object
34 35 36 37 |
# File 'lib/buzzcore/extra/html_truncate.rb', line 34 def self.plain_words(aHtmlText,aWords) result = CGI.unescapeHTML(Sanitize.clean(aHtmlText)) StringUtils.crop_to_word_count(result,aWords) end |
.url_name_from(*aValues) ⇒ Object
35 36 37 |
# File 'lib/buzzcore/html_utils.rb', line 35 def self.url_name_from(*aValues) gen_seo_title(*aValues).urlize end |
.word_safe_truncate(aHtmlText, aMaxLength, aSuffix = '...', aLevel = :BASIC) ⇒ Object
Truncates HTML text. Breaks on word boundaries and closes tags. valid values for aLevel are :NO_TAGS, :RESTRICTED, :BASIC and :RELAXED
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/buzzcore/extra/html_truncate.rb', line 8 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 |