Repository navigation
Expand file tree
/
Copy pathmarkdown_generator_dev.py
More file actions
254 lines (197 loc) · 7.16 KB
/
Copy pathmarkdown_generator_dev.py
File metadata and controls
254 lines (197 loc) · 7.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 17:40:04 2017
@author: bruyang
"""
#import argparse
import re
#import os
import utils
class Parser: #Parse each setting and rule to each block
def __init__(self):
self.rules = []
self.settings = []
def AddRule(self,Rule):
self.rules.append(Rule)
def AddSetting(self,Setting):
self.settings.append(Setting)
class SettingParser(Parser):
def ParseSetting(self,blocks):
#for block in blocks:
#match content with re module.
pass
class TextParser(Parser):
def __init__(self,handler):
Parser.__init__(self)
self.handler = handler
def ParseText(self,blocks):
for rule in self.rules:
blocks = rule.apply(blocks)
for setting in self.settings:
setting.apply()
return blocks
def Gen_file(self,blocks):
for rule in self.rules:
blocks = rule.apply(blocks)
return blocks
class Rules: #Rules applied to each block of text.
def __init__(self,handler,blocks):
self.type = ''
self.handler = handler
self.condition = ['quote','url','br','head','code']
self.blocks = blocks
def apply_sub(self,condition):
for block in self.blocks:
self.handler.sub(condition,block)
def add_condition(self,cond):
self.condition.append(cond)
def apply(self,blocks):
if self.type not in self.condition:
new_start = self.handler.start(self.type,blocks[self.start_num])
new_end = self.handler.end(self.type,blocks[self.end_num])
utils.replace_block(new_start,blocks[self.start_num],blocks)
if self.type != 'title':
utils.replace_block(new_end,blocks[self.end_num],blocks)
else:
for block in blocks:
new_block = self.handler.sub(self.type,block)
utils.replace_block(new_block,block,blocks)
return blocks
class file_rules(Rules):#apply each handler to each corresponding blocks.Rule is a detector.
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'file'
self.start_num = 0 #using block itself instead of the number will induce to error when the orginal block is changed.
self.end_num = -1
class title_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.start_num = self.title_match(blocks)
self.end_num = self.title_match(blocks)
self.type = 'title'
def title_match(self,blocks):
for block in blocks:
if re.match('t|Tiltle:',block):
return blocks.index(block)
class body_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'body'
self.condition = []
self.start_num = self.body_match(self.blocks)
self.end_num = -1
def body_match(self,blocks):
for block in blocks:
while not re.match('t|Title:',block):
return blocks.index(block)
class url_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'url'
class br_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'br'
class head_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'head'
class code_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'code'
class quote_rules(Rules):
def __init__(self,handler,blocks):
Rules.__init__(self,handler,blocks)
self.type = 'quote'
class Handler(): #how to handle each section.
def callfuction(self,preflix,name,*args):
method = getattr(self,preflix+name,None)
if callable(method):return method(*args)
def start(self,name,block):
return self.callfuction('start_',name,block)
def end(self,name,block):
return self.callfuction('end_',name,block)
def sub(self,name,block):
return self.callfuction('sub_',name,block)
class HTML_handler(Handler):
def start_file(self,block):
return '<html>'+block
def end_file(self,block):
return block + '</html>'
def start_title(self,block):
return '<head><title>' +block +'</title></head>'
def end_title(self,block):
pass
def start_body(self,block):
return '<body>'+block
def end_body(self,block):
return block + '</body>'
def sub_code(self,block):
pattern = r' |\t'
for item in re.findall(pattern,block):
if item != None:
repl = '<pre>'
repl_1 = '</pre>'
block = block.replace(item,repl) + repl_1
return block
def sub_url(self,block):
pattern = r'www.[0-9a-zA-Z]+.com'
for item in re.findall(pattern,block): #other function to replace findall?
if item != None:
repl = '<a href = "http://%s">%s</a>' %(item,item)
block = block.replace(item,repl)
return block
def sub_br(self,block):
pattern = r'\n'
for item in re.findall(pattern,block):
if item != None:
repl = '<br>'
block = block.replace(item,repl)
return block
def sub_head(self,block):
pattern = '[#]{1,6}'
for item in re.findall(pattern,block):
if item != None:
repl = '<h%s>' %(len(item))
repl_1 = '</h%s>' %(len(item))
block = block.replace(item,repl) + repl_1
return block
def sub_quote(self,block):
pattern = r'>>'
for item in re.findall(pattern,block):
if item != None:
#if '<blockquote>' not in block:
repl = '<blockquote>'
#else:
# repl = ''
repl_1 = '</blockquote>'
block = block.replace(item,repl)
return block
'''
def HTML_sort(in_list):
out_list = []
if 'body' in in_list:
in_list.pop('body')
out_list.append('body')
'''
def main():
#file_path = CommandParser()
file_path = "./test.txt"
g=utils.FileParser(file_path)
handler = HTML_handler()
parser = TextParser(handler)
candi = ['quote','code','head','br','url','title','body','file']
#candi = ['url','file']
for typename in candi:
new_rule = eval(typename+'_rules')
#if issubclass(new_rule,Rules):
if callable(new_rule):
rule = new_rule(handler,g)
parser.AddRule(rule)
g = parser.Gen_file(g)
print g
utils.Create_file(file_path,g)
#print g
if __name__ == '__main__':
main()