@@ -0,0 +1,49 @@
1+import sys
2+import json
3+
4+def walk(x, action, format = ""):
5+ if isinstance(x, list):
6+ array = []
7+ for item in x:
8+ if isinstance(item, dict):
9+ for k in item:
10+ res = action(k, item[k], format)
11+ if res is None:
12+ array.append(walk(item, action, format))
13+ elif isinstance(res, list):
14+ for z in res:
15+ array.append(walk(z, action, format))
16+ else:
17+ array.append(walk(res, action, format))
18+ else:
19+ array.append(walk(item, action, format))
20+ return array
21+ elif isinstance(x, dict):
22+ obj = {}
23+ for k in x:
24+ obj[k] = walk(x[k], action, format)
25+ return obj
26+ else:
27+ return x
28+
29+def toJSONFilter(action):
30+ doc = json.loads(sys.stdin.read())
31+ if len(sys.argv) > 1:
32+ format = sys.argv[1]
33+ else:
34+ format = ""
35+ altered = walk(doc, action, format)
36+ json.dump(altered, sys.stdout)
37+
38+def rawInline(format, s):
39+ return {"RawInline": [{"unFormat": format}, s]}
40+
41+def rawBlock(format, s):
42+ return {"RawBlock": [{"unFormat": format}, s]}
43+
44+def attributes(attrs):
45+ attrs = attrs or []
46+ ident = attrs["id"] or ""
47+ classes = attrs["classes"] or []
48+ keyvals = [x for x in attrs and x != "classes" and x != "id"]
49+ return [ident, classes, keyvals]