highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

(Python) F strings not highlighting properly

Open superadm1n opened this issue 2 years ago • 4 comments

Describe the issue When using F strings in python they do not highlight the variables embedded in curly braces differently than the string itself

Which language seems to have the issue? Python - I am not doing auto detection, I am specifying the language

<pre class="language-python">
  <code>
    CODE HERE 
  </code>
</pre>

Are you using highlight or highlightAuto? Not sure on what this specifically is, my call to highlight is <script>hljs.highlightAll();</script>

Please let me know if this is not enough information or what specifically you need.

Sample Code to Reproduce

print(f'Percent Complete: {x}%', end='\r')

Renders in highlightjs like the screenshot below

image

Notice the github syntax highlighting does catch the fstring, however the colors being used are hard to easily distinguish.

Expected behavior Below I have a screenshot from Pycharm and the main key here is it changes the color of the curly braces within the f string to orange and also changes the color of the variable to white. I'm not sure if these should be the color scheme but I do know in my opinion the orange curly braces are very easy to distinguish

image

Additional context I do also notice that escaped characters like \r or \n within strings also do not change highlight colors which I think would be very helpful. This is demonstrated in my screenshot in expected behavior.

Also It is worth noting that all of my examples show a single quoted f string, but I have tested this behavior is consistent across single or double quoted f strings.

Lastly if it is NOT an f string (missing prepended "f") if there is an embeded variable or curly braces they should NOT be highlighted in order to best indicate that the embeded variable or function will not be processed.

superadm1n avatar Sep 07 '22 13:09 superadm1n

We do not consider this a bug - but a feature request. Most likely would fall as a duplication of #2500. Any chance you'd be willing to work on a PR for this enhancement?

I agree all of these improvements would be nice to haves.

joshgoebel avatar Sep 07 '22 17:09 joshgoebel

I understand, I apologize for entering this as a bug versus a feature request.

I don't think I would be much of help for me to commit to contributing to this PR, I do very little in javascript and wouldnt know where to begin here. If I can get some time within the next few weeks I could look through the source and see if I catch on to much but I cant commit to anything.

superadm1n avatar Sep 07 '22 17:09 superadm1n

I'll leave this open for a bit to see if anyone bites, but this might eventually get closed as a dup of #2500 as if we left issues open for every small improvement to every language we'd have 1000s of issues.

joshgoebel avatar Sep 07 '22 18:09 joshgoebel

Very understandable. Thank you for the information.

superadm1n avatar Sep 07 '22 18:09 superadm1n

@superadm1n What version and theme of highlight.js are you using?

I can see f-string working correctly: image

It seems like support was added ~3 years ago: in #2195

Hirse avatar Oct 22 '22 19:10 Hirse