liquify 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dane Harrigan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = liquify
2
+
3
+ Liquify is a wrapper to Tobi's <a href="http://rubygems/liquid">Liquid Markup</a> gem.
4
+
5
+ == Examples
6
+
7
+ ## configuring Liquify
8
+ Liquify.setup do
9
+ register_filter MyFilters
10
+ register_tag :include, IncludeTag
11
+ register_drop :page, PageDrop
12
+ end
13
+
14
+ # Any filter, tag or drop registered to Liquify is available anything
15
+ # rendered through +Liquify.render+.
16
+
17
+ ## Using Liquify
18
+ # rendering source
19
+ Liquify.render("{{ page.name }}")
20
+
21
+ # rendering from a file (from a full path)
22
+ Liquify.render(:template => '/path/to/file.liquid')
23
+
24
+ # rendering from a file (from a relative path)
25
+ # setup template root location
26
+ Liquify.setup do
27
+ template_root '/path/to/templates'
28
+ end
29
+
30
+ Liquify.render(:template => 'relative/path/file.liquid')
31
+
32
+ ## my_filters.rb
33
+ module MyFilters
34
+ def bold_text(text)
35
+ text = Liquify.parameter(text)
36
+ "<strong>#{text}</strong>"
37
+ end
38
+ end
39
+
40
+ ## include_tag.rb
41
+ class IncludeTag < Liquify::Tag
42
+ def invoke
43
+ Liquify.render("#{parameter}.liquid")
44
+ end
45
+ end
46
+
47
+ == Todo
48
+
49
+ Make a helpful classes that improve on Liquid's Drop and Block class.
50
+
51
+
52
+ == Note on Patches/Pull Requests
53
+
54
+ * Fork the project.
55
+ * Make your feature addition or bug fix.
56
+ * Add tests for it. This is important so I don't break it in a
57
+ future version unintentionally.
58
+ * Commit, do not mess with rakefile, version, or history.
59
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
60
+ * Send me a pull request. Bonus points for topic branches.
61
+
62
+ == Copyright
63
+
64
+ Copyright (c) 2010 Dane Harrigan. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "liquify"
8
+ gem.version = "0.1"
9
+ gem.summary = %Q{Liquify is a wrapper to Liquid Markup to make it easier to use}
10
+ gem.description = gem.summary
11
+ gem.email = "dane.harrigan@gmail.com"
12
+ gem.homepage = "http://github.com/daneharrigan/liquify"
13
+ gem.authors = ["Dane Harrigan"]
14
+ gem.add_development_dependency "rspec", ">= 1.3.0"
15
+ gem.add_dependency "liquid", "2.1.2"
16
+
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ spec.rcov_opts = ['--exclude', 'gem,spec']
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "liquify #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/lib/liquify.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'liquid'
2
+
3
+ class Liquify
4
+ @@filters = []
5
+ @@drops = {}
6
+ @@tags = {}
7
+ @@template_root = nil
8
+
9
+ class << self
10
+ def setup(&block)
11
+ new.instance_eval(&block)
12
+ @@filters.each { |filter| Liquid::Template.register_filter(filter) }
13
+ @@tags.each { |tag, klass| Liquid::Template.register_tag(tag, klass) }
14
+ end
15
+
16
+ def render(args)
17
+ source = case
18
+ when String === args
19
+ args
20
+ when args[:template]
21
+ path = [@@template_root, args[:template]].compact.join('/')
22
+ File.read(path) if File.exist? path
23
+ end
24
+
25
+ Liquid::Template.parse(source).render(@@drops)
26
+ end
27
+
28
+ def parameters(params)
29
+ return if params.nil?
30
+ params = params.split(',')
31
+ params.map! { |param| param.strip.gsub(/(^('|")|('|")$)/,'') }
32
+ end
33
+
34
+ def parameter(params)
35
+ params = parameters(params)
36
+ params.first if params
37
+ end
38
+ end
39
+
40
+ # +register_filter+ - This method accepts a module and is used to add
41
+ # Liquid filters.
42
+ # Liquify.setup do
43
+ # register_filter MyFilters
44
+ # end
45
+ #
46
+ # A filter is a method stored in a module. The +MyFilters+ would be
47
+ # setup like this:
48
+ # module MyFilters
49
+ # def bold_text(text)
50
+ # "<strong>#{text}</strong>"
51
+ # end
52
+ # end
53
+ #
54
+ # The +MyFilters+ methods will be available in any liquid source
55
+ # rendered with +Liquify.render+.
56
+ #
57
+ # Liquify.render("{{ 'my sample' | bold_text }}")
58
+ # # or read from a file
59
+ # Liquify.render(:template => 'path/to/file.liquid')
60
+ # # => <strong>my sample</strong>
61
+ def register_filter(mod)
62
+ @@filters << mod unless @@filters.include? mod
63
+ end
64
+
65
+ def register_drop(name, klass)
66
+ @@drops[name.to_s] ||= klass.new
67
+ end
68
+
69
+ # +register_tag+ - This method accepts two arguments, a symbol
70
+ # of the tag name used within the Liquid source and the custom
71
+ # tag class made by you.
72
+ #
73
+ # Liquify.setup do
74
+ # register_tag :include, IncludeDrop
75
+ # end
76
+ #
77
+ # A tag is a class that inherits from the class +Liquid::Tag+
78
+ # or +<tt>Liquify::Tag</tt>+.
79
+ # setup like this:
80
+ # class IncludeTag < Liquify::Tag
81
+ # def invoke
82
+ # # your impressive ruby code here
83
+ # end
84
+ # end
85
+ #
86
+ # The +MyFilters+ methods will be available in any liquid source
87
+ # rendered with +Liquify.render+.
88
+ #
89
+ # Liquify.render("{{ 'my sample' | bold_text }}")
90
+ # # or read from a file
91
+ # Liquify.render(:template => 'path/to/file.liquid')
92
+ # # => <strong>my sample</strong>
93
+ def register_tag(name, klass)
94
+ @@tags[name.to_s] ||= klass
95
+ end
96
+
97
+ def template_root(path)
98
+ @@template_root = path
99
+ end
100
+ end
@@ -0,0 +1,20 @@
1
+ class Liquify
2
+ class Tag < Liquid::Tag
3
+ def initialize(tag_name, markup, tokens)
4
+ @_params = Liquify.parameters(markup)
5
+ end
6
+
7
+ def render(context)
8
+ invoke if respond_to? :invoke
9
+ end
10
+
11
+ private
12
+ def parameters
13
+ return @_params
14
+ end
15
+
16
+ def parameter
17
+ parameters.first
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ class SpecDrop < Liquid::Drop
2
+ def name
3
+ "Spec Drop: #{param}"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module SpecFilter
2
+ def bold_text(text)
3
+ text = Liquify.parameter(text)
4
+ "<strong>#{text}</strong>"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class SpecTag < Liquify::Tag
2
+ def invoke
3
+ "Spec Tag: #{parameter.capitalize}"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ {{ 'sample text' }}
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__)+'/../spec_helper')
2
+
3
+ describe "Liquify::Tag" do
4
+ it "should parse parameters" do
5
+ tag = Liquify::Tag.new('tag_name',"'param-1', 'param-2'", nil)
6
+ tag.send(:parameters).should == %W{param-1 param-2}
7
+ end
8
+
9
+ it "should parse a single parameter" do
10
+ tag = Liquify::Tag.new('tag_name',"'param-1'", nil)
11
+ tag.send(:parameter).should == 'param-1'
12
+ end
13
+ end
@@ -0,0 +1,93 @@
1
+ path = File.expand_path(File.dirname(__FILE__))
2
+ require "#{path}/spec_helper"
3
+
4
+ describe "Liquify" do
5
+ it "should have a setup method" do
6
+ Liquify.respond_to?(:setup).should == true
7
+ end
8
+
9
+ it "should return an array of parameters" do
10
+ # extra white space is intentional
11
+ output = Liquify.parameters("'param-1', 'param-2', 'param-3' ")
12
+ output.should == %W{param-1 param-2 param-3}
13
+ end
14
+
15
+ it "should return a single parameter" do
16
+ # extra white space is intentional
17
+ Liquify.parameter("'param-1' ").should == 'param-1'
18
+ end
19
+
20
+ context "when rendering a liquid template" do
21
+ it "should render the source" do
22
+ Liquify.render("{{ 'sample text' }}").should =~ /^sample text$/
23
+ end
24
+
25
+ it "should render the template" do
26
+ Liquify.render(:template => "#{path}/liquid/templates/sample.liquid").should =~ /^sample text$/
27
+ end
28
+
29
+ it "should render nil" do
30
+ Liquify.render(:template => "#{path}/does_not_exist.liquid").should be_empty
31
+ end
32
+
33
+ it "should render the template as a relative path" do
34
+ Liquify.setup do
35
+ template_root path
36
+ end
37
+
38
+ Liquify.render(:template => 'liquid/templates/sample.liquid')
39
+ end
40
+ end
41
+
42
+ context "when adding a filter" do
43
+ before(:all) do
44
+ Liquify.setup do
45
+ register_filter SpecFilter
46
+ end
47
+ end
48
+
49
+ it "should render bold text" do
50
+ output = Liquify.render("{{ 'sample text' | bold_text }}")
51
+ output.should =~ /^\<strong\>sample text\<\/strong\>$/
52
+ end
53
+ end
54
+
55
+ context "when adding a drop" do
56
+ before(:all) do
57
+ Liquify.setup do
58
+ register_drop :item, SpecDrop
59
+ end
60
+ end
61
+
62
+ it "should render the name" do
63
+ Liquify.render("{{ item.name }}").should =~ /Spec Drop/
64
+ end
65
+ end
66
+
67
+ context "when adding a tag" do
68
+ before(:all) do
69
+ Liquify.setup do
70
+ register_tag :tag_name, SpecTag
71
+ end
72
+ end
73
+
74
+ it "should render tag_name" do
75
+ Liquify.render("{% tag_name 'dane' %}").should =~ /Spec Tag: Dane/
76
+ end
77
+ end
78
+ end
79
+
80
+ # # initializer.rb
81
+ # Liquify.setup do
82
+ # register_filter SomeFilters
83
+ # register_tag :include, MyInclude
84
+ # register_drop :site, SiteDrop
85
+ # template_root ''
86
+ # end
87
+
88
+ # liquify :file => 'my/path/to/file'
89
+ # liquify '{{ my_source_code }}'
90
+ # liquify :template => 'dane/file'
91
+
92
+ # Liquify.render(:file => "my/path/to/file")
93
+ # Liquify.render("{{ this is my whatever }}")
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'liquify'
4
+ require 'liquify/tag'
5
+
6
+ require 'liquid/spec_filter'
7
+ require 'liquid/spec_tag'
8
+ require 'liquid/spec_drop'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquify
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Dane Harrigan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-11 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 1
31
+ - 3
32
+ - 0
33
+ version: 1.3.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: liquid
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 2
47
+ - 1
48
+ - 2
49
+ version: 2.1.2
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Liquify is a wrapper to Liquid Markup to make it easier to use
53
+ email: dane.harrigan@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - lib/liquify.rb
68
+ - lib/liquify/tag.rb
69
+ - spec/liquid/spec_drop.rb
70
+ - spec/liquid/spec_filter.rb
71
+ - spec/liquid/spec_tag.rb
72
+ - spec/liquid/templates/sample.liquid
73
+ - spec/liquify/tag_spec.rb
74
+ - spec/liquify_spec.rb
75
+ - spec/spec_helper.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/daneharrigan/liquify
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --charset=UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.3.7
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Liquify is a wrapper to Liquid Markup to make it easier to use
110
+ test_files:
111
+ - spec/liquid/spec_drop.rb
112
+ - spec/liquid/spec_filter.rb
113
+ - spec/liquid/spec_tag.rb
114
+ - spec/liquify/tag_spec.rb
115
+ - spec/liquify_spec.rb
116
+ - spec/spec_helper.rb