Module: Libertree::Model::HasDisplayText

Included in:
Comment, Message, Post
Defined in:
lib/libertree/model/has-display-text.rb

Overview

Provides a “glimpse” instance method to objects that have a “text” field.

Instance Method Summary collapse

Instance Method Details

#glimpse(length = 60) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/libertree/model/has-display-text.rb', line 7

def glimpse( length = 60 )
  set_without_blockquotes = text_to_nodeset
  set_without_blockquotes.xpath('.//blockquote').each(&:remove)
  plain_text = set_without_blockquotes.inner_text.strip

  if plain_text.empty?
    plain_text = text_to_nodeset.inner_text.strip
  end

  plain_text = plain_text.gsub("\n", ' ')
  snippet = plain_text[0...length]
  if plain_text.length > length
    snippet += '...'
  end

  snippet
end

#text_as_htmlObject



25
26
27
# File 'lib/libertree/model/has-display-text.rb', line 25

def text_as_html
  Render.to_html_nodeset(self.text)
end

#text_to_nodesetObject



29
30
31
# File 'lib/libertree/model/has-display-text.rb', line 29

def text_to_nodeset
  Render.to_html_nodeset(self.text, [:no_images, :filter_html])
end