More powerful match criteria for fragments
This commit is contained in:
parent
7b3e345a2a
commit
581126c748
@ -14,13 +14,12 @@ following contents:
|
|||||||
# Open any file with a fragment in vim, fragments are generated
|
# Open any file with a fragment in vim, fragments are generated
|
||||||
# by the hyperlink_grep kitten and nothing else so far.
|
# by the hyperlink_grep kitten and nothing else so far.
|
||||||
protocol file
|
protocol file
|
||||||
has_fragment yes
|
fragment_matches [0-9]+
|
||||||
action launch --type=overlay vim +${FRAGMENT} ${FILE_PATH}
|
action launch --type=overlay vim +${FRAGMENT} ${FILE_PATH}
|
||||||
|
|
||||||
# Open text files without fragments in the editor
|
# Open text files without fragments in the editor
|
||||||
protocol file
|
protocol file
|
||||||
mime text/*
|
mime text/*
|
||||||
has_fragment no
|
|
||||||
action launch --type=overlay ${EDITOR} ${FILE_PATH}
|
action launch --type=overlay ${EDITOR} ${FILE_PATH}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -75,8 +75,9 @@ lines. The various available criteria are:
|
|||||||
``url``
|
``url``
|
||||||
A regular expression that must match against the entire (unquoted) URL
|
A regular expression that must match against the entire (unquoted) URL
|
||||||
|
|
||||||
``has_fragment``
|
``fragment_matches``
|
||||||
A boolean (``yes/no``) on whether the URL has a fragment or not.
|
A regular expression that must match against the fragment (part after #) in
|
||||||
|
the URL
|
||||||
|
|
||||||
``mime``
|
``mime``
|
||||||
A comma separated list of MIME types, for example: ``text/*, image/*,
|
A comma separated list of MIME types, for example: ``text/*, image/*,
|
||||||
|
|||||||
@ -12,7 +12,7 @@ from typing import (
|
|||||||
)
|
)
|
||||||
from urllib.parse import ParseResult, unquote, urlparse
|
from urllib.parse import ParseResult, unquote, urlparse
|
||||||
|
|
||||||
from .conf.utils import to_bool, to_cmdline
|
from .conf.utils import to_cmdline
|
||||||
from .config import KeyAction, parse_key_action
|
from .config import KeyAction, parse_key_action
|
||||||
from .constants import config_dir
|
from .constants import config_dir
|
||||||
from .typing import MatchType
|
from .typing import MatchType
|
||||||
@ -53,7 +53,7 @@ def parse(lines: Iterable[str]) -> Generator[OpenAction, None, None]:
|
|||||||
x = parse_key_action(rest)
|
x = parse_key_action(rest)
|
||||||
if x is not None:
|
if x is not None:
|
||||||
actions.append(x)
|
actions.append(x)
|
||||||
elif key in ('mime', 'ext', 'protocol', 'file', 'path', 'url', 'has_fragment'):
|
elif key in ('mime', 'ext', 'protocol', 'file', 'path', 'url', 'fragment_matches'):
|
||||||
if key != 'url':
|
if key != 'url':
|
||||||
rest = rest.lower()
|
rest = rest.lower()
|
||||||
match_criteria.append(MatchCriteria(cast(MatchType, key), rest))
|
match_criteria.append(MatchCriteria(cast(MatchType, key), rest))
|
||||||
@ -107,8 +107,14 @@ def url_matches_criterion(purl: 'ParseResult', url: str, unquoted_path: str, mc:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if mc.type == 'has_fragment':
|
if mc.type == 'fragment_matches':
|
||||||
return to_bool(mc.value) == bool(purl.fragment)
|
import re
|
||||||
|
try:
|
||||||
|
pat = re.compile(mc.value)
|
||||||
|
except re.error:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return pat.search(unquote(purl.fragment)) is not None
|
||||||
|
|
||||||
if mc.type == 'path':
|
if mc.type == 'path':
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
|||||||
@ -43,7 +43,7 @@ from .config import ( # noqa; noqa
|
|||||||
)
|
)
|
||||||
|
|
||||||
EdgeLiteral = Literal['left', 'top', 'right', 'bottom']
|
EdgeLiteral = Literal['left', 'top', 'right', 'bottom']
|
||||||
MatchType = Literal['mime', 'ext', 'protocol', 'file', 'path', 'url', 'has_fragment']
|
MatchType = Literal['mime', 'ext', 'protocol', 'file', 'path', 'url', 'fragment_matches']
|
||||||
GRT_a = Literal['t', 'T', 'q', 'p', 'd']
|
GRT_a = Literal['t', 'T', 'q', 'p', 'd']
|
||||||
GRT_f = Literal[24, 32, 100]
|
GRT_f = Literal[24, 32, 100]
|
||||||
GRT_t = Literal['d', 'f', 't', 's']
|
GRT_t = Literal['d', 'f', 't', 's']
|
||||||
|
|||||||
@ -29,7 +29,7 @@ class TestOpenActions(BaseTest):
|
|||||||
spec = '''
|
spec = '''
|
||||||
protocol file
|
protocol file
|
||||||
mime text/*
|
mime text/*
|
||||||
has_fragment yes
|
fragment_matches .
|
||||||
AcTion launch $EDITOR $FILE_PATH $FRAGMENT
|
AcTion launch $EDITOR $FILE_PATH $FRAGMENT
|
||||||
action
|
action
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user