Skip to main content

Implements a new reST include directive to translate relative paths.

Project description

sphinxcontrib-relativeinclude

Build Docs PyPI - Version PyPI - Python Version License: MIT

About

This package implements a new reST directive to include files and translate paths included in those files.

Installation

The project is hosted on PyPI, and can be installed via pip.

pip install sphinxcontrib-relativeinclude

You can find the contents of the README and the module documentation for the latest release online.

Motivation

Let's assume you want to transclude the README.rst sitting in your repository root, in your docs/index.rst, so it automatically shows up in your generated documentation. You can just use reST's standard include directive.

.. include: ../README.rst

Sidenote: If you're using a parser, like MyST, you could also easily include markdown files, of course.

This will insert the contents of your README in the appropriate place, and even take care of heading levels for you.

The problem arises when you have images or other files included in there. Sphinx won't resolve those links properly, i.e., relative to the README, but instead relative to your Sphinx index document. That means those pictures won't show up, which, needless to say, is not optimal. If you use your valuable time to create visual resources for your documentation these should also be included in the documentation output.

A honey badger
Picture of a honey badger by Sumeetmoghe on Wikimedia Commons (CC-BY-SA-4.0)

This picture uses a relative path to a file in the docs/assets directory, and would happily show up in your Git repo, but not in your documentation.

This is what this extension is supposed to solve. It defines a new relativeinclude directive, that takes relative paths in included files, and resolves them into absolute ones. This way your images show up in your documentation output, but you don't have to hardcode absolute paths in your documentation. (Cf. this awesome honey badger here)

Caveat emptor: At this point in time, nested includes are unfortunately not supported. (See TODOs)

Usage

You have to register the directive in the setup function in your conf.py file. (This is a bug, see TODOs)

def setup(app):
    """Register the directive."""
    from sphinxcontrib_relativeinclude import RelativeInclude
    app.add_directive("relativeinclude", RelativeInclude)

Then you can use it in your documentation index under the registered name.

.. relativeinclude: ../README.md
    :parser: myst_parser.docutils_

It supports the same options as the standard include directive. If not, you've found a bug, and I'd be happy if you reported it on the issue tracker. Please provide thorough description, and a minimal reproducible example, i.e., (abbreviated) reST files you used, potentially your conf.py contents and maybe other relevant info.

If you want to see some real code, check out this repositories docs/index.rst.

TODOs

  • support multiple levels of indirection
  • properly register directive on install

License

sphinxcontrib-relativeinclude is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxcontrib_relativeinclude-0.0.1a1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file sphinxcontrib_relativeinclude-0.0.1a1.tar.gz.

File metadata

File hashes

Hashes for sphinxcontrib_relativeinclude-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 43074cf08e819c3ee730c12ada794e3d322929fd7f38e4fbe5140b46bf4f50fb
MD5 af38b94a17ba5faefc3fe357990e3d0e
BLAKE2b-256 2d3b9f717fb3ff4290c0451fb98d0e8ae5810b61f61499fda2592f3461596c71

See more details on using hashes here.

File details

Details for the file sphinxcontrib_relativeinclude-0.0.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_relativeinclude-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 c84befc9eccde352be1a5414796bfe389f46441988fbc6a354a79ac67dcb07af
MD5 cbeb7c718c728c6eede34a48c249d4ed
BLAKE2b-256 9bb27d401bccaf639aa30bb8edbe38c683908727252abeb545e490c654859127

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page