如何在 reStructuredText 中创建不间断的空格?

问题描述 投票:0回答:6

如何在 reStructuredText 中创建不间断的空格?

一个明显但有问题的解决方案是:

`word A`

但不同的实现可能会对其进行不同的处理,例如 rst2latex 或 rst2pdf。另外它以斜体呈现。

restructuredtext
6个回答
42
投票

您需要 unicode 指令,但它只能用于替换。所以你需要像这样定义一个替换:

.. |nbsp| unicode:: 0xA0 
   :trim:

然后像这样使用它:

xx |nbsp| xx

:trim:
是为了消除替换周围的空格。


10
投票

考虑到 reStructuredText 的目标是作为纯文本可读,您还可以使用

|_|
代替
|nbsp|
,这样在视觉上的干扰较小。


5
投票

我在这里没有看到问题,运行 docutils v0.9。 至少 rst2latex 和 rst2html 在不间断空白方面表现正常。 当您输入不间断字符 (\xa0, �) 时,Latex 会生成 ~,而 html 会生成  。

也许您遇到编辑器问题?如果你能设法输入字符,docutils 就会完成这项工作。


2
投票

我最终想出了 Sphinx 的解决方法。我覆盖了 HTML 和 LaTeX 编写器,将

~
字符转换为不间断空格。这是 HTML 的:

import sphinx.writers.html
BaseTranslator = sphinx.writers.html.SmartyPantsHTMLTranslator

class CustomHTMLTranslator(BaseTranslator):
    
    def bulk_text_processor(self, text):
        if '~' in text:
            text = text.replace('~', ' ')
        return text

sphinx.writers.html.SmartyPantsHTMLTranslator = CustomHTMLTranslator

还有 LaTeX 的:

import sphinx.writers.latex
BaseTranslator = sphinx.writers.latex.LaTeXTranslator

class DocTranslator(BaseTranslator):

    def visit_Text(self, node):
        if self.verbatim is not None:
            self.verbatim += node.astext()
        else:
            text = self.encode(node.astext())
            if '\\textasciitilde{}' in text:
                text = text.replace('\\textasciitilde{}', '~')
            if not self.no_contractions:
                text = educate_quotes_latex(text)
            self.body.append(text)

sphinx.writers.latex.LaTeXTranslator = DocTranslator

它不是那么漂亮,甚至不能让你逃脱

~
角色,但它适合我的目的。


1
投票

我还没有测试过它,但也许你可以使用 http://docutils.sourceforge.net/docs/ref/rst/directives.html#unicode-character-codes 和unicode“无中断空格”字符:http ://www.fileformat.info/info/unicode/char/a0/index.htm


0
投票

除了文字 NBSP 和自定义“unicode”替换之外的第三种选择是 包括reStructuredText标准定义文件,例如

.. include:: <isonum.txt>

xx\ |nbsp|\ xx

转义空格是必需的,因为标准定义文件不使用

:trim:
选项。

© www.soinside.com 2019 - 2024. All rights reserved.