Module: TruncateHTML

Defined in:
lib/middleman-blog/truncate_html.rb

Overview

Class Method Summary collapse

Class Method Details

.content_after_separator(text, separator) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/middleman-blog/truncate_html.rb', line 18

def self.content_after_separator(text, separator)
  text = text.encode('UTF-8') if text.respond_to?(:encode)
  parts = text.split(separator, 2)
  
  if parts.length >= 2
    # Take the last part (which should be after the separator)
    content_after = parts.last
    doc = Nokogiri::HTML::DocumentFragment.parse content_after
    doc.inner_html
  else
    text
  end
end

.truncate_at_length(text, max_length, ellipsis = '...') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/middleman-blog/truncate_html.rb', line 32

def self.truncate_at_length(text, max_length, ellipsis = '...')
  ellipsis_length = ellipsis.length
  text = text.encode('UTF-8') if text.respond_to?(:encode)
  doc = Nokogiri::HTML::DocumentFragment.parse text
  content_length = doc.inner_text.length
  actual_length = max_length - ellipsis_length
  if content_length > actual_length
    doc.truncate(actual_length, ellipsis).inner_html
  else
    text
  end
end

.truncate_at_separator(text, separator) ⇒ Object



12
13
14
15
16
# File 'lib/middleman-blog/truncate_html.rb', line 12

def self.truncate_at_separator(text, separator)
  text = text.encode('UTF-8') if text.respond_to?(:encode)
  doc = Nokogiri::HTML::DocumentFragment.parse text.split(separator).first
  doc.inner_html
end