Chromium Code Reviews

Unified Diff: tools/grit/grit/format/html_inline.py

Issue 1695022: NTP - Refactor the most visited code to uncouple it from the rest of the NTP.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
« no previous file with comments | « chrome/browser/resources/shared/js/class_list_test.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/grit/grit/format/html_inline.py
===================================================================
--- tools/grit/grit/format/html_inline.py (revision 45941)
+++ tools/grit/grit/format/html_inline.py (working copy)
@@ -86,50 +86,72 @@
if len(distribution) > 1 and distribution[0] == '_':
distribution = distribution[1:].lower()
- def SrcReplace(src_match):
+ def SrcReplace(src_match, filepath=input_filepath):
"""Helper function to provide SrcInlineAsDataURL with the base file path"""
- return SrcInlineAsDataURL(src_match, input_filepath, distribution)
+ return SrcInlineAsDataURL(src_match, filepath, distribution)
- def InlineFileContents(src_match, pattern):
- """Helper function to inline external script and css files"""
+ def GetFilepath(src_match):
filename = src_match.group('filename')
if filename.find(':') != -1:
# filename is probably a URL, which we don't want to bother inlining
- return src_match.group(0)
+ return None
filename = filename.replace('%DISTRIBUTION%', distribution)
- filepath = os.path.join(input_filepath, filename)
+ return os.path.join(input_filepath, filename)
+ def InlineFileContents(src_match, pattern):
+ """Helper function to inline external script and css files"""
+ filepath = GetFilepath(src_match)
+ if filepath is None:
+ return src_match.group(0)
return pattern % ReadFile(filepath)
def InlineScript(src_match):
"""Helper function to inline external script files"""
return InlineFileContents(src_match, '<script>%s</script>')
- def InlineCss(src_match):
+ def InlineCssText(text, css_filepath):
+ """Helper function that inlines external resources inn CSS text"""
+ filepath = os.path.dirname(css_filepath)
+ return InlineCssBackgroundImages(text, filepath)
+
+ def InlineCssFile(src_match):
"""Helper function to inline external css files"""
- return InlineFileContents(src_match, '<style>%s</style>')
+ filepath = GetFilepath(src_match)
+ if filepath is None:
+ return src_match.group(0)
+ # When resolving CSS files we need to pass in the path so that relative URLs
+ # can be resolved.
+ return '<style>%s</style>' % InlineCssText(ReadFile(filepath), filepath)
+
+ def InlineCssBackgroundImages(text, filepath=input_filepath):
+ """Helper function that inlines external images in CSS backgrounds"""
+ return re.sub('background(?:-image)?:[ ]*url\((?:\'|\")' +
+ '(?P<filename>[^"\'\)\(]*)(?:\'|\")',
+ lambda m: SrcReplace(m, filepath),
+ text)
+
+
# We need to inline css and js before we inline images so that image
# references gets inlined in the css and js
flat_text = re.sub('<script .*?src="(?P<filename>[^"\']*)".*?></script>',
InlineScript,
ReadFile(input_filename))
- flat_text = re.sub('<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>',
- InlineCss,
- flat_text)
+ flat_text = re.sub(
+ '<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>',
+ InlineCssFile,
+ flat_text)
# TODO(glen): Make this regex not match src="" text that is not inside a tag
flat_text = re.sub('src="(?P<filename>[^"\']*)"',
SrcReplace,
flat_text)
- # TODO(glen): Make this regex not match url('') that is not inside a style
- flat_text = re.sub('background(?:-image)?:[ ]*url\(\'(?P<filename>[^"\']*)\'',
- SrcReplace,
- flat_text)
+ # TODO(arv): Only do this inside <style> tags.
+ flat_text = InlineCssBackgroundImages(flat_text)
flat_text = re.sub('<link rel="icon".+?href="(?P<filename>[^"\']*)"',
SrcReplace,
@@ -139,6 +161,7 @@
out_file.writelines(flat_text)
out_file.close()
+
def main():
if len(sys.argv) <= 2:
print "Flattens a HTML file by inlining its external resources.\n"
« no previous file with comments | « chrome/browser/resources/shared/js/class_list_test.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine