【问题标题】:Use image in Sphinx as clickable link使用 Sphinx 中的图像作为可点击链接
【发布时间】:2020-07-29 11:50:20
【问题描述】:

问题

我有一张图片表,用作概览/介绍。我希望最终用户能够单击图像并链接到 HTML 页面以获取该图像的相应介绍信息1

问题是我似乎无法让链接部分工作。图像表显示得很好,但是单击图像只会将我带到未找到页面的屏幕(参见帖子底部的图像)。我在 Google 上花了大约一个小时,但还没有找到解决方案。




1每个图像都有一个对应的 .rst 文件,其中包含我希望最终用户看到的信息


信息:

  • 狮身人面像 1.8.5
  • Python 3.7.6 (MiniConda)
  • 从 reStructuredText 文件构建 html 页面
    • sphinx-build -b html source build
    • make clean html
    • make html
  • 在我的 conf.py 文件中没有做任何特别的事情,除了包含 RTD 主题
    • 我猜我需要在我的 conf.py 文件中做一些花哨的事情如果完全可以做我想做的事
  • 将 .rst 文件添加到 introduction_file.rst 中的 .. toc:: 指令没有帮助

这是我目前拥有的 reST 代码:

  • 图像指令都在一个表中;为简洁起见省略了表格
  • 我也相信“表格内的图像指令”不是问题
    .. filename is "introduction_file.rst"
    
    .. image:: images/my_first_image.png
       :scale: 100%
       :alt: My First PNG Image
       :align: center
       :target: introduction_files/my_first_image_intro_file.rst

    .. also didn't work:
    .. :target: introduction_files/my_first_image_intro_file.html 

文件结构:

  • Home.rst 是 HTML 页面的入口点(即在我重命名它并相应地重构 conf.py 之前,它曾经被称为 index.rst
--build
    ...
-- source
   |--Introduction/
      |--introduction_file.rst
      |--images/
         |--my_first_image.png
         |--my_second_image.png
             ...
      |--introduction_files/
         |--my_first_image_intro_file.rst
         |--my_second_image_intro_file.rst
             ...
   |--_static/
       ...
   |--_templates/
       ...
   |--conf.py
   |--Home.rst

我不反对在 HTML/CSS 中做我想做的事,但如果有一种方法可以在 sphinx 中做到这一点,那么我更愿意这样做。无论如何,我最终都会编辑 HTML 代码,但编辑越少越好; Sphinx 本质上是一个快速入门或模板。


当我单击图片表中的一张图片时,我在浏览器中看到的图片。 Chrome 中的 URL 栏显示了 .rst 文件的正确路径,所以我有点困惑。

  • 我尝试将 :target: 文件扩展名更改为 .html,但这也没有用


编辑:忘记将introduction_file.rst的位置添加到文件夹结构中



解决方案

混合路径。我正在链接到源目录中的文件,但需要链接到构建目录中的文件。必须导航回带有一些“../”前缀的根目录,然后导航到构建目录中的 .html 信息文件。换句话说,这就是它最终的样子:

    .. filename is "introduction_file.rst"
    
    .. image:: images/my_first_image.png
       :scale: 100%
       :alt: My First PNG Image
       :align: center
       :target: ../../../build/html/Introduction/introduction_files/my_first_image_intro_file.html

【问题讨论】:

    标签: html image python-sphinx restructuredtext


    【解决方案1】:

    target 选项的值必须相对于introduction_file.rst(您不提供其位置,因此您必须弄清楚),或者绝对相对于文档根目录,即/Introduction/introduction_files/my_first_image_intro_file.html

    【讨论】:

    • 忘记将其添加到文件夹结构中,已编辑。我想我已经得到它相对于 Introduction_file.rst 虽然。
    • ./introduction_files/my_first_image_intro_file.html 没用?如果没有,请尝试我在回答中建议的绝对路径。
    • 想通了。我正在链接到源目录中的文件,但需要链接到构建目录中的文件。几个 '../' + 'filename.stem' + '.html' 之后,它会去它现在应该去的地方。
    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2018-01-10
    • 2016-04-19
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多