Sha256: 50b5949bf29615927ae1fe87d35484f7434667baec9551ba265d2ec59baabfbf

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module YARD
  module Templates
    module Helpers
      # Helper methods for syntax highlighting.
      module HtmlSyntaxHighlightHelper
        # Highlights Ruby source
        # @param [String] source the Ruby source code
        # @return [String] the highlighted Ruby source
        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
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yard-0.6.4 lib/yard/templates/helpers/html_syntax_highlight_helper.rb
yard-0.6.3 lib/yard/templates/helpers/html_syntax_highlight_helper.rb
yard-0.6.2 lib/yard/templates/helpers/html_syntax_highlight_helper.rb
yard-0.6.1 lib/yard/templates/helpers/html_syntax_highlight_helper.rb
yard-0.6.0 lib/yard/templates/helpers/html_syntax_highlight_helper.rb