| 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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 | 286 |
| 287 # Check conditional elements, second pass. This catches conditionals in any | 287 # Check conditional elements, second pass. This catches conditionals in any |
| 288 # of the text we just inlined. | 288 # of the text we just inlined. |
| 289 flat_text = CheckConditionalElements(flat_text) | 289 flat_text = CheckConditionalElements(flat_text) |
| 290 | 290 |
| 291 # Allow custom modifications before inlining images. | 291 # Allow custom modifications before inlining images. |
| 292 if rewrite_function: | 292 if rewrite_function: |
| 293 flat_text = rewrite_function(input_filepath, flat_text, distribution) | 293 flat_text = rewrite_function(input_filepath, flat_text, distribution) |
| 294 | 294 |
| 295 flat_text = re.sub( | 295 flat_text = re.sub( |
| 296 '<(?!script)[^>]+?src=(?P<quote>")(?P<filename>[^"\']*)\\1', | 296 '<(?!script)\s(?:[^>]+?\s)?src=(?P<quote>")(?P<filename>[^"\']*)\\1', |
| 297 SrcReplace, flat_text) | 297 SrcReplace, flat_text) |
| 298 | 298 |
| 299 # TODO(arv): Only do this inside <style> tags. | 299 # TODO(arv): Only do this inside <style> tags. |
| 300 flat_text = InlineCSSImages(flat_text) | 300 flat_text = InlineCSSImages(flat_text) |
| 301 | 301 |
| 302 flat_text = re.sub( | 302 flat_text = re.sub( |
| 303 '<link rel="icon".+?href=(?P<quote>")(?P<filename>[^"\']*)\\1', | 303 '<link rel="icon"\s(?:[^>]+?\s)?href=(?P<quote>")(?P<filename>[^"\']*)\\1'
, |
| 304 SrcReplace, flat_text) | 304 SrcReplace, flat_text) |
| 305 | 305 |
| 306 if names_only: | 306 if names_only: |
| 307 flat_text = None # Will contains garbage if the flag is set anyway. | 307 flat_text = None # Will contains garbage if the flag is set anyway. |
| 308 return InlinedData(flat_text, inlined_files) | 308 return InlinedData(flat_text, inlined_files) |
| 309 | 309 |
| 310 | 310 |
| 311 def InlineToString(input_filename, grd_node, allow_external_script=False, | 311 def InlineToString(input_filename, grd_node, allow_external_script=False, |
| 312 rewrite_function=None): | 312 rewrite_function=None): |
| 313 """Inlines the resources in a specified file and returns it as a string. | 313 """Inlines the resources in a specified file and returns it as a string. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 364 |
| 365 def main(): | 365 def main(): |
| 366 if len(sys.argv) <= 2: | 366 if len(sys.argv) <= 2: |
| 367 print "Flattens a HTML file by inlining its external resources.\n" | 367 print "Flattens a HTML file by inlining its external resources.\n" |
| 368 print "html_inline.py inputfile outputfile" | 368 print "html_inline.py inputfile outputfile" |
| 369 else: | 369 else: |
| 370 InlineToFile(sys.argv[1], sys.argv[2], None) | 370 InlineToFile(sys.argv[1], sys.argv[2], None) |
| 371 | 371 |
| 372 if __name__ == '__main__': | 372 if __name__ == '__main__': |
| 373 main() | 373 main() |
| OLD | NEW |