sphinx_linkfix.extension

Sphinx extension to rewrite internal links in reStructuredText files.

Classes

RstImageRewriter

Transform to rewrite image paths early in the process.

RstLinkRewriter

Post-transform to rewrite internal links in reStructuredText files.

Functions

_is_external(→ bool)

Check if a given href is an external link.

_strip_docs_prefix(→ str)

Remove the leading docs folder prefix from a path string.

setup(→ dict[str, str | bool])

Set up the Sphinx extension.

Module Contents

class sphinx_linkfix.extension.RstImageRewriter(document, startnode=None)[source]

Bases: sphinx.transforms.SphinxTransform

Transform to rewrite image paths early in the process.

apply() None[source]

Rewrite image paths in the document.

default_priority = 210

Numerical priority of this transform, 0 through 999 (override).

class sphinx_linkfix.extension.RstLinkRewriter(document, startnode=None)[source]

Bases: sphinx.transforms.post_transforms.SphinxPostTransform

Post-transform to rewrite internal links in reStructuredText files.

_process_references(docs_relative_path: str, exts: tuple[str, Ellipsis]) int[source]

Process and rewrite reference nodes.

_sanitize_fragment_for_latex(fragment: str) str[source]

Sanitize a fragment for LaTeX compatibility.

Parameters:

fragment (str) – The fragment to sanitize.

Returns:

The sanitized fragment.

Return type:

str

run() None[source]

Rewrite internal links in the document.

default_priority = 999

Numerical priority of this transform, 0 through 999 (override).

sphinx_linkfix.extension._is_external(href: str) bool[source]

Check if a given href is an external link.

Parameters:

href (str) – The URL to check.

Returns:

True if the URL is external, False otherwise.

Return type:

bool

sphinx_linkfix.extension._strip_docs_prefix(path_str: str, docs_relative_path: str) str[source]

Remove the leading docs folder prefix from a path string.

This function handles both relative and absolute paths and normalizes different prefix formats: - Prefix “docs”, “docs/”, “/docs”, “/docs/” all work consistently - For relative paths like “docs/something”, it removes the docs prefix - For absolute paths like “/docs/something”, it removes the docs prefix

Parameters:
  • path_str (str) – The path string to modify.

  • docs_relative_path (str) – The prefix to remove (can be in various formats).

Returns:

The modified path string with the prefix removed.

Return type:

str

sphinx_linkfix.extension.setup(app: Any) dict[str, str | bool][source]

Set up the Sphinx extension.

Parameters:

app (Any) – The Sphinx application object.

Returns:

A dictionary with extension metadata.

Return type:

dict[str, str | bool]