2025-05-12 15:50:02 -06:00

67 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
# copy-bookmark
# copies links to specific headers/elements of a page
# by Dakedres
#
# I guessed at how Python works.
# Based on https://github.com/LaurenceWarne/qute-code-hint/blob/master/code_select.py
# It is recommended you add this to your config.py to use this script:
# c.hints.selectors["bookmarks"] = [
# "h1[id]",
# "h2[id]",
# "h3[id]",
# "h4[id]",
# "h5[id]",
# "h6[id]",
# "h1 > a[id]",
# "h2 > a[id]",
# "h3 > a[id]",
# "h4 > a[id]",
# "h5 > a[id]",
# "h6 > a[id]",
# "header[id]",
# "footer[id]",
# "article[id]",
# "section[id]",
# "p[id]",
# "div[itemtype=\"https://schema.org/Answer\"]" # Stackoverflow answers
# ]
# And call it with
# hint bookmarks userscript copy-bookmark
import os
from urllib.parse import urlparse, urlunparse
from html.parser import HTMLParser
class BookmarkParser(HTMLParser):
def handle_starttag(self, tag, attrs):
for attr in attrs:
print(attr[0])
if(attr[0] == "id"):
self.bookmark_id = attr[1]
def close(self):
HTMLParser.close(self)
return self.bookmark_id
def send_to_qute(command):
with open(os.environ.get("QUTE_FIFO"), "w") as f:
f.write(command)
def construct_bookmark_url():
parser = BookmarkParser()
parser.feed(os.environ.get("QUTE_SELECTED_HTML"))
bookmark_id = parser.close()
url = urlparse(os.environ.get("QUTE_CURRENT_URL") )
url = url._replace(fragment=bookmark_id)
return urlunparse(url)
def main():
send_to_qute("yank inline '{url}'".format(url=construct_bookmark_url() ) )
if __name__ == "__main__":
main()