Skip to content

Commit e571e00

Browse files
committed
Adopt the path convention of other directives like literalinclude.
- If the path is absolute, it is considered as relative to the document root (after stripping off the initial /) - If the path is relative, it is considered as relative to the source document
1 parent 65ea686 commit e571e00

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/jupyterlite_sphinx.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from sphinx.application import Sphinx
1616
from sphinx.util.docutils import SphinxDirective
17+
from sphinx.parsers import RSTParser
1718

1819

1920
CONTENT_DIR = "_contents"
@@ -184,12 +185,11 @@ def run(self):
184185
)
185186

186187
if self.arguments:
187-
notebook = self.arguments[0]
188-
189-
# If we didn't get an absolute path,
190-
# try to find the Notebook relatively to the source
191-
if not os.path.isabs(notebook):
192-
notebook = os.path.join(source_location, notebook)
188+
# As with other directives like literalinclude, an absolute path is
189+
# assumed to be relative to the document root, and a relative path
190+
# is assumed to be relative to the source file
191+
rel_filename, notebook = self.env.relfn2path(self.arguments[0])
192+
self.env.note_dependency(rel_filename)
193193

194194
notebook_name = os.path.basename(notebook)
195195

@@ -226,7 +226,7 @@ class RetroLiteDirective(_LiteDirective):
226226
iframe_cls = RetroLiteIframe
227227

228228

229-
class RetroLiteParser(rst.Parser):
229+
class RetroLiteParser(RSTParser):
230230
"""Sphinx source parser for Jupyter notebooks.
231231
232232
Shows the Notebook using retrolite."""
@@ -235,8 +235,10 @@ class RetroLiteParser(rst.Parser):
235235

236236
def parse(self, inputstring, document):
237237
title = os.path.splitext(os.path.basename(document.current_source))[0]
238+
# Make the "absolute" filename relative to the source root
239+
filename = "/" + os.path.relpath(document.current_source, self.env.app.srcdir)
238240
super().parse(
239-
f"{title}\n{'=' * len(title)}\n.. retrolite:: {document.current_source}",
241+
f"{title}\n{'=' * len(title)}\n.. retrolite:: {filename}",
240242
document,
241243
)
242244

0 commit comments

Comments
 (0)