@@ -288,34 +288,54 @@ def _make_absolute_href_path(
288288 parsed_source : URLParseResult ,
289289 parsed_start : URLParseResult ,
290290 start_is_dir : bool = False ,
291+ must_include_scheme : bool = False ,
291292) -> str :
292- # If the source is already absolute, just return it
293+ # If the source is already absolute and doesn't need a scheme just return it
293294 if os .path .isabs (parsed_source .path ):
294- return urlunparse (parsed_source )
295-
296- # If the start path is not a directory, get the parent directory
297- start_dir = (
298- parsed_start .path if start_is_dir else os .path .dirname (parsed_start .path )
299- )
295+ if must_include_scheme :
296+ abs_path = parsed_source .path
297+ else :
298+ return urlunparse (parsed_source )
299+ else :
300+ # If the start path is not a directory, get the parent directory
301+ start_dir = (
302+ parsed_start .path if start_is_dir else os .path .dirname (parsed_start .path )
303+ )
300304
301- # Join the start directory to the relative path and find the absolute path
302- abs_path = make_posix_style (
303- os .path .abspath (os .path .join (start_dir , parsed_source .path ))
304- )
305+ # Join the start directory to the relative path and find the absolute path
306+ abs_path = make_posix_style (
307+ os .path .abspath (os .path .join (start_dir , parsed_source .path ))
308+ )
305309
306- # Account for the normalization of abspath for
307- # things like /vsitar// prefixes by replacing the
308- # original start_dir text when abspath modifies the start_dir.
309- if not start_dir == make_posix_style (os .path .abspath (start_dir )):
310- abs_path = abs_path .replace (
311- make_posix_style (os .path .abspath (start_dir )), start_dir
310+ # Account for the normalization of abspath for
311+ # things like /vsitar// prefixes by replacing the
312+ # original start_dir text when abspath modifies the start_dir.
313+ if not start_dir == make_posix_style (os .path .abspath (start_dir )):
314+ abs_path = abs_path .replace (
315+ make_posix_style (os .path .abspath (start_dir )), start_dir
316+ )
317+
318+ # add a scheme if there isn't one already
319+ if must_include_scheme :
320+ return urlunparse (
321+ (
322+ "file" ,
323+ parsed_start .netloc ,
324+ abs_path ,
325+ parsed_source .params ,
326+ parsed_source .query ,
327+ parsed_source .fragment ,
328+ )
312329 )
313330
314331 return abs_path
315332
316333
317334def make_absolute_href (
318- source_href : str , start_href : str | None = None , start_is_dir : bool = False
335+ source_href : str ,
336+ start_href : str | None = None ,
337+ start_is_dir : bool = False ,
338+ must_include_scheme : bool = False ,
319339) -> str :
320340 """Returns a new string that represents ``source_href`` as an absolute path. If
321341 ``source_href`` is already absolute it is returned unchanged. If ``source_href``
@@ -332,6 +352,8 @@ def make_absolute_href(
332352 start_is_dir : If ``True``, ``start_href`` is treated as a directory.
333353 Otherwise, ``start_href`` is considered to be a path to a file. Defaults to
334354 ``False``.
355+ must_include_scheme : If ``True``, every output will have a scheme. This means
356+ that local filepaths will be prefixed with `file://`. Defaults to ``False``.
335357
336358 Returns:
337359 str: The absolute HREF.
@@ -349,7 +371,9 @@ def make_absolute_href(
349371 if parsed_source .scheme != "" or parsed_start .scheme != "" :
350372 return _make_absolute_href_url (parsed_source , parsed_start , start_is_dir )
351373 else :
352- return _make_absolute_href_path (parsed_source , parsed_start , start_is_dir )
374+ return _make_absolute_href_path (
375+ parsed_source , parsed_start , start_is_dir , must_include_scheme
376+ )
353377
354378
355379def is_absolute_href (href : str ) -> bool :
0 commit comments