diff options
author | Stephen Finucane <stephen@that.guru> | 2019-12-22 20:09:45 +0300 |
---|---|---|
committer | Adam Turner <9087854+AA-Turner@users.noreply.github.com> | 2022-09-26 19:50:57 +0300 |
commit | 6adbb006f9fa342297bc67b9f8496ea667eede95 (patch) | |
tree | c23454d18b1ca524c905fc74c7f3f8b2484506b1 | |
parent | eb5b3aa25dbc918c8250a2336aaaf2b753ad36b5 (diff) |
Use path2doc rather than reinventing the wheel
We already have a helper function that allows us to convert a filename
to a "docname". Don't reinvent the wheel and simply use this when
building specific files with Sphinx. Note that this does change behavior
slightly, insofar as filenames that don't resolve to valid docnames will
now be ignored rather than being passed with their suffix and silently
ignored later, but that seems sane.
Signed-off-by: Stephen Finucane <stephen@that.guru>
-rw-r--r-- | sphinx/builders/__init__.py | 29 | ||||
-rw-r--r-- | tests/test_application.py | 4 |
2 files changed, 17 insertions, 16 deletions
diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index dd0c4328e..caf0ed567 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -267,30 +267,31 @@ class Builder: def build_specific(self, filenames: List[str]) -> None: """Only rebuild as much as needed for changes in the *filenames*.""" - # bring the filenames to the canonical format, that is, - # relative to the source directory and without source_suffix. - dirlen = len(self.srcdir) + 1 - to_write = [] - suffixes: Tuple[str] = tuple(self.config.source_suffix) # type: ignore + docnames: List[str] = [] + for filename in filenames: filename = path.normpath(path.abspath(filename)) + if not filename.startswith(self.srcdir): logger.warning(__('file %r given on command line is not under the ' 'source directory, ignoring'), filename) continue + if not path.isfile(filename): logger.warning(__('file %r given on command line does not exist, ' 'ignoring'), filename) continue - filename = filename[dirlen:] - for suffix in suffixes: - if filename.endswith(suffix): - filename = filename[:-len(suffix)] - break - filename = filename.replace(path.sep, SEP) - to_write.append(filename) - self.build(to_write, method='specific', - summary=__('%d source files given on command line') % len(to_write)) + + docname = self.env.path2doc(filename) + if not docname: + logger.warning(__('file %r given on command line is not a valid ' + 'document, ignoring'), filename) + continue + + docnames.append(docname) + + self.build(docnames, method='specific', + summary=__('%d source files given on command line') % len(docnames)) def build_update(self) -> None: """Only rebuild what was changed or added since last build.""" diff --git a/tests/test_application.py b/tests/test_application.py index 90758a939..e297effce 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -149,7 +149,7 @@ def test_build_specific(app): app.srcdir / 'subdir/../subdir/excluded.txt'] # not normalized app.build(False, filenames) - expected = ['index', 'img.png', 'subdir/includes', 'subdir/excluded'] + expected = ['index', 'subdir/includes', 'subdir/excluded'] app.builder.build.assert_called_with(expected, method='specific', - summary='4 source files given on command line') + summary='3 source files given on command line') |