Created
July 7, 2011 12:31
-
-
Save inky/1069404 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
Reads a series of HTML entities from stdin, and outputs a table of the | |
corresponding Unicode characters. | |
""" | |
import sys | |
from htmlentitydefs import name2codepoint | |
def codepoint(entity): | |
name = entity[1:-1] | |
if name.startswith('#'): | |
if name[1] == 'x': | |
return int(name[2:], 16) | |
else: | |
return int(name[1:]) | |
else: | |
return name2codepoint.get(name, None) | |
def tokens(input): | |
return (w for w in input.split() if w.startswith('&') and w.endswith(';')) | |
def main(): | |
for token in tokens(sys.stdin.read()): | |
try: | |
uc = unichr(codepoint(token)) | |
except TypeError: | |
uc = 'n/a' | |
print "%s\t%s" % (uc, token) | |
if __name__ == '__main__': | |
main() |
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
$ python entity2unicode.py <<< '… … … &troll; ☃' | |
… … | |
… … | |
… … | |
n/a &troll; | |
☃ ☃ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment