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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 filepath = os.path.join(base_path, filename) | 75 filepath = os.path.join(base_path, filename) |
76 inlined_files.add(filepath) | 76 inlined_files.add(filepath) |
77 | 77 |
78 if names_only: | 78 if names_only: |
79 return "" | 79 return "" |
80 | 80 |
81 mimetype = mimetypes.guess_type(filename)[0] or 'text/plain' | 81 mimetype = mimetypes.guess_type(filename)[0] or 'text/plain' |
82 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) | 82 inline_data = base64.standard_b64encode(util.ReadFile(filepath, util.BINARY)) |
83 | 83 |
84 prefix = src_match.string[src_match.start():src_match.start('filename')-1] | 84 prefix = src_match.string[src_match.start():src_match.start('filename')-1] |
85 return "%s\"data:%s;base64,%s\"" % (prefix, mimetype, inline_data) | 85 return "%s'data:%s;base64,%s'" % (prefix, mimetype, inline_data) |
86 | 86 |
87 | 87 |
88 class InlinedData: | 88 class InlinedData: |
89 """Helper class holding the results from DoInline(). | 89 """Helper class holding the results from DoInline(). |
90 | 90 |
91 Holds the inlined data and the set of filenames of all the inlined | 91 Holds the inlined data and the set of filenames of all the inlined |
92 files. | 92 files. |
93 """ | 93 """ |
94 def __init__(self, inlined_data, inlined_files): | 94 def __init__(self, inlined_data, inlined_files): |
95 self.inlined_data = inlined_data | 95 self.inlined_data = inlined_data |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 | 353 |
354 def main(): | 354 def main(): |
355 if len(sys.argv) <= 2: | 355 if len(sys.argv) <= 2: |
356 print "Flattens a HTML file by inlining its external resources.\n" | 356 print "Flattens a HTML file by inlining its external resources.\n" |
357 print "html_inline.py inputfile outputfile" | 357 print "html_inline.py inputfile outputfile" |
358 else: | 358 else: |
359 InlineToFile(sys.argv[1], sys.argv[2], None) | 359 InlineToFile(sys.argv[1], sys.argv[2], None) |
360 | 360 |
361 if __name__ == '__main__': | 361 if __name__ == '__main__': |
362 main() | 362 main() |
OLD | NEW |