nimblepkglist.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import base64, strutils, json, htmlgen, dom, algorithm
  2. type
  3. TData = object
  4. content {.importc.}: cstring
  5. proc decodeContent(content: string): string =
  6. result = ""
  7. for line in content.splitLines:
  8. if line != "":
  9. result.add decode(line)
  10. proc contains(x: seq[JSonNode], s: string): bool =
  11. for i in x:
  12. assert i.kind == JString
  13. if i.str == s: return true
  14. proc processContent(content: string) =
  15. var jsonDoc = parseJson(content)
  16. assert jsonDoc.kind == JArray
  17. var jsonArr = jsonDoc.elems
  18. jsonArr.sort do (x, y: JsonNode) -> int:
  19. strutils.cmpIgnoreCase(x["name"].str, y["name"].str)
  20. var
  21. officialList = ""
  22. officialCount = 0
  23. unofficialList = ""
  24. unofficialCount = 0
  25. let
  26. endings = {'.', '!'}
  27. for pkg in jsonArr:
  28. assert pkg.kind == JObject
  29. if not pkg.hasKey"url": continue
  30. let pkgWeb =
  31. if pkg.hasKey("web"): pkg["web"].str
  32. else: pkg["url"].str
  33. let
  34. desc = pkg["description"].str
  35. dot = if desc.high > 0 and desc[desc.high] in endings: "" else: "."
  36. listItem = li(a(href=pkgWeb, pkg["name"].str), " ", desc & dot)
  37. if pkg["url"].str.startsWith("https://github.com/nim-lang") or
  38. pkg["url"].str.startsWith("git://github.com/nim-lang") or
  39. "official" in pkg["tags"].elems:
  40. officialCount.inc
  41. officialList.add listItem & "\n"
  42. else:
  43. unofficialCount.inc
  44. unofficialList.add listItem & "\n"
  45. var officialPkgListDiv = document.getElementById("officialPkgList")
  46. officialPkgListDiv.innerHTML =
  47. p("There are currently " & $officialCount &
  48. " official packages in the Nimble package repository.") &
  49. ul(officialList)
  50. var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
  51. unofficialPkgListDiv.innerHTML =
  52. p("There are currently " & $unofficialCount &
  53. " unofficial packages in the Nimble package repository.") &
  54. ul(unofficialList)
  55. proc gotPackageList(apiReply: TData) {.exportc.} =
  56. let decoded = decodeContent($apiReply.content)
  57. try:
  58. processContent(decoded)
  59. except:
  60. var officialPkgListDiv = document.getElementById("officialPkgList")
  61. var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
  62. let msg = p("Unable to retrieve package list: ",
  63. code(getCurrentExceptionMsg()))
  64. officialPkgListDiv.innerHTML = msg
  65. unofficialPkgListDiv.innerHTML = msg