Skip to content

Commit

Permalink
chore: add compressed icon index to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 16, 2023
1 parent d6bdb47 commit 2884660
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Binary file added mknodes/resources/icons.json.gzip
Binary file not shown.
32 changes: 24 additions & 8 deletions mknodes/utils/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import Any
import xml.etree.ElementTree as etree

from mknodes import paths


PYCONIFY_TO_PREFIXES = {
"mdi": "material",
Expand All @@ -17,7 +19,7 @@
}


def get_collection_map(*prefixes: str) -> dict[str, list[str]]:
def _get_collection_map(*prefixes: str) -> dict[str, list[str]]:
"""Return a dictionary with a mapping from pyconify name to icon prefixes.
In order to provide compatibility with the materialx-icon-index,
Expand All @@ -33,7 +35,7 @@ def get_collection_map(*prefixes: str) -> dict[str, list[str]]:
return mapping


def get_pyconify_icon_index(*collections: str) -> dict[str, dict[str, str]]:
def _get_pyconify_icon_index(*collections: str) -> dict[str, dict[str, str]]:
"""Return a icon index for the pymdownx emoji extension containing pyconify icons.
The dictionaries contain two key-value pairs:
Expand All @@ -44,7 +46,7 @@ def get_pyconify_icon_index(*collections: str) -> dict[str, dict[str, str]]:
import pyconify

index = {}
for coll, prefixes in get_collection_map(*collections).items():
for coll, prefixes in _get_collection_map(*collections).items():
collection = pyconify.collection(coll)
for icon_name in collection.get("uncategorized", []):
for prefix in prefixes:
Expand All @@ -68,7 +70,8 @@ def _patch_index_with_sets(icon_sets: Sequence[str]) -> dict[str, Any]:
"emoji": twemoji_db.emoji,
"aliases": twemoji_db.aliases,
}
icons = get_pyconify_icon_index(*icon_sets)
# icons = _get_pyconify_icon_index(*icon_sets)
icons = load_icon_index()
index["emoji"].update(icons)
return index

Expand Down Expand Up @@ -146,12 +149,25 @@ def get_icon_xml(icon: str) -> etree.Element:
return etree.fromstring(svg_text)


if __name__ == "__main__":
def write_icon_index():
import gzip
import json
import pathlib

mapping = get_pyconify_icon_index()
path = pathlib.Path() / "icons.json.gzip"
mapping = _get_pyconify_icon_index()
path = paths.RESOURCES / "icons.json.gzip"
with gzip.open(path, "w") as file:
file.write(json.dumps(mapping).encode())


def load_icon_index() -> dict:
import gzip
import json

path = paths.RESOURCES / "icons.json.gzip"
with gzip.open(path, "r") as file:
return json.loads(file.read())


if __name__ == "__main__":
idx = load_icon_index()
print(idx)

0 comments on commit 2884660

Please sign in to comment.