OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Flattens a HTML file by inlining its external resources. | 6 """Flattens a HTML file by inlining its external resources. |
7 | 7 |
8 This is a small script that takes a HTML file, looks for src attributes | 8 This is a small script that takes a HTML file, looks for src attributes |
9 and inlines the specified file, producing one HTML file with no external | 9 and inlines the specified file, producing one HTML file with no external |
10 dependencies. It recursively inlines the included files. | 10 dependencies. It recursively inlines the included files. |
11 """ | 11 """ |
12 | 12 |
13 import os | 13 import os |
14 import re | 14 import re |
15 import sys | 15 import sys |
16 import base64 | 16 import base64 |
17 import mimetypes | 17 import mimetypes |
18 | 18 |
19 from grit import lazy_re | 19 from grit import lazy_re |
20 from grit import util | 20 from grit import util |
21 | 21 |
22 # There is a python bug that makes mimetypes crash if the Windows | 22 # There is a python bug that makes mimetypes crash if the Windows |
23 # registry contains non-Latin keys ( http://bugs.python.org/issue9291 | 23 # registry contains non-Latin keys ( http://bugs.python.org/issue9291 |
24 # ). Initing manually and blocking external mime-type databases will | 24 # ). Initing manually and blocking external mime-type databases will |
25 # prevent that bug and still give us the data we need. | 25 # prevent that bug and if we add svg manually, it will still give us |
26 # the data we need. | |
26 mimetypes.init([]) | 27 mimetypes.init([]) |
28 mimetypes.add_type('image/svg+xml', '.svg') | |
27 | 29 |
28 DIST_DEFAULT = 'chromium' | 30 DIST_DEFAULT = 'chromium' |
29 DIST_ENV_VAR = 'CHROMIUM_BUILD' | 31 DIST_ENV_VAR = 'CHROMIUM_BUILD' |
30 DIST_SUBSTR = '%DISTRIBUTION%' | 32 DIST_SUBSTR = '%DISTRIBUTION%' |
31 | 33 |
32 # Matches beginning of an "if" block with trailing spaces. | 34 # Matches beginning of an "if" block with trailing spaces. |
33 _BEGIN_IF_BLOCK = lazy_re.compile( | 35 _BEGIN_IF_BLOCK = lazy_re.compile( |
34 '<if [^>]*?expr="(?P<expression>[^"]*)"[^>]*?>\s*') | 36 '<if [^>]*?expr="(?P<expression>[^"]*)"[^>]*?>\s*') |
35 | 37 |
36 # Matches ending of an "if" block with preceding spaces. | 38 # Matches ending of an "if" block with preceding spaces. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 # filename is probably a URL, which we don't want to bother inlining | 110 # filename is probably a URL, which we don't want to bother inlining |
109 return src_match.group(0) | 111 return src_match.group(0) |
110 | 112 |
111 filename = filename.replace(DIST_SUBSTR , distribution) | 113 filename = filename.replace(DIST_SUBSTR , distribution) |
112 filepath = os.path.normpath(os.path.join(base_path, filename)) | 114 filepath = os.path.normpath(os.path.join(base_path, filename)) |
113 inlined_files.add(filepath) | 115 inlined_files.add(filepath) |
114 | 116 |
115 if names_only: | 117 if names_only: |
116 return "" | 118 return "" |
117 | 119 |
118 mimetype = FixupMimeType(mimetypes.guess_type(filename)[0]) or 'text/plain' | 120 platform_mimetype_guess = mimetypes.guess_type(filename)[0] |
121 if platform_mimetype_guess is None: | |
122 raise Exception('%s is of an an unknown type and ' | |
123 'cannot be stored in a data url.' % filename) | |
124 else: | |
125 mimetype = FixupMimeType(platform_mimetype_guess) | |
newt (away)
2014/06/11 18:31:25
looks like FixusMimeType() isn't needed anymore, s
| |
119 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) | 126 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) |
120 | 127 |
121 prefix = src_match.string[src_match.start():src_match.start('filename')] | 128 prefix = src_match.string[src_match.start():src_match.start('filename')] |
122 suffix = src_match.string[src_match.end('filename'):src_match.end()] | 129 suffix = src_match.string[src_match.end('filename'):src_match.end()] |
123 return '%sdata:%s;base64,%s%s' % (prefix, mimetype, inline_data, suffix) | 130 return '%sdata:%s;base64,%s%s' % (prefix, mimetype, inline_data, suffix) |
124 | 131 |
125 | 132 |
126 class InlinedData: | 133 class InlinedData: |
127 """Helper class holding the results from DoInline(). | 134 """Helper class holding the results from DoInline(). |
128 | 135 |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 | 425 |
419 def main(): | 426 def main(): |
420 if len(sys.argv) <= 2: | 427 if len(sys.argv) <= 2: |
421 print "Flattens a HTML file by inlining its external resources.\n" | 428 print "Flattens a HTML file by inlining its external resources.\n" |
422 print "html_inline.py inputfile outputfile" | 429 print "html_inline.py inputfile outputfile" |
423 else: | 430 else: |
424 InlineToFile(sys.argv[1], sys.argv[2], None) | 431 InlineToFile(sys.argv[1], sys.argv[2], None) |
425 | 432 |
426 if __name__ == '__main__': | 433 if __name__ == '__main__': |
427 main() | 434 main() |
OLD | NEW |