Source

unicode-symbols-dashboard / scripts / make_popup_html.py

# -*- coding: utf-8 -*-
import os.path
import unicodedata
from jinja2 import Template, Environment, FileSystemLoader

#[unicodedata.name(char) for char in u'(╯‵Д′)╯彡┻━┻']


MOST_USED_NAMES = ['WHITE STAR', 'BLACK STAR',
                   'HEAVY CHECK MARK', 'HEAVY BALLOT X',
                   'HEAVY BLACK HEART', 'CIRCLED WHITE STAR',
                   'HOT SPRINGS',

                   'BLACK SPADE SUIT', 'BLACK HEART SUIT',
                   'BLACK DIAMOND SUIT', 'BLACK CLUB SUIT',

                   'QUARTER NOTE', 'EIGHTH NOTE',
                   'BEAMED EIGHTH NOTES', 'BEAMED SIXTEENTH NOTES',

                   'WHITE SMILING FACE', 'BLACK SMILING FACE',
                   'WHITE FROWNING FACE',

                   'SKULL AND CROSSBONES',
                   'RADIOACTIVE SIGN', 'BIOHAZARD SIGN',
                   'ATOM SYMBOL', 'HIGH VOLTAGE SIGN', 'WARNING SIGN',

                   'WHITE SUN WITH RAYS',
                   'FIRST QUARTER MOON', 'LAST QUARTER MOON',
                   'BLACK SUN WITH RAYS', 'CLOUD',
                   'UMBRELLA', 'SNOWMAN', 'COMET',

                   'CADUCEUS', 'ANKH', 'STAR AND CRESCENT',
                   'HAMMER AND SICKLE', 'PEACE SYMBOL',
                   'YIN YANG',

                    'BLACK SCISSORS', 'AIRPLANE', 'ENVELOPE', 
                    'VICTORY HAND', 'WRITING HAND', 'WHEELCHAIR SYMBOL',
                   
                   'COPYRIGHT SIGN', 'TRADE MARK SIGN',
                   'OPTION KEY', 'PLACE OF INTEREST SIGN', 'UPWARDS WHITE ARROW',

                   'KATAKANA LETTER TU', 'KATAKANA LETTER SI', 'KATAKANA LETTER SMALL TU',

                   'LOWER SEVEN EIGHTHS BLOCK', 'JAPANESE INDUSTRIAL STANDARD SYMBOL']



TYPOGRAPHY_SYMBOLS = [
    'DAGGER', 'DOUBLE DAGGER', 'DITTO MARK', 'INVERTED EXCLAMATION MARK', 'INVERTED QUESTION MARK',
    'COMMERCIAL MINUS SIGN', 'PER MILLE SIGN', 'PER TEN THOUSAND SIGN', 'PILCROW SIGN',
    'ASTERISM', 'DOWN TACK', 'UP TACK', 'THEREFORE', 'BECAUSE', 'INTERROBANG', 'REVERSED QUESTION MARK',
    'LOZENGE', 'REFERENCE MARK',

]



DINGBATS_RANGE = (0x2701, 0x27FF)
MISC_SYMBOLS_RANGE = (0x2600, 0x26FF)
ARROWS_RANGE = (0x2190, 0x21FF)
MATH_OPERATORS_RANGE = (0x2200, 0x22FF)
#SHAPES_RANGE = (0x25A0, 0x25FF)
#GREEK_LETTERS_RANGE = (0x0370, 0x03FF)

