reStructuredText/Sphinx中文段落内换行导致生成的HTML多出空格的问题

2016-07-13

Sphinx模块:

from docutils.nodes import *    def setup(app):      app.connect('doctree-resolved', process_chinese_paragraph)    class ParagraphVisitor(NodeVisitor):      def dispatch_visit(self, node):          if isinstance(node, TextElement):              for i in range(len(node.children)):                  if type(node[i]) == Text:                      node[i] = Text(node[i].astext().replace('
', '').replace('
', ''))    def process_chinese_paragraph(app, doctree, docname):      pv = ParagraphVisitor(doctree)      doctree.walk(pv)  
作用:输出时删去源文件行长限制下的文本格式性换行符。因为浏览器会将此类换行解释为空格,于英文等无碍,而于中文则有碍观瞻。