From 0edb252489ae4e5b642639322d052f2d0f5e0a0c Mon Sep 17 00:00:00 2001 From: Andrei Gladkyi Date: Sat, 30 Nov 2013 17:55:53 +0200 Subject: [PATCH 1/2] Option to specify custom pattern for template filenames --- lib/liquid/file_system.rb | 17 ++++++++++++++--- test/liquid/file_system_test.rb | 5 +++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/liquid/file_system.rb b/lib/liquid/file_system.rb index c7eeddc..b3efef6 100644 --- a/lib/liquid/file_system.rb +++ b/lib/liquid/file_system.rb @@ -31,11 +31,22 @@ module Liquid # file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid" # file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid" # + # Optionally in the second argument you can specify a custom pattern for template filenames. + # The Kernel::sprintf format specification is used. + # Default pattern is "_%s.liquid". + # + # Example: + # + # file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html") + # + # file_system.full_path("index") # => "/some/path/index.html" + # class LocalFileSystem attr_accessor :root - def initialize(root) + def initialize(root, pattern = "_%s.liquid") @root = root + @pattern = pattern end def read_template_file(template_path, context) @@ -49,9 +60,9 @@ module Liquid raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /^[^.\/][a-zA-Z0-9_\/]+$/ full_path = if template_path.include?('/') - File.join(root, File.dirname(template_path), "_#{File.basename(template_path)}.liquid") + File.join(root, File.dirname(template_path), @pattern % File.basename(template_path)) else - File.join(root, "_#{template_path}.liquid") + File.join(root, @pattern % template_path) end raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /^#{File.expand_path(root)}/ diff --git a/test/liquid/file_system_test.rb b/test/liquid/file_system_test.rb index e9abaa8..b9999e0 100644 --- a/test/liquid/file_system_test.rb +++ b/test/liquid/file_system_test.rb @@ -25,5 +25,10 @@ class FileSystemTest < Test::Unit::TestCase assert_raise(FileSystemError) do file_system.full_path("/etc/passwd") end + + # custom pattern for template filenames + file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html") + assert_equal "/some/path/mypartial.html" , file_system.full_path("mypartial") + assert_equal "/some/path/dir/mypartial.html", file_system.full_path("dir/mypartial") end end # FileSystemTest From 0ae42bbc32fdcbc7b139a4b89eca8b1be7c7e3d7 Mon Sep 17 00:00:00 2001 From: Andrei Gladkyi Date: Mon, 16 Dec 2013 17:48:43 +0200 Subject: [PATCH 2/2] Added separate test for custom patterns specifying + updated History.md --- History.md | 1 + test/liquid/file_system_test.rb | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 306e43c..4b114c1 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Allow specifying custom patterns for template filenames, see #284 [Andrei Gladkyi, agladkyi] * Allow drops to optimize loading a slice of elements, see #282 [Tom Burns, boourns] * Support for passing variables to snippets in subdirs, see #271 [Joost Hietbrink, joost] * Add a class cache to avoid runtime extend calls, see #249 [James Tucker, raggi] diff --git a/test/liquid/file_system_test.rb b/test/liquid/file_system_test.rb index b9999e0..4b2a2f1 100644 --- a/test/liquid/file_system_test.rb +++ b/test/liquid/file_system_test.rb @@ -25,8 +25,9 @@ class FileSystemTest < Test::Unit::TestCase assert_raise(FileSystemError) do file_system.full_path("/etc/passwd") end + end - # custom pattern for template filenames + def test_custom_template_filename_patterns file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html") assert_equal "/some/path/mypartial.html" , file_system.full_path("mypartial") assert_equal "/some/path/dir/mypartial.html", file_system.full_path("dir/mypartial")