From e571e0032bcda6dbe7194a184cbbc26b7d55b91c Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Thu, 16 Jun 2022 19:12:03 +0000 Subject: [PATCH] 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 --- src/jupyterlite_sphinx.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/jupyterlite_sphinx.py b/src/jupyterlite_sphinx.py index 20fef71..1a48c87 100644 --- a/src/jupyterlite_sphinx.py +++ b/src/jupyterlite_sphinx.py @@ -14,6 +14,7 @@ from sphinx.application import Sphinx from sphinx.util.docutils import SphinxDirective +from sphinx.parsers import RSTParser CONTENT_DIR = "_contents" @@ -184,12 +185,11 @@ def run(self): ) if self.arguments: - notebook = self.arguments[0] - - # If we didn't get an absolute path, - # try to find the Notebook relatively to the source - if not os.path.isabs(notebook): - notebook = os.path.join(source_location, notebook) + # As with other directives like literalinclude, an absolute path is + # assumed to be relative to the document root, and a relative path + # is assumed to be relative to the source file + rel_filename, notebook = self.env.relfn2path(self.arguments[0]) + self.env.note_dependency(rel_filename) notebook_name = os.path.basename(notebook) @@ -226,7 +226,7 @@ class RetroLiteDirective(_LiteDirective): iframe_cls = RetroLiteIframe -class RetroLiteParser(rst.Parser): +class RetroLiteParser(RSTParser): """Sphinx source parser for Jupyter notebooks. Shows the Notebook using retrolite.""" @@ -235,8 +235,10 @@ class RetroLiteParser(rst.Parser): def parse(self, inputstring, document): title = os.path.splitext(os.path.basename(document.current_source))[0] + # Make the "absolute" filename relative to the source root + filename = "/" + os.path.relpath(document.current_source, self.env.app.srcdir) super().parse( - f"{title}\n{'=' * len(title)}\n.. retrolite:: {document.current_source}", + f"{title}\n{'=' * len(title)}\n.. retrolite:: {filename}", document, )