Skip to content

Commit

Permalink
feat: allow file:// url schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Dec 28, 2024
1 parent 8adfb9b commit d6f38f1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ Then you can use a Sphinx substitution with your application name to refer to it
.. screenshot:: |example|/hello
```

## Local files
`sphinxcontrib-screenshot` supports opening local files with the `file://` URL scheme.

```rst
.. screenshot:: file:///path/to/my/project/index.html
```

You can also dynamically build the path where your files are stored by defining Sphinx substitutions in your configuration file:

```python
import pathlib
root = pathlib.Path(__file__).parent.resolve().as_uri()
rst_prolog = f".. |root| replace:: {root}"
```

```rst
.. screenshot:: |root|/index.html
```

## Local http server
`sphinxcontrib-screenshot` supports URLs with the HTTP and HTTPS protocols.
To take screenshots of local files and build the document while running a local server for them, you can use the NPM library [concurrently](https://www.npmjs.com/package/concurrently) in the following way:
Expand Down
4 changes: 2 additions & 2 deletions sphinxcontrib/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def run(self) -> typing.List[nodes.Node]:
pdf = 'pdf' in self.options
interactions = '\n'.join(self.content)

if urlparse(url).scheme not in {'http', 'https'}:
if urlparse(url).scheme not in {'http', 'https', 'file'}:
raise RuntimeError(
f'Invalid URL: {url}. Only HTTP/HTTPS URLs are supported.')
f'Invalid URL: {url}. Only HTTP/HTTPS/FILE URLs are supported.')

# Generate filename based on hash of parameters
hash_input = f'{url}_{height}_{width}_{color_scheme}_{interactions}'
Expand Down
7 changes: 3 additions & 4 deletions tests/roots/test-color-schemes/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import sys

sys.path.insert(0, str(pathlib.Path(__file__).parent.resolve()))

extensions = ['sphinxcontrib.screenshot']
screenshot_apps = {"example": "example_app:create_app"}

root = pathlib.Path(__file__).parent.resolve().as_uri()
rst_prolog = f".. |root| replace:: {root}"
27 changes: 0 additions & 27 deletions tests/roots/test-color-schemes/example_app.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/roots/test-color-schemes/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. screenshot:: |example|
.. screenshot:: |root|/index.html
:width: 800
:height: 600
:color-scheme: dark

0 comments on commit d6f38f1

Please sign in to comment.