-
Notifications
You must be signed in to change notification settings - Fork 29
/
make-md
executable file
·132 lines (101 loc) · 3.12 KB
/
make-md
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/python3
#
# generating markdown from my anotated markdown
#
import re
import os
import sys
import optparse
import subprocess
from collections import defaultdict
debug = False
# collect key binding for index or later use
def key_binding(md, opt):
# collect them later
return "`" + opt + "`"
def var_binding(md, opt):
# collect them later
return "`" + opt + "`"
def link_binding(md, opt):
# collect them later
return "`" + opt + "`"
def menu_binding(md, opt):
# collect them later
return "`" + opt + "`"
def func_binding(md, opt):
# collect them later
return "`" + opt + "`"
def todo_binding(md, opt):
# collect them later
return ""
def snap_binding(md, opt):
out = re.findall("-o[ ]+([^ ]+)", opt)
if out:
# append image path
return "../img/%s" % out[0]
print("error on parsing snapshot commands")
return ""
def num_binding(md, opt):
if not hasattr(num_binding, "num"):
num_binding.num = defaultdict(lambda:defaultdict(int))
num_binding.num[md][opt] += 1
if opt == "img":
return "그림-%d." % num_binding.num[md][opt]
print("error on parsing snapshot commands")
return ""
def sh(md, opt):
def __in_cwd(d, cmd):
old = os.getcwd()
os.chdir(d)
rtn = subprocess.check_output(cmd)
os.chdir(old)
return str(rtn, "utf8", "ignore")
(d, _) = os.path.split(md)
return __in_cwd(d, opt.split())
def preprocess(md, marks, src, dumps=[]):
dst = []
# for each keywords
for m in re.finditer("\\\\(.?){([^}]+)}", src):
key, opt = m.groups()
opt = opt.replace("\n", " ")
if key not in marks:
print("Can't find %s in %s: %s", key, marks.keys(), opt)
exit(1)
if key in dumps:
print(opt)
if debug:
print(m.span(), key, opt)
dst.append((m.span(), marks[key](md, opt)))
# sort inverse
for ((beg, end), new) \
in sorted(dst, key=lambda d:d[0][0], reverse=True):
# substitute
src = src[:beg] + new + src[end:]
return src
if __name__ == "__main__":
parser = optparse.OptionParser(__doc__.strip() if __doc__ else "")
parser.add_option("-s", "--snap",
help="list up snap", action="store_true",
dest="snap", default=False)
parser.add_option("-t", "--todo",
help="list up todo", action="store_true",
dest="todo", default=False)
(opts, args) = parser.parse_args()
# grep todo
if opts.todo:
os.system("grep -Hrn --color=always TODO %s" % " ".join(args))
exit(0)
dumps = []
if opts.snap:
dumps.append("s")
# create marks
marks = {k[0]:v for k, v in globals().items() \
if k.endswith("binding")}
marks["!"] = sh
# create markdown files
for f in args:
ifn = f
ofn = ifn.replace(".md", ".markdown")
with open(ofn, "w") as ofd:
with open(ifn) as ifd:
ofd.write(preprocess(ifn, marks, ifd.read(), dumps))