projects

showcase

A public git mirror

icons.py 5.1 KB · 119 lines Raw
"""Bring in the icons the site uses: Gitea's sets.    python3 -I scripts/icons.py <material-icon-theme package dir> <@primer/octicons package dir>File icons are a subset of material-icon-theme (MIT, the set Gitea uses forfiles; npm material-icon-theme 5.40.0). The chosen SVGs go toweb/static/icons/material/, and the name, extension and folder maps tointernal/render/icons_gen.go. Every mapping that points at a chosen icon iskept, so coverage is wide while the icon count stays small.UI icons (tabs, stats, clone, copy, CI, activity) are Octicons (MIT, Gitea'sUI set; npm @primer/octicons 19.15.0), 16px variants. Only the ones in UI arecopied, to web/static/icons/octicons/<site name>.svg."""import json, os, re, shutil, sysFILES = """file go go-mod go-workspace markdown readme license changelog contributing authorscss sass less html javascript typescript react react_ts vue svelte astro json yaml toml xmlconsole python rust swift c cpp h hpp java kotlin ruby php lua nix docker makefile just gitnodejs npm yarn pnpm lock image svg font pdf zip database settings document log certificatekey tune editorconfig prettier eslint todo templ terraform graphql proto csharp dart elixirerlang haskell zig r julia powershell apple android gradle vim http table jupyter tex renovatecaddy nginx hcl robots favicon webassembly assembly vite tailwindcss deno bun biome""".split()FOLDERS = """folder folder-open folder-src folder-docs folder-command folder-public folder-testfolder-github folder-gitea folder-scripts folder-resource folder-images folder-config folder-libfolder-dist folder-node folder-git folder-vscode folder-docker folder-template folder-componentsfolder-utils folder-api folder-server folder-client folder-android folder-app folder-cssfolder-font folder-javascript folder-include folder-tools folder-temp folder-contentfolder-database folder-ios folder-mock folder-log folder-routes folder-views folder-hookfolder-theme folder-core folder-plugin folder-target folder-private folder-securefolder-claude folder-nix folder-ci folder-environment folder-examples folder-benchmarkfolder-markdown folder-svg folder-video folder-audio folder-desktop folder-mobile""".split()# site UI icon name -> Octicon name. Add only what a template or docKinds uses.UI = {    "book": "book",    "branch": "git-branch",    "check": "check",    "chevron": "triangle-down",    "code": "code",    "comment": "comment",    "commit": "git-commit",    "conduct": "code-of-conduct",    "copy": "copy",    "dot": "dot-fill",    "download": "download",    "fire": "flame",    "heart": "heart",    "history": "history",    "info": "info",    "issue": "issue-opened",    "issue-closed": "issue-closed",    "law": "law",    "link": "link",    "lock": "lock",    "merge": "git-merge",    "organization": "organization",    "package": "package",    "pull": "git-pull-request",    "pulse": "pulse",    "quote": "quote",    "repo-new": "repo",    "shield": "shield",    "support": "comment-discussion",    "tag": "tag",    "unlock": "unlock",    "user": "person",    "users": "people",    "x": "x",}material, octicons = sys.argv[1], sys.argv[2]out = "web/static/icons"m = json.load(open(os.path.join(material, "dist/material-icons.json")))have = set(m["iconDefinitions"])icons = [n for n in FILES + FOLDERS if n in have]missing = sorted(set(FILES + FOLDERS) - have)if missing:    print("not in this version:", " ".join(missing))shutil.rmtree(out, ignore_errors=True)  # also drops icon sets no longer usedos.makedirs(os.path.join(out, "material"))os.makedirs(os.path.join(out, "octicons"))for n in icons:    src = os.path.normpath(os.path.join(material, "dist", m["iconDefinitions"][n]["iconPath"]))    shutil.copy(src, os.path.join(out, "material", n + ".svg"))shutil.copy(os.path.join(material, "LICENSE"), os.path.join(out, "material", "LICENSE"))for name, src in UI.items():    svg = open(os.path.join(octicons, "build/svg", src + "-16.svg")).read()    if 'viewBox="0 0 16 16"' not in svg or re.search(r"style=|<script|href=", svg):        sys.exit(f"{src}: unexpected SVG")    open(os.path.join(out, "octicons", name + ".svg"), "w").write(svg)shutil.copy(os.path.join(octicons, "LICENSE"), os.path.join(out, "octicons", "LICENSE"))def pick(table):    return {k.lower(): v for k, v in sorted(table.items()) if v in icons and '"' not in k}exts = pick(m["fileExtensions"])names = pick(m["fileNames"])folders = pick(m["folderNames"])for lang, icon in m.get("languageIds", {}).items():  # e.g. "go" for .go when only the language id maps    if icon in icons and lang not in exts:        exts[lang] = icondef gomap(name, d):    body = "".join(f'\t"{k}": "{v}",\n' for k, v in sorted(d.items()))    return f"var {name} = map[string]string{{\n{body}}}\n"with open("internal/render/icons_gen.go", "w") as f:    f.write("// Code generated by scripts/icons.py from material-icon-theme; DO NOT EDIT.\n\n")    f.write("package render\n\n")    f.write(gomap("iconExts", exts) + "\n" + gomap("iconNames", names) + "\n" + gomap("iconFolders", folders))print(f"{len(icons)} file icons, {len(UI)} UI icons, {len(exts)} extensions, {len(names)} file names, {len(folders)} folder names")