ssoroka (owner)

Forks

Revisions

gist: 175139 Download_button fork
public
Public Clone URL: git://gist.github.com/175139.git
Embed All Files: show embed
truncate_html_helper.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  # 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