pygments / pygments.rb

💎 Ruby wrapper for Pygments syntax highlighter
MIT License
572 stars 141 forks source link

Should pygments.rb handle lexer not found errors differently? #69

Closed n8 closed 11 years ago

n8 commented 11 years ago

Pygments is awesome, but I've noticed that as soon as I implemented this people trying to use backticks with lexers that don't exist or accidentally using backticks without any intention of adding a code block now get errors from mentos.rb about that lexer not being found.

get_lexer_by_name raise ClassNotFound('no lexer for alias %r found' % _alias) ClassNotFound: no lexer for alias 'Go' found

I was curious from your perspective, if it should be pygments.rb's responsibility to catch the class not found exception and just return the code block with some vanilla/default formatting instead of raising this exception up.

Or should this be an exception dealt with higher up the stack?

tmm1 commented 11 years ago

You should verify the lexer exists before trying to use it. See linguist for examples.

tnm commented 11 years ago

Closing this since clients of pygments.rb should ensure lexer existence.

ian-kelling commented 8 years ago

You should verify the lexer exists before trying to use it. See linguist for examples.

Just fyi: linguist doesn't use pygments.rb directly. Perhaps some dependency does. I hit this error and was wondering the proper way to deal with it, after a bunch of reading docs and messing with pygments,

begin
  Pygments.highlight(code, lexer: language)
rescue MentosError => e
  Pygments.highlight(code, lexer: 'text')
end