Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/jtag/string.rb

Instance Method Summary collapse

Instance Method Details

#break_camelObject

convert “WikiLink” to “Wiki link”



4
5
6
7
8
9
# File 'lib/jtag/string.rb', line 4

def break_camel
  return downcase if match(/\A[A-Z]+\z/)
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1 \2').
  gsub(/([a-z])([A-Z])/, '\1 \2').
  downcase
end

#strip_allObject



44
45
46
# File 'lib/jtag/string.rb', line 44

def strip_all
  strip_tags.strip_markdown.strip
end

#strip_markdownObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jtag/string.rb', line 11

def strip_markdown
  # strip all Markdown and Liquid tags
  gsub(/\{%.*?%\}/,'').
  gsub(/\[\^.+?\](\: .*?$)?/,'').
  gsub(/\s{0,2}\[.*?\]: .*?$/,'').
  gsub(/\!\[.*?\][\[\(].*?[\]\)]/,"").
  gsub(/\[(.*?)\][\[\(].*?[\]\)]/,"\\1").
  gsub(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/,'').
  gsub(/^\#{1,6}\s*/,'').
  gsub(/(\*{1,2})(\S.*?\S)\1/,"\\2").
  gsub(/(`{3,})(.*?)\1/m,"\\2").
  gsub(/^-{3,}\s*$/,"").
  gsub(/`(.+)`/,"\\1").
  gsub(/\n{2,}/,"\n\n")
end

#strip_tagsObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jtag/string.rb', line 27

def strip_tags
  return CGI.unescapeHTML(
      gsub(/<(script|style|pre|code|figure).*?>.*?<\/\1>/im, '').
      gsub(/<!--.*?-->/m, '').
      gsub(/<(img|hr|br).*?>/i, " ").
      gsub(/<(dd|a|h\d|p|small|b|i|blockquote|li)( [^>]*?)?>(.*?)<\/\1>/i, " \\3 ").
      gsub(/<\/?(dt|a|ul|ol)( [^>]+)?>/i, " ").
      gsub(/<[^>]+?>/, '').
      gsub(/\[\d+\]/, '').
      gsub(/&#8217;/,"'").gsub(/&.*?;/,' ').gsub(/;/,' ')
  ).lstrip.gsub("\xE2\x80\x98","'").gsub("\xE2\x80\x99","'").gsub("\xCA\xBC","'").gsub("\xE2\x80\x9C",'"').gsub("\xE2\x80\x9D",'"').gsub("\xCB\xAE",'"').squeeze(" ")
end

#strip_urlsObject



40
41
42
# File 'lib/jtag/string.rb', line 40

def strip_urls
  gsub(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/i,"")
end