sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

escaping curly braces in samp text role broken

Open rdzman opened this issue 3 years ago • 2 comments

Describe the bug

Escaping of curly braces in the samp text role was introduced in #4909, but it does not seem to be able to handle both escaped and non-escaped curly braces in the same sample.

How to Reproduce

- :samp:`\{ literal, {variable} \}`
- :samp:`\{ literal \}, {variable}`
- :samp:`{variable}, \{ literal \}`
- :samp:`{ variable, \{variable\} }`

Expected behavior

Ignoring any extra space in the rendering below, I expect the above to be rendered as ...

  • { literal, variable }
  • { literal },variable
  • variable, { literal }
  • variable, {variable}

But instead, it renders as ...

  • literal, {variable }
  • literal , variable
  • variable, literal
  • variable, {variable }

Your project

any

Screenshots

No response

OS

Mac

Python version

3.7.9

Sphinx version

5.0.2

Sphinx extensions

No response

Extra tools

No response

Additional context

No response

rdzman avatar Aug 18 '22 22:08 rdzman

I just ran into the same issue with python version 3.11.4 and sphinx version 7.2.6.

The issue is caused, by the fact, that we have a quotation `` around it, hence the backslash needs its own masking on the text level (Here the curly braces don't need to be escaped). Hence, while the issues isn't fixed, I'd like to provide a workaround how @rdzman's example behaves as expected, simply use two backslash each time to preserve one backslash for the moment it is needed to escape the curly brace:

- :samp:`\\{ literal, {variable} \\}`
- :samp:`\\{ literal \\}, {variable}`
- :samp:`{variable}, \\{ literal \\}`
- :samp:`{ variable, \\{variable\\} }`

mkruckow avatar Oct 23 '23 12:10 mkruckow

Encountered the same issue. Opened a PR to update the docs #11987

demetrios-koziris avatar Feb 17 '24 16:02 demetrios-koziris