Debian Codegraph @

codegen/genEscape.py

raw
1 #!/usr/bin/env python3
2
3 entities = [
4 [ '', '�' ],
5 [ '\t', '	' ],
6 [ '\n', '
' ],
7 [ '\r', '
' ],
8 [ '"', '"' ],
9 [ '&', '&' ],
10 [ '<', '&lt;' ],
11 [ '>', '&gt;' ],
12 ]
13
14 offset = [ None ] * 128
15
16 def gen_content(out):
17 pos = 0
18 r = ''
19
20 for rec in entities:
21 char, repl = rec
22
23 if char:
24 offset[ord(char)] = pos
25
26 if pos % 12 == 0: r += '\n '
27 else: r += ' '
28 r += '%3d,' % len(repl)
29 pos += 1
30
31 for c in repl:
32 if pos % 12 == 0: r += '\n '
33 else: r += ' '
34 r += "'%s'," % c
35 pos += 1
36
37 out.write('static const char xmlEscapeContent[] = {%s\n};\n\n' % r)
38
39 def gen_tab(out, name, escape, is_xml):
40 r = ''
41
42 for i in range(0x80):
43
44 if chr(i) in escape:
45 v = offset[i]
46 elif i == 0:
47 v = 0
48 elif is_xml and i < 32 and i != 9 and i != 10:
49 v = 0
50 else:
51 v = -1
52
53 if i % 16 == 0: r += '\n '
54 else: r += ' '
55 r += '%2d,' % v
56
57 out.write('static const signed char %s[128] = {%s\n};\n\n' % (name, r))
58
59 with open('codegen/escape.inc', 'w') as out:
60 gen_content(out)
61
62 gen_tab(out, 'xmlEscapeTab', '\r&<>', True)
63 gen_tab(out, 'xmlEscapeTabQuot', '\r"&<>', True)
64 gen_tab(out, 'xmlEscapeTabAttr', '\t\n\r"&<>', True)
65
66 out.write('#ifdef LIBXML_HTML_ENABLED\n\n')
67 gen_tab(out, 'htmlEscapeTab', '&<>', False)
68 gen_tab(out, 'htmlEscapeTabAttr', '"&<>', False)
69 out.write('#endif /* LIBXML_HTML_ENABLED */\n')