summary refs log tree commit diff stats
path: root/web/nimblepkglist.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-12-29 10:32:00 +0100
committerAraq <rumpf_a@web.de>2014-12-29 10:32:00 +0100
commit27f17437935ee335880c25c1f914f08f753b39ac (patch)
treeea5d48a5cd81a8809a0d86a6873f369bdf817044 /web/nimblepkglist.nim
parenta70a64b74d35c6e540ab914531741bbdcf4201a6 (diff)
downloadNim-27f17437935ee335880c25c1f914f08f753b39ac.tar.gz
release of 0.10.2
Diffstat (limited to 'web/nimblepkglist.nim')
-rw-r--r--web/nimblepkglist.nim76
1 files changed, 76 insertions, 0 deletions
diff --git a/web/nimblepkglist.nim b/web/nimblepkglist.nim
new file mode 100644
index 000000000..7070f281b
--- /dev/null
+++ b/web/nimblepkglist.nim
@@ -0,0 +1,76 @@
+import base64, strutils, json, htmlgen, dom, algorithm
+
+type
+  TData = object
+    content {.importc.}: cstring
+
+proc decodeContent(content: string): string =
+  result = ""
+  for line in content.splitLines:
+    if line != "":
+      result.add decode(line)
+
+proc contains(x: seq[JSonNode], s: string): bool =
+  for i in x:
+    assert i.kind == JString
+    if i.str == s: return true
+
+proc processContent(content: string) =
+  var jsonDoc = parseJson(content)
+  assert jsonDoc.kind == JArray
+  var jsonArr = jsonDoc.elems
+
+  jsonArr.sort do (x, y: JsonNode) -> int:
+    strutils.cmpIgnoreCase(x["name"].str, y["name"].str)
+
+  var
+    officialList = ""
+    officialCount = 0
+    unofficialList = ""
+    unofficialCount = 0
+  let
+    endings = {'.', '!'}
+
+  for pkg in jsonArr:
+    assert pkg.kind == JObject
+    let pkgWeb =
+      if pkg.hasKey("web"): pkg["web"].str
+      else: pkg["url"].str
+    let
+      desc = pkg["description"].str
+      dot = if desc.high > 0 and desc[desc.high] in endings: "" else: "."
+      listItem = li(a(href=pkgWeb, pkg["name"].str), " ", desc & dot)
+    if pkg["url"].str.startsWith("git://github.com/nimrod-code") or
+       pkg["url"].str.startsWith("git://github.com/nim-lang") or
+       "official" in pkg["tags"].elems:
+      officialCount.inc
+      officialList.add listItem & "\n"
+    else:
+      unofficialCount.inc
+      unofficialList.add listItem & "\n"
+
+  var officialPkgListDiv = document.getElementById("officialPkgList")
+
+  officialPkgListDiv.innerHTML =
+    p("There are currently " & $officialCount &
+      " official packages in the Nimble package repository.") &
+    ul(officialList)
+
+  var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
+
+  unofficialPkgListDiv.innerHTML =
+    p("There are currently " & $unofficialCount &
+      " unofficial packages in the Nimble package repository.") &
+    ul(unofficialList)
+
+proc gotPackageList(apiReply: TData) {.exportc.} =
+  let decoded = decodeContent($apiReply.content)
+  try:
+    processContent(decoded)
+  except:
+    var officialPkgListDiv = document.getElementById("officialPkgList")
+    var unofficialPkgListDiv = document.getElementById("unofficialPkgList")
+    let msg = p("Unable to retrieve package list: ",
+      code(getCurrentExceptionMsg()))
+    officialPkgListDiv.innerHTML = msg
+    unofficialPkgListDiv.innerHTML = msg