Python 斯芬克斯中的源级变换

Python 斯芬克斯中的源级变换,python,python-sphinx,Python,Python Sphinx,我试图编写一个sphinx扩展来执行源代码级转换,但我不知道如何实际更改输出文件 我的扩展如下所示: def my_source_handler(app, docname, source): import re print 'test' source = [re.sub("foo", "bar", source[0])] return source def setup(app): app.connect('source-read', my_source_

我试图编写一个sphinx扩展来执行源代码级转换,但我不知道如何实际更改输出文件

我的扩展如下所示:

def my_source_handler(app, docname, source):
    import re
    print 'test'
    source = [re.sub("foo", "bar", source[0])]
    return source

def setup(app):
    app.connect('source-read', my_source_handler)
    app.add_config_value('my_source_handler_include', True, False)
但是,当我将模块添加到扩展列表和
构建html
时,它会打印“test”,但实际上不会将输出html文件中的“foo”更改为“bar”

对于source参数,“您可以处理内容并替换此项以实现源代码级转换”,这有点模糊


问题是我不确定应该如何替换源参数。

事实上,经过一番挖掘,我找到了它,您应该替换
的第一个(也是唯一一个)元素的内容,而不是替换
本身,比如:

def my_source_handler(app, docname, source):
    import re
    print 'test'
    source[0] = re.sub("foo", "bar", source[0])