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

Unified Diff: grit/format/html_inline.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | grit/format/html_inline_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: grit/format/html_inline.py
===================================================================
--- grit/format/html_inline.py (revision 124)
+++ grit/format/html_inline.py (working copy)
@@ -72,7 +72,8 @@
def SrcInlineAsDataURL(
- src_match, base_path, distribution, inlined_files, names_only=False):
+ src_match, base_path, distribution, inlined_files, names_only=False,
+ filename_expansion_function=None):
"""regex replace function.
Takes a regex match for src="filename", attempts to read the file
@@ -93,6 +94,8 @@
string
"""
filename = src_match.group('filename')
+ if filename_expansion_function:
+ filename = filename_expansion_function(filename)
quote = src_match.group('quote')
if filename.find(':') != -1:
@@ -126,7 +129,7 @@
def DoInline(
input_filename, grd_node, allow_external_script=False, names_only=False,
- rewrite_function=None):
+ rewrite_function=None, filename_expansion_function=None):
"""Helper function that inlines the resources in a specified file.
Reads input_filename, finds all the src attributes and attempts to
@@ -139,10 +142,14 @@
names_only: |nil| will be returned for the inlined contents (faster).
rewrite_function: function(filepath, text, distribution) which will be
called to rewrite html content before inlining images.
+ filename_expansion_function: function(filename) which will be called to
+ rewrite filenames before attempting to read them.
Returns:
a tuple of the inlined data as a string and the set of filenames
of all the inlined files
"""
+ if filename_expansion_function:
+ input_filename = filename_expansion_function(input_filename)
input_filepath = os.path.dirname(input_filename)
distribution = GetDistribution()
@@ -153,7 +160,8 @@
inlined_files=inlined_files):
"""Helper function to provide SrcInlineAsDataURL with the base file path"""
return SrcInlineAsDataURL(
- src_match, filepath, distribution, inlined_files, names_only=names_only)
+ src_match, filepath, distribution, inlined_files, names_only=names_only,
+ filename_expansion_function=filename_expansion_function)
def GetFilepath(src_match, base_path = input_filepath):
filename = src_match.group('filename')
@@ -163,6 +171,8 @@
return None
filename = filename.replace('%DISTRIBUTION%', distribution)
+ if filename_expansion_function:
+ filename = filename_expansion_function(filename)
return os.path.normpath(os.path.join(base_path, filename))
def IsConditionSatisfied(src_match):
@@ -214,12 +224,16 @@
inlined_files.add(filepath)
if names_only:
- inlined_files.update(GetResourceFilenames(filepath,
- allow_external_script,
- rewrite_function))
+ inlined_files.update(GetResourceFilenames(
+ filepath,
+ allow_external_script,
+ rewrite_function,
+ filename_expansion_function=filename_expansion_function))
return ""
- return pattern % InlineToString(filepath, grd_node, allow_external_script)
+ return pattern % InlineToString(
+ filepath, grd_node, allow_external_script,
+ filename_expansion_function=filename_expansion_function)
def InlineIncludeFiles(src_match):
"""Helper function to directly inline generic external files (without
@@ -338,7 +352,7 @@
def InlineToString(input_filename, grd_node, allow_external_script=False,
- rewrite_function=None):
+ rewrite_function=None, filename_expansion_function=None):
"""Inlines the resources in a specified file and returns it as a string.
Args:
@@ -348,10 +362,12 @@
the inlined data as a string
"""
try:
- return DoInline(input_filename,
- grd_node,
- allow_external_script=allow_external_script,
- rewrite_function=rewrite_function).inlined_data
+ return DoInline(
+ input_filename,
+ grd_node,
+ allow_external_script=allow_external_script,
+ rewrite_function=rewrite_function,
+ filename_expansion_function=filename_expansion_function).inlined_data
except IOError, e:
raise Exception("Failed to open %s while trying to flatten %s. (%s)" %
(e.filename, input_filename, e.strerror))
@@ -378,14 +394,17 @@
def GetResourceFilenames(filename,
allow_external_script=False,
- rewrite_function=None):
+ rewrite_function=None,
+ filename_expansion_function=None):
"""For a grd file, returns a set of all the files that would be inline."""
try:
- return DoInline(filename,
- None,
- names_only=True,
- allow_external_script=allow_external_script,
- rewrite_function=rewrite_function).inlined_files
+ return DoInline(
+ filename,
+ None,
+ names_only=True,
+ allow_external_script=allow_external_script,
+ rewrite_function=rewrite_function,
+ filename_expansion_function=filename_expansion_function).inlined_files
except IOError, e:
raise Exception("Failed to open %s while trying to flatten %s. (%s)" %
(e.filename, filename, e.strerror))
« no previous file with comments | « no previous file | grit/format/html_inline_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698