From 70386a6a54c54189b8456b547a657873481a70b7 Mon Sep 17 00:00:00 2001 From: John MacFarlane <fiddlosopher@gmail.com> Date: Sun, 18 Aug 2013 15:36:54 -0700 Subject: [PATCH] Removed scripts directory. This has been put in its own github repo: https://github.com/jgm/pandoc-filters-python --- 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 ---------------------------------------- 10 files changed, 347 deletions(-) delete mode 100755 scripts/abc.py delete mode 100755 scripts/caps.py delete mode 100755 scripts/comments.py delete mode 100755 scripts/deemph.py delete mode 100755 scripts/deflists.py delete mode 100755 scripts/graphviz.py delete mode 100755 scripts/myemph.py delete mode 100755 scripts/pandoc.py delete mode 100755 scripts/tikz.py diff --git a/pandoc.cabal b/pandoc.cabal index a3d0dfa83..e22908918 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -112,16 +112,6 @@ Extra-Source-Files: -- generated man pages (produced post-build) man/man1/pandoc.1, man/man5/pandoc_markdown.5, - -- python library and sample python scripts - scripts/abc.py, - scripts/comments.py, - scripts/graphviz.py, - scripts/pandoc.py, - scripts/caps.py, - scripts/deemph.py, - scripts/myemph.py, - scripts/tikz.py, - scripts/deflists.py, -- tests tests/bodybg.gif, tests/docbook-reader.docbook diff --git a/scripts/abc.py b/scripts/abc.py deleted file mode 100755 index daecd1070..000000000 --- a/scripts/abc.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python - -""" -Pandoc filter to process code blocks with class "abc" containing -ABC notation into images. Assumes that abcm2ps and ImageMagick's -convert are in the path. Images are put in the abc-images directory. -""" - -import hashlib -import os -import sys -from pandoc import toJSONFilter -from subprocess import Popen, PIPE, call - -imagedir = "abc-images" - -def sha1(x): - return hashlib.sha1(x).hexdigest() - -def abc2eps(abc, filetype, outfile): - p = Popen(["abcm2ps", "-O", outfile + '.eps', "-"],stdin=PIPE) - p.stdin.write(abc) - p.communicate() - p.stdin.close() - call(["convert", outfile + '.eps', outfile + '.' + filetype]) - -def abc(key, value, format): - if key == 'CodeBlock': - [[ident,classes,keyvals], code] = value - if "abc" in classes: - outfile = imagedir + '/' + sha1(code) - if format == "html": - filetype = "png" - elif format == "latex": - filetype = "pdf" - else: - filetype = "png" - src = outfile + '.' + filetype - if not os.path.isfile(src): - try: - os.mkdir(imagedir) - sys.stderr.write('Created directory ' + imagedir + '\n') - except OSError: - pass - abc2eps(code, filetype, outfile) - sys.stderr.write('Created image ' + src + '\n') - return {'Para': [{'Image': [[], [src,""]]}]} - -if __name__ == "__main__": - toJSONFilter(abc) diff --git a/scripts/caps.py b/scripts/caps.py deleted file mode 100755 index b86cd1520..000000000 --- a/scripts/caps.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python - -""" -Pandoc filter to convert all regular text to uppercase. -Code, link URLs, etc. are not affected. -""" - -from pandoc import toJSONFilter - -def caps(key, value, format): - if key == 'Str': - return {'Str': value.upper()} - -if __name__ == "__main__": - toJSONFilter(caps) diff --git a/scripts/comments.py b/scripts/comments.py deleted file mode 100755 index ded21039c..000000000 --- a/scripts/comments.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -from pandoc import toJSONFilter -import re - -""" -Pandoc filter that causes everything between -'<!-- BEGIN COMMENT -->' and '<!-- END COMMENT -->' -to be ignored. The comment lines must appear on -lines by themselves, with blank lines surrounding -them. -""" - -incomment = False - -def comment(k,v,fmt): - global incomment - if k == 'RawBlock': - fmt, s = v - 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) diff --git a/scripts/deemph.py b/scripts/deemph.py deleted file mode 100755 index f69dac5b8..000000000 --- a/scripts/deemph.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python -from pandoc import walk, toJSONFilter -from caps import caps - -""" -Pandoc filter that causes emphasized text to be displayed -in ALL CAPS. -""" - -def deemph(key, val, fmt): - if key == 'Emph': - return walk(val, caps, fmt) - -if __name__ == "__main__": - toJSONFilter(deemph) diff --git a/scripts/deflists.py b/scripts/deflists.py deleted file mode 100755 index 502963419..000000000 --- a/scripts/deflists.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python - -""" -Pandoc filter to convert definition lists to bullet -lists with the defined terms in strong emphasis (for -compatibility with standard markdown). -""" - -from pandoc import toJSONFilter - -def deflists(key, value, format): - if key == 'DefinitionList': - return {'BulletList': [tobullet(t,d) for [t,d] in value]} - -def tobullet(term, defs): - return [{'Para': [{'Strong': term}]}] + [b for d in defs for b in d] - - -if __name__ == "__main__": - toJSONFilter(deflists) diff --git a/scripts/graphviz.py b/scripts/graphviz.py deleted file mode 100755 index 519a3a9cc..000000000 --- a/scripts/graphviz.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python - -""" -Pandoc filter to process code blocks with class "graphviz" into -graphviz-generated images. -""" - -import pygraphviz -import hashlib -import os -import sys -from pandoc import toJSONFilter - -def sha1(x): - return hashlib.sha1(x).hexdigest() - -imagedir = "graphviz-images" - -def graphviz(key, value, format): - if key == 'CodeBlock': - [[ident,classes,keyvals], code] = value - caption = "caption" - if "graphviz" in classes: - G = pygraphviz.AGraph(string = code) - G.layout() - filename = sha1(code) - if format == "html": - filetype = "png" - elif format == "latex": - filetype = "pdf" - else: - filetype = "png" - alt = [{'Str': caption}] - src = imagedir + '/' + filename + '.' + filetype - if not os.path.isfile(src): - try: - os.mkdir(imagedir) - sys.stderr.write('Created directory ' + imagedir + '\n') - except OSError: - pass - G.draw(src) - sys.stderr.write('Created image ' + src + '\n') - tit = "" - return {'Para': [{'Image': [alt, [src,tit]]}]} - -if __name__ == "__main__": - toJSONFilter(graphviz) diff --git a/scripts/myemph.py b/scripts/myemph.py deleted file mode 100755 index 2a322b385..000000000 --- a/scripts/myemph.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python -from pandoc import toJSONFilter - -""" -Pandoc filter that causes emphasis to be rendered using -the custom macro '\myemph{...}' rather than '\emph{...}' -in latex. Other output formats are unaffected. -""" - -def latex(s): - return {'RawInline': ['latex', s]} - -def myemph(k, v, f): - if k == 'Emph' and f == 'latex': - return [latex('\\myemph{')] + v + [latex('}')] - -if __name__ == "__main__": - toJSONFilter(myemph) diff --git a/scripts/pandoc.py b/scripts/pandoc.py deleted file mode 100755 index f21e9cc83..000000000 --- a/scripts/pandoc.py +++ /dev/null @@ -1,75 +0,0 @@ -# Author: John MacFarlane <jgm@berkeley.edu> -# Copyright: (C) 2013 John MacFarlane -# License: GPL version 2 or higher - -""" -Functions to aid writing python scripts that process the pandoc -AST serialized as JSON. -""" - -import sys -import json - -def walk(x, action, format = ""): - """Walk a tree, applying an action to every object. - Returns a modified tree. - """ - if isinstance(x, list): - array = [] - for item in x: - if isinstance(item, dict): - if item == {}: - array.append(walk(item, action, format)) - else: - 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): - """Converts an action into a filter that reads a JSON-formatted - pandoc document from stdin, transforms it by walking the tree - with the action, and returns a new JSON-formatted pandoc document - to stdout. The argument is a function action(key, value, format), - where key is the type of the pandoc object (e.g. 'Str', 'Para'), - value is the contents of the object (e.g. a string for 'Str', - a list of inline elements for 'Para'), and format is the target - output format (which will be taken for the first command line - argument if present). If the function returns None, the object - to which it applies will remain unchanged. If it returns an - object, the object will be replaced. If it returns a list, the - list will be spliced in to the list to which the target object - belongs. (So, returning an empty list deletes the object.) - """ - 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 attributes(attrs): - """Returns an attribute list, constructed from the - dictionary 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] diff --git a/scripts/tikz.py b/scripts/tikz.py deleted file mode 100755 index 4ff8b2383..000000000 --- a/scripts/tikz.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python - -""" -Pandoc filter to process raw latex tikz environments into images. -Assumes that pdflatex is in the path, and that the standalone -package is available. Also assumes that ImageMagick's convert -is in the path. Images are put in the tikz-images directory. -""" - -import hashlib -import re -import os -import sys -import shutil -from pandoc import toJSONFilter -from subprocess import Popen, PIPE, call -from tempfile import mkdtemp - -imagedir = "tikz-images" - -def sha1(x): - return hashlib.sha1(x).hexdigest() - -def tikz2image(tikz, filetype, outfile): - tmpdir = mkdtemp() - olddir = os.getcwd() - os.chdir(tmpdir) - f = open('tikz.tex', 'w') - f.write("""\\documentclass{standalone} - \\usepackage{tikz} - \\begin{document} - """) - f.write(tikz) - f.write("\n\\end{document}\n") - f.close() - p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr) - os.chdir(olddir) - if filetype == 'pdf': - shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf') - else: - call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype]) - shutil.rmtree(tmpdir) - -def tikz(key, value, format): - if key == 'RawBlock': - [fmt, code] = value - if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code): - outfile = imagedir + '/' + sha1(code) - if format == "html": - filetype = "png" - elif format == "latex": - filetype = "pdf" - else: - filetype = "png" - src = outfile + '.' + filetype - if not os.path.isfile(src): - try: - os.mkdir(imagedir) - sys.stderr.write('Created directory ' + imagedir + '\n') - except OSError: - pass - tikz2image(code, filetype, outfile) - sys.stderr.write('Created image ' + src + '\n') - return {'Para': [{'Image': [[], [src,""]]}]} - -if __name__ == "__main__": - toJSONFilter(tikz)