Class: TextUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/rad/lib/text_utils.rb

Overview

String.class_eval do

def to_url_with_escape
  to_url_without_escape.gsub /[^a-z0-9_-]/, ''
end
alias_method_chain :to_url, :escape

end

Defined Under Namespace

Modules: StringParser

Constant Summary collapse

RELAXED =
{
  :elements => [
    'a', 'b', 'blockquote', 'br', 'caption', 'cite', 'code', 'col',
    'colgroup', 'dd', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
    'i', 'img', 'li', 'ol', 'p', 'pre', 'q', 'small', 'strike', 'strong',
    'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'u',
    'ul', 'div', 'font', 'span'],

  :attributes => {
    :all         => ['class', 'style'],
    'a'          => ['href', 'title', 'rel'],
    'blockquote' => ['cite'],
    'col'        => ['span', 'width'],
    'colgroup'   => ['span', 'width'],
    'img'        => ['align', 'alt', 'height', 'src', 'title', 'width'],
    'ol'         => ['start', 'type'],
    'q'          => ['cite'],
    'table'      => ['summary', 'width'],
    'td'         => ['abbr', 'axis', 'colspan', 'rowspan', 'width'],
    'th'         => ['abbr', 'axis', 'colspan', 'rowspan', 'scope', 'width'],
    'ul'         => ['type']
  },

  :protocols => {
    'a'          => {'href' => ['ftp', 'http', 'https', 'mailto', :relative]},
    'blockquote' => {'cite' => ['http', 'https', :relative]},
    'img'        => {'src'  => ['http', 'https', :relative]},
    'q'          => {'cite' => ['http', 'https', :relative]}
  }
}

Class Method Summary collapse

Class Method Details

.markup(text) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rad/lib/text_utils.rb', line 48

def markup text
  return text if text.blank?

  text = gfm text      
  text = simplified_image_box text            
  text = hide_html_elements text # becouse markdown doesn't apply inside of html elements
  
  text = do_markdown text       

  text = restore_html_elements text      
  
  text = StringParser.urls_to_links text
  
  text = do_sanitaize text
  
  text = embed_metaweb text
  text = embed_tags text
  
  text = text.gsub /[\n]+/, "\n"      
  
  # text = text.gsub("”", "\"").gsub("“", "\"") # hack for Maruku special symbols
  
  # Escape all non-word unicode symbols, otherwise it will raise error when converting to BSON
  text = Iconv.conv('UTF-8//IGNORE//TRANSLIT', 'UTF-8', text)
  
  text
end

.random_string(length = 3) ⇒ Object



76
77
78
79
# File 'lib/rad/lib/text_utils.rb', line 76

def random_string length = 3
  @digits ||= ('a'..'z').to_a + (0..9).to_a
  (0..(length-1)).map{@digits[rand(@digits.size)]}.join
end

.truncate(str_or_html, length) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rad/lib/text_utils.rb', line 81

def truncate str_or_html, length
  str_or_html ||= ""
  
  # Sanitize
  str_or_html = do_sanitaize str_or_html
  
  # Strip from HTML tags
  str_or_html = str_or_html.gsub("<br", " <br").gsub("<p", " <p") # to preserve space in place of <> html elements
  doc = Nokogiri::XML("<div class='root'>#{str_or_html}</div>")
  str = doc.css('.root').first.content

  str = str.gsub(/\s+/, ' ')

  
  # Truncate with no broken words
  if str.length >= length
    shortened = str[0, length]
    splitted = shortened.split(/\s/)
    words = splitted.length
    splitted[0, words-1].join(" ") + ' ...'
  else
    str
  end
end