module ReVIEW::HTMLUtils

Constants

ESC

Public Instance Methods

escape(str)
Alias for: escape_html
escape_comment(str) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 41
def escape_comment(str)
  str.gsub('-', '-')
end
escape_html(str) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 22
def escape_html(str)
  t = ESC
  str.gsub(/[&"<>]/) {|c| t[c] }
end
Also aliased as: escape, h, escape, h
h(str)
Alias for: escape_html
highlight(ops) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 50
def highlight(ops)
  if @book.config["pygments"].present?
    raise ReVIEW::ConfigError, "'pygments:' in config.yml is obsoleted."
  end
  return ops[:body].to_s if !highlight?

  if @book.config["highlight"]["html"] == "pygments"
    highlight_pygments(ops)
  elsif @book.config["highlight"]["html"] == "rouge"
    highlight_rouge(ops)
  else
    raise ReVIEW::ConfigError, "unknown highlight method #{@book.config["highlight"]["html"]} in config.yml."
  end
end
highlight?() click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 45
def highlight?
  @book.config["highlight"] &&
    @book.config["highlight"]["html"]
end
highlight_pygments(ops) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 65
def highlight_pygments(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[:linenum]
    options[:nowrap] = false
    options[:linenos] = 'inline'
  end
  if ops[:options] && ops[:options].kind_of?(Hash)
    options.merge!(ops[:options])
  end

  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_rouge(ops) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 98
def highlight_rouge(ops)
  body = ops[:body] || ''
  if ops[:lexer].present?
    lexer = ops[:lexer]
  elsif @book.config["highlight"] && @book.config["highlight"]["lang"]
    lexer = @book.config["highlight"]["lang"] # default setting
  else
    lexer = 'text'
  end
  format = ops[:format] || ''

  first_line_num = 1 ## default
  if ops[:options] && ops[:options][:linenostart]
    first_line_num = ops[:options][:linenostart]
  end

  require 'rouge'
  lexer = Rouge::Lexer.find(lexer)
  raise "unknown lexer #{lexer}" unless lexer

  formatter = Rouge::Formatters::HTML.new(:css_class => 'highlight')
  if ops[:linenum]
    formatter = Rouge::Formatters::HTMLTable.new(formatter,
                                                 :table_class => 'highlight rouge-table',
                                                 :start_line => first_line_num)
  end
  raise "unknown formatter #{formatter}" unless formatter

  text = unescape_html(body)
  formatter.format(lexer.lex(text))
end
normalize_id(id) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 130
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) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 37
def strip_html(str)
  str.gsub(/<\/?[^>]*>/, "")
end
unescape(str)
Alias for: unescape_html
unescape_html(str) click to toggle source
# File ../../../../../lib/review/htmlutils.rb, line 30
def unescape_html(str)
  # FIXME: better code
  str.gsub('&quot;', '"').gsub('&gt;', '>').gsub('&lt;', '<').gsub('&amp;', '&')
end
Also aliased as: unescape, unescape