以编程方式使用 Sphinx 特定指令解析 .rst 文件
我希望能够在 Python 中解析基于 sphinx 的 rst 以便进一步处理和检查。类似的东西:
import sphinx
p = sphinx.parse("/path/to/file.rst")
do_something_with(p)
似乎在 docutils 中使用 docutils.core.publish_file 是可能的:
publish_file(open("/path/to/file.rst")
但这对 sphinx 特定指令等一无所知......
I would like to be able to parse sphinx based rst in Python for further processing and checking. Something like:
import sphinx
p = sphinx.parse("/path/to/file.rst")
do_something_with(p)
It seems that something is possible in docutils using the docutils.core.publish_file:
publish_file(open("/path/to/file.rst")
But that doesn't know anything about sphinx specific directives etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在最终写入之前使用 Sphinx 扩展 进行自定义处理。文档中有一个非常好的入门示例项目,它讨论了允许您自定义 Sphinx 的各种挂钩。
根据您尝试执行的操作,您可能需要提供
do_something
函数作为这些事件之一的回调参数。然后你可以如下扩展sphinx
如果Sphinx教程中的例子不够详细,Doug Hellmann还有一个 关于为 Sphinx 创建拼写检查器的博客文章。我发现它对于我不久前必须编写的 Sphinx 扩展来说是一个有用的参考。
You can use Sphinx Extensions to do custom processing before the final write. There is a very good getting started example project in the documentation that discusses various hooks that allow you to customize Sphinx.
Depending on what you're trying to do, you may need to supply your
do_something
function as a call back argument to one of these events.And then you can extend sphinx as follows
If the example in the Sphinx tutorial is not detailed enough, Doug Hellmann also has a blog post about creating a spell checker for Sphinx. I found it to be a useful reference for the Sphinx extension I had to write a while back.