pygments / pygments/pygments

py3tb, monokai, and html / css needs update

Open
#1,143 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

S-minor T-bug X-imported
Dominant language
Python
Stars
2.2k
Forks
885
PR merge metrics
No merged PRs in 30d

Description

(Original issue 1439 created by svenevs on 2018-04-16T08:22:18.835692+00:00)

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 :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the py3tb lexer tokens through the monokai style and HTML formatter, comparing the generated CSS and HTML with the native output shown in the issue. Done means the monokai style assigns suitable CSS for the traceback tokens without caller-side overrides; the issue does not name files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.