Python sphinx Sphinx文档-如何在为HTML构建时渲染动画GIF,而在为latexpdf构建时渲染PNG?

Python sphinx Sphinx文档-如何在为HTML构建时渲染动画GIF,而在为latexpdf构建时渲染PNG?,python-sphinx,restructuredtext,Python Sphinx,Restructuredtext,正如标题所述,我使用的是sphinx文档,我真的希望在构建输出为latexpdf时有条件地呈现静态PNG,在为web构建时有条件地呈现动画GIF 理想情况下,能够以某种方式在rst文件本身中实现这一点会很好。。。从语义上来说,a: 如果生成器==html: .. 图片:废话 elif builder==latexpdf: .. 图片:废话废话来自: Sphinx通过允许星号作为扩展来扩展标准docutils行为: .. image:: gnu.* Sphinx然后搜索与提供的模式匹配的所有图像

正如标题所述,我使用的是sphinx文档,我真的希望在构建输出为latexpdf时有条件地呈现静态PNG,在为web构建时有条件地呈现动画GIF

理想情况下,能够以某种方式在rst文件本身中实现这一点会很好。。。从语义上来说,a:

如果生成器==html: .. 图片:废话 elif builder==latexpdf: .. 图片:废话废话来自:

Sphinx通过允许星号作为扩展来扩展标准docutils行为:

.. image:: gnu.*
Sphinx然后搜索与提供的模式匹配的所有图像并确定其类型。然后,每个构建者从这些候选图像中选择最佳图像。例如,如果给定了文件名
gnu.*
,并且源代码树中存在两个文件
gnu.pdf
gnu.png
,LaTeX生成器将选择前者,而HTML生成器将选择后者。支持的图像类型和选择优先级在中定义

要自定义给定生成器的“最佳图像”顺序,请编辑您的
conf.py
以使用您喜欢的
supported\u image\u类型
顺序覆盖
standaronehtmlbuilder

from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.supported_image_types = [
    'image/svg+xml',
    'image/gif',
    'image/png',
    'image/jpeg'
]

为了防止它对其他人有用,我使用了Steve Piercy非常有用的答案的变体,它试图证明代码的未来性,并防止神秘的图像丢失。它使用相同的结构,但附加了StandaloneHTMLBuilder.supported_image_类型中默认不在我们提供的新集合中的所有项。我在考虑,如果斯芬克斯开始支持像HEIC图像这样的东西,或者其他新标准出现,这将允许它们无缝集成

new_supported_image_types = [
    'image/svg+xml',
    'image/gif',
    'image/png',
    'image/jpeg'
]

# construct it this way so that if Sphinx adds default support for additional images, such
# as HEIC, then what we do is add any of those to the end. We start with the ones
# we want to support in this order, then subtract them from the defaults to identify
# any remaining items that we append to the end of the list

additional_default_supported_images = list(set(StandaloneHTMLBuilder.supported_image_types) - set(new_supported_image_types))
StandaloneHTMLBuilder.supported_image_types = new_supported_image_types + additional_default_supported_images

是的,我看到了,但是如果我理解的话,HTML构建器总是喜欢png图像而不是动画gif。还是我遗漏了什么?是的,刚刚证实了这是事实。。。虽然这修复了最新的xpdf构建,但现在html构建并不是我想要的方式(好的,我知道了如何实现这一点……如果您编辑您的答案,我会将其标记为已接受的答案。我必须在conf.py文件中的StandaloneHTMLBuilder类的monkeypatch中添加一些代码,方法是重新定义支持的_image_类型,将image/gif置于image/pngad之前,但您没有提供明确的代码。下面是我的猜测
支持的图片图片图片类型=['image/gif'、'image/svg+xml'、'image/png'、'image/jpeg']
来自sphinx.builders.html导入StandaloneHTMLBuilder StandaloneHTMLBuilder.supported图片图片图片图片图片类型=['image/svg+xml'、/gif'、'image/png'、/image/jpeg']