Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

**Released: WiP**

- Add a configuration option to allow for "traditional quit".
- Added a configuration option to allow for "traditional quit".
([#148](https://github.com/davep/hike/pull/148))
- Added support for requesting a Markdown version of a URL before deciding
it can't be handled in Hike.
([#150](https://github.com/davep/hike/pull/150))

## v1.2.1

Expand Down
8 changes: 7 additions & 1 deletion src/hike/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
update_configuration,
)
from .history import load_history, save_history
from .location_types import is_editable, looks_urllike, maybe_markdown
from .location_types import (
can_be_negotiated_to_markdown,
is_editable,
looks_urllike,
maybe_markdown,
)

##############################################################################
# Exports.
__all__ = [
"Bookmark",
"Bookmarks",
"can_be_negotiated_to_markdown",
"Configuration",
"is_editable",
"load_bookmarks",
Expand Down
42 changes: 41 additions & 1 deletion src/hike/data/location_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

##############################################################################
# httpx imports.
from httpx import URL
from httpx import URL, AsyncClient, HTTPStatusError, RequestError

##############################################################################
from typing_extensions import TypeIs

##############################################################################
# Local imports.
from .. import USER_AGENT
from ..types import HikeLocation
from .config import load_configuration

Expand Down Expand Up @@ -50,6 +51,45 @@ def _(location: URL) -> bool:
return maybe_markdown(location.path)


##############################################################################
async def can_be_negotiated_to_markdown(location: HikeLocation) -> bool:
"""Can the given location be negotiated to Markdown?

Args:
location: The location to test.

Returns:
`True` if the location can be negotiated to Markdown, `False` if
not.
"""
if isinstance(location, Path):
return False
async with AsyncClient() as client:
try:
response = await client.head(
location,
follow_redirects=True,
headers={
"user-agent": USER_AGENT,
"Accept": ",".join(
load_configuration().markdown_content_types + ["*/*;q=0.1"]
),
},
)
except RequestError:
return False
try:
response.raise_for_status()
except HTTPStatusError:
return False
if content_type := response.headers.get("content-type"):
return any(
content_type.startswith(accepted_type)
for accepted_type in load_configuration().markdown_content_types
)
return False


##############################################################################
def looks_urllike(candidate: str) -> bool:
"""Does the given value look like a URL?
Expand Down
7 changes: 5 additions & 2 deletions src/hike/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
ToggleNavigation,
)
from ..data import (
can_be_negotiated_to_markdown,
load_bookmarks,
load_command_history,
load_configuration,
Expand Down Expand Up @@ -246,13 +247,15 @@ def _copy_text_to_clipbaord(self, message: CopyToClipboard) -> None:
self.notify("Copied")

@on(OpenLocation)
def _open_markdown(self, message: OpenLocation) -> None:
async def _open_markdown(self, message: OpenLocation) -> None:
"""Open a file for viewing.

Args:
message: The message requesting the file be opened.
"""
if maybe_markdown(message.to_open):
if maybe_markdown(message.to_open) or await can_be_negotiated_to_markdown(
message.to_open
):
self.query_one(Viewer).location = message.to_open
if load_configuration().focus_viewer_on_load:
self.query_one(Viewer).focus()
Expand Down
7 changes: 6 additions & 1 deletion src/hike/widgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ async def _load_from_url(self, location: URL, remember: bool) -> None:
response = await client.get(
location,
follow_redirects=True,
headers={"user-agent": USER_AGENT},
headers={
"user-agent": USER_AGENT,
"Accept": ",".join(
load_configuration().markdown_content_types + ["*/*;q=0.1"]
),
},
)
except RequestError as error:
self.notify(str(error), title="Request error", severity="error", timeout=8)
Expand Down