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

Side by Side Diff: grit/gather/chrome_html.py

Issue 16539002: GRIT: Enable variable expansion in filenames during HTML inlining. (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: Created 7 years, 6 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 unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Prepares a Chrome HTML file by inlining resources and adding references to 6 """Prepares a Chrome HTML file by inlining resources and adding references to
7 high DPI resources and removing references to unsupported scale factors. 7 high DPI resources and removing references to unsupported scale factors.
8 8
9 This is a small gatherer that takes a HTML file, looks for src attributes 9 This is a small gatherer that takes a HTML file, looks for src attributes
10 and inlines the specified file, producing one HTML file with no external 10 and inlines the specified file, producing one HTML file with no external
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 if _THEME_SOURCE.match(filename): 71 if _THEME_SOURCE.match(filename):
72 images = [('1x', filename)] 72 images = [('1x', filename)]
73 for scale_factor in scale_factors: 73 for scale_factor in scale_factors:
74 images.append((scale_factor, "%s@%s" % (filename, scale_factor))) 74 images.append((scale_factor, "%s@%s" % (filename, scale_factor)))
75 return images 75 return images
76 76
77 if filename.find(':') != -1: 77 if filename.find(':') != -1:
78 # filename is probably a URL, only return filename itself. 78 # filename is probably a URL, only return filename itself.
79 return [('1x', filename)] 79 return [('1x', filename)]
80 80
81 filename = filename.replace(DIST_SUBSTR, distribution) 81 filename = filename.replace(DIST_SUBSTR, distribution)
flackr 2013/06/06 14:10:26 Should do filename substitution here as well so th
dconnelly 2013/06/07 09:10:38 Done.
flackr 2013/06/07 14:50:40 Can you add a test case to chrome_html_unittest.py
dconnelly 2013/06/07 16:00:40 Done, and found two more cases.
82 filepath = os.path.join(base_path, filename) 82 filepath = os.path.join(base_path, filename)
83 images = [('1x', filename)] 83 images = [('1x', filename)]
84 84
85 for scale_factor in scale_factors: 85 for scale_factor in scale_factors:
86 # Check for existence of file and add to image set. 86 # Check for existence of file and add to image set.
87 scale_path = os.path.split(os.path.join(base_path, filename)) 87 scale_path = os.path.split(os.path.join(base_path, filename))
88 scale_image_path = os.path.join(scale_path[0], scale_factor, scale_path[1]) 88 scale_image_path = os.path.join(scale_path[0], scale_factor, scale_path[1])
89 if os.path.isfile(scale_image_path): 89 if os.path.isfile(scale_image_path):
90 # HTML/CSS always uses forward slashed paths. 90 # HTML/CSS always uses forward slashed paths.
91 scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)', 91 scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)',
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 translateable messages and instead generates a single DataPack resource. 231 translateable messages and instead generates a single DataPack resource.
232 """ 232 """
233 233
234 def __init__(self, *args, **kwargs): 234 def __init__(self, *args, **kwargs):
235 super(ChromeHtml, self).__init__(*args, **kwargs) 235 super(ChromeHtml, self).__init__(*args, **kwargs)
236 self.allow_external_script_ = False 236 self.allow_external_script_ = False
237 self.flatten_html_ = False 237 self.flatten_html_ = False
238 # 1x resources are implicitly already in the source and do not need to be 238 # 1x resources are implicitly already in the source and do not need to be
239 # added. 239 # added.
240 self.scale_factors_ = [] 240 self.scale_factors_ = []
241 self.filename_expansion_function = None
241 242
242 def SetAttributes(self, attrs): 243 def SetAttributes(self, attrs):
243 self.allow_external_script_ = ('allowexternalscript' in attrs and 244 self.allow_external_script_ = ('allowexternalscript' in attrs and
244 attrs['allowexternalscript'] == 'true') 245 attrs['allowexternalscript'] == 'true')
245 self.flatten_html_ = ('flattenhtml' in attrs and 246 self.flatten_html_ = ('flattenhtml' in attrs and
246 attrs['flattenhtml'] == 'true') 247 attrs['flattenhtml'] == 'true')
247 248
248 def SetDefines(self, defines): 249 def SetDefines(self, defines):
249 if 'scale_factors' in defines: 250 if 'scale_factors' in defines:
250 self.scale_factors_ = defines['scale_factors'].split(',') 251 self.scale_factors_ = defines['scale_factors'].split(',')
251 252
252 def GetText(self): 253 def GetText(self):
253 """Returns inlined text of the HTML document.""" 254 """Returns inlined text of the HTML document."""
254 return self.inlined_text_ 255 return self.inlined_text_
255 256
256 def GetTextualIds(self): 257 def GetTextualIds(self):
257 return [self.extkey] 258 return [self.extkey]
258 259
259 def GetData(self, lang, encoding): 260 def GetData(self, lang, encoding):
260 """Returns inlined text of the HTML document.""" 261 """Returns inlined text of the HTML document."""
261 return self.inlined_text_ 262 return self.inlined_text_
262 263
263 def GetHtmlResourceFilenames(self): 264 def GetHtmlResourceFilenames(self):
264 """Returns a set of all filenames inlined by this file.""" 265 """Returns a set of all filenames inlined by this file."""
265 if self.flatten_html_: 266 if self.flatten_html_:
266 return html_inline.GetResourceFilenames( 267 return html_inline.GetResourceFilenames(
267 self.grd_node.ToRealPath(self.GetInputPath()), 268 self.grd_node.ToRealPath(self.GetInputPath()),
268 allow_external_script=self.allow_external_script_, 269 allow_external_script=self.allow_external_script_,
269 rewrite_function=lambda fp, t, d: ProcessImageSets( 270 rewrite_function=lambda fp, t, d: ProcessImageSets(
270 fp, t, self.scale_factors_, d)) 271 fp, t, self.scale_factors_, d),
272 filename_expansion_function=self.filename_expansion_function)
271 return [] 273 return []
272 274
273 def Translate(self, lang, pseudo_if_not_available=True, 275 def Translate(self, lang, pseudo_if_not_available=True,
274 skeleton_gatherer=None, fallback_to_english=False): 276 skeleton_gatherer=None, fallback_to_english=False):
275 """Returns this document translated.""" 277 """Returns this document translated."""
276 return self.inlined_text_ 278 return self.inlined_text_
277 279
280 def SetFilenameExpansionFunction(self, fn):
281 self.filename_expansion_function = fn
282
278 def Parse(self): 283 def Parse(self):
279 """Parses and inlines the represented file.""" 284 """Parses and inlines the represented file."""
280 285
281 filename = self.GetInputPath() 286 filename = self.GetInputPath()
287 if self.filename_expansion_function:
288 filename = self.filename_expansion_function(filename)
282 # Hack: some unit tests supply an absolute path and no root node. 289 # Hack: some unit tests supply an absolute path and no root node.
283 if not os.path.isabs(filename): 290 if not os.path.isabs(filename):
284 filename = self.grd_node.ToRealPath(filename) 291 filename = self.grd_node.ToRealPath(filename)
285 if self.flatten_html_: 292 if self.flatten_html_:
286 self.inlined_text_ = html_inline.InlineToString( 293 self.inlined_text_ = html_inline.InlineToString(
287 filename, 294 filename,
288 self.grd_node, 295 self.grd_node,
289 allow_external_script = self.allow_external_script_, 296 allow_external_script = self.allow_external_script_,
290 rewrite_function=lambda fp, t, d: ProcessImageSets( 297 rewrite_function=lambda fp, t, d: ProcessImageSets(
291 fp, t, self.scale_factors_, d)) 298 fp, t, self.scale_factors_, d),
299 filename_expansion_function=self.filename_expansion_function)
292 else: 300 else:
293 distribution = html_inline.GetDistribution() 301 distribution = html_inline.GetDistribution()
294 self.inlined_text_ = ProcessImageSets( 302 self.inlined_text_ = ProcessImageSets(
295 os.path.dirname(filename), 303 os.path.dirname(filename),
296 util.ReadFile(filename, 'utf-8'), 304 util.ReadFile(filename, 'utf-8'),
297 self.scale_factors_, 305 self.scale_factors_,
298 distribution) 306 distribution)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698