readthedocs / sphinx-autoapi

A new approach to API documentation in Sphinx.
https://sphinx-autoapi.readthedocs.io/
MIT License
415 stars 126 forks source link

html_static_path variable doesn't work as expected due to RST nesting. #454

Closed sachahu1 closed 1 week ago

sachahu1 commented 2 weeks ago

I believe the html_static_path variable isn't working as expected on sphinx-autoapi. Sphinx copies the _static folders correctly into the build but the RST structure generated by autoapi is nested and paths aren't automatically adjusted to match that. This results in the _static paths needing to be modified for any file to be imported correctly.

For instance, the following example doesn't import the image correctly withou manual modifications:

doc_package.cli

def another_function(a: int, b: List[float]):
  """A function

  Args:
    a: An integer
    b: a list of floats

  Returns:
    Nothing

    .. image:: _static/img/an_image.png
      :scale: 50 %
  """
  pass

It generates the following RST:

autoapi/doc_package/cli/index.rst


.. py:function:: another_function(a, b)

   A function

   :param a: An integer
   :param b: a list of floats

   :returns: Nothing 

             .. image:: _static/img/an_image.png
               :scale: 50 %

Given that _static is several directories higher, for the image to import properly, it would need to be ../../../_static/img/an_image.png.

AWhetter commented 1 week ago

This behaviour seems correct to me. If you don't want to have to change the path depending on the nesting, then you can prepend the path with a / to use a path relative to the documentation source directory (the one with the conf.py).

.. image:: /_static/img/an_image.png
    :scale: 50 %

See https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#images

sachahu1 commented 1 week ago

@AWhetter You're absolutely right, my mistake!