Sphinx Doc - 如何在为 HTML 构建时呈现动画 GIF 而在为 latexpdf 构建时呈现 PNG?
Sphinx Doc - How do I render an animated GIF when building for HTML but a PNG when building for latexpdf?
如标题所述,我正在使用 sphinx-doc 并且我真的想在构建输出为 latexpdf 时有条件地呈现静态 PNG,在为网络构建时呈现动画 GIF。
理想情况下,能够以某种方式在第一个文件本身中执行此操作会很好...语义上 a:
如果构建器 == html:
.. 图片:等等等等
elif 生成器 == latexpdf:
.. 图片:blah blah
来自Sphinx documentation for images:
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 files gnu.pdf
and gnu.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.
要为给定的构建器自定义 "best image" 顺序,请编辑您的 conf.py
以使用您喜欢的 supported_image_types
顺序覆盖 StandaloneHTMLBuilder
class。
from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.supported_image_types = [
'image/svg+xml',
'image/gif',
'image/png',
'image/jpeg'
]
为了防止它对其他人有用,我使用了 Steve Piercy 的非常有用的答案的变体,该变体试图对代码进行未来验证并防止神秘丢失图像。它使用相同的结构,但默认情况下附加 StandaloneHTMLBuilder.supported_image_types 中不在我们提供的新集合中的任何项目。我在考虑如果 Sphinx 开始支持像 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
如标题所述,我正在使用 sphinx-doc 并且我真的想在构建输出为 latexpdf 时有条件地呈现静态 PNG,在为网络构建时呈现动画 GIF。
理想情况下,能够以某种方式在第一个文件本身中执行此操作会很好...语义上 a:
如果构建器 == html: .. 图片:等等等等 elif 生成器 == latexpdf: .. 图片:blah blah
来自Sphinx documentation for images:
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.
要为给定的构建器自定义 "best image" 顺序,请编辑您的 conf.py
以使用您喜欢的 supported_image_types
顺序覆盖 StandaloneHTMLBuilder
class。
from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.supported_image_types = [
'image/svg+xml',
'image/gif',
'image/png',
'image/jpeg'
]
为了防止它对其他人有用,我使用了 Steve Piercy 的非常有用的答案的变体,该变体试图对代码进行未来验证并防止神秘丢失图像。它使用相同的结构,但默认情况下附加 StandaloneHTMLBuilder.supported_image_types 中不在我们提供的新集合中的任何项目。我在考虑如果 Sphinx 开始支持像 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