正如标题所述,我正在使用sphinx-doc,并且我真的希望在构建输出为latexpdf时有条件地呈现静态PNG,而在为web构建时则为动画GIF。
理想情况下,希望能够在rst文件本身中以某种方式实现这一点...语义上:
如果构建器 == html: .. image: blah blah elif 构建器 == latexpdf: .. image: blah blah
正如标题所述,我正在使用sphinx-doc,并且我真的希望在构建输出为latexpdf时有条件地呈现静态PNG,而在为web构建时则为动画GIF。
理想情况下,希望能够在rst文件本身中以某种方式实现这一点...语义上:
如果构建器 == html: .. image: blah blah elif 构建器 == latexpdf: .. image: blah blah
以下是针对图片的Sphinx文档:
Sphinx extends the standard docutils behavior by allowing an asterisk for the extension:
.. image:: gnu.*
Sphinx then searches for all images matching the provided pattern and determines their type. Each builder then chooses the best image out of these candidates. For instance, if the file name
gnu.*
was given and two filesgnu.pdf
andgnu.png
existed in the source tree, the LaTeX builder would choose the former, while the HTML builder would prefer the latter. Supported image types and choosing priority are defined at Available builders.
要为给定的构建器定制“最佳图像”顺序,请编辑您的conf.py
,用您喜欢的supported_image_types
顺序来覆盖StandaloneHTMLBuilder
类。
from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.supported_image_types = [
'image/svg+xml',
'image/gif',
'image/png',
'image/jpeg'
]
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
supported_image_types = ['image/gif', 'image/svg+xml', 'image/png', 'image/jpeg']
。 - Steve Piercyfrom sphinx.builders.html import StandaloneHTMLBuilder StandaloneHTMLBuilder.supported_image_types = [ 'image/svg+xml', 'image/gif', 'image/png', 'image/jpeg' ]
- Robert Neville