git.iko.soy/typeable/pandoc
on master help sign in
Commented python modules/sample scripts.
commit96c2d542977e5cc510f315aad7c2ee357fe988c9 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 14 Aug 2013, 11:48 (13 years ago) parentf6b5735
5 files changed+59−7
scripts/caps.py+5 scripts/comments.py+8−1 scripts/deemph.py+7−3 scripts/myemph.py+6−3 scripts/pandoc.py+33
scripts/caps.py+5
@@ -1,4 +1,9 @@
11#!/usr/bin/env python
2+
3+"""Pandoc filter to convert all regular text to uppercase.
4+Code, link URLs, etc. are not affected.
5+"""
6+
27from pandoc import toJSONFilter
38
49def caps(key, value, format):
scripts/comments.py+8−1
@@ -2,9 +2,16 @@
22from pandoc import toJSONFilter
33import re
44
5+"""Pandoc filter that causes everything between
6+'<!-- BEGIN COMMENT -->' and '<!-- END COMMENT -->'
7+to be ignored. The comment lines must appear on
8+lines by themselves, with blank lines surrounding
9+them.
10+"""
11+
512incomment = False
613
7−def comment(k,v,format=""):
14+def comment(k,v,fmt):
815 global incomment
916 if k == 'RawBlock':
1017 f, s = v
scripts/deemph.py+7−3
@@ -2,9 +2,13 @@
22from pandoc import walk, toJSONFilter
33from caps import caps
44
5−def deemph(k,v,f):
6− if k == 'Emph' and f == 'html':
7− return walk(v,caps,f)
5+"""Pandoc filter that causes emphasized text to be displayed
6+in ALL CAPS.
7+"""
8+
9+def deemph(key, val, fmt):
10+ if key == 'Emph':
11+ return walk(val, caps, fmt)
812
913if __name__ == "__main__":
1014 toJSONFilter(deemph)
scripts/myemph.py+6−3
@@ -1,11 +1,14 @@
11#!/usr/bin/env python
22from pandoc import toJSONFilter, rawInline
33
4+"""Pandoc filter that causes emphasis to be rendered using
5+the custom macro '\myemph{...}' rather than '\emph{...}'
6+in latex. Other output formats are unaffected.
7+"""
8+
49def myemph(k, v, f):
510 if k == 'Emph' and f == 'latex':
6− v.insert(0, rawInline("latex", "\\myemph{"))
7− v.append(rawInline("latex", "}"))
8− return v
11+ return [rawInline("latex", "\\myemph{")] + v + [rawInline("latex","}")]
912
1013if __name__ == "__main__":
1114 toJSONFilter(myemph)
scripts/pandoc.py+33
@@ -1,7 +1,19 @@
1+# Author: John MacFarlane <jgm@berkeley.edu>
2+# Copyright: (C) 2013 John MacFarlane
3+# License: GPL version 2 or higher
4+
5+"""
6+Functions to aid writing python scripts that process the pandoc
7+AST serialized as JSON.
8+"""
9+
110import sys
211import json
312
413def walk(x, action, format = ""):
14+ """Walk a tree, applying an action to every object.
15+ Returns a modified tree.
16+ """
517 if isinstance(x, list):
618 array = []
719 for item in x:
@@ -27,6 +39,20 @@ def walk(x, action, format = ""):
2739 return x
2840
2941def toJSONFilter(action):
42+ """Converts an action into a filter that reads a JSON-formatted
43+ pandoc document from stdin, transforms it by walking the tree
44+ with the action, and returns a new JSON-formatted pandoc document
45+ to stdout. The argument is a function action(key, value, format),
46+ where key is the type of the pandoc object (e.g. 'Str', 'Para'),
47+ value is the contents of the object (e.g. a string for 'Str',
48+ a list of inline elements for 'Para'), and format is the target
49+ output format (which will be taken for the first command line
50+ argument if present). If the function returns None, the object
51+ to which it applies will remain unchanged. If it returns an
52+ object, the object will be replaced. If it returns a list, the
53+ list will be spliced in to the list to which the target object
54+ belongs. (So, returning an empty list deletes the object.)
55+ """
3056 doc = json.loads(sys.stdin.read())
3157 if len(sys.argv) > 1:
3258 format = sys.argv[1]
@@ -36,12 +62,19 @@ def toJSONFilter(action):
3662 json.dump(altered, sys.stdout)
3763
3864def rawInline(format, s):
65+ """Returns a 'RawInline' inline object.
66+ """
3967 return {"RawInline": [{"unFormat": format}, s]}
4068
4169def rawBlock(format, s):
70+ """Returns a 'RawBlock' inline object.
71+ """
4272 return {"RawBlock": [{"unFormat": format}, s]}
4373
4474def attributes(attrs):
75+ """Returns an attribute list, constructed from the
76+ dictionary attrs.
77+ """
4578 attrs = attrs or []
4679 ident = attrs["id"] or ""
4780 classes = attrs["classes"] or []