OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Prepares a Chrome HTML file by inlining resources and adding references to hi gh DPI resources. | |
Jói
2012/05/17 19:39:12
80 char limit
flackr
2012/05/22 21:25:03
Done.
| |
7 | |
8 This is a small script that takes a HTML file, looks for src attributes | |
9 and inlines the specified file, producing one HTML file with no external | |
10 dependencies. It recursively inlines the included files. When inlining CSS | |
11 image files this script also checks for the existence of high DPI versions | |
12 of the inlined file including those on relevant platforms. | |
13 """ | |
14 | |
15 import os | |
16 import re | |
17 import sys | |
18 import types | |
19 import base64 | |
20 import mimetypes | |
21 | |
22 from grit.gather import interface | |
23 from grit.format import html_inline | |
24 from grit import lazy_re | |
25 from grit import util | |
26 | |
27 # Matches a chrome theme source URL. | |
28 _THEME_SOURCE = lazy_re.compile('chrome://theme/IDR_[A-Z0-9_]*') | |
29 | |
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.
| |
30 def InsertImageSet( | |
31 src_match, base_path, scale_factors, distribution): | |
32 filename = src_match.group('filename') | |
33 attr = src_match.group('attribute') | |
34 prefix = src_match.string[src_match.start():src_match.start('filename')-1] | |
35 | |
36 # Any matches for which a chrome URL handler will serve all scale factors | |
37 # can simply request all scale factors. | |
38 if _THEME_SOURCE.match(filename): | |
39 images = ["url(\"%s\") %s" % (filename, '1x')] | |
40 for sc in scale_factors: | |
41 images.append("url(\"%s@%s\") %s" % (filename, sc, sc)) | |
42 return "%s: -webkit-image-set(%s" % (attr, ', '.join(images)) | |
43 | |
44 if filename.find(':') != -1: | |
45 # filename is probably a URL, which we don't want to bother inlining | |
46 return src_match.group(0) | |
47 | |
48 filename = filename.replace('%DISTRIBUTION%', distribution) | |
49 filepath = os.path.join(base_path, filename) | |
50 images = ["url(\"%s\") %s" % (filename, '1x')] | |
51 | |
52 for sc in scale_factors: | |
53 # check for existence of file and add to image set. | |
54 scale_path = os.path.split(os.path.join(base_path, filename)) | |
55 scale_image_path = "%s/%s/%s" % (scale_path[0], sc, scale_path[1]) | |
56 if os.path.isfile(scale_image_path): | |
57 scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)', | |
58 '\\g<path>' + sc + '/\\g<file>', | |
59 filename) | |
60 images.append("url(\"%s\") %s" % (scale_image_name, sc)) | |
61 return "%s: -webkit-image-set(%s" % (attr, ', '.join(images)) | |
62 | |
63 def InsertImageSets( | |
64 filepath, text, scale_factors, distribution): | |
65 """Helper function that inlines external images in CSS backgrounds.""" | |
66 # Add high DPI urls for css attributes: content, background, | |
67 # or *-image. | |
68 return re.sub('(?P<attribute>content|background|[\w-]*-image):[ ]*' + | |
69 'url\((?:\'|\")(?P<filename>[^"\'\)\(]*)(?:\'|\")', | |
70 lambda m: InsertImageSet(m, filepath, scale_factors, | |
71 distribution), | |
72 text).encode('ascii', 'ignore') | |
73 | |
74 class ChromeHtml(interface.GathererBase): | |
75 '''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.
| |
76 | |
77 def __init__(self, html): | |
78 '''Creates a new object that represents 'text'. | |
79 Args: | |
80 html: 'filename.html' | |
81 ''' | |
82 super(type(self), self).__init__() | |
83 self.filename_ = html | |
84 self.inlined_text_ = None | |
85 self.scale_factors_ = [] | |
86 | |
87 def SetAttributes(self, attrs): | |
88 '''Sets node attributes used by the gatherer. | |
89 | |
90 This checks the scale_factors attribute. | |
91 | |
92 Args: | |
93 attrs: The mapping of node attributes. | |
94 ''' | |
95 if 'scale_factors' in attrs: | |
96 self.scale_factors_ = attrs['scale_factors'].split(' ') | |
97 | |
98 def GetText(self): | |
99 '''Returns the original text of the HTML document''' | |
100 return self.inlined_text_ | |
101 | |
102 def GetData(self, lang, encoding): | |
103 '''Return inlined text of the HTML document''' | |
104 return self.inlined_text_ | |
105 | |
106 def Translate(self, lang, pseudo_if_not_available=True, | |
107 skeleton_gatherer=None, fallback_to_english=False): | |
108 '''Returns this document translated.''' | |
109 return self.inlined_text_ | |
110 | |
111 def Parse(self): | |
112 self.inlined_text_ = html_inline.InlineToString(self.filename_, None, | |
113 rewrite_function=lambda fp, t, d: InsertImageSets( | |
114 fp, t, self.scale_factors_, d)) | |
115 | |
116 @staticmethod | |
117 def FromFile(html, extkey=None, encoding = 'utf-8'): | |
118 '''Creates a ChromeHtml object for the contents of 'html'. Returns a new | |
119 ChromeHtml object. | |
120 | |
121 Args: | |
122 html: file('') | 'filename.html' | |
123 extkey: ignored | |
124 encoding: 'utf-8' (encoding is ignored) | |
125 | |
126 Return: | |
127 ChromeHtml(text_of_file) | |
128 ''' | |
129 if not isinstance(html, types.StringTypes): | |
130 html = html.name | |
131 | |
132 return ChromeHtml(html) | |
OLD | NEW |