Module: Piola::Html

Defined in:
lib/piola/html.rb

Instance Method Summary collapse

Instance Method Details

#html_decodeObject

html to chars



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/piola/html.rb', line 38

def html_decode
  require 'htmlentities'

  str = self

  coder = HTMLEntities.new
  return coder.decode(str)
rescue ArgumentError => e
  if e.message == 'invalid byte sequence in UTF-8'
    str = str.encode( 'UTF-8', 'Windows-1252' )

    return coder.decode(str)
  else
    raise e
  end
end

#html_encodeObject

chars to html



30
31
32
33
34
35
# File 'lib/piola/html.rb', line 30

def html_encode
  require 'htmlentities'

  coder = HTMLEntities.new
  coder.encode(self, :named)
end

#html_leftover?Boolean

Determines if a string might be an html/style/js leftover

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/piola/html.rb', line 56

def html_leftover?
  [
    /\/\*/,
    /\*\//,
    '{',
    '}',
    /document\./i,
    /text\/javascript/i,
    /this_options/i,
    /socialwrap/i,
    /followwrap/i,
    /addtoany_list/i,
    /addto/i,
    /akocomment/i,
    /imagetransform/i,
    /warning\: mysql/i,
    /error\: mysql/i,
    '<',
    '>'
  ].each do |suspect|
    return true if self.match(suspect)
  end
  false
end

#strip_tagsObject

Remove html tags



6
7
8
9
10
11
12
13
14
# File 'lib/piola/html.rb', line 6

def strip_tags
  str = self
  str = str.gsub(/<\/?[^>]*>/, '')
  str = str.gsub(/&lt;.*?&gt;/, '')
  str = str.gsub('&raquo;', '')
  str = str.gsub('&nbsp;', ' ')
  str = str.remove_all_parenthesis
  str
end

#strip_tags_enters(remove_parens = true) ⇒ Object

Remove html tags but leaves enters instead of tags



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/piola/html.rb', line 17

def strip_tags_enters(remove_parens = true)
  str = self
  str = str.gsub(/<\/?[^>]*>/, "\n").gsub('&raquo;', '').gsub('&nbsp;', ' ')

  str = str.split("\n").map do |parragraph|
    parragraph.strip
  end.compact.join("\n")

  str = str.remove_all_parenthesis(false) if remove_parens
  str
end