#!/usr/bin/env python3
# SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense
#
# Script to create a bookmarklets that copy characters into the clipboard, for th
# purpose of convenient access to special symbols that are awkward to type.
# The output of this script is a bookmarks.html that can be imported into Firefox.
#
# Note that the way the bookmarklet works, the current document (i.e the document
# of the current browser tab) needs to be responding and modifiable; usually it is,
# but if the bookmarklet appears to not work, that's why.
#
import urllib.parse
CHARACTERS = ''.join([
'—',
'←↑→↓↝',
'⇒⇔↔≡¬∧∨⊕⊤⊥∀∃⊢⊨⊬⊭□◇∴'
'∩∪∅∈∉⊂⊃⊄⊅⊆⊇⊈⊉⊊⊋',
])
BOOKMARKLET_TEMPLATE = """
javascript:(function(){
const a = document.createElement('textarea');
a.value = '${CHARACTER}';
document.body.appendChild(a);
a.select();
a.setSelectionRange(0, 99999);
document.execCommand('copy');
document.body.removeChild(a);
})();
""".replace('\n', '')
BOOKMARKS_HTML_TEMPLATE = """
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>
<DL><p>
<DT><H3>Copy Characters</H3>
<DL><p>
${BOOKMARKS}
</DL><p>
</DL>
"""
def main():
bookmarks = []
for character in CHARACTERS:
bookmarklet = BOOKMARKLET_TEMPLATE.replace(
'${CHARACTER}',
urllib.parse.quote_plus(character)
)
bookmark = ' <DT><A HREF="{}">{}</A>\n'.format(
bookmarklet, character
)
bookmarks.append(bookmark)
bookmarks = ''.join(bookmarks)
html = BOOKMARKS_HTML_TEMPLATE.replace('${BOOKMARKS}', bookmarks)
print(html)
if __name__ == '__main__':
main()