From de17ab159c33d8a89dcccae69c167c5d4a6cda68 Mon Sep 17 00:00:00 2001 From: "arv@chromium.org" Date: Tue, 5 May 2009 19:54:05 +0000 Subject: Move the shared JS and CSS out of the HTML files and into separate files. Review URL: http://codereview.chromium.org/99151 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15323 0039d316-1c4b-4281-b951-d872f2087c98 --- tools/grit/grit/format/html_inline.py | 41 ++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py index 9871fbd..e5bb2f4 100755 --- a/tools/grit/grit/format/html_inline.py +++ b/tools/grit/grit/format/html_inline.py @@ -37,7 +37,7 @@ def ReadFile(input_filename): f.close() return file_contents -def SrcInline(src_match, base_path, distribution): +def SrcInlineAsDataURL(src_match, base_path, distribution): """regex replace function. Takes a regex match for src="filename", attempts to read the file @@ -88,16 +88,47 @@ def InlineFile(input_filename, output_filename): distribution = distribution[1:].lower() def SrcReplace(src_match): - """Helper function to provide SrcInline with the base file path""" - return SrcInline(src_match, input_filepath, distribution) + """Helper function to provide SrcInlineAsDataURL with the base file path""" + return SrcInlineAsDataURL(src_match, input_filepath, distribution) + + def InlineFileContents(src_match, pattern): + """Helper function to inline external script and css files""" + 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) + + filename = filename.replace('%DISTRIBUTION%', distribution) + filepath = os.path.join(input_filepath, filename) + + return pattern % ReadFile(filepath) + + def InlineScript(src_match): + """Helper function to inline external script files""" + return InlineFileContents(src_match, '') + + def InlineCss(src_match): + """Helper function to inline external css files""" + return InlineFileContents(src_match, '') + + # 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('', + InlineScript, + ReadFile(input_filename)) + + flat_text = re.sub('', + InlineCss, + flat_text) # TODO(glen): Make this regex not match src="" text that is not inside a tag flat_text = re.sub('src="(?P[^"\']*)"', SrcReplace, - ReadFile(input_filename)) + flat_text) # TODO(glen): Make this regex not match url('') that is not inside a style - flat_text = re.sub('background:[ ]*url\(\'(?P[^"\']*)\'', + flat_text = re.sub('background(?:-image)?:[ ]*url\(\'(?P[^"\']*)\'', SrcReplace, flat_text) -- cgit v1.1