Skip to content

Commit

Permalink
docs: fix parsing package name
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda committed Mar 27, 2024
1 parent 93b3f62 commit addbe15
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Lightning-DevToolbox documentation

.. figure:: https://pl-public-data.s3.amazonaws.com/assets_lightning/Lightning.gif
:alt: What is Lightning gif.
:width: 100 %
:width: 80 %

.. toctree::
:maxdepth: 1
Expand Down
8 changes: 8 additions & 0 deletions docs/source/test-page.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:orphan:

Testing page
============

This is some page serving exclusively for testing purposes.

Link to scikit-learn stable documentation: https://scikit-learn.org/stable/index.html
2 changes: 1 addition & 1 deletion src/lightning_utilities/docs/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def adjust_linked_external_docs(
return

# find the expression for package version in {} brackets if any, use re to find it
pkg_ver_all = re.findall(r"{([\w.]+)}", target_link)
pkg_ver_all = re.findall(r"{(.+)}", target_link)
for pkg_ver in pkg_ver_all:
target_link = _update_link_based_imported_package(target_link, pkg_ver, version_digits)

Expand Down
23 changes: 14 additions & 9 deletions tests/unittests/docs/test_formatting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import re

import pytest
from lightning_utilities.docs import adjust_linked_external_docs
Expand All @@ -8,25 +9,29 @@
def test_adjust_linked_external_docs(temp_docs):
# take config as it includes API references with `stable`
path_conf = os.path.join(temp_docs, "conf.py")
path_testpage = os.path.join(temp_docs, "test-page.rst")

def _get_line_with_numpy(path_rst: str) -> str:
def _get_line_with_numpy(path_rst: str, pattern: str) -> str:
with open(path_rst, encoding="UTF-8") as fopen:
lines = fopen.readlines()
# find the first line with figure reference
return next(ln for ln in lines if ln.lstrip().startswith('"numpy":'))
return next(ln for ln in lines if pattern in ln)

# validate the initial expectations
line = _get_line_with_numpy(path_conf)
line = _get_line_with_numpy(path_conf, pattern='"numpy":')
assert "https://numpy.org/doc/stable/" in line
line = _get_line_with_numpy(path_testpage, pattern="Link to scikit-learn stable documentation:")
assert "https://scikit-learn.org/stable/" in line

adjust_linked_external_docs(
"https://numpy.org/doc/stable/", "https://numpy.org/doc/{numpy.__version__}/", temp_docs
)

import numpy as np

np_version = np.__version__.split(".")
adjust_linked_external_docs(
"https://scikit-learn.org/stable/", "https://scikit-learn.org/{scikit-learn}/", temp_docs
)

# validate the final state of index page
line = _get_line_with_numpy(path_conf)
assert f"https://numpy.org/doc/{'.'.join(np_version[:2])}/" in line
line = _get_line_with_numpy(path_conf, pattern='"numpy":')
assert re.search(r"https://numpy.org/doc/([1-9]\d*)\.(\d+)/", line)
line = _get_line_with_numpy(path_testpage, pattern="Link to scikit-learn stable documentation:")
assert re.search(r"https://scikit-learn.org/([1-9]\d*)\.(\d+)/", line)

0 comments on commit addbe15

Please sign in to comment.