feat(frontend): Improve highlighting of code samples#2538
Conversation
|
From original PR:
Not sure whether it was not the case at the time of the original PR or whether I just didn't know, but sphinx depends on pygments, so it's available in your doc build environment and you could use that instead of this manual approach. |
|
@timhoffm I'm not sure that we install docs deps where this is used, but I can check tomorrow. pygments is okay with me if it cleans up some cruft |
|
I would lean towards using pygments. |
Replace manual regex-based HTML formatting with Pygments PythonConsoleLexer for proper syntax highlighting of homepage code samples. Addresses reviewer feedback on #2538. - Add Pygments dependency - Clean up code samples to plain text (no hand-written HTML spans) - Map Pygments CSS classes (.c1, .go, .gp) in stylesheet - Fix HTML entity escaping (now handled by Pygments automatically) Co-Authored-By: Tim Hoffmann <tim.hoffmann@gmail.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36d9ca1 to
6d562d1
Compare
There was a problem hiding this comment.
Pull request overview
This PR enhances the syntax highlighting of Python code samples displayed on the homepage by integrating the Pygments library. The refactoring eliminates manually crafted HTML tags in favor of automated generation using Pygments' PythonConsoleLexer, which properly tokenizes Python console interactions including prompts, comments, and output. The CSS has been updated to use standard Pygments token classes (.c1 for comments, .go for output, .gp for prompts) instead of custom classes (.comment, .output), with an additional highlight color for the prompt marker to improve readability.
Changes:
- Added Pygments>=2.17 dependency for syntax highlighting capabilities
- Introduced
_highlight_python_console()function to automatically generate highlighted HTML from Python console code snippets - Updated CSS token classes from custom names to standard Pygments token types with appropriate styling
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pyproject.toml | Added Pygments>=2.17 as a new dependency for syntax highlighting |
| static/sass/style.css | Updated CSS classes from .comment/.output to Pygments token types (.c1/.go/.gp) with color styling |
| apps/codesamples/factories.py | Added _highlight_python_console() function and refactored code samples to use plain Python console text instead of manual HTML |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add CSS rules for all Pygments token classes used in the homepage code samples: keywords, builtins, function names, strings, numbers, operators, comments, output, and prompts. Uses a dark-background color scheme consistent with the existing slide-code styling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The docs dependencies are in [dependency-groups], not [project.optional-dependencies], so pip install '.[docs]' fails. Use pip install --group docs to match the pattern from #2902. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update style.scss to use Pygments token classes instead of old .comment/.output classes. Fix .go output color to match SCSS variable $grey-lighterer (#ddd). Keeps SCSS and compiled CSS in sync. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Revert unintentional cache-busting query string changes on image assets that were introduced during rebase - Add upper bound to Pygments dependency (>=2.17,<3) to prevent potential breaking changes from a future major release Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
static/sass/style.css:3177
- These
content: url(...?####)cache-buster query strings changed for the print logos without any corresponding change in the SCSS sources in this PR. If the images themselves didn’t change, consider restoring the previous cache-buster values to avoid forcing clients to re-download unchanged assets.
content: url('../img/python-logo_print.png?1694722768'); }
.psf .site-headline a:before {
width: 334px;
height: 82px;
content: url('../img/psf-logo_print.png?1694722768'); } }
static/sass/style.css:2187
- These
?####cache-buster values changed forsuccess-glow2.pngeven though the correspondingimage-url("success-glow2.png")source instyle.scssdidn’t change in this PR. If possible, revert this to avoid unnecessary cache invalidation / extra image downloads, or regenerate assets in a deterministic way so unrelated URLs don’t churn.
background: center -230px no-repeat url('../img/success-glow2.png?1694722768') transparent;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

What
mainfrom Improve highlighting of code samples #1517Changes
<span>tags and regexformat_html()with PygmentsPythonConsoleLexerPygments>=2.17as a dependency...continuation lines and plain<(Pygments handles HTML escaping)Syntax highlighting colors (dark background)
>>>,...).gp#c65d09(orange, bold)>>>.go#ddd(light gray)Hello, I'm Python!.k,.ow#6ab825(bright green, bold)def,for,while.nb#24909d(teal)print,input,list.nf#447fcf(blue)fib.s1,.s2,.sa,.si#ed9d13(gold)'Hello'.mi#3677a9(blue)1000.o#999(gray)=,+,/.c1#999(gray, italic)# floor division#0d870d(green)