py3tb, monokai, and html / css needs update

Issue #1439 new
Stephen McDowell
created an issue

Hello, I am working with an application where I snag traceback information and use the py3tb lexer to generate html using the monokai style. I was playing around with different style choices, and ultimately believe I stumbled on an area where this style simply hasn't been "told" how to generate the CSS for py3tb.

I tried to use console instead of html, but ultimately that isn't helpful in this report because (as far as I can tell) choosing a style will not affect the output.

To start, the vanilla monokai formatting of a py3tb looks like this:

pygments_monokai.jpeg

I believe the native style choice is the same one that is used when using console instead of html (but don't actually know for sure). It's results look like this

pygments_native.jpeg

I went through a lot of circles trying to figure out what the "right" values should be here, and my lack of understanding of how pygments works is quite obvious from the following code. All I did was print out the generated CSS and HTML and replace or insert the "correct" style information:

import pygments
from pygments import lexers, formatters
style       = "monokai"  # switch to "native" to avoid hacks below
LexerClass  = lexers.find_lexer_class_by_name("py3tb")  # tb: traceback
fmt         = formatters.get_formatter_by_name("html", style=style)
highlighted = pygments.highlight(excDesc, LexerClass(), fmt)
css         = fmt.get_style_defs()

# monokai css does not support all of py3tb lexer at this time,
# replacement values were chosen by inspecting css and comparing to
# the "native" style
if style == "monokai":
    # Traceback (most recent call last):
    #   File "/path/to/file.py", line 316, in generate
    #     for cx, cy, color in coords:
    # ValueError: not enough values to unpack (expected 3, got 2)
    #
    # - Traceback: token.Keyword.Pseudo -> token.Generic.Traceback
    #   At this time Generic.Traceback is not included with monokai,
    #   this will force override it (when it is incorporated), since we
    #   are adding it to the end (CSS will override).
    css = "{0}\n.gt {{ color: #66d9ef }} /* Generic.Traceback */".format(css)  # noqa
    # - "path/to/file.py": token.Name.Builtin -> token.String
    css = css.replace(".nb { color: #f8f8f2 }", ".nb { color: #e6db74 }")
    # - ValueError: token.Error -> token.Generic.Error
    #   At this time Generic.Error is not included with monokai, this
    #   will force override it (when it is incorporated), since we are
    #   adding it to the end (CSS will override).
    css = "{0}\n.gr {{ color: #960050; background-color: #1e0010 }} /* Error */".format(css)  # noqa

This now produces the following:

pygments_monokai_fixed.jpeg

I would be happy to help update monokai, but I don't really understand the rules here or what the right values to choose are. Please give counsel on where to fix things / any guidance on choosing the right values :)

Comments (1)

  1. Log in to comment