Removed scripts directory.

This has been put in its own github repo:
https://github.com/jgm/pandoc-filters-python
This commit is contained in:
John MacFarlane 2013-08-18 15:36:54 -07:00
parent 8d441af3da
commit 70386a6a54
10 changed files with 0 additions and 347 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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]

View file

@ -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)