• 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)  
    作用:輸出時刪去源文件行長限制下的文本格式性換行符。因為瀏覽器會將此類換行解釋為空格,于英文等無礙,而于中文則有礙觀瞻。


    天堂网