git.iko.soy/typeable/pandoc
on master help sign in
Removed scripts directory.

This has been put in its own github repo: https://github.com/jgm/pandoc-filters-python

commit70386a6a54c54189b8456b547a657873481a70b7 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 18 Aug 2013, 15:36 (13 years ago) parent8d441af
10 files changed−347
pandoc.cabal−10 scripts/abc.py−50 scripts/caps.py−15 scripts/comments.py−30 scripts/deemph.py−15 scripts/deflists.py−20 scripts/graphviz.py−47 scripts/myemph.py−18 scripts/pandoc.py−75 scripts/tikz.py−67
pandoc.cabal−10
@@ -112,16 +112,6 @@ Extra-Source-Files:
112112 -- generated man pages (produced post-build)
113113 man/man1/pandoc.1,
114114 man/man5/pandoc_markdown.5,
115− -- python library and sample python scripts
116− scripts/abc.py,
117− scripts/comments.py,
118− scripts/graphviz.py,
119− scripts/pandoc.py,
120− scripts/caps.py,
121− scripts/deemph.py,
122− scripts/myemph.py,
123− scripts/tikz.py,
124− scripts/deflists.py,
125115 -- tests
126116 tests/bodybg.gif,
127117 tests/docbook-reader.docbook
scripts/abc.pydeleted−50
@@ -1,50 +0,0 @@
1−#!/usr/bin/env python
2−
3−"""
4−Pandoc filter to process code blocks with class "abc" containing
5−ABC notation into images. Assumes that abcm2ps and ImageMagick's
6−convert are in the path. Images are put in the abc-images directory.
7−"""
8−
9−import hashlib
10−import os
11−import sys
12−from pandoc import toJSONFilter
13−from subprocess import Popen, PIPE, call
14−
15−imagedir = "abc-images"
16−
17−def sha1(x):
18− return hashlib.sha1(x).hexdigest()
19−
20−def abc2eps(abc, filetype, outfile):
21− p = Popen(["abcm2ps", "-O", outfile + '.eps', "-"],stdin=PIPE)
22− p.stdin.write(abc)
23− p.communicate()
24− p.stdin.close()
25− call(["convert", outfile + '.eps', outfile + '.' + filetype])
26−
27−def abc(key, value, format):
28− if key == 'CodeBlock':
29− [[ident,classes,keyvals], code] = value
30− if "abc" in classes:
31− outfile = imagedir + '/' + sha1(code)
32− if format == "html":
33− filetype = "png"
34− elif format == "latex":
35− filetype = "pdf"
36− else:
37− filetype = "png"
38− src = outfile + '.' + filetype
39− if not os.path.isfile(src):
40− try:
41− os.mkdir(imagedir)
42− sys.stderr.write('Created directory ' + imagedir + '\n')
43− except OSError:
44− pass
45− abc2eps(code, filetype, outfile)
46− sys.stderr.write('Created image ' + src + '\n')
47− return {'Para': [{'Image': [[], [src,""]]}]}
48−
49−if __name__ == "__main__":
50− toJSONFilter(abc)
scripts/caps.pydeleted−15
@@ -1,15 +0,0 @@
1−#!/usr/bin/env python
2−
3−"""
4−Pandoc filter to convert all regular text to uppercase.
5−Code, link URLs, etc. are not affected.
6−"""
7−
8−from pandoc import toJSONFilter
9−
10−def caps(key, value, format):
11− if key == 'Str':
12− return {'Str': value.upper()}
13−
14−if __name__ == "__main__":
15− toJSONFilter(caps)
scripts/comments.pydeleted−30
@@ -1,30 +0,0 @@
1−#!/usr/bin/env python
2−from pandoc import toJSONFilter
3−import re
4−
5−"""
6−Pandoc filter that causes everything between
7−'<!-- BEGIN COMMENT -->' and '<!-- END COMMENT -->'
8−to be ignored. The comment lines must appear on
9−lines by themselves, with blank lines surrounding
10−them.
11−"""
12−
13−incomment = False
14−
15−def comment(k,v,fmt):
16− global incomment
17− if k == 'RawBlock':
18− fmt, s = v
19− if fmt == "html":
20− if re.search("<!-- BEGIN COMMENT -->", s):
21− incomment = True
22− return []
23− elif re.search("<!-- END COMMENT -->", s):
24− incomment = False
25− return []
26− if incomment:
27− return [] # suppress anything in a comment
28−
29−if __name__ == "__main__":
30− toJSONFilter(comment)
scripts/deemph.pydeleted−15
@@ -1,15 +0,0 @@
1−#!/usr/bin/env python
2−from pandoc import walk, toJSONFilter
3−from caps import caps
4−
5−"""
6−Pandoc filter that causes emphasized text to be displayed
7−in ALL CAPS.
8−"""
9−
10−def deemph(key, val, fmt):
11− if key == 'Emph':
12− return walk(val, caps, fmt)
13−
14−if __name__ == "__main__":
15− toJSONFilter(deemph)
scripts/deflists.pydeleted−20
@@ -1,20 +0,0 @@
1−#!/usr/bin/env python
2−
3−"""
4−Pandoc filter to convert definition lists to bullet
5−lists with the defined terms in strong emphasis (for
6−compatibility with standard markdown).
7−"""
8−
9−from pandoc import toJSONFilter
10−
11−def deflists(key, value, format):
12− if key == 'DefinitionList':
13− return {'BulletList': [tobullet(t,d) for [t,d] in value]}
14−
15−def tobullet(term, defs):
16− return [{'Para': [{'Strong': term}]}] + [b for d in defs for b in d]
17−
18−
19−if __name__ == "__main__":
20− toJSONFilter(deflists)
scripts/graphviz.pydeleted−47
@@ -1,47 +0,0 @@
1−#!/usr/bin/env python
2−
3−"""
4−Pandoc filter to process code blocks with class "graphviz" into
5−graphviz-generated images.
6−"""
7−
8−import pygraphviz
9−import hashlib
10−import os
11−import sys
12−from pandoc import toJSONFilter
13−
14−def sha1(x):
15− return hashlib.sha1(x).hexdigest()
16−
17−imagedir = "graphviz-images"
18−
19−def graphviz(key, value, format):
20− if key == 'CodeBlock':
21− [[ident,classes,keyvals], code] = value
22− caption = "caption"
23− if "graphviz" in classes:
24− G = pygraphviz.AGraph(string = code)
25− G.layout()
26− filename = sha1(code)
27− if format == "html":
28− filetype = "png"
29− elif format == "latex":
30− filetype = "pdf"
31− else:
32− filetype = "png"
33− alt = [{'Str': caption}]
34− src = imagedir + '/' + filename + '.' + filetype
35− if not os.path.isfile(src):
36− try:
37− os.mkdir(imagedir)
38− sys.stderr.write('Created directory ' + imagedir + '\n')
39− except OSError:
40− pass
41− G.draw(src)
42− sys.stderr.write('Created image ' + src + '\n')
43− tit = ""
44− return {'Para': [{'Image': [alt, [src,tit]]}]}
45−
46−if __name__ == "__main__":
47− toJSONFilter(graphviz)
scripts/myemph.pydeleted−18
@@ -1,18 +0,0 @@
1−#!/usr/bin/env python
2−from pandoc import toJSONFilter
3−
4−"""
5−Pandoc filter that causes emphasis to be rendered using
6−the custom macro '\myemph{...}' rather than '\emph{...}'
7−in latex. Other output formats are unaffected.
8−"""
9−
10−def latex(s):
11− return {'RawInline': ['latex', s]}
12−
13−def myemph(k, v, f):
14− if k == 'Emph' and f == 'latex':
15− return [latex('\\myemph{')] + v + [latex('}')]
16−
17−if __name__ == "__main__":
18− toJSONFilter(myemph)
scripts/pandoc.pydeleted−75
@@ -1,75 +0,0 @@
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−
10−import sys
11−import json
12−
13−def walk(x, action, format = ""):
14− """Walk a tree, applying an action to every object.
15− Returns a modified tree.
16− """
17− if isinstance(x, list):
18− array = []
19− for item in x:
20− if isinstance(item, dict):
21− if item == {}:
22− array.append(walk(item, action, format))
23− else:
24− for k in item:
25− res = action(k, item[k], format)
26− if res is None:
27− array.append(walk(item, action, format))
28− elif isinstance(res, list):
29− for z in res:
30− array.append(walk(z, action, format))
31− else:
32− array.append(walk(res, action, format))
33− else:
34− array.append(walk(item, action, format))
35− return array
36− elif isinstance(x, dict):
37− obj = {}
38− for k in x:
39− obj[k] = walk(x[k], action, format)
40− return obj
41− else:
42− return x
43−
44−def toJSONFilter(action):
45− """Converts an action into a filter that reads a JSON-formatted
46− pandoc document from stdin, transforms it by walking the tree
47− with the action, and returns a new JSON-formatted pandoc document
48− to stdout. The argument is a function action(key, value, format),
49− where key is the type of the pandoc object (e.g. 'Str', 'Para'),
50− value is the contents of the object (e.g. a string for 'Str',
51− a list of inline elements for 'Para'), and format is the target
52− output format (which will be taken for the first command line
53− argument if present). If the function returns None, the object
54− to which it applies will remain unchanged. If it returns an
55− object, the object will be replaced. If it returns a list, the
56− list will be spliced in to the list to which the target object
57− belongs. (So, returning an empty list deletes the object.)
58− """
59− doc = json.loads(sys.stdin.read())
60− if len(sys.argv) > 1:
61− format = sys.argv[1]
62− else:
63− format = ""
64− altered = walk(doc, action, format)
65− json.dump(altered, sys.stdout)
66−
67−def attributes(attrs):
68− """Returns an attribute list, constructed from the
69− dictionary attrs.
70− """
71− attrs = attrs or []
72− ident = attrs["id"] or ""
73− classes = attrs["classes"] or []
74− keyvals = [x for x in attrs and x != "classes" and x != "id"]
75− return [ident, classes, keyvals]
scripts/tikz.pydeleted−67
@@ -1,67 +0,0 @@
1−#!/usr/bin/env python
2−
3−"""
4−Pandoc filter to process raw latex tikz environments into images.
5−Assumes that pdflatex is in the path, and that the standalone
6−package is available. Also assumes that ImageMagick's convert
7−is in the path. Images are put in the tikz-images directory.
8−"""
9−
10−import hashlib
11−import re
12−import os
13−import sys
14−import shutil
15−from pandoc import toJSONFilter
16−from subprocess import Popen, PIPE, call
17−from tempfile import mkdtemp
18−
19−imagedir = "tikz-images"
20−
21−def sha1(x):
22− return hashlib.sha1(x).hexdigest()
23−
24−def tikz2image(tikz, filetype, outfile):
25− tmpdir = mkdtemp()
26− olddir = os.getcwd()
27− os.chdir(tmpdir)
28− f = open('tikz.tex', 'w')
29− f.write("""\\documentclass{standalone}
30− \\usepackage{tikz}
31− \\begin{document}
32− """)
33− f.write(tikz)
34− f.write("\n\\end{document}\n")
35− f.close()
36− p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr)
37− os.chdir(olddir)
38− if filetype == 'pdf':
39− shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf')
40− else:
41− call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype])
42− shutil.rmtree(tmpdir)
43−
44−def tikz(key, value, format):
45− if key == 'RawBlock':
46− [fmt, code] = value
47− if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code):
48− outfile = imagedir + '/' + sha1(code)
49− if format == "html":
50− filetype = "png"
51− elif format == "latex":
52− filetype = "pdf"
53− else:
54− filetype = "png"
55− src = outfile + '.' + filetype
56− if not os.path.isfile(src):
57− try:
58− os.mkdir(imagedir)
59− sys.stderr.write('Created directory ' + imagedir + '\n')
60− except OSError:
61− pass
62− tikz2image(code, filetype, outfile)
63− sys.stderr.write('Created image ' + src + '\n')
64− return {'Para': [{'Image': [[], [src,""]]}]}
65−
66−if __name__ == "__main__":
67− toJSONFilter(tikz)