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_comment(str) ⇒ Object



39
40
41
# File 'lib/review/htmlutils.rb', line 39

def escape_comment(str)
  str.gsub('-', '&#45;')
end

#escape_html(str) ⇒ Object Also known as: escape



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



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
75
76
77
# File 'lib/review/htmlutils.rb', line 48

def highlight(ops)
  body = ops[:body] || ''
  if @book.config["highlight"] && @book.config["highlight"]["lang"]
    lexer = @book.config["highlight"]["lang"] # default setting
  else
    lexer = 'text'
  end
  lexer = ops[:lexer] if ops[:lexer].present?
  format = ops[:format] || ''
  options = {:nowrap => true, :noclasses => true}
  if ops[:options] && ops[:options].kind_of?(Hash)
    options.merge!(ops[:options])
  end
  return body if !highlight?

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

#highlight?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/review/htmlutils.rb', line 43

def highlight?
  @book.config["pygments"].present? ||
    @book.config["highlight"] && @book.config["highlight"]["html"] == "pygments"
end

#normalize_id(id) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/review/htmlutils.rb', line 79

def normalize_id(id)
  if id =~ /\A[a-z][a-z0-9_.-]*\Z/i
    return id
  elsif id =~ /\A[0-9_.-][a-z0-9_.-]*\Z/i
    return "id_#{id}" # dummy prefix
  else
    return "id_#{CGI.escape(id.gsub("_", "__")).gsub("%", "_").gsub("+", "-")}" # escape all
  end
end

#strip_html(str) ⇒ Object



35
36
37
# File 'lib/review/htmlutils.rb', line 35

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

#unescape_html(str) ⇒ Object Also known as: unescape



28
29
30
31
# File 'lib/review/htmlutils.rb', line 28

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