OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
4 import sys, os, codecs, re, json, argparse | 4 import sys, os, codecs, re, json, argparse |
5 import xml.dom.minidom as minidom | 5 import xml.dom.minidom as minidom |
6 baseDir = os.path.abspath(os.path.dirname(__file__)) | 6 baseDir = os.path.abspath(os.path.dirname(__file__)) |
7 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) | 7 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) |
8 from abp_rewrite import doRewrite | 8 from abp_rewrite import doRewrite |
9 | 9 |
10 def toCString(string): | 10 class CStringArray: |
11 string = string.replace('\\', '\\\\').replace('"', '\\"') | 11 def __init__(self): |
12 string = string.replace('\r', '').replace('\n', '\\n') | 12 self._buffer = [] |
13 # Work around MSVC line length limitation | 13 self._strings = [] |
14 #(see http://msdn.microsoft.com/en-us/library/dddywwsc(v=vs.80).aspx) | |
15 string = re.sub(r'((?:[^\\]|\\.){16000})(.)', r'\1"\n"\2', string, re.S) | |
16 return '"%s"' % string.encode('utf-8') | |
17 | 14 |
18 def printFilesVerbatim(outHandle, files): | 15 def add(self, string): |
| 16 string = string.encode('utf-8').replace('\r', '') |
| 17 self._strings.append('std::string(buffer + %i, %i)' % (len(self._buffer), le
n(string))) |
| 18 self._buffer.extend(map(lambda c: str(ord(c)), string)) |
| 19 |
| 20 def write(self, outHandle, arrayName): |
| 21 print >>outHandle, '#include <string>' |
| 22 print >>outHandle, 'namespace {' |
| 23 print >>outHandle, ' const char buffer[] = {%s};' % ', '.join(self._buffer) |
| 24 print >>outHandle, '}' |
| 25 print >>outHandle, 'std::string %s[] = {%s, std::string()};' % (arrayName, '
, '.join(self._strings)) |
| 26 |
| 27 def addFilesVerbatim(array, files): |
19 for file in files: | 28 for file in files: |
20 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 29 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
21 print >>outHandle, toCString(os.path.basename(file)) + ',' | 30 array.add(os.path.basename(file)) |
22 print >>outHandle, toCString(fileHandle.read()) + ',' | 31 array.add(fileHandle.read()) |
23 fileHandle.close() | 32 fileHandle.close() |
24 | 33 |
25 def convertXMLFile(outHandle, file): | 34 def convertXMLFile(array, file): |
26 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 35 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
27 doc = minidom.parse(file) | 36 doc = minidom.parse(file) |
28 fileHandle.close() | 37 fileHandle.close() |
29 | 38 |
30 data = [] | 39 data = [] |
31 for node in doc.documentElement.childNodes: | 40 for node in doc.documentElement.childNodes: |
32 if node.nodeType != node.ELEMENT_NODE: | 41 if node.nodeType != node.ELEMENT_NODE: |
33 continue | 42 continue |
34 result = {'type': node.tagName} | 43 result = {'type': node.tagName} |
35 for name, value in node.attributes.items(): | 44 for name, value in node.attributes.items(): |
36 result[name] = value | 45 result[name] = value |
37 data.append(result) | 46 data.append(result) |
38 fileNameString = toCString(os.path.basename(file)) | 47 fileName = os.path.basename(file) |
39 print >>outHandle, fileNameString + ',' | 48 array.add(fileName) |
40 print >>outHandle, toCString('require.scopes[%s] = %s;' % (fileNameString, j
son.dumps(data))) + ',' | 49 array.add('require.scopes["%s"] = %s;' % (fileName, json.dumps(data))) |
41 fileHandle.close() | 50 fileHandle.close() |
42 | 51 |
43 def convertJsFile(outHandle, file): | 52 def convertJsFile(array, file): |
44 converted = doRewrite([os.path.abspath(file)], ['module=true', 'source_repo=ht
tps://hg.adblockplus.org/adblockplus/']) | 53 converted = doRewrite([os.path.abspath(file)], ['module=true', 'source_repo=ht
tps://hg.adblockplus.org/adblockplus/']) |
45 print >>outHandle, toCString(os.path.basename(file)) + ',' | 54 array.add(os.path.basename(file)) |
46 print >>outHandle, toCString(converted) + ',' | 55 array.add(converted) |
47 | 56 |
48 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 57 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
49 outHandle = open(outFile, 'wb') | 58 array = CStringArray() |
50 print >>outHandle, 'const char* jsSources[] = {' | 59 addFilesVerbatim(array, verbatimBefore) |
51 | |
52 printFilesVerbatim(outHandle, verbatimBefore) | |
53 | 60 |
54 for file in convertFiles: | 61 for file in convertFiles: |
55 if file.endswith('.xml'): | 62 if file.endswith('.xml'): |
56 convertXMLFile(outHandle, file) | 63 convertXMLFile(array, file) |
57 else: | 64 else: |
58 convertJsFile(outHandle, file) | 65 convertJsFile(array, file) |
59 | 66 |
60 printFilesVerbatim(outHandle, verbatimAfter) | 67 addFilesVerbatim(array, verbatimAfter) |
61 | 68 |
62 print >>outHandle, '0, 0' | 69 outHandle = open(outFile, 'wb') |
63 print >>outHandle, '};' | 70 array.write(outHandle, 'jsSources') |
| 71 outHandle.close() |
64 | 72 |
65 if __name__ == '__main__': | 73 if __name__ == '__main__': |
66 parser = argparse.ArgumentParser(description='Convert JavaScript files') | 74 parser = argparse.ArgumentParser(description='Convert JavaScript files') |
67 parser.add_argument('--before', metavar='verbatim_file', nargs='+', | 75 parser.add_argument('--before', metavar='verbatim_file', nargs='+', |
68 help='JavaScript file to include verbatim at the beginning') | 76 help='JavaScript file to include verbatim at the beginning') |
69 parser.add_argument('--convert', metavar='file_to_convert', nargs='+', | 77 parser.add_argument('--convert', metavar='file_to_convert', nargs='+', |
70 help='JavaScript files to convert') | 78 help='JavaScript files to convert') |
71 parser.add_argument('--after', metavar='verbatim_file', nargs='+', | 79 parser.add_argument('--after', metavar='verbatim_file', nargs='+', |
72 help='JavaScript file to include verbatim at the end') | 80 help='JavaScript file to include verbatim at the end') |
73 parser.add_argument('output_file', | 81 parser.add_argument('output_file', |
74 help='output from the conversion') | 82 help='output from the conversion') |
75 args = parser.parse_args() | 83 args = parser.parse_args() |
76 convert(args.before, args.convert, args.after, args.output_file) | 84 convert(args.before, args.convert, args.after, args.output_file) |
OLD | NEW |