rouge-ruby / rouge

A pure Ruby code highlighter that is compatible with Pygments
https://rouge.jneen.net/
Other
3.3k stars 732 forks source link

`guess` with source and `guess_by_source` frequently returning PlainText lexer #2028

Closed depthbomb closed 3 months ago

depthbomb commented 3 months ago

Describe the bug Given many code strings, using guess({ source: ... }) or guess_by_source(...) returns Rouge::Lexers::PlainText.

To Reproduce

# frozen_string_literal: true

require 'rouge'

code = <<~CODE
  {
    "s": "t",
    "i": 123,
    "b": true
  }
CODE

formatter = Rouge::Formatters::HTMLInline.new(Rouge::Themes::Github.new)
lexer = Rouge::Lexer.guess({ source: code }) # or Rouge::Lexer.guess_by_source(code)
output = formatter.format(lexer.lex(code))

puts lexer.title
puts output

Expected behavior Correctly guesses the lexer to use.

Screenshots None

System

Additional context I'm not sure if this is entirely a library bug or I'm attempting to use the code incorrectly as there wasn't much info about the usage regarding guessing by code source.