Skip to content

Commit

Permalink
Handle links to local files that are relative to the document
Browse files Browse the repository at this point in the history
See #52.
  • Loading branch information
davep committed May 28, 2023
1 parent beba092 commit 506759c
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions frogmouth/screens/main.py
Original file line number Diff line number Diff line change
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 in it exists, 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 506759c

Please sign in to comment.