-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmarkdown_converter.py
executable file
·151 lines (112 loc) · 3.41 KB
/
markdown_converter.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import re, os, sys
from optparse import OptionParser
def convertText(c):
# replace all links
# c = re.sub(r'\[([^\]]+)\]\(([^\]]+)\)', r'[\1|\2]', c)
c = re.sub(r'\[(.*?)\]\((.*?)\)', r'[\1|\2]', c)
# replace bold temporarily
c = re.sub(r'\*\*(.*?)\*\*', r'bdirkb\1bdirkb', c)
# replace italics
c = re.sub(r'\*(.*?)\*', r'_\1_', c)
# replace bold
c = re.sub(r'bdirkb(.*?)bdirkb', r'*\1*', c)
# replace inline code
c = re.sub(r'`(.*?)`', r'*\1*', c)
# print c
c = c.split('\n')
words = []
words.append( ['#','h1.'] )
words.append( ['##','h2.'] )
words.append( ['###','h3.'] )
words.append( ['####','h4.'] )
words.append( ['#####','h5.'] )
words.append( ['######','h6.'] )
newContent = []
i = 0
isCode = 0
indent = 0
isQuote = 0
isList = 0
for l in c:
i += 1
if 0 == 0:
# print l[:30]
k = l
if l[0:1]=='*':
isList = 1
if l == '':
isList = 0
if l[0:1] == '>':
if isQuote==0:
k = '{quote}\n'+k[1:]
isQuote = 1
else:
k = k[1:]
if isList == 0:
if isCode == 1:
if l[0:1] == ' ' or l[0:1]=='\t':
k = k[indent:]
else:
k = '{code}\n'+k
isCode = 0
indent = -1
else:
if l[0:1]==' ' or l[0:1]=='\t':
indent = len(k)-len(k.lstrip())
k = '{code}\n'+k[indent:]
isCode = 1
else:
if l[0:4]=='\t\t\t*':
k = '****' + l[4:]
if l[0:3]=='\t\t*':
k = '***' + l[3:]
if l[0:2]=='\t*':
k = '**' + l[2:]
for w in words:
# print l[:len(w[0])]
if l[:len(w[0])] == w[0]:
k = w[1]+l[len(w[0]):]
if l[0:1] != '>' and isQuote == 1:
k = '{quote}\n'+k
isQuote = 0
if l[0:3] != '| -':
newContent.append(k)
# print k
i# newContent.append(k)
return '\n'.join(newContent)
def convertFile(filename,output_dir):
old_content = open(filename, 'r').read()
new_content = convertText(old_content)
if output_dir != None:
open('%s/%s.txt' % (output_dir,filename),'w').write(new_content)
else:
open(filename+'.txt','w').write(new_content)
print "converted",filename
def convertAllFiles(dirname,output_dir):
f_counter = 0
for f in os.listdir(dirname):
fname = os.path.join(dirname,f)
if fname[-3:] =='.md':
f_counter += 1
convertFile(fname,output_dir)
def init_options():
usage="%prog [options] input_files.txt"
parser = OptionParser(usage)
parser.add_option("--output_dir",dest="output_dir",help="Output all files to a specified directory")
return parser.parse_args()
if __name__=='__main__':
(options,args) = init_options()
if options.output_dir != None and not os.path.exists(options.output_dir):
os.mkdir(options.output_dir)
if len(args) == 0:
convertAllFiles(".",options.output_dir)
sys.exit()
infile = args[0]
if not os.path.exists(infile):
sys.stderr("%s does not exist!" %infile)
sys.exit(15)
if os.path.isdir(infile):
convertAllFiles(infile,options.output_dir)
else:
convertFile(infile,options.output_dir)