9

Highlight.js treats Python REPL (the Python interpreter in interactive mode) as its own language separate from Python—it uses the class names python-repl and pycon. I checked Stack Exchange's highlight-loader.js and didn't see python-repl loaded anywhere. Would you mind adding support?

Here's a CodePen illustrating the difference between how highlight.js handles lang-python vs lang-python-repl when highlighting Python interpreter interactive sessions.

Also, as @Luuklag insightfully pointed out, nearly all questions tagged "python-interactive" are also tagged "python", so if Python REPL highlight.js support is added, it would probably be best not to add default highlighting to the "python-interactive" tag, because the highlighter only adds default highlighting when there is exactly one tag with a language set used. I just want to manually make ```python-repl code blocks.

15
  • According to the FAQ won't the python-interactive tag need editing, much like the python tag, to include the highlighting language specified in its properties - or couldn't you just add the python tag or use ```lang-python or ```python before the block? --- The FAQ seems to state that SE won't modify highlight-loader.js
    – Rob
    Oct 11, 2020 at 3:24
  • And seeing that almost all Q's tagged python-interactive are also tagged with python setting a language for both tags will result in the default tag-highlighting. As the highlighter only takes a language from a tag when there is exactly one tag with a language set.
    – Luuklag
    Oct 11, 2020 at 7:15
  • 1
    @Rob Oh, when the FAQ said "Keep in mind that Stack Exchange does not maintain this syntax highlighter", I took it to mean that they don't maintain highlight.js itself. My post is acting on a line earlier in the FAQ that says, "If a language is already on the list [of highlight.js-supported languages] but not used on Stack Exchange, please raise a feature request here on Meta to have it deployed on the network."
    – Ty Mick
    Oct 12, 2020 at 4:41
  • 1
    The fact that the "python" tag nearly always accompanies the "python-interactive" tag would probably be good reason to not edit the "python-interactive" tag after adding Python REPL highlight.js support. I was just wanting to make ```python-repl code blocks.
    – Ty Mick
    Oct 12, 2020 at 4:50
  • Ty, a Dev should be available to look at this in the next few days. --- If there's something that needs fixing 'on our end' (like for MathJax): meta.stackexchange.com/questions/348676/… or meta.stackexchange.com/q/229363/282094 they are inclined to fix it, but actually editing / changing the 3rd party source code is something that they are reluctant to do; not saying absolutely that they won't - that's their decision.
    – Rob
    Oct 12, 2020 at 5:25
  • Ty, is there anything wrong with the syntax coloring of these Python REPL posts: stackoverflow.com/q/30412846/3648282, stackoverflow.com/a/14926430/3648282 or stackoverflow.com/a/15243503/3648282 ?
    – Rob
    Oct 12, 2020 at 5:56
  • 3
    @Rob It's just a nitpicky thing: certain parts of the outputs are highlighted that ought not be. Here's a CodePen illustrating the difference. Not a big deal in the grand scheme of things—it's just technically more correct if you're weird like me and want to go to the trouble of making your Python REPL sessions look a bit nicer. 😅 Definitely not a bug or anything: just a nice-to-have feature addition.
    – Ty Mick
    Oct 12, 2020 at 16:27
  • 3
    @Rob And I'm pretty sure highlight-loader.js is first-party code—it's hosted on a Stack Exchange domain (sstatic.net), it mentions Stack Overflow in the comments at the top of the file, and the highlight.js repository doesn't contain any files named "highlight-loader.js".
    – Ty Mick
    Oct 12, 2020 at 16:33
  • 1
    Several grammars are split this way (for accuracy and specificity for those specifying the language by hand). A good approach for a site like SO would be to have a tag => list of language mapping and use that to clue the auto-detect (see the other thread on using tags to improve auto-detect). So the simple thing if a post was tagged python would be to ask Highlight.js to consider highlighting it with both python and python-repl - and let us decide which is a better "fit" using auto-detect. But yes, first SO would need to add python-repl in general to it's loaded grammars. Oct 17, 2020 at 21:05
  • 1
    @Rob Any news on the support of python-repl?
    – Géry Ogam
    May 8, 2021 at 12:11
  • 1
    @Maggyero, I know nothing beyond the above comments. You could work on it off-site (link above) to get everything done, and attract more attention by searching out these kind of Q&As here on MSE for further advice: "How do I get attention for one of my own questions without a good answer?", "How to draw attention to a question without creating a bounty", etc.
    – Rob
    May 8, 2021 at 14:58
  • @Rob Yes, there is much wrong with the syntax coloring. in should definitely not be highlighted in tracebacks on the lines that specify files where code was found, and words like is, not, for, with etc. should not be highlighted in exception messages (they are textual explanations of the error, and not Python code). It's debatable whether the line numbers should be highlighted. The highlighting of file names (which look like string literals to the highlighter) is accidental, but nice. It would also be nice to highlight the word Traceback, and >>> and/or ... prompts. Jan 14, 2023 at 5:52
  • (However, I haven't checked exactly what highlight.js does with these.) Jan 14, 2023 at 5:52
  • "So the simple thing if a post was tagged python would be to ask Highlight.js to consider highlighting it with both python and python-repl - and let us decide which is a better "fit" using auto-detect." Detection should be trivial: use REPL formatting if any line starts with >>>, and Python source code formatting otherwise. Jan 14, 2023 at 5:53
  • (REPL sessions are used to show both program output and traceback; program output should probably be plain in general, but it could include quotes that confuse the formatter. Honestly, I would be satisfied seeing the actually input code with full highlighting, and everything else plain....) Jan 14, 2023 at 5:55

0

You must log in to answer this question.

Browse other questions tagged .