From 0155bf4021e34c70b3b88eaf75ce41e1140fe474 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 8 Aug 2011 19:51:41 -0700 Subject: use binread to read the files --- actionpack/lib/action_view/template/resolver.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 5f7fe81bd5..eea869e7a0 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -1,5 +1,6 @@ require "pathname" require "active_support/core_ext/class" +require "active_support/core_ext/io" require "action_view/template" module ActionView @@ -137,7 +138,7 @@ module ActionView next if File.directory?(p) || !sanitizer[p].include?(p) handler, format = extract_handler_and_format(p, formats) - contents = File.open(p, "rb") { |io| io.read } + contents = File.binread p templates << Template.new(contents, File.expand_path(p), handler, :virtual_path => path.virtual, :format => format, :updated_at => mtime(p)) -- cgit v1.2.3 From 940404096fecd57fdece94d0a6cfe524f20a2aa8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 8 Aug 2011 21:01:19 -0700 Subject: use meaningful names with our variables --- actionpack/lib/action_view/template/resolver.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index eea869e7a0..7c1636fb60 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -134,14 +134,14 @@ module ActionView templates = [] sanitizer = Hash.new { |h,k| h[k] = Dir["#{File.dirname(k)}/*"] } - Dir[query].each do |p| - next if File.directory?(p) || !sanitizer[p].include?(p) + Dir[query].each do |template| + next if File.directory?(template) || !sanitizer[template].include?(template) - handler, format = extract_handler_and_format(p, formats) - contents = File.binread p + handler, format = extract_handler_and_format(template, formats) + contents = File.binread template - templates << Template.new(contents, File.expand_path(p), handler, - :virtual_path => path.virtual, :format => format, :updated_at => mtime(p)) + templates << Template.new(contents, File.expand_path(template), handler, + :virtual_path => path.virtual, :format => format, :updated_at => mtime(template)) end templates -- cgit v1.2.3 From 295a7fd1bb8f56a1c35162bb944c36357008001a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 8 Aug 2011 21:03:40 -0700 Subject: hash on the template directory in order to improve cache hits --- actionpack/lib/action_view/template/resolver.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 7c1636fb60..979be701b2 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -132,10 +132,11 @@ module ActionView def query(path, details, formats) query = build_query(path, details) templates = [] - sanitizer = Hash.new { |h,k| h[k] = Dir["#{File.dirname(k)}/*"] } + sanitizer = Hash.new { |h,dir| h[dir] = Dir["#{dir}/*"] } Dir[query].each do |template| - next if File.directory?(template) || !sanitizer[template].include?(template) + next if File.directory?(template) + next unless sanitizer[File.dirname(template)].include?(template) handler, format = extract_handler_and_format(template, formats) contents = File.binread template -- cgit v1.2.3 From 128467e9278f488e6a97899ee5f7880a8a0c129f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 8 Aug 2011 21:42:45 -0700 Subject: reduce file stats by improving our dir glob pattern --- actionpack/lib/action_view/template/resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 979be701b2..c66990e1ca 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -241,7 +241,7 @@ module ActionView exts.each do |ext| query << "{" - ext.compact.each { |e| query << ".#{e}," } + ext.compact.uniq.each { |e| query << ".#{e}," } query << "}" end -- cgit v1.2.3 From 37b77c6ae7effdba51ecd8b3b91e2cdb0020b6aa Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 8 Aug 2011 22:16:52 -0700 Subject: refactor the optimized build_query a bit --- actionpack/lib/action_view/template/resolver.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index c66990e1ca..d0b5c76f1a 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -237,15 +237,10 @@ module ActionView class OptimizedFileSystemResolver < FileSystemResolver #:nodoc: def build_query(path, details) exts = EXTENSIONS.map { |ext| details[ext] } - query = File.join(@path, path) - exts.each do |ext| - query << "{" - ext.compact.uniq.each { |e| query << ".#{e}," } - query << "}" - end - - query + File.join(@path, path) + exts.map { |ext| + "{#{ext.compact.uniq.map { |e| ".#{e}," }.join}}" + }.join end end -- cgit v1.2.3 From 233696a02771abebf867dacc02bce7124b5aa647 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 9 Aug 2011 09:32:16 -0700 Subject: use functional style to build a list of template objects --- actionpack/lib/action_view/template/resolver.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index d0b5c76f1a..3abc1b861d 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -131,21 +131,24 @@ module ActionView def query(path, details, formats) query = build_query(path, details) - templates = [] + + # deals with case-insensitive file systems. sanitizer = Hash.new { |h,dir| h[dir] = Dir["#{dir}/*"] } - Dir[query].each do |template| - next if File.directory?(template) - next unless sanitizer[File.dirname(template)].include?(template) + template_paths = Dir[query].reject { |filename| + File.directory?(filename) || + !sanitizer[File.dirname(filename)].include?(filename) + } + template_paths.map { |template| handler, format = extract_handler_and_format(template, formats) contents = File.binread template - templates << Template.new(contents, File.expand_path(template), handler, - :virtual_path => path.virtual, :format => format, :updated_at => mtime(template)) - end - - templates + Template.new(contents, File.expand_path(template), handler, + :virtual_path => path.virtual, + :format => format, + :updated_at => mtime(template)) + } end # Helper for building query glob string based on resolver's pattern. -- cgit v1.2.3 From 1fbc4704df96a8a7e526a3ce4c8110c8e5e26a18 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 9 Aug 2011 10:09:46 -0700 Subject: the freeze trick does nothing on arrays used as hash keys. --- actionpack/lib/action_view/template/resolver.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 3abc1b861d..896baef83c 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -101,13 +101,12 @@ module ActionView if :symbol.respond_to?("<=>") def sort_locals(locals) #:nodoc: - locals.sort.freeze + locals.sort end else def sort_locals(locals) #:nodoc: locals = locals.map{ |l| l.to_s } locals.sort! - locals.freeze end end end -- cgit v1.2.3 From 14a8fd146a64f7ed8399339fa5bc0200b54838ea Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 9 Aug 2011 10:13:02 -0700 Subject: Just remove the sort_locals method --- actionpack/lib/action_view/template/resolver.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 896baef83c..7abaa07bc7 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -69,7 +69,7 @@ module ActionView # before returning it. def cached(key, path_info, details, locals) #:nodoc: name, prefix, partial = path_info - locals = sort_locals(locals) + locals = locals.map { |x| x.to_s }.sort! if key && caching? @cached[key][name][prefix][partial][locals] ||= decorate(yield, path_info, details, locals) @@ -98,17 +98,6 @@ module ActionView t.virtual_path ||= (cached ||= build_path(*path_info)) end end - - if :symbol.respond_to?("<=>") - def sort_locals(locals) #:nodoc: - locals.sort - end - else - def sort_locals(locals) #:nodoc: - locals = locals.map{ |l| l.to_s } - locals.sort! - end - end end # An abstract class that implements a Resolver with path semantics. -- cgit v1.2.3 From 5f94b93279f6d0682fafb237c301302c107a9552 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Aug 2011 15:16:45 -0700 Subject: Properly escape glob characters. --- actionpack/lib/action_view/template/resolver.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 7abaa07bc7..e78d3a82be 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -142,8 +142,12 @@ module ActionView # Helper for building query glob string based on resolver's pattern. def build_query(path, details) query = @pattern.dup - query.gsub!(/\:prefix(\/)?/, path.prefix.empty? ? "" : "#{path.prefix}\\1") # prefix can be empty... - query.gsub!(/\:action/, path.partial? ? "_#{path.name}" : path.name) + + prefix = path.prefix.empty? ? "" : "#{escape_entry(path.prefix)}\\1" + query.gsub!(/\:prefix(\/)?/, prefix) + + partial = escape_entry(path.partial? ? "_#{path.name}" : path.name) + query.gsub!(/\:action/, partial) details.each do |ext, variants| query.gsub!(/\:#{ext}/, "{#{variants.compact.uniq.join(',')}}") @@ -152,6 +156,10 @@ module ActionView File.expand_path(query, @path) end + def escape_entry(entry) + entry.gsub(/(\*|\[|\]|\{|\}|\?)/, "\\\\\\1") + end + # Returns the file mtime from the filesystem. def mtime(p) File.mtime(p) @@ -228,8 +236,9 @@ module ActionView class OptimizedFileSystemResolver < FileSystemResolver #:nodoc: def build_query(path, details) exts = EXTENSIONS.map { |ext| details[ext] } + query = escape_entry(File.join(@path, path)) - File.join(@path, path) + exts.map { |ext| + query + exts.map { |ext| "{#{ext.compact.uniq.map { |e| ".#{e}," }.join}}" }.join end -- cgit v1.2.3 From cc90adfd2a171c5fc9f90f6a6506c5ac937efab3 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Thu, 18 Aug 2011 02:59:02 -0700 Subject: minor details revised in a gsub Regexps have a construct to express alternation of characters, which is character classes. In addition to being the most specific idiom to write this pattern, it reads better without the backslashes. Also, it is better not to use a capture if none is needed. As a side-effect of these changes, the gsub is marginally faster, but speed is not the point of this commit. --- actionpack/lib/action_view/template/resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index e78d3a82be..f855ea257c 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -157,7 +157,7 @@ module ActionView end def escape_entry(entry) - entry.gsub(/(\*|\[|\]|\{|\}|\?)/, "\\\\\\1") + entry.gsub(/[*?{}\[\]]/, '\\\\\\&') end # Returns the file mtime from the filesystem. -- cgit v1.2.3 From 6093d88ed8b50647c1ca235cd4c167ee78955e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 29 Nov 2011 10:17:25 +0100 Subject: Fixes for using action view template in isolation. --- actionpack/lib/action_view/template/handlers/builder.rb | 11 ++++++++++- actionpack/lib/action_view/template/handlers/erb.rb | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/handlers/builder.rb b/actionpack/lib/action_view/template/handlers/builder.rb index 2c52cfd90e..34397c3bcf 100644 --- a/actionpack/lib/action_view/template/handlers/builder.rb +++ b/actionpack/lib/action_view/template/handlers/builder.rb @@ -6,12 +6,21 @@ module ActionView self.default_format = Mime::XML def call(template) - require 'builder' + require_engine "xml = ::Builder::XmlMarkup.new(:indent => 2);" + "self.output_buffer = xml.target!;" + template.source + ";xml.target!;" end + + protected + + def require_engine + @required ||= begin + require "builder" + true + end + end end end end diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index 77720e2bc8..25f26dd609 100644 --- a/actionpack/lib/action_view/template/handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -1,5 +1,5 @@ require 'action_dispatch/http/mime_type' -require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/class/attribute' require 'erubis' module ActionView -- cgit v1.2.3 From 13cab6ef50ab665e634f2834acbb0212300a3797 Mon Sep 17 00:00:00 2001 From: lest Date: Wed, 30 Nov 2011 21:51:01 +0300 Subject: fix exception page when template contains utf-8 and parameters contain utf-8 --- actionpack/lib/action_view/template/error.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/error.rb b/actionpack/lib/action_view/template/error.rb index 587e37a84f..fe27e54037 100644 --- a/actionpack/lib/action_view/template/error.rb +++ b/actionpack/lib/action_view/template/error.rb @@ -89,10 +89,14 @@ module ActionView line_counter = start_on_line return unless source_code = source_code[start_on_line..end_on_line] - source_code.sum do |line| + extract = source_code.sum do |line| line_counter += 1 "#{indent}#{line_counter}: #{line}\n" end + + extract.encode! if extract.respond_to?(:encode!) + + extract end def sub_template_of(template_path) -- cgit v1.2.3