nim-nanovg/examples/generate_icons.nim
Joey Yakimowich-Payne 386e04dc49 Use generated icons
2020-09-17 20:25:07 -06:00

51 lines
No EOL
1.4 KiB
Nim

import os
import json, algorithm
import strutils, strformat
import httpcore
import httpclient
template camelCase(str: string, sep = '-'): string =
var res = newStringOfCap(str.len)
var i = 1
res.add(str[0].toUpperAscii)
while i < str.len:
if str[i] == sep and i < str.len - 1:
res.add(str[i+1].toUpperAscii)
i += 1
else:
res.add(str[i])
i += 1
res
proc main() =
const fontLicense {.strdefine.} = "free"
let url = "https://api.fontawesome.com/"
let client = newHttpClient()
let query = "{ \"query\": \"query { release(version:\\\"latest\\\") { icons(license:\\\"" & fontLicense & "\\\") {id, unicode} } }\" }"
let resp = client.request(url, HttpPost, body=query, headers = newHttpHeaders({"Content-type": "application/json"}))
let jsonStr = resp.body
let json = parseJson(jsonStr)
let icons = json{"data", "release", "icons"}
var res = """# NOTE: This file is autogenerated by generate_icons.nim
type
Icon* {.pure.} = enum
"""
if icons.isNil:
raise newException(CatchableError, "Icons is nil")
for icon in icons.getElems.sorted(proc (a, b: JsonNode): int = cmp(a["id"].to(string), b["id"].to(string))):
var id = icon["id"].to(string).camelCase()
if id[0] in {'0' .. '9'}:
id = "I" & id
let code = icon["unicode"].to(string).toUpperAscii
res.add(fmt" {id} = " & """"\u""" & code & "\"" & "\n")
writeFile(currentSourcePath().parentDir / "icons.nim", res)
when isMainModule:
main()