Skip to content

Commit a74b13e

Browse files
authored
Merge pull request #207 from basnijholt/fix-182
Introduce new jupyter-download syntax, fixes #182
2 parents d427c79 + cfa0879 commit a74b13e

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

doc/source/index.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,16 @@ Downloading the code as a script
372372
--------------------------------
373373

374374
Jupyter Sphinx includes 2 roles that can be used to download the code embedded in a document:
375-
``:jupyter-download:script:`` (for a raw script file) and ``:jupyter-download:notebook:`` or ``:jupyter-download:nb:`` (for
375+
``:jupyter-download-script:`` (for a raw script file) and ``:jupyter-download-notebook:`` or ``:jupyter-download-nb:`` (for
376376
a Jupyter notebook).
377377

378378
These roles are equivalent to the standard sphinx `download role <https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-download>`__, **except** the extension of the file should not be given.
379379
For example, to download all the code from this document as a script we
380380
would use::
381381

382-
:jupyter-download:script:`click to download <index>`
382+
:jupyter-download-script:`click to download <index>`
383383

384-
Which produces a link like this: :jupyter-download:script:`click to download <index>`. The target that the role is
384+
Which produces a link like this: :jupyter-download-nb:`click to download <index>`. The target that the role is
385385
applied to (``index`` in this case) is the name of the document for which you wish to download
386386
the code. If a document contains ``jupyter-kernel`` directives with ``:id:`` specified, then
387387
the name provided to ``:id:`` can be used to get the code for the cells belonging to the
@@ -477,6 +477,7 @@ Release 0.4.0
477477
- Improve script handling by using ``nbconvert`` directly.
478478
- Remove deprecated enabling of the extension as ``jupyter_sphinx.execute``.
479479
- Implement different output priorities in HTML and LaTeX builders. In practice this allows to provide a better fallback in PDF output.
480+
- Introduce new ``jupyter-download`` syntax compatible with Sphinx≥4, ``jupyter-download-nb``, ``jupyter-download-notebook``, and ``jupyter-download-script``
480481

481482
Release 0.3.0
482483
~~~~~~~~~~~~~

jupyter_sphinx/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,13 @@ def setup(app):
271271
app.add_directive("jupyter-input", CellInput)
272272
app.add_directive("jupyter-output", CellOutput)
273273
app.add_directive("thebe-button", ThebeButton)
274-
app.add_role("jupyter-download:notebook", JupyterDownloadRole())
275-
app.add_role("jupyter-download:nb", JupyterDownloadRole())
276-
app.add_role("jupyter-download:script", JupyterDownloadRole())
274+
for sep in [":", "-"]:
275+
# Since Sphinx 4.0.0 using ":" inside of a role/directive does not work.
276+
# Therefore, we add "-" as separator to get e.g., jupyter-download-notebook
277+
# We leave the ":" syntax for backward compatibility reasons.
278+
app.add_role(f"jupyter-download{sep}notebook", JupyterDownloadRole())
279+
app.add_role(f"jupyter-download{sep}nb", JupyterDownloadRole())
280+
app.add_role(f"jupyter-download{sep}script", JupyterDownloadRole())
277281
app.add_transform(CombineCellInputOutput)
278282
app.add_transform(ExecuteJupyterCells)
279283

jupyter_sphinx/ast.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Manipulating the Sphinx AST with Jupyter objects"""
22

33
import json
4+
import warnings
45
from pathlib import Path
56

67
import docutils
@@ -587,7 +588,14 @@ def apply_styling(node, thebe_config):
587588

588589
class JupyterDownloadRole(ReferenceRole):
589590
def run(self):
590-
_, filetype = self.name.split(":")
591+
sep = ":" if ":" in self.name else "-"
592+
name, filetype = self.name.rsplit(sep, maxsplit=1)
593+
if sep == ":":
594+
warnings.warn(
595+
f"The {self.name} syntax is deprecated and "
596+
f"will be removed in 0.5.0, please use {name}-{filetype}",
597+
category=DeprecationWarning,
598+
)
591599

592600
assert filetype in ("notebook", "nb", "script")
593601
ext = ".ipynb" if filetype in ("notebook", "nb") else ".py"

tests/test_execute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ def test_download_role(text, reftarget, caption, tmp_path):
610610
"reporter.get_source_and_line": lambda l: ("source", l),
611611
}
612612
mock_inliner.configure_mock(**config)
613-
ret, msg = role("jupyter-download:notebook", text, text, 0, mock_inliner)
613+
ret, msg = role("jupyter-download-notebook", text, text, 0, mock_inliner)
614614

615615
if os.name == "nt":
616616
# Get equivalent abs path for Windows

0 commit comments

Comments
 (0)