diff options
| author | Araq <rumpf_a@web.de> | 2015-08-19 01:45:57 +0200 |
|---|---|---|
| committer | Araq <rumpf_a@web.de> | 2015-08-19 01:45:57 +0200 |
| commit | 1e55e2f771dbcecbadcfef7f1a2b49fbe99667e8 (patch) | |
| tree | 6abd2f3f6f25ed363ab42f5270ba2adf15f2caed | |
| parent | 5a262a07c689964062e395ec19be52382a818c4d (diff) | |
| download | packages-1e55e2f771dbcecbadcfef7f1a2b49fbe99667e8.tar.gz packages-1e55e2f771dbcecbadcfef7f1a2b49fbe99667e8.zip | |
added pretty_json.nim script
| -rw-r--r-- | pretty_json.nim | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/pretty_json.nim b/pretty_json.nim new file mode 100644 index 0000000..4249727 --- /dev/null +++ b/pretty_json.nim @@ -0,0 +1,41 @@ + +import strutils, json, os + +proc cleanupWhitespace(s: string): string = + ## Removes trailing whitespace and normalizes line endings to LF. + result = newStringOfCap(s.len) + var i = 0 + while i < s.len: + if s[i] == ' ': + var j = i+1 + while s[j] == ' ': inc j + if s[j] == '\c': + inc j + if s[j] == '\L': inc j + result.add '\L' + i = j + elif s[j] == '\L': + result.add '\L' + i = j+1 + else: + result.add ' ' + inc i + elif s[i] == '\c': + inc i + if s[i] == '\L': inc i + result.add '\L' + elif s[i] == '\L': + result.add '\L' + inc i + else: + result.add s[i] + inc i + if result[^1] != '\L': + result.add '\L' + +proc editJson() = + var contents = parseFile("packages.json") + doAssert contents.kind == JArray + writeFile("packages.json", contents.pretty.cleanupWhitespace) + +editJson() |
