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 | |
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.
| |
7 high DPI resources. | |
8 | |
9 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.
| |
10 and inlines the specified file, producing one HTML file with no external | |
11 dependencies. It recursively inlines the included files. When inlining CSS | |
12 image files this script also checks for the existence of high DPI versions | |
13 of the inlined file including those on relevant platforms. | |
14 ''' | |
15 | |
16 import os | |
17 import re | |
18 import sys | |
19 import types | |
20 import base64 | |
21 import mimetypes | |
22 | |
23 from grit.gather import interface | |
24 from grit.format import html_inline | |
25 from grit import lazy_re | |
26 from grit import util | |
27 | |
28 | |
29 # Matches a chrome theme source URL. | |
30 _THEME_SOURCE = lazy_re.compile('chrome://theme/IDR_[A-Z0-9_]*') | |
31 | |
32 | |
33 def InsertImageSet( | |
34 src_match, base_path, scale_factors, distribution): | |
35 '''Regex replace function which inserts -webkit-image-set. | |
36 | |
37 Takes a regex match for url('path'). If the file is local, checks for | |
38 files of the same name in folders corresponding to the supported scale | |
39 factors. If the file is from a chrome://theme/ source, inserts the | |
40 supported @Nx scale factor request. In either case inserts a | |
41 -webkit-image-set rule to fetch the appropriate image for the current | |
42 scale factor. | |
43 | |
44 Args: | |
45 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.
| |
46 base_path: path to look for relative file paths in | |
47 scale_factors: a list of the supported scale factors (i.e. ['2x']) | |
48 distribution: string that should replace %DISTRIBUTION%. | |
49 | |
50 Returns: | |
51 string | |
52 ''' | |
53 filename = src_match.group('filename') | |
54 attr = src_match.group('attribute') | |
55 prefix = src_match.string[src_match.start():src_match.start('filename')-1] | |
56 | |
57 # Any matches for which a chrome URL handler will serve all scale factors | |
58 # can simply request all scale factors. | |
59 if _THEME_SOURCE.match(filename): | |
60 images = ["url(\"%s\") %s" % (filename, '1x')] | |
61 for sc in scale_factors: | |
62 images.append("url(\"%s@%s\") %s" % (filename, sc, sc)) | |
63 return "%s: -webkit-image-set(%s" % (attr, ', '.join(images)) | |
64 | |
65 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.
| |
66 # filename is probably a URL, which we don't want to bother inlining | |
67 return src_match.group(0) | |
68 | |
69 filename = filename.replace('%DISTRIBUTION%', distribution) | |
70 filepath = os.path.join(base_path, filename) | |
71 images = ["url(\"%s\") %s" % (filename, '1x')] | |
72 | |
73 for sc in scale_factors: | |
74 # check for existence of file and add to image set. | |
75 scale_path = os.path.split(os.path.join(base_path, filename)) | |
76 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
| |
77 if os.path.isfile(scale_image_path): | |
78 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.
| |
79 '\\g<path>' + sc + '/\\g<file>', | |
80 filename) | |
81 images.append("url(\"%s\") %s" % (scale_image_name, sc)) | |
82 return "%s: -webkit-image-set(%s" % (attr, ', '.join(images)) | |
83 | |
84 def InsertImageSets( | |
85 filepath, text, scale_factors, distribution): | |
86 '''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.
| |
87 # Add high DPI urls for css attributes: content, background, | |
88 # or *-image. | |
89 return re.sub('(?P<attribute>content|background|[\w-]*-image):[ ]*' + | |
90 'url\((?:\'|\")(?P<filename>[^"\'\)\(]*)(?:\'|\")', | |
91 lambda m: InsertImageSet(m, filepath, scale_factors, | |
92 distribution), | |
93 text).decode('utf-8').encode('ascii', 'ignore') | |
94 | |
95 | |
96 class ChromeHtml(interface.GathererBase): | |
97 '''Represents an HTML document processed for Chrome WebUI. | |
98 | |
99 HTML documents used in Chrome WebUI have local resources inlined and | |
100 automatically insert references to high DPI assets used in CSS properties | |
101 with the use of the -webkit-image-set value. This does not generate any | |
102 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.
| |
103 | |
104 def __init__(self, html): | |
105 '''Creates a new object that represents the file 'html'. | |
106 Args: | |
107 html: 'filename.html' | |
108 ''' | |
109 super(type(self), self).__init__() | |
110 self.filename_ = html | |
111 self.inlined_text_ = None | |
112 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.
| |
113 | |
114 def SetDefines(self, defines): | |
115 if 'scale_factors' in defines: | |
116 self.scale_factors_ = defines['scale_factors'].split(',') | |
117 | |
118 def GetText(self): | |
119 '''Returns inlined text of the HTML document''' | |
120 return self.inlined_text_ | |
121 | |
122 def GetData(self, lang, encoding): | |
123 '''Returns inlined text of the HTML document''' | |
124 return self.inlined_text_ | |
125 | |
126 def Translate(self, lang, pseudo_if_not_available=True, | |
127 skeleton_gatherer=None, fallback_to_english=False): | |
128 '''Returns this document translated.''' | |
129 return self.inlined_text_ | |
130 | |
131 def Parse(self): | |
132 '''Parses and inlines the represented file.''' | |
133 self.inlined_text_ = html_inline.InlineToString(self.filename_, None, | |
134 rewrite_function=lambda fp, t, d: InsertImageSets( | |
135 fp, t, self.scale_factors_, d)) | |
136 | |
137 @staticmethod | |
138 def FromFile(html, extkey=None, encoding = 'utf-8'): | |
139 '''Creates a ChromeHtml object for the contents of 'html'. Returns a new | |
140 ChromeHtml object. | |
141 | |
142 Args: | |
143 html: file('') | 'filename.html' | |
144 extkey: ignored | |
145 encoding: 'utf-8' (encoding is ignored) | |
146 | |
147 Return: | |
148 ChromeHtml(text_of_file) | |
149 ''' | |
150 if not isinstance(html, types.StringTypes): | |
151 html = html.name | |
152 | |
153 return ChromeHtml(html) | |
OLD | NEW |