用于树叶的预处理器,以逃避/消除原始内容。
foliantcontrib.escapecode的Python项目详细描述
escapecode和unescapecode
escapecode和unescapecode预处理器成对工作。
escapecode在源标记内容中查找任何下一个预处理器都不应修改的部分。应该保留原始内容的示例:栅栏代码块、前置代码块、内联代码。
escapecode用unescapecode预处理器识别的伪xml标记替换这些原始内容部分。
escapecode将原始内容部分保存到文件中。稍后,unescapecode将从文件还原此内容。
另外,在替换之前,escapecode会规范化源标记内容,以统一和简化进一步的操作。预处理器用LF
替换CRLF
,删除多余的空白字符,提供尾随换行符等。
安装
要安装escapecode和unescapecode预处理器,请运行:
$ pip install foliantcontrib.includes
请参阅下面的详细信息。
与植物相结合,包括
您可以显式调用escapecode和unescapecode,但这些预处理器与foliant core(从1.0.10版开始)和includes预处理器(从1.1.1版开始)集成。
如果将escape_code
项目的config选项设置为true
,则提供在所有其他预处理器之前应用escapecode,在所有其他预处理器之后应用unescapecode。这个选项还告诉includes预处理器对每个包含的文件应用escapecode。
在此模式下,escapecode和unescapecode预处理器不推荐使用unescape预处理器。
> **Note**
>
> The preprocessor _unescape is a part of Foliant core. It allows to use pseudo-XML tags in code examples. If you want an opening tag not to be interpreted by any preprocessor, precede this tag with the `<` character. The preprocessor _unescape applies after all other preprocessors and removes such characters.
配置示例:
title:My Awesome Projectchapters:-index.md...escape_code:truepreprocessors:...- includes......
如果未使用escape_code
选项或将其设置为false
,则涉及向后兼容模式。在此模式下,不会自动应用escapecode和unescapecode,但会应用unescape预处理器。
包含escapecode和unescapecode预处理器的python包是自1.1.1版以来includes预处理器的依赖项。同时,这个包并不是叶核的依赖。要在foliant core中使用escape_code
config选项,必须分别安装带有escapecode和unescapecode预处理器的包。
显式启用
不能使用escape_code
选项并显式调用预处理器:
preprocessors:-escapecode# usually the first list item...-unescapecode# usually the last list item
两个预处理器都允许重写用于存储临时文件的目录的路径:
preprocessors:-escapecode:cache_dir:!path.escapecodecache...-unescapecode:cache_dir:!path.escapecodecache
默认值如本例所示。escapecode和相关的unescapecode必须与同一个缓存目录一起工作。
请注意,如果使用includes预处理器,并且所包含的内容不属于当前叶类项目,则在应用includes预处理器之前,无法转义此内容的原始部分。
用法
下面您可以看到带有代码块和内联代码的标记内容示例。
# Heading
Text that contains some `inline code`.
Below is a fence code block, language is optional:
```python
import this
```
One more fence code block:
~~~
# This is a comment that should not be interpreted as a heading
print('Hello World')
~~~
And this is a pre code block:
mov dx, hello;
mov ah, 9;
int 21h;
预处理器转义代码将执行以下替换:
# Heading
Text that contains some <escaped hash="2bb20aeb00314e915ecfefd86d26f46a"></escaped>.
Below is a fence code block, language is optional:
<escaped hash="15e1e46a75ef29eb760f392bb2df4ebb"></escaped>
One more fence code block:
<escaped hash="91c3d3da865e24c33c4b366760c99579"></escaped>
And this is a pre code block:
<escaped hash="a1e51c9ad3da841d393533f1522ab17e"></escaped>
转义的内容部分将保存到缓存目录中的文件中。文件名对应于hash
属性的值。例如,这是文件的内容15e1e46a75ef29eb760f392bb2df4ebb.md
:
```python
import this
```