-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathunescape-xml-in-code-samples.py
More file actions
executable file
·67 lines (51 loc) · 1.89 KB
/
unescape-xml-in-code-samples.py
File metadata and controls
executable file
·67 lines (51 loc) · 1.89 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
#! /usr/bin/env python3
# Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>
# Licensed under the MIT license
import argparse
import re
def process_file(filename):
with open(filename, 'r') as f:
content = f.read()
chunks = []
prev_end = 0
inside_pre = False
for match in re.finditer(
'(?P<open_pre><pre>)'
'|(?P<close_pre></pre>)'
'|(?P<lt1>&lt;)'
'|(?P<lt2>&</span>lt<span class="p">;)'
'|(?P<lt3>&</span><span class="n">lt</span><span class="p">;)'
'|(?P<gt1>&gt;)'
'|(?P<gt2>&</span>gt<span class="p">;)'
'|(?P<gt3>&</span><span class="n">gt</span><span class="p">;)'
'|(?P<amp1>&amp;)'
'|(?P<amp2>&</span><span class="n">amp</span><span class="p">;)'
, content):
gap = content[prev_end:match.start()]
chunks.append(gap)
if inside_pre:
if match.group('lt1') or match.group('lt2') or match.group('lt3'):
text = '<'
elif match.group('gt1') or match.group('gt2') or match.group('gt3'):
text = '>'
elif match.group('amp1') or match.group('amp2'):
text = '&'
else:
if match.group('close_pre'):
inside_pre = False
text = match.group()
else:
if match.group('open_pre'):
inside_pre = True
text = match.group()
chunks.append(text)
prev_end = match.end()
chunks.append(content[prev_end:])
with open(filename, 'w') as f:
f.write(''.join(chunks))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='+')
config = parser.parse_args()
for filename in config.filenames:
process_file(filename)