1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#!/usr/bin/python3
"""
Pandoc filter to exact python code blocks and write each snippet out.
"""
from pandocfilters import toJSONFilter, Str
n = 0
def caps(key, value, format, meta):
global n
if key == "CodeBlock":
py_types = value[0][1][0]
if py_types.encode("ascii") == "python":
code_block = value[-1]
# eval(code_block)
with open("readme-snippet-{n}.py".format(n=n), 'a') as f:
f.write("# example {k}-{n}\n".format(k=key, n=n))
f.write("{v}\n".format(v=code_block))
n += 1
if __name__ == "__main__":
toJSONFilter(caps)
|