Created
August 25, 2009 23:46
-
-
Save ssoroka/175139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# truncates html text to the desired text length. | |
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities. | |
# | |
# accepts options: | |
# :link_to => url | |
def truncate_html(text, max_length = 400, options = {}) | |
return text if text.blank? | |
elipsis = '…' | |
tag_delimiter_count = 0 | |
in_html_entity = false | |
character_count = 0 | |
pre = '' | |
post = '' | |
# figure out where to split | |
text.split('').each{|char| | |
if (character_count < max_length) || (tag_delimiter_count > 0) || in_html_entity | |
if char == '<' | |
tag_delimiter_count += 1 | |
elsif char == '>' | |
tag_delimiter_count -= 1 | |
elsif char == '&' | |
in_html_entity = true | |
# count the first character, but no others. | |
character_count += 1 | |
elsif char == ';' && in_html_entity | |
in_html_entity = false | |
elsif tag_delimiter_count == 0 && !in_html_entity && !%Q(\n\r).include?(char) | |
# don't count cr lf | |
character_count += 1 | |
end | |
pre << char | |
else | |
post << char | |
end | |
} | |
# output! | |
if post.blank? | |
pre | |
else | |
if options[:link_to] | |
pre << %(<div id="text_more"> #{elipsis} #{link_to('More', options[:link_to])}</div>) | |
else | |
appear_click = '$("text_part_2").appear({delay:0.3});$("text_more").fade({duration:0.3});return false;' | |
fade_click = '$("text_part_2").fade({duration:0.3});$("text_more").appear({delay:0.3});return false;' | |
%Q{<span id="text_part_1">#{pre}</span><span id="text_more"> #{elipsis} #{link_to('More', '#', :onclick => appear_click, :class => 'accordion_more')}</span><span id="text_part_2" style="display:none;">#{(post)} #{link_to('Less', '', :onclick => fade_click, :class => 'accordion_less')}</span>} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment