Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1176)

Unified Diff: grit/gather/chrome_html.py

Issue 10386189: Add chrome_html gatherer, which inlines html and automatically generates image set… (Closed) Base URL: http://git.chromium.org/external/grit-i18n.git@master
Patch Set: chrome_html comment. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: grit/gather/chrome_html.py
diff --git a/grit/gather/chrome_html.py b/grit/gather/chrome_html.py
new file mode 100755
index 0000000000000000000000000000000000000000..3d0d5b96c22643bc5c84528edd6e9a873edb4002
--- /dev/null
+++ b/grit/gather/chrome_html.py
@@ -0,0 +1,153 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 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.
+
+'''Prepares a Chrome HTML file by inlining resources and adding references to
Jói 2012/05/23 10:22:46 For triple quotes, we prefer """ (in grit at least
flackr 2012/05/23 19:34:14 Done, thanks.
+high DPI resources.
+
+This is a small script that takes a HTML file, looks for src attributes
Jói 2012/05/23 10:22:46 small script -> small gatherer (?)
flackr 2012/05/23 19:34:14 Done.
+and inlines the specified file, producing one HTML file with no external
+dependencies. It recursively inlines the included files. When inlining CSS
+image files this script also checks for the existence of high DPI versions
+of the inlined file including those on relevant platforms.
+'''
+
+import os
+import re
+import sys
+import types
+import base64
+import mimetypes
+
+from grit.gather import interface
+from grit.format import html_inline
+from grit import lazy_re
+from grit import util
+
+
+# Matches a chrome theme source URL.
+_THEME_SOURCE = lazy_re.compile('chrome://theme/IDR_[A-Z0-9_]*')
+
+
+def InsertImageSet(
+ src_match, base_path, scale_factors, distribution):
+ '''Regex replace function which inserts -webkit-image-set.
+
+ Takes a regex match for url('path'). If the file is local, checks for
+ files of the same name in folders corresponding to the supported scale
+ factors. If the file is from a chrome://theme/ source, inserts the
+ supported @Nx scale factor request. In either case inserts a
+ -webkit-image-set rule to fetch the appropriate image for the current
+ scale factor.
+
+ Args:
+ src_match: regex match object with 'filename' named capturing group
Jói 2012/05/23 10:22:46 I would suggest making the regex a lazy_re.compile
flackr 2012/05/23 19:34:14 Done.
+ base_path: path to look for relative file paths in
+ scale_factors: a list of the supported scale factors (i.e. ['2x'])
+ distribution: string that should replace %DISTRIBUTION%.
+
+ Returns:
+ string
+ '''
+ filename = src_match.group('filename')
+ attr = src_match.group('attribute')
+ prefix = src_match.string[src_match.start():src_match.start('filename')-1]
+
+ # Any matches for which a chrome URL handler will serve all scale factors
+ # can simply request all scale factors.
+ if _THEME_SOURCE.match(filename):
+ images = ["url(\"%s\") %s" % (filename, '1x')]
+ for sc in scale_factors:
+ images.append("url(\"%s@%s\") %s" % (filename, sc, sc))
+ return "%s: -webkit-image-set(%s" % (attr, ', '.join(images))
+
+ if filename.find(':') != -1:
Jói 2012/05/23 10:22:46 why not do if filename.find('http://') != -1 and
flackr 2012/05/23 19:34:14 This is the same condition as used in html_inline,
Jói 2012/05/23 22:59:54 OK. Fine by me to leave as is.
+ # 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)
+ images = ["url(\"%s\") %s" % (filename, '1x')]
+
+ for sc in scale_factors:
+ # check for existence of file and add to image set.
+ scale_path = os.path.split(os.path.join(base_path, filename))
+ scale_image_path = "%s/%s/%s" % (scale_path[0], sc, scale_path[1])
Jói 2012/05/23 10:22:46 Have you tested this on Windows? A more platform-
flackr 2012/05/23 19:34:14 Done. Good point, here we want an OS path but belo
+ if os.path.isfile(scale_image_path):
+ scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)',
Jói 2012/05/23 10:22:46 Again here you may need to be careful with / vs. \
flackr 2012/05/23 19:34:14 Done.
+ '\\g<path>' + sc + '/\\g<file>',
+ filename)
+ images.append("url(\"%s\") %s" % (scale_image_name, sc))
+ return "%s: -webkit-image-set(%s" % (attr, ', '.join(images))
+
+def InsertImageSets(
+ filepath, text, scale_factors, distribution):
+ '''Helper function that inlines external images in CSS backgrounds.'''
Jói 2012/05/23 10:22:46 I think this docstring is copy-pasted and needs to
flackr 2012/05/23 19:34:14 Done.
+ # Add high DPI urls for css attributes: content, background,
+ # or *-image.
+ return re.sub('(?P<attribute>content|background|[\w-]*-image):[ ]*' +
+ 'url\((?:\'|\")(?P<filename>[^"\'\)\(]*)(?:\'|\")',
+ lambda m: InsertImageSet(m, filepath, scale_factors,
+ distribution),
+ text).decode('utf-8').encode('ascii', 'ignore')
+
+
+class ChromeHtml(interface.GathererBase):
+ '''Represents an HTML document processed for Chrome WebUI.
+
+ HTML documents used in Chrome WebUI have local resources inlined and
+ automatically insert references to high DPI assets used in CSS properties
+ with the use of the -webkit-image-set value. This does not generate any
+ translateable messages and instead generates a single DataPack resource.'''
Jói 2012/05/23 10:22:46 For multi-line docstrings, prefer to put the closi
flackr 2012/05/23 19:34:14 Done.
+
+ def __init__(self, html):
+ '''Creates a new object that represents the file 'html'.
+ Args:
+ html: 'filename.html'
+ '''
+ super(type(self), self).__init__()
+ self.filename_ = html
+ self.inlined_text_ = None
+ self.scale_factors_ = []
Jói 2012/05/23 10:22:46 Should this include 100 right off the bat? I guess
flackr 2012/05/23 19:34:14 Yes, for 1x/100 we don't add an image set.
+
+ def SetDefines(self, defines):
+ if 'scale_factors' in defines:
+ self.scale_factors_ = defines['scale_factors'].split(',')
+
+ def GetText(self):
+ '''Returns inlined text of the HTML document'''
+ return self.inlined_text_
+
+ def GetData(self, lang, encoding):
+ '''Returns inlined text of the HTML document'''
+ return self.inlined_text_
+
+ def Translate(self, lang, pseudo_if_not_available=True,
+ skeleton_gatherer=None, fallback_to_english=False):
+ '''Returns this document translated.'''
+ return self.inlined_text_
+
+ def Parse(self):
+ '''Parses and inlines the represented file.'''
+ self.inlined_text_ = html_inline.InlineToString(self.filename_, None,
+ rewrite_function=lambda fp, t, d: InsertImageSets(
+ fp, t, self.scale_factors_, d))
+
+ @staticmethod
+ def FromFile(html, extkey=None, encoding = 'utf-8'):
+ '''Creates a ChromeHtml object for the contents of 'html'. Returns a new
+ ChromeHtml object.
+
+ Args:
+ html: file('') | 'filename.html'
+ extkey: ignored
+ encoding: 'utf-8' (encoding is ignored)
+
+ Return:
+ ChromeHtml(text_of_file)
+ '''
+ if not isinstance(html, types.StringTypes):
+ html = html.name
+
+ return ChromeHtml(html)

Powered by Google App Engine
This is Rietveld 408576698