-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrm_tag_in_pre.py
More file actions
35 lines (30 loc) · 912 Bytes
/
rm_tag_in_pre.py
File metadata and controls
35 lines (30 loc) · 912 Bytes
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
from pyquery import PyQuery as pq
import sys
import os
from os import path
import re
def rep_func(m):
code = m.group(1)
code = re.sub(r'<[^>]*?>', '', code)
code = re.sub(r'(\r?\n)+', r'\1', code)
return f'<pre>{code}</pre>'
def process_file(fname):
print(fname)
if not fname.endswith('.html'):
return
html = open(fname, encoding='utf-8').read()
html = html.replace('<?xml version="1.0" encoding="utf-8"?>', '')
html = re.sub(r'<pre[^>]*>([\s\S]+?)</pre>', rep_func, html)
open(fname, 'w', encoding='utf-8').write(html)
def process_dir(dname):
fnames = os.listdir(dname)
for f in fnames:
f = path.join(dname, f)
process_file(f)
def main():
fname = sys.argv[1]
if path.isfile(fname):
process_file(fname)
else:
process_dir(fname)
if __name__ == '__main__': main()