Chromium Code Reviews| 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..5b273f9a7b1f7d104551c4031800c5cc378aee09 |
| --- /dev/null |
| +++ b/grit/gather/chrome_html.py |
| @@ -0,0 +1,132 @@ |
| +#!/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 high DPI resources. |
|
Jói
2012/05/17 19:39:12
80 char limit
flackr
2012/05/22 21:25:03
Done.
|
| + |
| +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. 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_]*') |
| + |
|
Jói
2012/05/17 19:39:12
two blank lines between top-level elements (here a
flackr
2012/05/22 21:25:03
Done.
|
| +def InsertImageSet( |
| + src_match, base_path, scale_factors, distribution): |
| + 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: |
| + # 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]) |
| + if os.path.isfile(scale_image_path): |
| + scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)', |
| + '\\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.""" |
| + # 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).encode('ascii', 'ignore') |
| + |
| +class ChromeHtml(interface.GathererBase): |
| + '''Represents an HTML document.''' |
|
Jói
2012/05/17 19:39:12
Maybe extend this to note the functionality of thi
flackr
2012/05/22 21:25:03
Done.
|
| + |
| + def __init__(self, html): |
| + '''Creates a new object that represents 'text'. |
| + Args: |
| + html: 'filename.html' |
| + ''' |
| + super(type(self), self).__init__() |
| + self.filename_ = html |
| + self.inlined_text_ = None |
| + self.scale_factors_ = [] |
| + |
| + def SetAttributes(self, attrs): |
| + '''Sets node attributes used by the gatherer. |
| + |
| + This checks the scale_factors attribute. |
| + |
| + Args: |
| + attrs: The mapping of node attributes. |
| + ''' |
| + if 'scale_factors' in attrs: |
| + self.scale_factors_ = attrs['scale_factors'].split(' ') |
| + |
| + def GetText(self): |
| + '''Returns the original text of the HTML document''' |
| + return self.inlined_text_ |
| + |
| + def GetData(self, lang, encoding): |
| + '''Return 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): |
| + 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) |