简体   繁体   English

如何在 reStructuredText 的代码块中强制使用空格

[英]How to force whitespace in code block in reStructuredText

In RST, we use some whitespaces in front of a block to say this is a code block.在 RST 中,我们在块前面使用一些空格来表示这是一个代码块。 Because Python also uses whitespace to indent a code block, I would like my RST code block to preserve those whitespaces if I were writing Python code.因为 Python 也使用空格来缩进代码块,所以如果我正在编写 Python 代码,我希望我的 RST 代码块保留这些空格。 How can I do that?我怎样才能做到这一点?

Let's say we have a class:假设我们有一个 class:

class Test(object):

And we want to write a method called __init__ that is a member of this class.我们想编写一个名为__init__的方法,它是这个 class 的成员。 This method belongs to another code block but we want to have some visual clue so that readers know that this second block is a continuation of the previous one.此方法属于另一个代码块,但我们希望有一些视觉线索,以便读者知道第二个代码块是前一个代码块的延续。 At the moment, I use # to mark the vertical guide line of a code block like this:目前,我使用#来标记代码块的垂直引导线,如下所示:

    def __init__(self):
        pass
#

Without the # , def __init__(self) would be printed at the same indentation level as class Test(object) .如果没有#def __init__(self)将以与class Test(object)相同的缩进级别打印。 There's gotta be more elegant way.必须有更优雅的方式。

You need to define your own directive (it's true that the standard .. code:: directive gobbles spaces but you can make your own directive that doesn't):您需要定义自己的指令(标准.. code::指令确实会吞噬空格,但您可以制作自己的指令,但不会):

import re
from docutils.parsers.rst import directives

INDENTATION_RE = re.compile("^ *")

def measure_indentation(line):
    return INDENTATION_RE.match(line).end()

class MyCodeBlock(directives.body.CodeBlock):
    EXPECTED_INDENTATION = 3

    def run(self):
        block_lines = self.block_text.splitlines()
        block_header_len = self.content_offset - self.lineno + 1
        block_indentation = measure_indentation(self.block_text)
        code_indentation = block_indentation + MyCodeBlock.EXPECTED_INDENTATION
        self.content = [ln[code_indentation:] for ln in block_lines[block_header_len:]]
        return super(MyCodeBlock, self).run()

directives.register_directive("my-code", MyCodeBlock)

You could of course overwrite the standard .. code:: directive with this, too.你当然也可以用这个覆盖标准的.. code::指令。

Ah... I've run into this before;).啊...我以前遇到过这个;)。 The # trick is usually what I use, alas. # 技巧通常是我使用的,唉。 If you read the spec it sounds like it will always take away the leading indent.如果您阅读规范,听起来它总是会带走前导缩进。 [1] [1]

You could also use an alternate syntax:您还可以使用另一种语法:

::

>     def foo(x):
>         pass

With the leading ">" that will preserve leading space.使用前导“>”将保留前导空间。

[1]: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#indented-literal-blocks [1]: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#indented-literal-blocks

EDIT编辑

Just dug through the docutils code (this has been bugging me a lot too) and can confirm that it will always strip out the common indent, no questions asked.只需挖掘 docutils 代码(这也让我很烦恼),并且可以确认它总是会去除常见的缩进,没有问题。 It would be easy to modify to change this behavior but that would make the resulting restructured text non-standard.修改以更改此行为很容易,但这会使生成的重组文本不标准。

You can also try Line Blocks which look like this:您也可以尝试如下所示的线块

|     def foo(x):
|         pass

though they aren't specific to code examples.尽管它们并非特定于代码示例。

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

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