Skip to content

Commit

Permalink
Extract local html detection logic to separate function and use the `…
Browse files Browse the repository at this point in the history
…urllib.parse` module
  • Loading branch information
nstrayer committed Jul 30, 2024
1 parent 032c9e2 commit f7a0011
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
UiFrontendEvent,
WorkingDirectoryParams,
)
from .utils import JsonData, JsonRecord, alias_home
from .utils import JsonData, JsonRecord, alias_home, is_local_html_file

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -179,9 +179,8 @@ def open(self, url, new=0, autoraise=True):
if not self._comm:
return False

# Check if the url starts with 'file://', and if it does send the comm message
# for opening an html file and ends with '.html' or '.htm' to open the file in the browser
if url.startswith("file://") and (url.endswith(".html") or url.endswith(".htm")):
# If url is pointing to an HTML file, route to the ShowHtmlFile comm
if is_local_html_file(url):
self._comm.send_event(
name=UiFrontendEvent.ShowHtmlFile,
payload={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Union,
cast,
)
from urllib.parse import urlparse, unquote

JsonData = Union[Dict[str, "JsonData"], List["JsonData"], str, int, float, bool, None]
JsonRecord = Dict[str, JsonData]
Expand Down Expand Up @@ -384,3 +385,23 @@ def positron_ipykernel_usage():
"numpy.cdouble",
"numpy.clongdouble",
]


def is_local_html_file(url: str) -> bool:
"""Check if a URL points to a local HTML file."""
try:
parsed_url = urlparse(unquote(url))

# Check if it's a file scheme
if parsed_url.scheme not in ("file"):
return False

# Check if the path contains the .html or .htm extensions
path = parsed_url.path.lower()
if any(ext in path for ext in (".html", ".htm")):
return True

return False

except Exception:
return False

0 comments on commit f7a0011

Please sign in to comment.