Skip to content

Commit 78ea670

Browse files
committed
Syntax highlighting using Prism.parse_lex
Completely drops Ripper dependency
1 parent fb15250 commit 78ea670

12 files changed

Lines changed: 544 additions & 402 deletions

File tree

lib/rdoc/generator/markup.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ def markup_code
125125

126126
src = RDoc::TokenStream.to_html @token_stream
127127

128-
# add initial whitespace so that the indent gets calculated correctly
129-
src.prepend(' ' * @token_stream.first[:char_no]) if source_language == 'ruby' && @token_stream.first
130-
131128
# dedent the source
132129
common_indent = src.length
133130
src.scan(/^ *(?=\S)/) do |whitespace|

lib/rdoc/generator/template/aliki/css/rdoc.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,6 @@ main h6 a:hover {
10211021
.ruby-ivar { color: var(--code-orange); }
10221022
.ruby-operator { color: var(--code-green); }
10231023
.ruby-identifier { color: var(--code-blue); }
1024-
.ruby-node { color: var(--code-purple); }
10251024

10261025
.ruby-comment {
10271026
color: var(--color-neutral-500);
@@ -1037,7 +1036,6 @@ main h6 a:hover {
10371036
[data-theme="dark"] .ruby-ivar { color: var(--code-orange); }
10381037
[data-theme="dark"] .ruby-operator { color: var(--code-green); }
10391038
[data-theme="dark"] .ruby-identifier { color: var(--code-blue); }
1040-
[data-theme="dark"] .ruby-node { color: var(--code-purple); }
10411039

10421040
[data-theme="dark"] .ruby-comment {
10431041
color: var(--color-neutral-400);

lib/rdoc/generator/template/darkfish/css/rdoc.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ main h6 {
449449
.ruby-ivar { color: #B57614; } /* Brown */
450450
.ruby-operator { color: #427B58; } /* Dark Teal */
451451
.ruby-identifier { color: #076678; } /* Deep Teal */
452-
.ruby-node { color: #8F3F71; } /* Plum */
453452
.ruby-comment { color: #928374; font-style: italic; } /* Gray */
454453
.ruby-regexp { color: #8F3F71; } /* Plum */
455454
.ruby-value { color: #AF3A03; } /* Dark Orange */

lib/rdoc/markup/to_html.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'cgi/escape'
33
require 'cgi/util' unless defined?(CGI::EscapeExt)
44
require 'prism'
5+
require 'rdoc/parser/ruby_colorizer'
56

67
##
78
# Outputs RDoc markup as HTML.
@@ -321,34 +322,33 @@ def accept_paragraph(paragraph)
321322
@res << "</p>\n"
322323
end
323324

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+
324334
##
325335
# Adds +verbatim+ to the output
326336

327337
def accept_verbatim(verbatim)
328338
text = verbatim.text.rstrip
329339
format = verbatim.format
330340

331-
klass = nil
332-
333341
# Apply Ruby syntax highlighting if
334342
# - explicitly marked as Ruby (via ruby? which accepts :ruby or :rb)
335343
# - no format specified but the text is parseable as Ruby
336344
# 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
352352

353353
if @pipe
354354
@res << "\n<pre><code>#{CGI.escapeHTML text}\n</code></pre>\n"

lib/rdoc/parser/ripper_state_lex.rb

Lines changed: 0 additions & 302 deletions
This file was deleted.

0 commit comments

Comments
 (0)