summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-09 21:58:55 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-09 21:58:55 +0000
commit1bce0b66d9de24d21afc8cd672f7e1100dfe0b55 (patch)
tree0fa9daf6423986af953f53c5fb0f226034fcc773 /tools
parent4ca1372726d465d18036bf3142f45ca47006ba4e (diff)
downloadchromium_src-1bce0b66d9de24d21afc8cd672f7e1100dfe0b55.zip
chromium_src-1bce0b66d9de24d21afc8cd672f7e1100dfe0b55.tar.gz
chromium_src-1bce0b66d9de24d21afc8cd672f7e1100dfe0b55.tar.bz2
Move browser_resources.rc into a a grd file. While I'm at it,
I removed the flatten_html visual studio rule and just rolled the functionality directly into GRIT. The sln change is to have browser depend on browser_resources (now needed for browser_resources.h). Review URL: http://codereview.chromium.org/21148 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9418 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rwxr-xr-xtools/grit/grit/format/html_inline.py117
-rw-r--r--tools/grit/grit/format/rc.py16
-rw-r--r--tools/grit/grit/node/include.py4
3 files changed, 133 insertions, 4 deletions
diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py
new file mode 100755
index 0000000..f66737c
--- /dev/null
+++ b/tools/grit/grit/format/html_inline.py
@@ -0,0 +1,117 @@
+#!/usr/bin/python
+# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Flattens a HTML file by inlining its external resources.
+
+This is a small script that takes a HTML file, looks for src attributes
+and inlines the specified file, producing one HTML file with no external
+dependencies.
+
+This does not inline CSS styles, nor does it inline anything referenced
+from an inlined file.
+"""
+
+import os
+import re
+import sys
+import base64
+import mimetypes
+
+DIST_DEFAULT = 'chromium'
+DIST_ENV_VAR = 'CHROMIUM_BUILD'
+DIST_SUBSTR = '%DISTRIBUTION%'
+
+def ReadFile(input_filename):
+ """Helper function that returns input_filename as a string.
+
+ Args:
+ input_filename: name of file to be read
+
+ Returns:
+ string
+ """
+ f = open(input_filename, 'rb')
+ file_contents = f.read()
+ f.close()
+ return file_contents
+
+def SrcInline(src_match, base_path, distribution):
+ """regex replace function.
+
+ Takes a regex match for src="filename", attempts to read the file
+ at 'filename' and returns the src attribute with the file inlined
+ as a data URI. If it finds DIST_SUBSTR string in file name, replaces
+ it with distribution.
+
+ Args:
+ src_match: regex match object with 'filename' named capturing group
+ base_path: path that to look for files in
+ distribution: string that should replace DIST_SUBSTR
+
+ Returns:
+ string
+ """
+ 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(base_path, filename)
+ mimetype = mimetypes.guess_type(filename)[0] or 'text/plain'
+ inline_data = base64.standard_b64encode(ReadFile(filepath))
+
+ prefix = src_match.string[src_match.start():src_match.start('filename')-1]
+ return "%s\"data:%s;base64,%s\"" % (prefix, mimetype, inline_data)
+
+def InlineFile(input_filename, output_filename):
+ """Inlines the resources in a specified file.
+
+ Reads input_filename, finds all the src attributes and attempts to
+ inline the files they are referring to, then writes the result
+ to output_filename.
+
+ Args:
+ input_filename: name of file to read in
+ output_filename: name of file to be written to
+ """
+ print "inlining %s to %s" % (input_filename, output_filename)
+ input_filepath = os.path.dirname(input_filename)
+
+ distribution = DIST_DEFAULT
+ if DIST_ENV_VAR in os.environ.keys():
+ distribution = os.environ[DIST_ENV_VAR]
+ if len(distribution) > 1 and distribution[0] == '_':
+ 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)
+
+ # TODO(glen): Make this regex not match src="" text that is not inside a tag
+ flat_text = re.sub('src="(?P<filename>[^"\']*)"',
+ SrcReplace,
+ ReadFile(input_filename))
+
+ # TODO(glen): Make this regex not match url('') that is not inside a style
+ flat_text = re.sub('background:[ ]*url\(\'(?P<filename>[^"\']*)\'',
+ SrcReplace,
+ flat_text)
+
+ out_file = open(output_filename, 'wb')
+ 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"
+ print "html_inline.py inputfile outputfile"
+ else:
+ InlineFile(sys.argv[1], sys.argv[2])
+
+if __name__ == '__main__':
+ main()
+
diff --git a/tools/grit/grit/format/rc.py b/tools/grit/grit/format/rc.py
index a2cdca0..1975acf 100644
--- a/tools/grit/grit/format/rc.py
+++ b/tools/grit/grit/format/rc.py
@@ -11,9 +11,9 @@ import types
import re
from grit import util
+from grit.format import html_inline
from grit.format import interface
-
# Matches all different types of linebreaks.
_LINEBREAKS = re.compile('\r\n|\n|\r')
@@ -390,7 +390,8 @@ class RcSection(interface.ItemFormatter):
class RcInclude(interface.ItemFormatter):
'''Writes out an item that is included in an .rc file (e.g. an ICON)'''
- def __init__(self, type, filenameWithoutPath = 0, relative_path = 0):
+ def __init__(self, type, filenameWithoutPath = 0, relative_path = 0,
+ flatten_html = 0):
'''Indicates to the instance what the type of the resource include is,
e.g. 'ICON' or 'HTML'. Case must be correct, i.e. if the type is all-caps
the parameter should be all-caps.
@@ -401,6 +402,7 @@ class RcInclude(interface.ItemFormatter):
self.type_ = type
self.filenameWithoutPath = filenameWithoutPath
self.relative_path_ = relative_path
+ self.flatten_html = flatten_html
def Format(self, item, lang='en', begin_item=True, output_dir='.'):
if not begin_item:
@@ -419,10 +421,18 @@ class RcInclude(interface.ItemFormatter):
# The FileForLanguage() Function has the side effect of generating the file
# if needed (e.g. if it is an HTML file include).
filename = os.path.abspath(item.FileForLanguage(lang, output_dir))
- if self.filenameWithoutPath:
+ if self.flatten_html:
+ # Generate the flattened HTML file.
+ flat_filename = os.path.join(output_dir, os.path.basename(filename))
+ html_inline.InlineFile(filename, flat_filename)
+
+ # Include the flattened HTML file.
+ filename = os.path.basename(filename)
+ elif self.filenameWithoutPath:
filename = os.path.basename(filename)
elif self.relative_path_:
filename = _MakeRelativePath(output_dir, filename)
+
filename = filename.replace('\\', '\\\\') # escape for the RC format
if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc():
diff --git a/tools/grit/grit/node/include.py b/tools/grit/grit/node/include.py
index 47599a42..032674b 100644
--- a/tools/grit/grit/node/include.py
+++ b/tools/grit/grit/node/include.py
@@ -27,6 +27,7 @@ class IncludeNode(base.Node):
return {'translateable' : 'true',
'generateid': 'true',
'filenameonly': 'false',
+ 'flattenhtml': 'false',
'relativepath': 'false',
}
@@ -37,7 +38,8 @@ class IncludeNode(base.Node):
self.SatisfiesOutputCondition()):
return grit.format.rc.RcInclude(self.attrs['type'].upper(),
self.attrs['filenameonly'] == 'true',
- self.attrs['relativepath'] == 'true')
+ self.attrs['relativepath'] == 'true',
+ self.attrs['flattenhtml'] == 'true')
else:
return super(type(self), self).ItemFormatter(t)