-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-code.py
More file actions
executable file
·34 lines (28 loc) · 1.16 KB
/
extract-code.py
File metadata and controls
executable file
·34 lines (28 loc) · 1.16 KB
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
28
29
30
31
32
33
34
#!/usr/bin/env python3
##############################################################################
# Script to extract code blocks from markdown
##############################################################################
# Requires pip package: mdextractor
import argparse
from mdextractor import extract_md_blocks
parser = argparse.ArgumentParser(
description='Extract code blocks from markdown',
epilog='')
parser.add_argument('filename')
parser.add_argument('-o', '--output_file', default=None, help="output filename")
parser.add_argument('-b', '--bash_header', default=False, action=argparse.BooleanOptionalAction, help="add bash header")
args = parser.parse_args()
with open(args.filename, "r") as fin:
md = ''.join(fin.readlines())
code = '\n'.join(extract_md_blocks(md))
if(args.output_file==None):
print(code)
else:
with open(args.output_file, "w") as fout:
if(args.bash_header):
fout.write("#!/usr/bin/bash\n")
fout.write("# exit script on first error:\n")
fout.write("set -e\n")
fout.write("# show commands executed\n")
fout.write("set -o xtrace\n")
fout.write(code)