showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
icons.py
5.1 KB · 119 lines
1"""Bring in the icons the site uses: Gitea's sets.23 python3 -I scripts/icons.py <material-icon-theme package dir> <@primer/octicons package dir>45File icons are a subset of material-icon-theme (MIT, the set Gitea uses for6files; npm material-icon-theme 5.40.0). The chosen SVGs go to7web/static/icons/material/, and the name, extension and folder maps to8internal/render/icons_gen.go. Every mapping that points at a chosen icon is9kept, so coverage is wide while the icon count stays small.1011UI icons (tabs, stats, clone, copy, CI, activity) are Octicons (MIT, Gitea's12UI set; npm @primer/octicons 19.15.0), 16px variants. Only the ones in UI are13copied, to web/static/icons/octicons/<site name>.svg.14"""15import json, os, re, shutil, sys1617FILES = """file go go-mod go-workspace markdown readme license changelog contributing authors18css sass less html javascript typescript react react_ts vue svelte astro json yaml toml xml19console python rust swift c cpp h hpp java kotlin ruby php lua nix docker makefile just git20nodejs npm yarn pnpm lock image svg font pdf zip database settings document log certificate21key tune editorconfig prettier eslint todo templ terraform graphql proto csharp dart elixir22erlang haskell zig r julia powershell apple android gradle vim http table jupyter tex renovate23caddy nginx hcl robots favicon webassembly assembly vite tailwindcss deno bun biome24""".split()25FOLDERS = """folder folder-open folder-src folder-docs folder-command folder-public folder-test26folder-github folder-gitea folder-scripts folder-resource folder-images folder-config folder-lib27folder-dist folder-node folder-git folder-vscode folder-docker folder-template folder-components28folder-utils folder-api folder-server folder-client folder-android folder-app folder-css29folder-font folder-javascript folder-include folder-tools folder-temp folder-content30folder-database folder-ios folder-mock folder-log folder-routes folder-views folder-hook31folder-theme folder-core folder-plugin folder-target folder-private folder-secure32folder-claude folder-nix folder-ci folder-environment folder-examples folder-benchmark33folder-markdown folder-svg folder-video folder-audio folder-desktop folder-mobile34""".split()3536# site UI icon name -> Octicon name. Add only what a template or docKinds uses.37UI = {38 "book": "book",39 "branch": "git-branch",40 "check": "check",41 "chevron": "triangle-down",42 "code": "code",43 "comment": "comment",44 "commit": "git-commit",45 "conduct": "code-of-conduct",46 "copy": "copy",47 "dot": "dot-fill",48 "download": "download",49 "fire": "flame",50 "heart": "heart",51 "history": "history",52 "info": "info",53 "issue": "issue-opened",54 "issue-closed": "issue-closed",55 "law": "law",56 "link": "link",57 "lock": "lock",58 "merge": "git-merge",59 "organization": "organization",60 "package": "package",61 "pull": "git-pull-request",62 "pulse": "pulse",63 "quote": "quote",64 "repo-new": "repo",65 "shield": "shield",66 "support": "comment-discussion",67 "tag": "tag",68 "unlock": "unlock",69 "user": "person",70 "users": "people",71 "x": "x",72}7374material, octicons = sys.argv[1], sys.argv[2]75out = "web/static/icons"76m = json.load(open(os.path.join(material, "dist/material-icons.json")))77have = set(m["iconDefinitions"])78icons = [n for n in FILES + FOLDERS if n in have]79missing = sorted(set(FILES + FOLDERS) - have)80if missing:81 print("not in this version:", " ".join(missing))8283shutil.rmtree(out, ignore_errors=True) # also drops icon sets no longer used84os.makedirs(os.path.join(out, "material"))85os.makedirs(os.path.join(out, "octicons"))86for n in icons:87 src = os.path.normpath(os.path.join(material, "dist", m["iconDefinitions"][n]["iconPath"]))88 shutil.copy(src, os.path.join(out, "material", n + ".svg"))89shutil.copy(os.path.join(material, "LICENSE"), os.path.join(out, "material", "LICENSE"))90for name, src in UI.items():91 svg = open(os.path.join(octicons, "build/svg", src + "-16.svg")).read()92 if 'viewBox="0 0 16 16"' not in svg or re.search(r"style=|<script|href=", svg):93 sys.exit(f"{src}: unexpected SVG")94 open(os.path.join(out, "octicons", name + ".svg"), "w").write(svg)95shutil.copy(os.path.join(octicons, "LICENSE"), os.path.join(out, "octicons", "LICENSE"))969798def pick(table):99 return {k.lower(): v for k, v in sorted(table.items()) if v in icons and '"' not in k}100101102exts = pick(m["fileExtensions"])103names = pick(m["fileNames"])104folders = pick(m["folderNames"])105for lang, icon in m.get("languageIds", {}).items(): # e.g. "go" for .go when only the language id maps106 if icon in icons and lang not in exts:107 exts[lang] = icon108109110def gomap(name, d):111 body = "".join(f'\t"{k}": "{v}",\n' for k, v in sorted(d.items()))112 return f"var {name} = map[string]string{{\n{body}}}\n"113114115with open("internal/render/icons_gen.go", "w") as f:116 f.write("// Code generated by scripts/icons.py from material-icon-theme; DO NOT EDIT.\n\n")117 f.write("package render\n\n")118 f.write(gomap("iconExts", exts) + "\n" + gomap("iconNames", names) + "\n" + gomap("iconFolders", folders))119print(f"{len(icons)} file icons, {len(UI)} UI icons, {len(exts)} extensions, {len(names)} file names, {len(folders)} folder names")