OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 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. | 10 dependencies. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 filename = filename.replace('%DISTRIBUTION%', distribution) | 100 filename = filename.replace('%DISTRIBUTION%', distribution) |
101 return os.path.join(input_filepath, filename) | 101 return os.path.join(input_filepath, filename) |
102 | 102 |
103 def InlineFileContents(src_match, pattern): | 103 def InlineFileContents(src_match, pattern): |
104 """Helper function to inline external script and css files""" | 104 """Helper function to inline external script and css files""" |
105 filepath = GetFilepath(src_match) | 105 filepath = GetFilepath(src_match) |
106 if filepath is None: | 106 if filepath is None: |
107 return src_match.group(0) | 107 return src_match.group(0) |
108 return pattern % ReadFile(filepath) | 108 return pattern % ReadFile(filepath) |
109 | 109 |
| 110 def InlineIncludeFiles(src_match): |
| 111 """Helper function to inline external script files""" |
| 112 return InlineFileContents(src_match, '%s') |
| 113 |
110 def InlineScript(src_match): | 114 def InlineScript(src_match): |
111 """Helper function to inline external script files""" | 115 """Helper function to inline external script files""" |
112 return InlineFileContents(src_match, '<script>%s</script>') | 116 return InlineFileContents(src_match, '<script>%s</script>') |
113 | 117 |
114 def InlineCssText(text, css_filepath): | 118 def InlineCssText(text, css_filepath): |
115 """Helper function that inlines external resources in CSS text""" | 119 """Helper function that inlines external resources in CSS text""" |
116 filepath = os.path.dirname(css_filepath) | 120 filepath = os.path.dirname(css_filepath) |
117 return InlineCssBackgroundImages(text, filepath) | 121 return InlineCssBackgroundImages(text, filepath) |
118 | 122 |
119 def InlineCssFile(src_match): | 123 def InlineCssFile(src_match): |
(...skipping 25 matching lines...) Expand all Loading... |
145 # references gets inlined in the css and js | 149 # references gets inlined in the css and js |
146 flat_text = re.sub('<script .*?src="(?P<filename>[^"\']*)".*?></script>', | 150 flat_text = re.sub('<script .*?src="(?P<filename>[^"\']*)".*?></script>', |
147 InlineScript, | 151 InlineScript, |
148 ReadFile(input_filename)) | 152 ReadFile(input_filename)) |
149 | 153 |
150 flat_text = re.sub( | 154 flat_text = re.sub( |
151 '<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>', | 155 '<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>', |
152 InlineCssFile, | 156 InlineCssFile, |
153 flat_text) | 157 flat_text) |
154 | 158 |
| 159 flat_text = re.sub( |
| 160 '<!--\s*include\s+file="(?P<filename>[^"\']*)".*-->', |
| 161 InlineIncludeFiles, |
| 162 flat_text) |
| 163 |
155 # TODO(glen): Make this regex not match src="" text that is not inside a tag | 164 # TODO(glen): Make this regex not match src="" text that is not inside a tag |
156 flat_text = re.sub('src="(?P<filename>[^"\']*)"', | 165 flat_text = re.sub('src="(?P<filename>[^"\']*)"', |
157 SrcReplace, | 166 SrcReplace, |
158 flat_text) | 167 flat_text) |
159 | 168 |
160 # TODO(arv): Only do this inside <style> tags. | 169 # TODO(arv): Only do this inside <style> tags. |
161 flat_text = InlineCssBackgroundImages(flat_text) | 170 flat_text = InlineCssBackgroundImages(flat_text) |
162 | 171 |
163 flat_text = re.sub('<link rel="icon".+?href="(?P<filename>[^"\']*)"', | 172 flat_text = re.sub('<link rel="icon".+?href="(?P<filename>[^"\']*)"', |
164 SrcReplace, | 173 SrcReplace, |
165 flat_text) | 174 flat_text) |
166 | 175 |
167 out_file = open(output_filename, 'wb') | 176 out_file = open(output_filename, 'wb') |
168 out_file.writelines(flat_text) | 177 out_file.writelines(flat_text) |
169 out_file.close() | 178 out_file.close() |
170 | 179 |
171 def main(): | 180 def main(): |
172 if len(sys.argv) <= 2: | 181 if len(sys.argv) <= 2: |
173 print "Flattens a HTML file by inlining its external resources.\n" | 182 print "Flattens a HTML file by inlining its external resources.\n" |
174 print "html_inline.py inputfile outputfile" | 183 print "html_inline.py inputfile outputfile" |
175 else: | 184 else: |
176 InlineFile(sys.argv[1], sys.argv[2]) | 185 InlineFile(sys.argv[1], sys.argv[2]) |
177 | 186 |
178 if __name__ == '__main__': | 187 if __name__ == '__main__': |
179 main() | 188 main() |
OLD | NEW |