1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
"""Generate the files index.html and all.html that will contain a table of all supported emoji
Run with -minify to minify HTML for production"""
import sys
import os
import codecs
import django.conf
import django.template
from htmlmin.minify import html_minify
try:
import emoji
except ImportError:
include = os.path.relpath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.insert(0, include)
import emoji
print('Imported emoji from %s' % os.path.abspath(os.path.join(include, 'emoji')))
# Configuration
OUT_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_FILE = os.path.join(TEMPLATE_DIR, 'template.html')
data = {'defaultLang': 'en'}
languages = {
'en': emoji.emojize('en :United_Kingdom:'),
'alias': emoji.emojize('alias :United_Kingdom:'),
'es': emoji.emojize('es :Spain:'),
'ja': emoji.emojize('ja :Japan:'),
'ko': emoji.emojize('ko :South_Korea:'),
'pt': emoji.emojize('pt :Portugal:'),
'it': emoji.emojize('it :Italy:'),
'fr': emoji.emojize('fr :France:'),
'de': emoji.emojize('de :Germany:'),
'fa': emoji.emojize('fa :Iran:'),
'id': emoji.emojize('id :Indonesia:'),
'zh': emoji.emojize('zh :China:'),
'ru': emoji.emojize('ru :Russia:'),
'ar': emoji.emojize('ar :Saudi_Arabia:'),
'tr': emoji.emojize('tr :Turkey:', language='alias'),
}
language_args = {}
minify_enabled = sys.argv[-1] == '-minify'
if not minify_enabled:
print('Run with -minify to minify HTML')
print('Collecting emoji data...')
data['lists'] = lists = []
for language in languages:
emoji.config.load_language(language)
if language in language_args:
language_arg = language_args[language]
elif language == data['defaultLang']:
language_arg = ''
else:
language_arg = f'language="{language}"'
emoji_list = []
for code, emoji_data in emoji.EMOJI_DATA.items():
if language not in emoji_data:
continue
names = (
[emoji_data[language]]
if isinstance(emoji_data[language], str)
else emoji_data[language]
)
for name in names:
emoji_list.append(
{
'code': code,
'name': name,
'unicode': code.encode('ascii', 'backslashreplace').decode('ascii'),
'charname': code.encode('ascii', 'namereplace').decode('ascii'),
'xml': code.encode('ascii', 'xmlcharrefreplace').decode('ascii'),
}
)
listentry = {
'name': language,
'pretty': languages[language],
'languageArg': language_arg,
'emojis': emoji_list,
}
lists.append(listentry)
# Render with django
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
}
]
django.conf.settings.configure(TEMPLATES=TEMPLATES)
django.setup()
template = django.template.loader.get_template(TEMPLATE_FILE)
print("Render 'all.html' ...")
with codecs.open(os.path.join(OUT_DIR, 'all.html'), 'w', encoding='utf-8') as f:
html = template.render(data)
if minify_enabled:
print("Minify 'all.html' ...")
html = html_minify(html)
f.write(html)
print(f"Wrote to {os.path.join(OUT_DIR, 'all.html')}")
print("Render 'index.html' ...")
# Remove emoji except for default list
for listentry in lists:
if listentry['name'] != data['defaultLang']:
listentry['emojis'] = []
with codecs.open(os.path.join(OUT_DIR, 'index.html'), 'w', encoding='utf-8') as f:
html = template.render(data)
if minify_enabled:
print("Minify 'index.html' ...")
html = html_minify(html)
f.write(html)
print(f"Wrote to {os.path.join(OUT_DIR, 'index.html')}")
print('Done.')
|