1#!/usr/bin/env python
2# Copyright (C) 2014 Apple Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1.  Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9# 2.  Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24import io
25import os
26from optparse import OptionParser
27from StringIO import StringIO
28from jsmin import JavascriptMinify
29
30
31def stringifyCodepoint(code):
32    if code < 128:
33        return '{0:d}'.format(code)
34    else:
35        return "'\\x{0:02x}'".format(code)
36
37
38def chunk(list, chunkSize):
39    for i in xrange(0, len(list), chunkSize):
40        yield list[i:i + chunkSize]
41
42
43def main():
44    parser = OptionParser(usage="usage: %prog [--no-minify] header source [input [input...]]")
45    parser.add_option('--no-minify', action='store_true', help='Do not run the input files through jsmin')
46    (options, arguments) = parser.parse_args()
47    if len(arguments) < 3:
48        print 'Error: must provide at least 3 arguments'
49        parser.print_usage()
50        exit(-1)
51
52    headerPath = arguments[0]
53    sourcePath = arguments[1]
54    inputPaths = arguments[2:]
55
56    headerFile = open(headerPath, 'w')
57    print >> headerFile, 'namespace WebCore {'
58
59    sourceFile = open(sourcePath, 'w')
60    print >> sourceFile, '#include "{0:s}"'.format(os.path.basename(headerPath))
61    print >> sourceFile, 'namespace WebCore {'
62
63    jsm = JavascriptMinify()
64
65    for inputFileName in inputPaths:
66        inputStream = io.FileIO(inputFileName)
67        outputStream = StringIO()
68
69        if not options.no_minify:
70            jsm.minify(inputStream, outputStream)
71            characters = outputStream.getvalue()
72        else:
73            characters = inputStream.read()
74
75        size = len(characters)
76        variableName = os.path.splitext(os.path.basename(inputFileName))[0]
77
78        print >> headerFile, 'extern const char {0:s}JavaScript[{1:d}];'.format(variableName, size)
79        print >> sourceFile, 'const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size)
80
81        codepoints = map(ord, characters)
82        for codepointChunk in chunk(codepoints, 16):
83            print >> sourceFile, '    {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk)))
84
85        print >> sourceFile, '};'
86
87    print >> headerFile, '}'
88    print >> sourceFile, '}'
89
90if __name__ == '__main__':
91    main()
92