| 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. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 _SRC_RE = lazy_re.compile( | 40 _SRC_RE = lazy_re.compile( |
| 41 r'<(?!script)(?:[^>]+?\s)src=(?P<quote>")(?P<filename>[^"\']*)\1', | 41 r'<(?!script)(?:[^>]+?\s)src=(?P<quote>")(?P<filename>[^"\']*)\1', |
| 42 re.MULTILINE) | 42 re.MULTILINE) |
| 43 _ICON_RE = lazy_re.compile( | 43 _ICON_RE = lazy_re.compile( |
| 44 r'<link rel="icon"\s(?:[^>]+?\s)?' | 44 r'<link rel="icon"\s(?:[^>]+?\s)?' |
| 45 'href=(?P<quote>")(?P<filename>[^"\']*)\1', | 45 'href=(?P<quote>")(?P<filename>[^"\']*)\1', |
| 46 re.MULTILINE) | 46 re.MULTILINE) |
| 47 | 47 |
| 48 | 48 |
| 49 | 49 |
| 50 def FixupMimeType(mime_type): |
| 51 """Helper function that normalizes platform differences in the mime type |
| 52 returned by the Python's mimetypes.guess_type API. |
| 53 """ |
| 54 mappings = { |
| 55 'image/x-png': 'image/png' |
| 56 } |
| 57 return mappings[mime_type] if mime_type in mappings else mime_type |
| 58 |
| 59 |
| 50 def GetDistribution(): | 60 def GetDistribution(): |
| 51 """Helper function that gets the distribution we are building. | 61 """Helper function that gets the distribution we are building. |
| 52 | 62 |
| 53 Returns: | 63 Returns: |
| 54 string | 64 string |
| 55 """ | 65 """ |
| 56 distribution = DIST_DEFAULT | 66 distribution = DIST_DEFAULT |
| 57 if DIST_ENV_VAR in os.environ.keys(): | 67 if DIST_ENV_VAR in os.environ.keys(): |
| 58 distribution = os.environ[DIST_ENV_VAR] | 68 distribution = os.environ[DIST_ENV_VAR] |
| 59 if len(distribution) > 1 and distribution[0] == '_': | 69 if len(distribution) > 1 and distribution[0] == '_': |
| (...skipping 29 matching lines...) Expand all Loading... |
| 89 # filename is probably a URL, which we don't want to bother inlining | 99 # filename is probably a URL, which we don't want to bother inlining |
| 90 return src_match.group(0) | 100 return src_match.group(0) |
| 91 | 101 |
| 92 filename = filename.replace(DIST_SUBSTR , distribution) | 102 filename = filename.replace(DIST_SUBSTR , distribution) |
| 93 filepath = os.path.normpath(os.path.join(base_path, filename)) | 103 filepath = os.path.normpath(os.path.join(base_path, filename)) |
| 94 inlined_files.add(filepath) | 104 inlined_files.add(filepath) |
| 95 | 105 |
| 96 if names_only: | 106 if names_only: |
| 97 return "" | 107 return "" |
| 98 | 108 |
| 99 mimetype = mimetypes.guess_type(filename)[0] or 'text/plain' | 109 mimetype = FixupMimeType(mimetypes.guess_type(filename)[0]) or 'text/plain' |
| 100 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) | 110 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) |
| 101 | 111 |
| 102 prefix = src_match.string[src_match.start():src_match.start('filename')] | 112 prefix = src_match.string[src_match.start():src_match.start('filename')] |
| 103 suffix = src_match.string[src_match.end('filename'):src_match.end()] | 113 suffix = src_match.string[src_match.end('filename'):src_match.end()] |
| 104 return '%sdata:%s;base64,%s%s' % (prefix, mimetype, inline_data, suffix) | 114 return '%sdata:%s;base64,%s%s' % (prefix, mimetype, inline_data, suffix) |
| 105 | 115 |
| 106 | 116 |
| 107 class InlinedData: | 117 class InlinedData: |
| 108 """Helper class holding the results from DoInline(). | 118 """Helper class holding the results from DoInline(). |
| 109 | 119 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 | 393 |
| 384 def main(): | 394 def main(): |
| 385 if len(sys.argv) <= 2: | 395 if len(sys.argv) <= 2: |
| 386 print "Flattens a HTML file by inlining its external resources.\n" | 396 print "Flattens a HTML file by inlining its external resources.\n" |
| 387 print "html_inline.py inputfile outputfile" | 397 print "html_inline.py inputfile outputfile" |
| 388 else: | 398 else: |
| 389 InlineToFile(sys.argv[1], sys.argv[2], None) | 399 InlineToFile(sys.argv[1], sys.argv[2], None) |
| 390 | 400 |
| 391 if __name__ == '__main__': | 401 if __name__ == '__main__': |
| 392 main() | 402 main() |
| OLD | NEW |