sphinx.ext.viewcode -- 添加指向突出显示的源代码的链接¶
模块作者: Georg Brandl
在 1.0 版本加入.
这个扩展查看您的Python对象描述(.. class::, .. function:: 等)并尝试查找包含对象的源文件。找到后,将为每个模块输出一个单独的HTML页面,其中突出显示源代码版本,并将向所有对象描述添加一个链接,该链接指向所描述对象的源代码。还将插入从源到描述的链接。
警告
基本上,viewcode 扩展将导入链接到的模块。如果任何模块对导入有副作用,则在运行“sphinx build”时将执行这些副作用。
如果你要引入脚本(而不是库模块),确保主程序 main 有这个条件保护着:if __name__ == '__main__'。
另外,如果不想通过“viewcode”导入模块,可以使用 viewcode-find-source 事件将源代码的位置告知“viewcode”。
如果启用 viewcode_follow_imported_members,则还需要使用 viewcode-follow-imported 事件解析导入的属性。
此插件仅适用于与HTML相关的生成器,如 HTML、 applehelp、 devhelp、 htmlhelp、 qthelp 等等,但 singlehtml 除外。epub 构建器默认不支持这个扩展。(参考 viewcode_enable_epub )。
配置¶
- viewcode_follow_imported_members¶
- 类型:
bool- 默认:
True
如果为
True,viewcode扩展将发出viewcode-follow-imported事件以通过其他扩展解析模块的名称。在 1.3 版本加入.
在 1.8 版本发生变更: 已从
viewcode_import重命名为viewcode_follow_imported_members。
- viewcode_enable_epub¶
- 类型:
bool- 默认:
False
如果为
True,则即使使用epub生成器,也会启用viewcode扩展。此扩展在toctree之外生成页面,但这不是epub格式的首选。Until 1.4.x, this extension is always enabled. If you want to generate epub as same as 1.4.x, you should set this to
True, but epub format checker's score worsens.在 1.5 版本加入.
警告
Not all epub readers support pages generated by viewcode extension. These readers ignore links to pages that are not under any toctree.
Some reader's rendering result are corrupted and epubcheck's score becomes worse even if the reader supports these pages.
- viewcode_line_numbers¶
- 类型:
bool- 默认:
False
如果设置为
True,则会将内联行号添加到突出显示的代码中。在 7.2 版本加入.
- viewcode-find-source(app, modname)¶
在 1.8 版本加入.
查找模块的源代码。此事件的事件处理程序应返回源代码本身的元组和标记字典。字典将类、函数、属性等的名称映射到其类型、起始行号和结束行号的元组。类型应为“class”、“def”或“other”之一。
- 参数:
app -- Sphinx应用程序对象。
modname -- 要查找其源代码的模块的名称。
- viewcode-follow-imported(app, modname, attribute)¶
在 1.8 版本加入.
查找属性的原始模块的名称。
- 参数:
app -- Sphinx应用程序对象。
modname -- 属性所属模块的名称。
attribute -- 要跟随的成员的名称。