Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sopel/sopel/modules/unicode_info.py /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
71 lines (58 sloc)
2.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| unicode_info.py - Sopel Codepoints Plugin | |
| Copyright 2013, Elsie Powell, embolalia.com | |
| Copyright 2008, Sean B. Palmer, inamidst.com | |
| Licensed under the Eiffel Forum License 2. | |
| https://sopel.chat | |
| """ | |
| from __future__ import annotations | |
| from sopel import plugin | |
| # Python built-in unicodedata uses UCD version 13 (as of Python 3.10) | |
| # unicodedata2 can provide a more recent version, so we use that if present | |
| # See also: https://docs.python.org/3/library/unicodedata.html | |
| try: | |
| # ignore type check for these imports (no stubs for unicodedata2) | |
| import unicodedata2 as unicodedata # type: ignore[import] | |
| except ImportError: | |
| import unicodedata # type: ignore[no-redef] | |
| def get_codepoint_name(char): | |
| """Retrieve the code point (and name, if possible) for a given character""" | |
| # Get the hex value for the code point, and drop the 0x from the front | |
| point = hex(ord(char))[2:] | |
| # Make the hex 4 characters long with preceding 0s, and all upper case | |
| point = point.rjust(4, '0').upper() | |
| # get codepoint's name | |
| name = None | |
| try: | |
| name = unicodedata.name(char) | |
| except ValueError: | |
| pass | |
| return point, name | |
| @plugin.command('u') | |
| @plugin.example('.u ‽', 'U+203D INTERROBANG (‽)', user_help=True) | |
| @plugin.example('.u 203D', 'U+203D INTERROBANG (‽)', user_help=True) | |
| @plugin.output_prefix('[unicode] ') | |
| def codepoint(bot, trigger): | |
| """Look up a Unicode character or a hexadecimal code point.""" | |
| arg = trigger.group(2) | |
| if not arg: | |
| bot.reply('What code point do you want me to look up?') | |
| return plugin.NOLIMIT | |
| stripped = arg.strip() | |
| if len(stripped) > 0: | |
| arg = stripped | |
| if len(arg) > 1: | |
| if arg.startswith('U+'): | |
| arg = arg[2:] | |
| try: | |
| arg = chr(int(arg, 16)) | |
| except (ValueError, TypeError): | |
| bot.reply("That's not a valid code point.") | |
| return plugin.NOLIMIT | |
| point, name = get_codepoint_name(arg) | |
| if name is None: | |
| name = '(No name found)' | |
| template = 'U+%s %s (\xe2\x97\x8c%s)' | |
| if not unicodedata.combining(arg): | |
| template = 'U+%s %s (%s)' | |
| bot.say(template % (point, name, arg)) |