Module: ReVIEW::HTMLUtils

Included in:
HTMLBuilder, HTMLTOCPrinter, IDGXMLBuilder
Defined in:
lib/review/htmlutils.rb

Constant Summary collapse

ESC =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;'
}

Instance Method Summary collapse

Instance Method Details

#escape_html(str) ⇒ Object



21
22
23
24
# File 'lib/review/htmlutils.rb', line 21

def escape_html(str)
  t = ESC
  str.gsub(/[&"<>]/) {|c| t[c] }
end

#highlight(ops) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/review/htmlutils.rb', line 35

def highlight(ops)
  body = ops[:body] || ''
  lexer = ops[:lexer] || ''
  format = ops[:format] || ''

  begin
    require 'pygments'
    begin
      Pygments.highlight(
               unescape_html(body),
               :options => {
                           :nowrap => true,
                           :noclasses => true
                         },
               :formatter => format,
               :lexer => lexer)
    rescue MentosError
      body
    end
  rescue LoadError
      body
  end
end

#strip_html(str) ⇒ Object



31
32
33
# File 'lib/review/htmlutils.rb', line 31

def strip_html(str)
  str.gsub(/<\/?[^>]*>/, "")
end

#unescape_html(str) ⇒ Object



26
27
28
29
# File 'lib/review/htmlutils.rb', line 26

def unescape_html(str)
  # FIXME better code
  str.gsub('&quot;', '"').gsub('&gt;', '>').gsub('&lt;', '<').gsub('&amp;', '&')
end