Module: YARD::Templates::Helpers::HtmlSyntaxHighlightHelper

Defined in:
lib/yard/templates/helpers/html_syntax_highlight_helper.rb,
lib/yard/templates/helpers/html_syntax_highlight_helper18.rb

Overview

Helper methods for syntax highlighting.

Instance Method Summary (collapse)

Instance Method Details

- (String) html_syntax_highlight_ruby(source)

Highlights Ruby source

Parameters:

  • source (String)

    the Ruby source code

Returns:

  • (String)

    the highlighted Ruby source



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yard/templates/helpers/html_syntax_highlight_helper.rb', line 9

def html_syntax_highlight_ruby(source)
  tokenlist = Parser::Ruby::Legacy::TokenList.new(source)
  tokenlist.map do |s| 
    prettyclass = s.class.class_name.sub(/^Tk/, '').downcase
    prettysuper = s.class.superclass.class_name.sub(/^Tk/, '').downcase

    case s
    when Parser::Ruby::Legacy::RubyToken::TkWhitespace, Parser::Ruby::Legacy::RubyToken::TkUnknownChar
      h s.text
    when Parser::Ruby::Legacy::RubyToken::TkId
      prettyval = h(s.text)
      "<span class='#{prettyval} #{prettyclass} #{prettysuper}'>#{prettyval}</span>"
    else
      "<span class='#{prettyclass} #{prettysuper}'>#{h s.text}</span>"
    end
  end.join
end