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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 """Helper class holding the results from DoInline(). | 121 """Helper class holding the results from DoInline(). |
122 | 122 |
123 Holds the inlined data and the set of filenames of all the inlined | 123 Holds the inlined data and the set of filenames of all the inlined |
124 files. | 124 files. |
125 """ | 125 """ |
126 def __init__(self, inlined_data, inlined_files): | 126 def __init__(self, inlined_data, inlined_files): |
127 self.inlined_data = inlined_data | 127 self.inlined_data = inlined_data |
128 self.inlined_files = inlined_files | 128 self.inlined_files = inlined_files |
129 | 129 |
130 def DoInline( | 130 def DoInline( |
131 input_filename, grd_node, allow_external_script=False, | 131 input_filename, evaluate_condition, allow_external_script=False, |
132 preprocess_only=False, names_only=False, rewrite_function=None, | 132 preprocess_only=False, names_only=False, rewrite_function=None, |
133 filename_expansion_function=None): | 133 filename_expansion_function=None): |
134 """Helper function that inlines the resources in a specified file. | 134 """Helper function that inlines the resources in a specified file. |
135 | 135 |
136 Reads input_filename, finds all the src attributes and attempts to | 136 Reads input_filename, finds all the src attributes and attempts to |
137 inline the files they are referring to, then returns the result and | 137 inline the files they are referring to, then returns the result and |
138 the set of inlined files. | 138 the set of inlined files. |
139 | 139 |
140 Args: | 140 Args: |
141 input_filename: name of file to read in | 141 input_filename: name of file to read in |
142 grd_node: html node from the grd file for this include tag | 142 evaluate_condition: a function to evaluate boolean conditions |
Dan Beam
2016/07/14 17:47:58
this isn't really too helpful of a variable name,
| |
143 preprocess_only: Skip all HTML processing, only handle <if> and <include>. | 143 preprocess_only: Skip all HTML processing, only handle <if> and <include>. |
144 names_only: |nil| will be returned for the inlined contents (faster). | 144 names_only: |nil| will be returned for the inlined contents (faster). |
145 rewrite_function: function(filepath, text, distribution) which will be | 145 rewrite_function: function(filepath, text, distribution) which will be |
146 called to rewrite html content before inlining images. | 146 called to rewrite html content before inlining images. |
147 filename_expansion_function: function(filename) which will be called to | 147 filename_expansion_function: function(filename) which will be called to |
148 rewrite filenames before attempting to read them. | 148 rewrite filenames before attempting to read them. |
149 Returns: | 149 Returns: |
150 a tuple of the inlined data as a string and the set of filenames | 150 a tuple of the inlined data as a string and the set of filenames |
151 of all the inlined files | 151 of all the inlined files |
152 """ | 152 """ |
(...skipping 19 matching lines...) Expand all Loading... | |
172 # filename is probably a URL, which we don't want to bother inlining | 172 # filename is probably a URL, which we don't want to bother inlining |
173 return None | 173 return None |
174 | 174 |
175 filename = filename.replace('%DISTRIBUTION%', distribution) | 175 filename = filename.replace('%DISTRIBUTION%', distribution) |
176 if filename_expansion_function: | 176 if filename_expansion_function: |
177 filename = filename_expansion_function(filename) | 177 filename = filename_expansion_function(filename) |
178 return os.path.normpath(os.path.join(base_path, filename)) | 178 return os.path.normpath(os.path.join(base_path, filename)) |
179 | 179 |
180 def IsConditionSatisfied(src_match): | 180 def IsConditionSatisfied(src_match): |
181 expression = src_match.group('expression') | 181 expression = src_match.group('expression') |
182 return grd_node is None or grd_node.EvaluateCondition(expression) | 182 return evaluate_condition is None or evaluate_condition(expression) |
183 | 183 |
184 def CheckConditionalElements(str): | 184 def CheckConditionalElements(str): |
185 """Helper function to conditionally inline inner elements""" | 185 """Helper function to conditionally inline inner elements""" |
186 while True: | 186 while True: |
187 begin_if = _BEGIN_IF_BLOCK.search(str) | 187 begin_if = _BEGIN_IF_BLOCK.search(str) |
188 if begin_if is None: | 188 if begin_if is None: |
189 if _END_IF_BLOCK.search(str) is not None: | 189 if _END_IF_BLOCK.search(str) is not None: |
190 raise Exception('Unmatched </if>') | 190 raise Exception('Unmatched </if>') |
191 return str | 191 return str |
192 | 192 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 | 229 |
230 if names_only: | 230 if names_only: |
231 inlined_files.update(GetResourceFilenames( | 231 inlined_files.update(GetResourceFilenames( |
232 filepath, | 232 filepath, |
233 allow_external_script, | 233 allow_external_script, |
234 rewrite_function, | 234 rewrite_function, |
235 filename_expansion_function=filename_expansion_function)) | 235 filename_expansion_function=filename_expansion_function)) |
236 return "" | 236 return "" |
237 | 237 |
238 return pattern % InlineToString( | 238 return pattern % InlineToString( |
239 filepath, grd_node, allow_external_script=allow_external_script, | 239 filepath, |
240 evaluate_condition, | |
241 allow_external_script=allow_external_script, | |
240 filename_expansion_function=filename_expansion_function) | 242 filename_expansion_function=filename_expansion_function) |
241 | 243 |
242 def InlineIncludeFiles(src_match): | 244 def InlineIncludeFiles(src_match): |
243 """Helper function to directly inline generic external files (without | 245 """Helper function to directly inline generic external files (without |
244 wrapping them with any kind of tags). | 246 wrapping them with any kind of tags). |
245 """ | 247 """ |
246 return InlineFileContents(src_match, '%s') | 248 return InlineFileContents(src_match, '%s') |
247 | 249 |
248 def InlineScript(match): | 250 def InlineScript(match): |
249 """Helper function to inline external script files""" | 251 """Helper function to inline external script files""" |
250 attrs = (match.group('attrs1') + match.group('attrs2')).strip() | 252 attrs = (match.group('attrs1') + match.group('attrs2')).strip() |
251 if attrs: | 253 if attrs: |
252 attrs = ' ' + attrs | 254 attrs = ' ' + attrs |
253 return InlineFileContents(match, '<script' + attrs + '>%s</script>') | 255 return InlineFileContents(match, '<script' + attrs + '>%s</script>') |
254 | 256 |
255 def InlineCSSText(text, css_filepath): | 257 def InlineCSSText(text, css_filepath): |
256 """Helper function that inlines external resources in CSS text""" | 258 """Helper function that inlines external resources in CSS text""" |
257 filepath = os.path.dirname(css_filepath) | 259 filepath = os.path.dirname(css_filepath) |
258 # Allow custom modifications before inlining images. | 260 # Allow custom modifications before inlining images. |
259 if rewrite_function: | 261 if rewrite_function: |
260 text = rewrite_function(filepath, text, distribution) | 262 text = rewrite_function(filepath, text, distribution) |
261 text = InlineCSSImages(text, filepath) | 263 text = InlineCSSImages(text, filepath) |
262 return InlineCSSImports(text, filepath) | 264 return InlineCSSImports(text, filepath) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 # TODO(arv): Only do this inside <style> tags. | 351 # TODO(arv): Only do this inside <style> tags. |
350 flat_text = InlineCSSImages(flat_text) | 352 flat_text = InlineCSSImages(flat_text) |
351 | 353 |
352 flat_text = _ICON_RE.sub(SrcReplace, flat_text) | 354 flat_text = _ICON_RE.sub(SrcReplace, flat_text) |
353 | 355 |
354 if names_only: | 356 if names_only: |
355 flat_text = None # Will contains garbage if the flag is set anyway. | 357 flat_text = None # Will contains garbage if the flag is set anyway. |
356 return InlinedData(flat_text, inlined_files) | 358 return InlinedData(flat_text, inlined_files) |
357 | 359 |
358 | 360 |
359 def InlineToString(input_filename, grd_node, preprocess_only = False, | 361 def InlineToString(input_filename, evaluate_condition, preprocess_only = False, |
360 allow_external_script=False, rewrite_function=None, | 362 allow_external_script=False, rewrite_function=None, |
361 filename_expansion_function=None): | 363 filename_expansion_function=None): |
362 """Inlines the resources in a specified file and returns it as a string. | 364 """Inlines the resources in a specified file and returns it as a string. |
363 | 365 |
364 Args: | 366 Args: |
365 input_filename: name of file to read in | 367 input_filename: name of file to read in |
366 grd_node: html node from the grd file for this include tag | 368 evaluate_condition: a function to evaluate boolean conditions |
367 Returns: | 369 Returns: |
368 the inlined data as a string | 370 the inlined data as a string |
369 """ | 371 """ |
370 try: | 372 try: |
371 return DoInline( | 373 return DoInline( |
372 input_filename, | 374 input_filename, |
373 grd_node, | 375 evaluate_condition, |
374 preprocess_only=preprocess_only, | 376 preprocess_only=preprocess_only, |
375 allow_external_script=allow_external_script, | 377 allow_external_script=allow_external_script, |
376 rewrite_function=rewrite_function, | 378 rewrite_function=rewrite_function, |
377 filename_expansion_function=filename_expansion_function).inlined_data | 379 filename_expansion_function=filename_expansion_function).inlined_data |
378 except IOError, e: | 380 except IOError, e: |
379 raise Exception("Failed to open %s while trying to flatten %s. (%s)" % | 381 raise Exception("Failed to open %s while trying to flatten %s. (%s)" % |
380 (e.filename, input_filename, e.strerror)) | 382 (e.filename, input_filename, e.strerror)) |
381 | 383 |
382 | 384 |
383 def InlineToFile(input_filename, output_filename, grd_node): | 385 def InlineToFile(input_filename, output_filename, evaluate_condition): |
384 """Inlines the resources in a specified file and writes it. | 386 """Inlines the resources in a specified file and writes it. |
385 | 387 |
386 Reads input_filename, finds all the src attributes and attempts to | 388 Reads input_filename, finds all the src attributes and attempts to |
387 inline the files they are referring to, then writes the result | 389 inline the files they are referring to, then writes the result |
388 to output_filename. | 390 to output_filename. |
389 | 391 |
390 Args: | 392 Args: |
391 input_filename: name of file to read in | 393 input_filename: name of file to read in |
392 output_filename: name of file to be written to | 394 output_filename: name of file to be written to |
393 grd_node: html node from the grd file for this include tag | 395 evaluate_condition: a function to evaluate boolean conditions |
394 Returns: | 396 Returns: |
395 a set of filenames of all the inlined files | 397 a set of filenames of all the inlined files |
396 """ | 398 """ |
397 inlined_data = InlineToString(input_filename, grd_node) | 399 inlined_data = InlineToString(input_filename, evaluate_condition) |
398 with open(output_filename, 'wb') as out_file: | 400 with open(output_filename, 'wb') as out_file: |
399 out_file.writelines(inlined_data) | 401 out_file.writelines(inlined_data) |
400 | 402 |
401 | 403 |
402 def GetResourceFilenames(filename, | 404 def GetResourceFilenames(filename, |
403 allow_external_script=False, | 405 allow_external_script=False, |
404 rewrite_function=None, | 406 rewrite_function=None, |
405 filename_expansion_function=None): | 407 filename_expansion_function=None): |
406 """For a grd file, returns a set of all the files that would be inline.""" | 408 """For a grd file, returns a set of all the files that would be inline.""" |
407 try: | 409 try: |
(...skipping 12 matching lines...) Expand all Loading... | |
420 | 422 |
421 def main(): | 423 def main(): |
422 if len(sys.argv) <= 2: | 424 if len(sys.argv) <= 2: |
423 print "Flattens a HTML file by inlining its external resources.\n" | 425 print "Flattens a HTML file by inlining its external resources.\n" |
424 print "html_inline.py inputfile outputfile" | 426 print "html_inline.py inputfile outputfile" |
425 else: | 427 else: |
426 InlineToFile(sys.argv[1], sys.argv[2], None) | 428 InlineToFile(sys.argv[1], sys.argv[2], None) |
427 | 429 |
428 if __name__ == '__main__': | 430 if __name__ == '__main__': |
429 main() | 431 main() |
OLD | NEW |