简体   繁体   English

使用Sphinx转换为HTML时如何在reStructuredText中转义单引号

[英]How to escape single quotes in reStructuredText when converting to HTML using Sphinx

For a documentation project I am writing I need to include a table with date format strings. 对于正在编写的文档项目,我需要包括一个带有日期格式字符串的表。 Now almost everything works fine, but at the end I have this slight problem where I want to print a literal ' quote and two literal quotes (separately and between other quotes). 现在几乎所有东西都可以正常工作,但是最后我遇到了一个小问题,我想打印一个文字'和两个文字引号(分别在其他引号和其他引号之间)。 Sphinx changes these to up/down quotes, which usually looks really neat, but in this particular case makes the text unreadable. Sphinx将其更改为上/下引号,通常看起来很整洁,但是在这种情况下会使文本不可读。 The best I could come up with was: 我能想到的最好的是:

======   ===========   ======  =====================
``'``    Escape for            | " ``'`` hour ``'`` h" -> "hour 9"
         for text 
         Delimiter
``''``   Single                | "ss ``''`` SSS" -> "45 ``'`` 876"
         quote 
         Literal  
======   ===========   ======  =====================

This produces all the right quotes, but it inserts extra spaces before and after, which I would like to see removed, since the example is not syntactically correct that way. 这会产生所有正确的引号,但会在前面和后面插入多余的空格,我希望看到这些空格已删除,因为该示例在语法上不是那样。 So one could also rephrase my question as: How to remove extra spaces before and after literal quotes when using backticks. 因此,也可以将我的问题改写为:使用反引号时,如何在文字引号之前和之后删除多余的空格。

I have tried standard ways of escaping. 我已经尝试了标准的转义方法。 Backslashes have no effect, since ' is not a reStructuredText special character. 反斜杠无效,因为'不是reStructuredText特殊字符。 If I remove the spaces the backticks `` won't work anymore. 如果我删除空格,则反引号``将不再起作用。

Sample output with extra spaces: 示例输出中有多余的空格: 在此处输入图片说明

I just found the answer burried in the documentation: 我刚刚在文档中找到了答案:

http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup

It turns out you can escape space characters by using a backslash "\\" before them. 事实证明,您可以在空格字符前使用反斜杠“ \\”来对其进行转义。 So "\\ " will be rendered as "". 因此,“ \\”将呈现为“”。 This is useful for instances where you need to use whitespace for the formatting, but don't want that whitespace to show up, as in my question above. 对于需要使用空格进行格式化但又不希望出现空白的情况,这很有用,就像上面的问题一样。

So the solution would be: 因此解决方案将是:

======   ===========   ======  =====================
``'``    Escape for            | "\ ``'``\ hour\ ``'``\ h" -> "hour 9"
         for text 
         Delimiter
``''``   Single                | "ss\ ``''``\ SSS" -> "45\ ``'``\ 876"
         quote 
         Literal  
======   ===========   ======  =====================

Ugly to read, but effective. 难以阅读,但有效。

Another example with inline formatting of function calls: 内联格式化函数调用的另一个示例:

**dateformat (**\ *<string,number,void>* **sourceDate,** *string* **sourceDateFormat,** *string* **returnDateFormat)**

It turns out this is the only way to get the formatting to be correct (variable types italic and the rest bold, whithout having a space between the opening parenthesis and the variable type). 事实证明,这是使格式正确的唯一方法(变量类型为斜体,其余为粗体,而左括号和变量类型之间没有空格)。

Well, not sure which parts you think are mis-spaced, but the quotes on the far left don't have extra spaces in the sceenshot, that's just how single quotes render in monospaced fonts. 好吧,不确定您认为哪些部分的间距不正确,但是最左边的引号在场景图中没有多余的空格,这就是单引号以等宽字体呈现的方式。 As for the right column, why not double-backtick-enclose the whole cell? 至于右列,为什么不对整个单元格进行两次反引号括起来呢? That also kills the "smart" quotes, which, I think, is what you're going for. 这也扼杀了“智能”引号,我认为这就是您想要的。

``ss '' SSS" -> "45 ' 876"``
# could instead be this, if these were the spurious spaces
``ss''SSS -> "45'876"``

You can't otherwise get rid of the spacing around double-backticked spans - as far as the parser is concerned, those make up a "word", and it's up to the writer to handle the whitespace between words. 否则,您不能摆脱双反引号跨度之间的间距-就解析器而言,它们构成一个“单词”,由作者来处理单词之间的空白。 Where you need to control whitespace, it's not "prose", it's a literal section, and the whole whitespace-significant text should be double-backticked. 在需要控制空格的地方,它不是“散文”,而是一个文字部分,整个空格重要的文本应加双引号。

Now, if you want to have the "smart" quotes inside the backtick'd section, I think you'll need to go the character-entity-reference route, but I don't know what that'd look like - should be in the docs tho. 现在,如果您想在反引号部分内使用“智能”引号,我认为您需要走字符实体引用路线,但是我不知道那是什么样子-应该是在文档中

As mentioned by other people in the comments, this process where Sphinx changes plain " into curled and so on is called SmartQuotes. 正如其他人在评论中提到的那样,Sphinx将纯文本"变成卷曲过程称为SmartQuotes。

I'm not sure if you specifically wanted the literals at all in the first place, or if they were only a compromise to avoid the SmartQuotes, but there are (at least) two ways to stop the SmartQuotes without needing to use literals: 我不确定您是否一开始就特别希望使用文字,或者它们只是避免使用SmartQuote的折衷方案,但是(至少)有两种方法可以停止SmartQuote,而无需使用文字:

1. Disable SmartQuotes for the whole project: 1.禁用整个项目的SmartQuotes:

If you don't want SmartQuotes, either add: 如果您不想要SmartQuotes,请添加:

smartquotes = False

to your conf.py file 到您的conf.py file

Or add a docutils.conf file at the same level as conf.py with this contents: 或添加docutils.conf在同一级别的文件conf.py与此内容:

[parsers]
smart_quotes: no

(solution from this GitHub issue ; see the Sphinx documentation for how these two settings interact - TL;DR: switching off in docutils.conf will override even if they're turned on in conf.py ) (从解决这个问题的GitHub ;看到狮身人面像的文档如何这两个设置互动- TL; DR:在关闭docutils.conf即使他们是在开启后就会重写conf.py

2. Escape the individual quotes you don't want to be 'smart': 2.转义您不想变得“聪明”的单引号:

You can use double-backslashes \\\\ to escape the quote marks you want to be straight, eg \\\\' and \\\\" . So in your example, this: 您可以使用双反斜杠\\\\来转义想要直引号,例如\\\\'\\\\" 。因此,在您的示例中,这是:

"\\'hour\\'h" -> "hour 9"
"ss\\'\\'SSS" -> "45\\'876"

would give output of: 将给出以下输出:

“'hour'h” -> “hour 9”
“ss''SSS” -> “45'876”

with the outer double quotes left as 'smart', but I think that is the behaviour you wanted. 外部的双引号保留为“ smart”,但我认为这是您想要的行为。

(see the official docutils documentation for details on escaping) (有关转义的详细信息,请参阅官方docutils文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM