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

Support doctest in Python lexer #1932

Closed tancnle closed 1 year ago

tancnle commented 1 year ago

Add support to highlight doctest in Python lexer.

Before After
before after

Resolves https://github.com/rouge-ruby/rouge/issues/920

tancnle commented 1 year ago

👋🏼 @Erotemic could you verify if this addresses the reported issue? 🙏🏼

Erotemic commented 1 year ago

This looks great!

I'm perfectly happy with this as-is. The only thing I might suggest is that the "want" part of the doctest where you specify the expected result would be more readable if it was distinguished from the rest of the string by a different color. For example vim does it this way:

image

The logic would be that a "want" statement starts after the last >>> or ... prefix (except in the case where the line is only ... in which case it should be interpreted as an ellipsis - but that is an edge case that could be ignored) and ends whenever there is a blank newline.

However, if that's difficult to implement, then this addresses 99% of the problem and is a huge readability improvement! I can't wait to be able to read my doctests on gitlab!

tancnle commented 1 year ago

Thank you for the insights @Erotemic. I have rejigged the PR to provide better support for test output. I have also ensured the lexer can distinguish between ... as a prompt and a builtin. Let me know what you think.

tancnle commented 1 year ago

I can't wait to be able to read my doctests on gitlab

I believe GitLab only uses Rouge for language detection, not for syntax highlighting. It has switched to Highlight JS for syntax highlighting (see doc).

Erotemic commented 1 year ago

Let me know what you think.

That looks much better. I would call this feature complete.

It has switched to Highlight JS for syntax highlighting (see doc).

Ah, good to know. Well more doctest support in general is better, so thank you for implementing this.