Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: filters/inline_file.py

Issue 4661048523096064: Issue 2120 - Add support for animations. (Closed)
Patch Set: Use base64 module for the base64 encoding in the inline_file filter. Created April 11, 2015, 2:04 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | static/js/animation.js » ('j') | static/js/animation.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: filters/inline_file.py
diff --git a/filters/inline_file.py b/filters/inline_file.py
new file mode 100644
index 0000000000000000000000000000000000000000..23a67f7450748b5536d4580b212de664292bc7d9
--- /dev/null
+++ b/filters/inline_file.py
@@ -0,0 +1,38 @@
+# This file is part of the Adblock Plus website,
+# Copyright (C) 2006-2015 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import base64
+import mimetypes
+import urllib
+
+from jinja2 import contextfilter
+
+@contextfilter
+def inline_file(context, path, mime_type=None):
+ if mime_type is None:
+ mime_type = mimetypes.guess_type(path)[0] or "application/octet-stream"
Sebastian Noack 2015/04/11 14:15:32 I wonder whether we should make the mime_type argu
kzar 2015/04/12 17:01:45 I think defaulting to application/oclet-stream is
Sebastian Noack 2015/04/13 08:12:58 Well, if the path doesn't contain a distinct file
kzar 2015/04/13 09:05:35 Done.
+
+ source = context["source"]
+ for locale in (context["locale"], "en"):
+ if source.has_localizable_file(locale, path):
Sebastian Noack 2015/04/11 14:15:32 Do we actually need this branch? Assuming we need
kzar 2015/04/12 17:01:45 We do need this branch, the images used in the ani
+ file_contents = source.read_localizable_file(locale, path)
+ break
+ else:
+ file_contents = source.read_static(path)
+
+ return "data:%s;base64,%s" % (
+ mime_type,
+ urllib.quote(base64.b64encode(file_contents))
+ )
« no previous file with comments | « no previous file | static/js/animation.js » ('j') | static/js/animation.js » ('J')

Powered by Google App Engine
This is Rietveld