如何 assemble 一个使用附加指令解析 reStructuredText 的工具
How to assemble a tool that parses reStructuredText with additional directives
假设我想向 reStructuredText 标准指令添加一个新指令。
有一个 how-to for creating reStructuredText directives 概述了每个指令本质上是如何从指令 class 或其子 class 派生的。它还建议感兴趣的 reader 应该检查标准指令的实现方式并将其用作起点。
到目前为止一切顺利,但教程实际上到此为止。如何将新指令纳入新工具?我应该如何实际使用它?
“Hacker's Guide”概述了前端工具如何从 Reader、Parser、Transformer 和 Writer 中 assembled,似乎不完整且绝对不包含任何代码。
那么,我应该如何 assemble 标准之外的新工具 reader + 我的扩展解析器识别新指令 + 可能是一个新的 Writer 适当地处理新指令它产生的输出?
我非常感谢在正确方向上的推动。
docutils.core.Publisher
class 是 "A facade encapsulating the high-level logic of a Docutils system." 根据其文档:
Calling the publish_*
convenience functions (or instantiating a
Publisher
object) with component names will result in default
behavior. For custom behavior (setting component options), create
custom component objects first, and pass them to
publish_*
/Publisher
.
因此,为了构建一个工具,例如,解析 .rst 文件并将 html5 输出到标准输出,使用标准组件,可以编写:
import sys
import docutils.core
import docutils.parsers.rst
import docutils.writers.html5_polyglot
public = docutils.core.publish_file(
source=open("filename.rst", 'r'),
parser=docutils.parsers.rst.Parser(),
writer=docutils.writers.html5_polyglot.Writer())
用您自己的扩展 classes 替换解析器或编写器的标准组件,您就大功告成了。
假设我想向 reStructuredText 标准指令添加一个新指令。
有一个 how-to for creating reStructuredText directives 概述了每个指令本质上是如何从指令 class 或其子 class 派生的。它还建议感兴趣的 reader 应该检查标准指令的实现方式并将其用作起点。
到目前为止一切顺利,但教程实际上到此为止。如何将新指令纳入新工具?我应该如何实际使用它?
“Hacker's Guide”概述了前端工具如何从 Reader、Parser、Transformer 和 Writer 中 assembled,似乎不完整且绝对不包含任何代码。
那么,我应该如何 assemble 标准之外的新工具 reader + 我的扩展解析器识别新指令 + 可能是一个新的 Writer 适当地处理新指令它产生的输出?
我非常感谢在正确方向上的推动。
docutils.core.Publisher
class 是 "A facade encapsulating the high-level logic of a Docutils system." 根据其文档:
Calling the
publish_*
convenience functions (or instantiating aPublisher
object) with component names will result in default behavior. For custom behavior (setting component options), create custom component objects first, and pass them topublish_*
/Publisher
.
因此,为了构建一个工具,例如,解析 .rst 文件并将 html5 输出到标准输出,使用标准组件,可以编写:
import sys
import docutils.core
import docutils.parsers.rst
import docutils.writers.html5_polyglot
public = docutils.core.publish_file(
source=open("filename.rst", 'r'),
parser=docutils.parsers.rst.Parser(),
writer=docutils.writers.html5_polyglot.Writer())
用您自己的扩展 classes 替换解析器或编写器的标准组件,您就大功告成了。