Skip to content

Make RDoc::Markup::ToHtml#parseable? use a real parser #320

@jeremyevans

Description

@jeremyevans

Currently, parseable? uses a very basic regexp to guess if text is ruby or not (so that it can be syntax highlighted). See https://github.com/rdoc/rdoc/blob/0494067cffba009e3dc9a3f4545980d5b61700bd/lib/rdoc/markup/to_html.rb#L385. This misses a lot of valid ruby code, and it also syntax highlights some invalid ruby code (making it harder to spot invalid code examples in the documentation).

Would you be open to using a real parser (if available) to determine whether or not text should be considered ruby? Here's what I'm using in hanna-nouveau as a monkey patch using the parser gem:

begin
  require 'parser/current'

  class RDoc::Markup::ToHtml
    def parseable? text
      parser = Parser::CurrentRuby.new
      parser.diagnostics.consumer = lambda{|d|}
      buffer = Parser::Source::Buffer.new('(string)')
      buffer.source = text
      parser.parse(buffer)
      true
    rescue
      false
    end
  end
rescue LoadError
end

This change increases the time for rake rdoc in Sequel from 127 seconds to 140 seconds, or about 10%, which seems reasonable considering the advantages.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions