Created
March 15, 2022 01:29
-
-
Save apacheli/424ccf516c91736c978fcc60321b7442 to your computer and use it in GitHub Desktop.
mdbook_emoji.py
This file contains hidden or 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
import aiohttp | |
import asyncio | |
import os | |
async def main(): | |
async with aiohttp.ClientSession() as session: | |
headers = { | |
"accept": "application/vnd.github.v3+json" | |
} | |
async with session.request("GET", "https://api.github.com/emojis", headers=headers) as response: | |
s = await response.text() | |
s = s.replace("https://github.githubassets.com/images/icons/emoji/unicode/", "") | |
s = s.replace(".png?v8", "") | |
with open("emojis/emojis.json", "w") as f: | |
f.write(s) | |
if __name__ == "__main__" and not os.path.exists("emojis/emojis.json"): | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
This file contains hidden or 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
import json | |
import re | |
import sys | |
emojis = json.load(open("emojis/emojis.json", "r")) | |
def repl(match): | |
a = match.group(1) | |
b = emojis.get(a) | |
return f"&#x{b.replace('-', ';&#x')};" if b else f":{a}:" | |
def main(): | |
if len(sys.argv) > 1: | |
if sys.argv[1] == "supports": | |
sys.exit(0) | |
context, book = json.load(sys.stdin) | |
for section in book["sections"]: | |
section["Chapter"]["content"] = re.sub(r":(.+?):", repl, section["Chapter"]["content"]) | |
print(json.dumps(book)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment