Add command for generating fragment with occurrence number.

This commit is contained in:
nervuri 2021-12-25 18:10:50 +00:00
parent 56dd6bf08d
commit 83469384c1
1 changed files with 19 additions and 5 deletions

24
av98.py
View File

@ -1470,19 +1470,33 @@ Use 'ls -l' to see URLs."""
@needs_gi
def do_fragment(self, *args):
"""Add search fragment to the current URI."""
err = """Please provide text to include in the fragment."""
"""Add search fragment to the current URI.
Select Nth occurence of text: -tN."""
err = """Please provide text to include in the fragment.
Select Nth occurence of text: -tN."""
if not self.mime == 'text/gemini':
print("Can't generate fragment for %s." % self.mime)
return
fragment_input = args[0]
if not fragment_input:
# Get fragment text and occurrence number.
fragment_text = args[0]
occurrence = 0
if args[0][0:2] == '-t':
split = args[0][2:].split(' ')
if split[0].isdigit():
occurrence = int(split[0])
if len(split) > 1 or not occurrence:
fragment_text = ' '.join(split[1:]) # all but the first element
else:
fragment_text = ''
if not fragment_text:
print(err)
return
# Convert occurrence number to string.
occurrence = ':%d' % occurrence if occurrence else ''
# Percent-encode input, append as fragment and visit the new URI.
uri_without_fragment = self.gi.url.split('#')[0]
self.do_go(uri_without_fragment\
+ '#' + urllib.parse.quote(fragment_input))
+ '#' + urllib.parse.quote(fragment_text) + occurrence)
@needs_gi
def do_fold(self, *args):