2022-09-02 00:25:15 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-09-02 13:12:29 +02:00
|
|
|
import argparse
|
2022-09-02 00:25:15 +02:00
|
|
|
import os
|
2022-09-02 20:03:18 +02:00
|
|
|
import rcssmin
|
2022-09-02 00:25:15 +02:00
|
|
|
import unicodedata
|
2022-09-02 00:48:54 +02:00
|
|
|
from time import ctime
|
2022-09-02 20:03:18 +02:00
|
|
|
from pathlib import Path
|
2022-09-02 00:25:15 +02:00
|
|
|
from urllib.request import urlopen
|
|
|
|
|
2022-09-02 13:12:29 +02:00
|
|
|
cssmin = rcssmin._make_cssmin(python_only=True)
|
2022-09-02 00:25:15 +02:00
|
|
|
|
|
|
|
# https://stackoverflow.com/a/518232
|
|
|
|
def StripAccents(s):
|
|
|
|
return ''.join(c for c in unicodedata.normalize('NFD', s)
|
|
|
|
if unicodedata.category(c) != 'Mn')
|
|
|
|
|
|
|
|
def ReplaceList(Text, Match, Replace):
|
|
|
|
for m in Match:
|
|
|
|
Text = Text.replace(m, Replace)
|
|
|
|
return Text
|
|
|
|
|
2022-09-02 13:12:29 +02:00
|
|
|
def GetEmojiData(EmojiVer):
|
|
|
|
Response = urlopen(f"https://unicode.org/Public/emoji/{EmojiVer}/emoji-test.txt")
|
|
|
|
return Response.read().decode("utf-8")
|
|
|
|
|
|
|
|
def ParseEmojiData(Data):
|
|
|
|
Emojis = []
|
|
|
|
for Line in Data.splitlines():
|
|
|
|
if CheckEmojiLine(Line):
|
|
|
|
Emojis += [GetEmojiMeta(Line)]
|
|
|
|
return Emojis
|
|
|
|
|
2022-09-02 00:25:15 +02:00
|
|
|
def CheckEmojiLine(Line):
|
|
|
|
if Line.startswith("#"):
|
|
|
|
return False
|
|
|
|
if "; fully-qualified" not in Line:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def GetEmojiMeta(Line):
|
|
|
|
Line = Line.lower()
|
|
|
|
|
|
|
|
Code = Line.split(";")[0].rstrip(" ")
|
|
|
|
Code = ReplaceList(Code, ["fe0f","200d"], "")
|
|
|
|
Code = Code.replace(" ", " ")
|
|
|
|
Code = ReplaceList(Code, [" ","--"], "-")
|
|
|
|
Code = Code.removesuffix("-")
|
|
|
|
|
|
|
|
Char = Line.split("# ")[1].split(" ")[0]
|
|
|
|
|
|
|
|
Name = StripAccents("-".join(Line.split("# ")[1].split(" ")[2:]))
|
|
|
|
Name = ReplaceList(Name, [":",".","&","(",")"], "")
|
|
|
|
Name = Name.replace("--", "-").replace("--", "-") # Strange bug?
|
|
|
|
|
|
|
|
return {"Code":Code, "Char":Char, "Name":Name}
|
|
|
|
|
2022-09-02 20:03:18 +02:00
|
|
|
def WriteCSS(Emojis, Output, URLPrefix):
|
|
|
|
Path(Output).mkdir(parents=True, exist_ok=True)
|
2022-09-02 13:12:29 +02:00
|
|
|
with open("Preamble.css", "r") as f:
|
|
|
|
Preamble = f.read() + "\n"
|
|
|
|
with open("Comment.css", "r") as f:
|
|
|
|
Comment = f.read().format(BuildTime=ctime(), EmojiCount=str(len(Emojis)))
|
|
|
|
|
|
|
|
for Type in ["Chars Names", "Chars", "Names"]:
|
|
|
|
CSS = Preamble
|
|
|
|
Line = ""
|
|
|
|
if "Chars" in Type:
|
|
|
|
Line = ".twa-{EmojiChar} " + Line
|
|
|
|
if "Names" in Type:
|
|
|
|
Line = ".twa-{EmojiName} " + Line
|
|
|
|
if Type == "Chars Names":
|
|
|
|
Line = Line.replace(" .twa-", ", .twa-")
|
|
|
|
|
|
|
|
for Emoji in Emojis:
|
|
|
|
NewLine = Line.format(EmojiChar=Emoji["Char"], EmojiName=Emoji["Name"])
|
|
|
|
CSS += f"""\
|
|
|
|
{NewLine}{{
|
|
|
|
background-image: url("{URLPrefix}{Emoji["Code"]}.svg");
|
|
|
|
}}
|
|
|
|
"""
|
|
|
|
FileName = "twemoji-astonishing"
|
|
|
|
if Type == "Chars":
|
|
|
|
FileName += ".chars"
|
|
|
|
elif Type == "Names":
|
|
|
|
FileName += ".names"
|
|
|
|
|
2022-09-02 20:03:18 +02:00
|
|
|
with open(f"{Output}{FileName}.css", "w") as f:
|
2022-09-02 13:12:29 +02:00
|
|
|
f.write(CSS.replace("{CommentBlock}", Comment))
|
2022-09-02 20:03:18 +02:00
|
|
|
with open(f"{Output}{FileName}.min.css", "w") as f:
|
2022-09-02 13:12:29 +02:00
|
|
|
f.write(cssmin(CSS).replace("{CommentBlock}", "\n"+Comment))
|
|
|
|
|
|
|
|
def Main(Args):
|
2022-09-02 20:03:18 +02:00
|
|
|
Output = Args.Output if Args.Output else "Build/"
|
2022-09-02 13:12:29 +02:00
|
|
|
EmojiVer = Args.EmojiVer if Args.EmojiVer else "15.0"
|
2022-09-02 20:03:18 +02:00
|
|
|
URLPrefix = Args.URLPrefix if Args.URLPrefix else "i/"
|
2022-09-02 00:25:15 +02:00
|
|
|
|
|
|
|
print(f"[I] Getting v{EmojiVer} emoji data")
|
2022-09-02 13:12:29 +02:00
|
|
|
Data = GetEmojiData(EmojiVer)
|
2022-09-02 00:25:15 +02:00
|
|
|
|
|
|
|
print("[I] Parsing emoji data")
|
2022-09-02 13:12:29 +02:00
|
|
|
Emojis = ParseEmojiData(Data)
|
2022-09-02 00:25:15 +02:00
|
|
|
|
2022-09-02 20:03:18 +02:00
|
|
|
print(f"[I] Writing CSS to: {Output}")
|
|
|
|
WriteCSS(Emojis, Output, URLPrefix)
|
2022-09-02 00:25:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-09-02 13:12:29 +02:00
|
|
|
Parser = argparse.ArgumentParser()
|
2022-09-02 20:03:18 +02:00
|
|
|
Parser.add_argument('--Output', type=str)
|
2022-09-02 13:12:29 +02:00
|
|
|
Parser.add_argument('--EmojiVer', type=str)
|
|
|
|
Parser.add_argument('--URLPrefix', type=str)
|
|
|
|
|
|
|
|
Main(Parser.parse_args())
|