COMBOS = [
            ('LOOK OF DISAPPROVAL', ('KANNADA LETTER TTHA', 'LOW LINE', 'KANNADA LETTER TTHA')),
            ('A WHALE THING', ('HIRAGANA LETTER DU', 'CIRCLED DOT OPERATOR', 'WAVY LOW LINE',
                                'CIRCLED DOT OPERATOR', 'RIGHT PARENTHESIS', 'HIRAGANA LETTER DU')),
            ('JAPANESE FLIPPING TABLES', ('LEFT PARENTHESIS',
                                 'BOX DRAWINGS LIGHT ARC UP AND LEFT',
                                 'REVERSED PRIME',
                                 'CYRILLIC CAPITAL LETTER DE',
                                 'PRIME',
                                 'RIGHT PARENTHESIS',
                                 'BOX DRAWINGS LIGHT ARC UP AND LEFT',
                                 'CJK UNIFIED IDEOGRAPH-5F61',
                                 'BOX DRAWINGS HEAVY UP AND HORIZONTAL',
                                 'BOX DRAWINGS HEAVY HORIZONTAL',
                                 'BOX DRAWINGS HEAVY UP AND HORIZONTAL')),
    
           ('FLIPPING TABLES', ('LEFT PARENTHESIS',
                                  'BOX DRAWINGS LIGHT ARC UP AND LEFT',
                                  'DEGREE SIGN',
                                  'WHITE SQUARE',
                                  'DEGREE SIGN',
                                  'RIGHT PARENTHESIS',
                                  'SPACE',
                                  'BOX DRAWINGS LIGHT ARC UP AND LEFT',
                                  'PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS',
                                  'SPACE',
                                  'BOX DRAWINGS HEAVY UP AND HORIZONTAL',
                                  'BOX DRAWINGS HEAVY HORIZONTAL',
                                  'BOX DRAWINGS HEAVY UP AND HORIZONTAL')),

            ('GUN', (   'PRESENTATION FORM FOR VERTICAL LEFT BLACK LENTICULAR BRACKET',
                        'BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL',
                        'BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE',
                        'BOX DRAWINGS LIGHT HORIZONTAL')),

            ('REDACTED', ['LOWER SEVEN EIGHTHS BLOCK']*7)
]



def make_symbols_from_range(unicode_range):
    start, stop = unicode_range
    symbols = list()
    for i in range(start, stop+1):
        try:
            name = unicodedata.name(unichr(i))
            symbols.append((name, i))
        except ValueError:
            # skip the non mapped codepoints
            pass

    return symbols


def make_symbols_from_names(unicode_names):
    symbols = list()
    for name in unicode_names:
        unicode_point_value = ord(unicodedata.lookup(name))
        symbols.append((name, unicode_point_value))

    return symbols


def make_html_entity_strings_from_combos(combos):
    out = list()
    for name, char_names in combos:
        unicode_values = [ord(unicodedata.lookup(char_name)) for char_name in char_names ]
        html_string = ''.join(['&#{0};'.format(v) for v in unicode_values])
        out.append((name, html_string))
    return out



def main(outfile):
    most_used_symbols = make_symbols_from_names(MOST_USED_NAMES)
    dingbats_symbols = make_symbols_from_range(DINGBATS_RANGE)
    misc_symbols = make_symbols_from_range(MISC_SYMBOLS_RANGE)
    arrow_symbols = make_symbols_from_range(ARROWS_RANGE)
    #math_symbols = make_symbols_from_range(MATH_OPERATORS_RANGE)
    typography_symbols = make_symbols_from_names(TYPOGRAPHY_SYMBOLS)
    

    all_symbols = [
        ('most-used', most_used_symbols),
        ('typography', typography_symbols),
        ('dingbats', dingbats_symbols),
        ('misc-symbols', misc_symbols),
        #('arrows', arrow_symbols),
        #('math-operators', math_symbols),
    ]


    

    ENV = Environment( loader=FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')))
    template = ENV.get_template(os.path.join('chrome_popup_template.html'))

    print('Generating chrome extension')
    chrome_popup_html = template.render(all_symbols=all_symbols,
                                        combos=make_html_entity_strings_from_combos(COMBOS))

    with open(outfile, 'w') as f:
        print('Saving generated html to {0}'.format(outfile))
        f.write(chrome_popup_html)


if __name__=="__main__":
    import argparse
    parser = argparse.ArgumentParser(description='create the popup.html file for the chrome extension')
    parser.add_argument('--outfile', dest='outfile', type=str, required=True, help='path to output file')

    args = parser.parse_args()
    main(args.outfile)