File: extract-readme-snippets.py

package info (click to toggle)
python-pbcommand 2.1.1%2Bgit20220616.3f2e6c2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 940 kB
  • sloc: python: 6,929; makefile: 220; sh: 71
file content (27 lines) | stat: -rw-r--r-- 639 bytes parent folder | download | duplicates (3)
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)