Module: ReVIEW::HTMLUtils
- Included in:
- HTMLBuilder, HTMLTOCPrinter, IDGXMLBuilder, MARKDOWNBuilder, WEBTOCPrinter
- Defined in:
- lib/review/htmlutils.rb
Constant Summary collapse
- ESC =
{ '&' => '&', '<' => '<', '>' => '>', '"' => '"' }
Instance Method Summary collapse
-
#escape(str) ⇒ Object
(also: #escape_html, #h)
.freeze.
- #escape_comment(str) ⇒ Object
- #highlight(ops) ⇒ Object
- #highlight? ⇒ Boolean
- #highlight_pygments(ops) ⇒ Object
- #highlight_rouge(ops) ⇒ Object
- #normalize_id(id) ⇒ Object
- #strip_html(str) ⇒ Object
- #unescape(str) ⇒ Object (also: #unescape_html)
Instance Method Details
#escape(str) ⇒ Object Also known as: escape_html, h
.freeze
20 21 22 23 |
# File 'lib/review/htmlutils.rb', line 20 def escape(str) t = ESC str.gsub(/[&"<>]/) { |c| t[c] } end |
#escape_comment(str) ⇒ Object
39 40 41 |
# File 'lib/review/htmlutils.rb', line 39 def escape_comment(str) str.gsub('-', '-') end |
#highlight(ops) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/review/htmlutils.rb', line 48 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? ⇒ Boolean
43 44 45 46 |
# File 'lib/review/htmlutils.rb', line 43 def highlight? @book.config['highlight'] && @book.config['highlight']['html'] end |
#highlight_pygments(ops) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/review/htmlutils.rb', line 63 def highlight_pygments(ops) body = ops[:body] || '' format = ops[:format] || '' 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 = { nowrap: true, noclasses: true } if ops[:linenum] [:nowrap] = false [:linenos] = 'inline' end if ops[:options] && ops[:options].is_a?(Hash) .merge!(ops[:options]) end begin require 'pygments' begin Pygments.highlight(body, options: , formatter: format, lexer: lexer) rescue MentosError body end rescue LoadError body end end |
#highlight_rouge(ops) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/review/htmlutils.rb', line 97 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) 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) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/review/htmlutils.rb', line 134 def normalize_id(id) if id =~ /\A[a-z][a-z0-9_.-]*\Z/i id elsif id =~ /\A[0-9_.-][a-z0-9_.-]*\Z/i "id_#{id}" # dummy prefix else "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(%r{</?[^>]*>}, '') end |
#unescape(str) ⇒ Object Also known as: unescape_html
28 29 30 31 |
# File 'lib/review/htmlutils.rb', line 28 def unescape(str) # FIXME: better code str.gsub('"', '"').gsub('>', '>').gsub('<', '<').gsub('&', '&') end |