Skip to content

Commit

Permalink
Merge pull request #53 from Textualize/bug/52/document-relative-files
Browse files Browse the repository at this point in the history
Handle document-relative links to non-markdown files when CWD isn't the document's location
  • Loading branch information
davep authored May 28, 2023
2 parents 8e0b596 + 0253266 commit f16b6d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

- Added some extra error capture when attempting to build a forge URL while
inferring the main branch name.
- Fixed following local file links where the file is document-relative and
you're visiting with a CWD other than the document's.
[#52](https://github.com/Textualize/frogmouth/issues/52)

## [0.6.0] - 2023-05-24

Expand Down
33 changes: 27 additions & 6 deletions frogmouth/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def visit(self, location: Path | URL, remember: bool = True) -> None:
# resource seems to exist...
if location.exists():
# ...ask the OS to open it.
open_url(str(location))
open_url(f"file:///{location.absolute()}")
else:
# It's a Path but it doesn't exist, there's not much else we
# can do with it.
Expand Down Expand Up @@ -385,19 +385,40 @@ def on_markdown_link_clicked(self, event: Markdown.LinkClicked) -> None:
Args:
event: The Markdown link click event to handle.
"""
# We'll be using the current location to help work out some relative
# things.
current_location = self.query_one(Viewer).location
# If the link we're to handle obviously looks like URL...
if is_likely_url(event.href):
# ...handle it as such. No point in truing trying to do anything
# else.
# ...handle it as such. No point in trying to do anything else.
self.visit(URL(event.href))
elif isinstance(current_location := self.query_one(Viewer).location, URL):
elif isinstance(current_location, URL):
# Seems we're currently visiting a remote location, and the href
# looks like a simple file path, so let's make a best effort to
# visit the file at the remote location.
self.visit(current_location.copy_with().join(event.href))
elif (local_file := Path(event.href)).exists():
# It looks like a local file and it exists...
self.visit(local_file)
elif (
isinstance(current_location, Path)
and (local_file := (current_location.parent / Path(event.href)))
.absolute()
.exists()
):
# It looks like a local file, and tested relative to the
# document we found it exists in the local filesystem, so let's
# assume that's what we're supposed to handle.
self.visit(local_file)
else:
# Final option is that it's a local path.
self.visit(Path(event.href))
# Yeah, not sure *what* this link is. Rather than silently fail,
# let's let the user know we don't know how to process this.
self.app.push_screen(
ErrorDialog(
"Unable to handle link",
f"Unable to work out how to handle this link:\n\n{event.href}",
)
)

def on_paste(self, event: Paste) -> None:
"""Handle a paste event.
Expand Down

0 comments on commit f16b6d8

Please sign in to comment.