为 Juptyer 笔记本添加 reStructuredText 支持



我需要在JupyterLab中查看reStructuredText文件,到目前为止,我发现的最好的是@akaihola对github上相关问题的回答。

我添加了一个解决方法,允许在不查看源代码的情况下呈现文件,如下所示。

如果其他人可能需要它,这是我目前正在使用的解决方案:

import docutils.core
import docutils.writers.html5_polyglot
from IPython.core.magic import register_cell_magic, register_line_magic
from IPython.display import HTML
@register_cell_magic
def rst(line, cell):
"Render ReStructuredText"
writer = docutils.writers.html5_polyglot.Writer()
return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))
@register_line_magic
def rstfile(filename):
"Render ReStructuredText"
writer = docutils.writers.html5_polyglot.Writer()
with open(filename, 'r') as file:
cell = file.read()
return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))

要查看不带源文件的rst文件,请执行以下操作:

%rstfile <your-rst-filename>

若要将原始解决方案用作rst单元格,同时显示 ReStructuredText 源和呈现的输出,请执行以下操作:

%%rst
============
Main title
============
Some **heavy** markup.

相关内容

  • 没有找到相关文章

最新更新