nimblepkglist.nim 2.4 KB

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