module ReVIEW::HTMLUtils
Public Instance Methods
escape(str)
click to toggle source
# File lib/review/htmlutils.rb, line 18 def escape(str) CGI.escapeHTML(str) end
Also aliased as: escape_html, h
escape_comment(str)
click to toggle source
# File lib/review/htmlutils.rb, line 36 def escape_comment(str) str.gsub('-', '-') end
highlight(ops)
click to toggle source
# File lib/review/htmlutils.rb, line 45 def highlight(ops) if @book.config['pygments'].present? raise ReVIEW::ConfigError, %Q('pygments:' in config.yml is obsoleted.) end return ops[:body].to_s unless 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 40 def highlight? @book.config['highlight'] && @book.config['highlight']['html'] end
highlight_pygments(ops)
click to toggle source
# File lib/review/htmlutils.rb, line 60 def highlight_pygments(ops) body = ops[:body] || '' format = ops[:format] || '' lexer = if ops[:lexer].present? ops[:lexer] elsif @book.config['highlight'] && @book.config['highlight']['lang'] @book.config['highlight']['lang'] # default setting else 'text' end options = { nowrap: true, noclasses: true } if ops[:linenum] options[:nowrap] = false options[:linenos] = 'inline' end if ops[:options] && ops[:options].is_a?(Hash) options.merge!(ops[:options]) end begin require 'pygments' begin Pygments.highlight(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 94 def highlight_rouge(ops) body = ops[:body] || '' lexer = if ops[:lexer].present? ops[:lexer] elsif @book.config['highlight'] && @book.config['highlight']['lang'] @book.config['highlight']['lang'] # default setting else '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) unless lexer return body end 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 unless formatter return body end formatter.format(lexer.lex(body)) end
normalize_id(id)
click to toggle source
# File lib/review/htmlutils.rb, line 131 def normalize_id(id) if /\A[a-z][a-z0-9_.-]*\Z/i.match?(id) id elsif /\A[0-9_.-][a-z0-9_.-]*\Z/i.match?(id) "id_#{id}" # dummy prefix else "id_#{CGI.escape(id.gsub('_', '__')).tr('%', '_').tr('+', '-')}" # escape all end end
strip_html(str)
click to toggle source
# File lib/review/htmlutils.rb, line 32 def strip_html(str) str.gsub(%r{</?[^>]*>}, '') end
unescape(str)
click to toggle source
# File lib/review/htmlutils.rb, line 25 def unescape(str) # FIXME: better code str.gsub('"', '"').gsub('>', '>').gsub('<', '<').gsub('&', '&') end
Also aliased as: unescape_html