|
2 | 2 | require 'cgi/escape' |
3 | 3 | require 'cgi/util' unless defined?(CGI::EscapeExt) |
4 | 4 | require 'prism' |
| 5 | +require 'rdoc/parser/ruby_colorizer' |
5 | 6 |
|
6 | 7 | ## |
7 | 8 | # Outputs RDoc markup as HTML. |
@@ -321,34 +322,33 @@ def accept_paragraph(paragraph) |
321 | 322 | @res << "</p>\n" |
322 | 323 | end |
323 | 324 |
|
| 325 | + # Generate syntax highlighted html for ruby-like text. |
| 326 | + |
| 327 | + def parsable_text_to_html(text) |
| 328 | + tokens = RDoc::Parser::RubyColorizer.colorize(text) |
| 329 | + result = RDoc::TokenStream.to_html tokens |
| 330 | + result = result + "\n" unless result.end_with?("\n") |
| 331 | + result |
| 332 | + end |
| 333 | + |
324 | 334 | ## |
325 | 335 | # Adds +verbatim+ to the output |
326 | 336 |
|
327 | 337 | def accept_verbatim(verbatim) |
328 | 338 | text = verbatim.text.rstrip |
329 | 339 | format = verbatim.format |
330 | 340 |
|
331 | | - klass = nil |
332 | | - |
333 | 341 | # Apply Ruby syntax highlighting if |
334 | 342 | # - explicitly marked as Ruby (via ruby? which accepts :ruby or :rb) |
335 | 343 | # - no format specified but the text is parseable as Ruby |
336 | 344 | # Otherwise, add language class when applicable and skip Ruby highlighting |
337 | | - content = if verbatim.ruby? || (format.nil? && parseable?(text)) |
338 | | - begin |
339 | | - tokens = RDoc::Parser::RipperStateLex.parse text |
340 | | - klass = ' class="ruby"' |
341 | | - |
342 | | - result = RDoc::TokenStream.to_html tokens |
343 | | - result = result + "\n" unless "\n" == result[-1] |
344 | | - result |
345 | | - rescue |
346 | | - CGI.escapeHTML text |
347 | | - end |
348 | | - else |
349 | | - klass = " class=\"#{format}\"" if format |
350 | | - CGI.escapeHTML text |
351 | | - end |
| 345 | + if verbatim.ruby? || (format.nil? && parseable?(text)) |
| 346 | + content = parsable_text_to_html(text) |
| 347 | + klass = ' class="ruby"' |
| 348 | + else |
| 349 | + content = CGI.escapeHTML text |
| 350 | + klass = " class=\"#{format}\"" if format |
| 351 | + end |
352 | 352 |
|
353 | 353 | if @pipe |
354 | 354 | @res << "\n<pre><code>#{CGI.escapeHTML text}\n</code></pre>\n" |
|
0 commit comments