Skip to content

Commit

Permalink
update simpleicons css generation
Browse files Browse the repository at this point in the history
  • Loading branch information
arv-anshul committed Aug 18, 2024
1 parent e29bef0 commit 88d8596
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 43 deletions.
4 changes: 4 additions & 0 deletions data/simpleicons.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"githubactions",
"gnubash",
"html5",
"huggingface",
"instagram",
"jinja",
"json",
"jupyter",
"kaggle",
"langchain",
"linkedin",
"materialformkdocs",
"medium",
Expand All @@ -37,11 +39,13 @@
"rust",
"rye",
"scikitlearn",
"scrapy",
"selenium",
"spotify",
"sqlite",
"starship",
"streamlit",
"taipy",
"task",
"visualstudiocode",
"youtube",
Expand Down
20 changes: 14 additions & 6 deletions docs/stylesheets/simpleicons.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
.gnubash-hover:hover { color: #4EAA25; }
.html5 { color: #E34F26; }
.html5-hover:hover { color: #E34F26; }
.huggingface { color: #FFD21E; }
.huggingface-hover:hover { color: #FFD21E; }
.instagram { color: #E4405F; }
.instagram-hover:hover { color: #E4405F; }
.jinja { color: #B41717; }
Expand All @@ -34,6 +36,8 @@
.jupyter-hover:hover { color: #F37626; }
.kaggle { color: #20BEFF; }
.kaggle-hover:hover { color: #20BEFF; }
.langchain { color: #1C3C3C; }
.langchain-hover:hover { color: #1C3C3C; }
.linkedin { color: #0A66C2; }
.linkedin-hover:hover { color: #0A66C2; }
.materialformkdocs { color: #526CFE; }
Expand All @@ -48,12 +52,12 @@
.mysql-hover:hover { color: #4479A1; }
.numpy { color: #013243; }
.numpy-hover:hover { color: #013243; }
.obsidian { color: #483699; }
.obsidian-hover:hover { color: #483699; }
.obsidian { color: #7C3AED; }
.obsidian-hover:hover { color: #7C3AED; }
.pandas { color: #150458; }
.pandas-hover:hover { color: #150458; }
.playwright { color: #2EAD33; }
.playwright-hover:hover { color: #2EAD33; }
.playwright { color: var(--md-typeset-color); }
.playwright-hover:hover { color: var(--md-typeset-color); }
.plotly { color: #3F4F75; }
.plotly-hover:hover { color: #3F4F75; }
.polars { color: #CD792C; }
Expand All @@ -78,6 +82,8 @@
.rye-hover:hover { color: #90c820; }
.scikitlearn { color: #F7931E; }
.scikitlearn-hover:hover { color: #F7931E; }
.scrapy { color: #60A839; }
.scrapy-hover:hover { color: #60A839; }
.selenium { color: #43B02A; }
.selenium-hover:hover { color: #43B02A; }
.spotify { color: #1DB954; }
Expand All @@ -88,10 +94,12 @@
.starship-hover:hover { color: #DD0B78; }
.streamlit { color: #FF4B4B; }
.streamlit-hover:hover { color: #FF4B4B; }
.taipy { color: #FF371A; }
.taipy-hover:hover { color: #FF371A; }
.task { color: #29BEB0; }
.task-hover:hover { color: #29BEB0; }
.visualstudiocode { color: #007ACC; }
.visualstudiocode-hover:hover { color: #007ACC; }
.visualstudiocode { color: var(--md-typeset-color); }
.visualstudiocode-hover:hover { color: var(--md-typeset-color); }
.youtube { color: #FF0000; }
.youtube-hover:hover { color: #FF0000; }
.zedindustries { color: #084CCF; }
Expand Down
85 changes: 48 additions & 37 deletions docs_src/generate_simpleicons_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,70 @@
from __future__ import annotations

import json
import logging
from pathlib import Path

from simpleicons import icons
from urllib.request import urlopen

SIMPLEICONS_CSS_PATH = Path("docs/stylesheets/simpleicons.css")
SIMPLEICONS_JSON_PATH = Path("data/simpleicons.json")

SLUG_DATA_URL = "https://raw.githubusercontent.com/simple-icons/simple-icons/develop/slugs.md" # fmt: skip
SIMPLE_ICON_JSON_URL = "https://raw.githubusercontent.com/simple-icons/simple-icons/develop/_data/simple-icons.json"

UNAVAILABLE_ICONS = {
"arc": "FCBFBD",
"json": "var(--md-typeset-color)",
"materialformkdocs": "526CFE",
"polars": "CD792C",
"pydantic": "E92063",
"raycast": "FF6363",
"ruff": "D7FF64",
"rust": "var(--md-typeset-color)",
"rye": "90c820",
"zedindustries": "084CCF",
"rye": "90C820",
}

logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: - %(message)s",
)

def _fetch_icons_with_slug() -> list[dict[str, str]]:
with urlopen(SLUG_DATA_URL) as response: # noqa: S310
data = response.read().decode("utf-8").splitlines()
slug_md_lines = data[10:]

def generate_tech_css(tech_data: dict[str, str]) -> str:
"""Generates CSS for each tech."""
css_rules = [
"/*\n😎 Generated by 'docs_src/generate_simpleicons_css.py'\n*/\n",
]
for tech, color in tech_data.items():
css_rules.append(f".{tech} {{ color: {color}; }}")
css_rules.append(f".{tech}-hover:hover {{ color: {color}; }}")
return "\n".join(css_rules) + "\n"
def extract_slug(line: str) -> dict[str, str]:
title, slug = line[3:-3].split("` | `", 1)
return {"title": title, "slug": slug}

return [extract_slug(i) for i in slug_md_lines]


def generate_simpleicons_css() -> dict[str, str]:
simpleicons_data: set[str] = set(json.loads(SIMPLEICONS_JSON_PATH.read_bytes()))
def _fetch_icons_with_hex():
with urlopen(SIMPLE_ICON_JSON_URL) as response: # noqa: S310
data = json.loads(response.read().decode("utf-8"))
return [{"title": icon["title"], "hex": icon["hex"]} for icon in data["icons"]]

tech_colors: dict[str, str] = {}
for tech in sorted(simpleicons_data):
try:
icon_hex = UNAVAILABLE_ICONS.get(tech) or getattr(icons, f"si_{tech}").hex
except AttributeError:
logging.warning(f"{tech!r} not in 'simpleicons.icon' module.")
icon_hex = "FFFFFF"
tech_colors[tech] = icon_hex if icon_hex.startswith("v") else f"#{icon_hex}"
return tech_colors

def get_all_icons_dict() -> dict[str, str]:
data: dict[str, str] = {}
icons_with_hex = _fetch_icons_with_hex()
icons_with_slug = _fetch_icons_with_slug()
for slug in icons_with_slug:
for hex in icons_with_hex:
if slug["title"] == hex["title"]:
data[slug["slug"]] = hex["hex"]
break
return data


def generate_css(all_icons_dict: dict[str, str], slugs_set: set[str]) -> str:
"""Generates CSS for each tech."""
css_lines = [
"/*\n😎 Generated by 'docs_src/generate_simpleicons_css.py'\n*/",
"",
]
for slug in sorted(slugs_set):
color = UNAVAILABLE_ICONS.get(slug) or all_icons_dict.get(
slug, "var(--md-typeset-color)"
)
color = color if color.startswith("v") else f"#{color}"
css_lines.append(f".{slug} {{ color: {color}; }}")
css_lines.append(f".{slug}-hover:hover {{ color: {color}; }}")
return "\n".join(css_lines) + "\n"


if __name__ == "__main__":
tech_colors = generate_simpleicons_css()
simpleicons_css = generate_tech_css(tech_colors)
slugs_set: set[str] = set(json.loads(SIMPLEICONS_JSON_PATH.read_bytes()))
all_icons_dict = get_all_icons_dict()
simpleicons_css = generate_css(all_icons_dict, slugs_set)
SIMPLEICONS_CSS_PATH.write_text(simpleicons_css)

0 comments on commit 88d8596

Please sign in to comment.