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

Included in:
HtmlHelper
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

#html_syntax_highlight_ruby(source) ⇒ String

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
26
27
# File 'lib/yard/templates/helpers/html_syntax_highlight_helper.rb', line 9

def html_syntax_highlight_ruby(source)
  tokenlist = Parser::Ruby::RubyParser.parse(source, "(syntax_highlight)").tokens
  output = ""
  tokenlist.each do |s|
    output << "<span class='tstring'>" if [:tstring_beg, :regexp_beg].include?(s[0])
    case s.first
    when :nl, :ignored_nl, :sp
      output << h(s.last)
    when :ident
      output << "<span class='id #{h(s.last)}'>#{h(s.last)}</span>"
    else
      output << "<span class='#{s.first}'>#{h(s.last)}</span>"
    end
    output << "</span>" if [:tstring_end, :regexp_end].include?(s[0])
  end
  output
rescue Parser::ParserSyntaxError
  h(source)
end