如何将 docutils 文档树转换为 HTML 字符串?
How do I convert a docutils document tree into an HTML string?
我正在尝试使用 docutils 包将 ReST 转换为 HTML。 This answer 简洁地使用 docutils publish_*
便利函数一步完成。我要转换的 ReST 文档有多个部分,我想在结果 HTML 中将它们分开。因此,我想分解这个过程:
- 将 ReST 解析为节点树。
- 根据需要分隔节点。
- 将我想要的节点转换成HTML.
这是我正在努力的第三步。以下是我执行第一步和第二步的方法:
from docutils import utils
from docutils.frontend import OptionParser
from docutils.parsers.rst import Parser
# preamble
rst = '*NB:* just an example.' # will actually have many sections
path = 'some.url.com'
settings = OptionParser(components=(Parser,)).get_default_values()
# step 1
document = utils.new_document(path, settings)
Parser().parse(rst, document)
# step 2
for node in document:
do_something_with(node)
# step 3: Help!
for node in filtered(document):
print(convert_to_html(node))
我找到了 HTMLTranslator
class 和 Publisher
class。它们看起来很相关,但我正在努力寻找好的文档。我该如何实现convert_to_html
功能?
我的问题是我试图在太低的级别上使用 docutils 包。他们为这类事情提供了一个接口:
from docutils.core import publish_doctree, publish_from_doctree
rst = '*NB:* just an example.'
# step 1
tree = publish_doctree(rst)
# step 2
# do something with the tree
# step 3
html = publish_from_doctree(tree, writer_name='html').decode()
print(html)
第一步现在简单多了。也就是说,我对结果还是有点不满意;我意识到我真正想要的是 publish_node
函数。如果您知道更好的方法,请 post 去做。
我还应该注意,我还没有设法使它与 Python 3.
一起工作
真正的教训
我实际上想做的是从 doctree 中提取所有边栏元素,以便可以将它们与文章的主体分开处理。这不是 docutils
旨在解决的那种用例。因此没有 publish_node
函数。
一旦我意识到这一点,正确的方法就足够简单了:
- 使用
docutils
生成 HTML。
- 使用
BeautifulSoup
提取侧边栏元素。
这是完成工作的代码:
from docutils.core import publish_parts
from bs4 import BeautifulSoup
rst = get_rst_string_from_somewhere()
# get just the body of an HTML document
html = publish_parts(rst, writer_name='html')['html_body']
soup = BeautifulSoup(html, 'html.parser')
# docutils wraps the body in a div with the .document class
# we can just dispose of that div altogether
wrapper = soup.select('.document')[0]
wrapper.unwrap()
# knowing that docutils gives all sidebar elements the
# .sidebar class makes extracting those elements easy
sidebar = ''.join(tag.extract().prettify() for tag in soup.select('.sidebar'))
# leaving the non-sidebar elements as the document body
body = soup.prettify()
我正在尝试使用 docutils 包将 ReST 转换为 HTML。 This answer 简洁地使用 docutils publish_*
便利函数一步完成。我要转换的 ReST 文档有多个部分,我想在结果 HTML 中将它们分开。因此,我想分解这个过程:
- 将 ReST 解析为节点树。
- 根据需要分隔节点。
- 将我想要的节点转换成HTML.
这是我正在努力的第三步。以下是我执行第一步和第二步的方法:
from docutils import utils
from docutils.frontend import OptionParser
from docutils.parsers.rst import Parser
# preamble
rst = '*NB:* just an example.' # will actually have many sections
path = 'some.url.com'
settings = OptionParser(components=(Parser,)).get_default_values()
# step 1
document = utils.new_document(path, settings)
Parser().parse(rst, document)
# step 2
for node in document:
do_something_with(node)
# step 3: Help!
for node in filtered(document):
print(convert_to_html(node))
我找到了 HTMLTranslator
class 和 Publisher
class。它们看起来很相关,但我正在努力寻找好的文档。我该如何实现convert_to_html
功能?
我的问题是我试图在太低的级别上使用 docutils 包。他们为这类事情提供了一个接口:
from docutils.core import publish_doctree, publish_from_doctree
rst = '*NB:* just an example.'
# step 1
tree = publish_doctree(rst)
# step 2
# do something with the tree
# step 3
html = publish_from_doctree(tree, writer_name='html').decode()
print(html)
第一步现在简单多了。也就是说,我对结果还是有点不满意;我意识到我真正想要的是 publish_node
函数。如果您知道更好的方法,请 post 去做。
我还应该注意,我还没有设法使它与 Python 3.
一起工作真正的教训
我实际上想做的是从 doctree 中提取所有边栏元素,以便可以将它们与文章的主体分开处理。这不是 docutils
旨在解决的那种用例。因此没有 publish_node
函数。
一旦我意识到这一点,正确的方法就足够简单了:
- 使用
docutils
生成 HTML。 - 使用
BeautifulSoup
提取侧边栏元素。
这是完成工作的代码:
from docutils.core import publish_parts
from bs4 import BeautifulSoup
rst = get_rst_string_from_somewhere()
# get just the body of an HTML document
html = publish_parts(rst, writer_name='html')['html_body']
soup = BeautifulSoup(html, 'html.parser')
# docutils wraps the body in a div with the .document class
# we can just dispose of that div altogether
wrapper = soup.select('.document')[0]
wrapper.unwrap()
# knowing that docutils gives all sidebar elements the
# .sidebar class makes extracting those elements easy
sidebar = ''.join(tag.extract().prettify() for tag in soup.select('.sidebar'))
# leaving the non-sidebar elements as the document body
body = soup.prettify()