git.iko.soy/typeable/pandoc
on master help sign in
Added module for writing python scripts, with several examples.

See scripts subdirectory.

commitf6b5735d095dcfe2b0ef4aab02ff85acb47140c6 authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 13 Aug 2013, 23:11 (13 years ago) parentbd73d73
6 files changed+110−6
pandoc.hs+8−6 scripts/caps.py+9 scripts/comments.py+23 scripts/deemph.py+10 scripts/myemph.py+11 scripts/pandoc.py+49
pandoc.hs+8−6
@@ -91,14 +91,16 @@ isTextFormat :: String -> Bool
9191isTextFormat s = takeWhile (`notElem` "+-") s `notElem` ["odt","docx","epub","epub3"]
9292
9393externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
94−externalFilter f args' d = E.catch
95− (do (exitcode, outbs, errbs) <- pipeProcess Nothing f args' $ encode d
94+externalFilter f args' d = E.handle filterException $
95+ do (exitcode, outbs, errbs) <- pipeProcess Nothing f args' $ encode d
9696 case exitcode of
9797 ExitSuccess -> return $ either error id $ eitherDecode' outbs
98− ExitFailure _ -> err 83 $ "Error running filter `" ++ UTF8.toStringLazy outbs ++
99− UTF8.toStringLazy errbs ++ "'")
100− (\e -> let _ = (e :: E.SomeException)
101− in err 83 $ "Error running filter `" ++ f ++ "'")
98+ ExitFailure _ -> err 83 $ "Error running filter " ++ f ++ "\n" ++
99+ UTF8.toStringLazy outbs ++
100+ UTF8.toStringLazy errbs
101+ where filterException :: E.SomeException -> IO Pandoc
102+ filterException e = err 83 $ "Error running filter " ++ f ++
103+ "\n" ++ show e
102104
103105-- | Data structure for command line options.
104106data Opt = Opt
scripts/caps.pynew file+9
@@ -0,0 +1,9 @@
1+#!/usr/bin/env python
2+from pandoc import toJSONFilter
3+
4+def caps(key, value, format):
5+ if key == 'Str':
6+ return {'Str': value.upper()}
7+
8+if __name__ == "__main__":
9+ toJSONFilter(caps)
scripts/comments.pynew file+23
@@ -0,0 +1,23 @@
1+#!/usr/bin/env python
2+from pandoc import toJSONFilter
3+import re
4+
5+incomment = False
6+
7+def comment(k,v,format=""):
8+ global incomment
9+ if k == 'RawBlock':
10+ f, s = v
11+ fmt = f['unFormat']
12+ if fmt == "html":
13+ if re.search("<!-- BEGIN COMMENT -->", s):
14+ incomment = True
15+ return []
16+ elif re.search("<!-- END COMMENT -->", s):
17+ incomment = False
18+ return []
19+ if incomment:
20+ return [] # suppress anything in a comment
21+
22+if __name__ == "__main__":
23+ toJSONFilter(comment)
scripts/deemph.pynew file+10
@@ -0,0 +1,10 @@
1+#!/usr/bin/env python
2+from pandoc import walk, toJSONFilter
3+from caps import caps
4+
5+def deemph(k,v,f):
6+ if k == 'Emph' and f == 'html':
7+ return walk(v,caps,f)
8+
9+if __name__ == "__main__":
10+ toJSONFilter(deemph)
scripts/myemph.pynew file+11
@@ -0,0 +1,11 @@
1+#!/usr/bin/env python
2+from pandoc import toJSONFilter, rawInline
3+
4+def myemph(k, v, f):
5+ if k == 'Emph' and f == 'latex':
6+ v.insert(0, rawInline("latex", "\\myemph{"))
7+ v.append(rawInline("latex", "}"))
8+ return v
9+
10+if __name__ == "__main__":
11+ toJSONFilter(myemph)
scripts/pandoc.pynew file+49
@@ -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]