Skip to content

Commit

Permalink
Use pathlib for url _embed_code_links (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyleeow committed Aug 2, 2023
1 parent c81ad78 commit 2e1e2a8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions sphinx_gallery/docs_resolv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import urllib.request as urllib_request
import urllib.parse as urllib_parse
from pathlib import Path
from urllib.error import HTTPError, URLError

from sphinx.errors import ExtensionError
Expand Down Expand Up @@ -143,23 +144,22 @@ def __init__(self, config, doc_url, gallery_dir, relative=False):
self.relative = relative
self._link_cache = {}

if doc_url.startswith(('http://', 'https://')):
if isinstance(doc_url, Path):
index_url = os.path.join(doc_url, 'index.html')
searchindex_url = os.path.join(doc_url, 'searchindex.js')
docopts_url = os.path.join(
doc_url, '_static', 'documentation_options.js')
else:
if relative:
raise ExtensionError(
'Relative links are only supported for local '
'URLs (doc_url cannot be absolute)')
index_url = doc_url + '/'
searchindex_url = doc_url + '/searchindex.js'
docopts_url = doc_url + '/_static/documentation_options.js'
else:
index_url = os.path.join(doc_url, 'index.html')
searchindex_url = os.path.join(doc_url, 'searchindex.js')
docopts_url = os.path.join(
doc_url, '_static', 'documentation_options.js')

# detect if we are using relative links on a Windows system
if (os.name.lower() == 'nt' and
not doc_url.startswith(('http://', 'https://'))):
if (os.name.lower() == 'nt' and isinstance(doc_url, Path)):
if not relative:
raise ExtensionError(
'You have to use relative=True for the local'
Expand Down Expand Up @@ -328,7 +328,7 @@ def _embed_code_links(app, gallery_conf, gallery_dir):
if url is None:
doc_resolvers[this_module] = SphinxDocLinkResolver(
app.config.sphinx_gallery_conf,
str(app.builder.outdir), src_gallery_dir, relative=True)
Path(app.builder.outdir), src_gallery_dir, relative=True)
else:
doc_resolvers[this_module] = SphinxDocLinkResolver(
app.config.sphinx_gallery_conf,
Expand Down

0 comments on commit 2e1e2a8

Please sign in to comment.