Added module for writing python scripts, with several examples.

See scripts subdirectory.
This commit is contained in:
John MacFarlane 2013-08-13 23:11:33 -07:00
parent bd73d73a28
commit f6b5735d09
6 changed files with 110 additions and 6 deletions

View file

@ -91,14 +91,16 @@ isTextFormat :: String -> Bool
isTextFormat s = takeWhile (`notElem` "+-") s `notElem` ["odt","docx","epub","epub3"]
externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
externalFilter f args' d = E.catch
(do (exitcode, outbs, errbs) <- pipeProcess Nothing f args' $ encode d
externalFilter f args' d = E.handle filterException $
do (exitcode, outbs, errbs) <- pipeProcess Nothing f args' $ encode d
case exitcode of
ExitSuccess -> return $ either error id $ eitherDecode' outbs
ExitFailure _ -> err 83 $ "Error running filter `" ++ UTF8.toStringLazy outbs ++
UTF8.toStringLazy errbs ++ "'")
(\e -> let _ = (e :: E.SomeException)
in err 83 $ "Error running filter `" ++ f ++ "'")
ExitFailure _ -> err 83 $ "Error running filter " ++ f ++ "\n" ++
UTF8.toStringLazy outbs ++
UTF8.toStringLazy errbs
where filterException :: E.SomeException -> IO Pandoc
filterException e = err 83 $ "Error running filter " ++ f ++
"\n" ++ show e
-- | Data structure for command line options.
data Opt = Opt

9
scripts/caps.py Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env python
from pandoc import toJSONFilter
def caps(key, value, format):
if key == 'Str':
return {'Str': value.upper()}
if __name__ == "__main__":
toJSONFilter(caps)

23
scripts/comments.py Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env python
from pandoc import toJSONFilter
import re
incomment = False
def comment(k,v,format=""):
global incomment
if k == 'RawBlock':
f, s = v
fmt = f['unFormat']
if fmt == "html":
if re.search("<!-- BEGIN COMMENT -->", s):
incomment = True
return []
elif re.search("<!-- END COMMENT -->", s):
incomment = False
return []
if incomment:
return [] # suppress anything in a comment
if __name__ == "__main__":
toJSONFilter(comment)

10
scripts/deemph.py Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env python
from pandoc import walk, toJSONFilter
from caps import caps
def deemph(k,v,f):
if k == 'Emph' and f == 'html':
return walk(v,caps,f)
if __name__ == "__main__":
toJSONFilter(deemph)

11
scripts/myemph.py Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env python
from pandoc import toJSONFilter, rawInline
def myemph(k, v, f):
if k == 'Emph' and f == 'latex':
v.insert(0, rawInline("latex", "\\myemph{"))
v.append(rawInline("latex", "}"))
return v
if __name__ == "__main__":
toJSONFilter(myemph)

49
scripts/pandoc.py Executable file
View file

@ -0,0 +1,49 @@
import sys
import json
def walk(x, action, format = ""):
if isinstance(x, list):
array = []
for item in x:
if isinstance(item, dict):
for k in item:
res = action(k, item[k], format)
if res is None:
array.append(walk(item, action, format))
elif isinstance(res, list):
for z in res:
array.append(walk(z, action, format))
else:
array.append(walk(res, action, format))
else:
array.append(walk(item, action, format))
return array
elif isinstance(x, dict):
obj = {}
for k in x:
obj[k] = walk(x[k], action, format)
return obj
else:
return x
def toJSONFilter(action):
doc = json.loads(sys.stdin.read())
if len(sys.argv) > 1:
format = sys.argv[1]
else:
format = ""
altered = walk(doc, action, format)
json.dump(altered, sys.stdout)
def rawInline(format, s):
return {"RawInline": [{"unFormat": format}, s]}
def rawBlock(format, s):
return {"RawBlock": [{"unFormat": format}, s]}
def attributes(attrs):
attrs = attrs or []
ident = attrs["id"] or ""
classes = attrs["classes"] or []
keyvals = [x for x in attrs and x != "classes" and x != "id"]
return [ident, classes, keyvals]