Permalink
Browse files

Add tilt

  • Loading branch information...
1 parent a2a4bb4 commit f10e56c48b63604ad307ac4ed090a824e6020204 @brianjlandau brianjlandau committed Nov 18, 2010
Showing with 3,332 additions and 0 deletions.
  1. +1 −0 dependencies
  2. +18 −0 vendor/tilt-1.1/COPYING
  3. +198 −0 vendor/tilt-1.1/README.md
  4. +80 −0 vendor/tilt-1.1/Rakefile
  5. +439 −0 vendor/tilt-1.1/TEMPLATES.md
  6. +110 −0 vendor/tilt-1.1/bin/tilt
  7. +868 −0 vendor/tilt-1.1/lib/tilt.rb
  8. +68 −0 vendor/tilt-1.1/test/contest.rb
  9. +1 −0 vendor/tilt-1.1/test/markaby/locals.mab
  10. +1 −0 vendor/tilt-1.1/test/markaby/markaby.mab
  11. +1 −0 vendor/tilt-1.1/test/markaby/markaby_other_static.mab
  12. +1 −0 vendor/tilt-1.1/test/markaby/render_twice.mab
  13. +1 −0 vendor/tilt-1.1/test/markaby/scope.mab
  14. +2 −0 vendor/tilt-1.1/test/markaby/yielding.mab
  15. +44 −0 vendor/tilt-1.1/test/tilt_buildertemplate_test.rb
  16. +32 −0 vendor/tilt-1.1/test/tilt_cache_test.rb
  17. +25 −0 vendor/tilt-1.1/test/tilt_coffeescripttemplate_test.rb
  18. +86 −0 vendor/tilt-1.1/test/tilt_compilesite_test.rb
  19. +212 −0 vendor/tilt-1.1/test/tilt_erbtemplate_test.rb
  20. +136 −0 vendor/tilt-1.1/test/tilt_erubistemplate_test.rb
  21. +144 −0 vendor/tilt-1.1/test/tilt_hamltemplate_test.rb
  22. +20 −0 vendor/tilt-1.1/test/tilt_lesstemplate_test.rb
  23. +73 −0 vendor/tilt-1.1/test/tilt_liquidtemplate_test.rb
  24. +73 −0 vendor/tilt-1.1/test/tilt_markaby_test.rb
  25. +54 −0 vendor/tilt-1.1/test/tilt_nokogiritemplate_test.rb
  26. +70 −0 vendor/tilt-1.1/test/tilt_radiustemplate_test.rb
  27. +40 −0 vendor/tilt-1.1/test/tilt_rdiscounttemplate_test.rb
  28. +19 −0 vendor/tilt-1.1/test/tilt_rdoctemplate_test.rb
  29. +20 −0 vendor/tilt-1.1/test/tilt_redclothtemplate_test.rb
  30. +32 −0 vendor/tilt-1.1/test/tilt_sasstemplate_test.rb
  31. +163 −0 vendor/tilt-1.1/test/tilt_stringtemplate_test.rb
  32. +158 −0 vendor/tilt-1.1/test/tilt_template_test.rb
  33. +65 −0 vendor/tilt-1.1/test/tilt_test.rb
  34. +77 −0 vendor/tilt-1.1/tilt.gemspec
View
@@ -14,3 +14,4 @@ webrat 0.7.2 (test) git://github.com/brynary/webrat.git
mocha 0.9.8 (test)
factory_girl 1.2.3 (test)
haml 2.2.24
+tilt 1.1
View
@@ -0,0 +1,18 @@
+Copyright (c) 2010 Ryan Tomayko <http://tomayko.com/about>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,198 @@
+Tilt
+====
+
+Tilt is a thin interface over a bunch of different Ruby template engines in
+an attempt to make their usage as generic possible. This is useful for web
+frameworks, static site generators, and other systems that support multiple
+template engines but don't want to code for each of them individually.
+
+The following features are supported for all template engines (assuming the
+feature is relevant to the engine):
+
+ * Custom template evaluation scopes / bindings
+ * Ability to pass locals to template evaluation
+ * Support for passing a block to template evaluation for "yield"
+ * Backtraces with correct filenames and line numbers
+ * Template file caching and reloading
+ * Fast, method-based template source compilation
+
+The primary goal is to get all of the things listed above right for all
+template engines included in the distribution.
+
+Support for these template engines is included with the package:
+
+ ENGINE FILE EXTENSIONS REQUIRED LIBRARIES
+ -------------------------- ----------------- ----------------------------
+ ERB .erb none (included ruby stdlib)
+ Interpolated String .str none (included ruby core)
+ Haml .haml haml
+ Sass .sass haml
+ Less CSS .less less
+ Builder .builder builder
+ Liquid .liquid liquid
+ RDiscount .markdown rdiscount
+ RedCloth .textile redcloth
+ RDoc .rdoc rdoc
+ Radius .radius radius
+ Markaby .mab markaby
+ Nokogiri .nokogiri nokogiri
+ CoffeeScript .coffee coffee-script (+node coffee)
+
+See [TEMPLATES.md][t] for detailed information on template engine
+options and supported features.
+
+[t]: http://github.com/rtomayko/tilt/blob/master/TEMPLATES.md
+ "Tilt Template Engine Documentation"
+
+Basic Usage
+-----------
+
+Instant gratification:
+
+ require 'erb'
+ require 'tilt'
+ template = Tilt.new('templates/foo.erb')
+ => #<Tilt::ERBTemplate @file="templates/foo.rb" ...>
+ output = template.render
+ => "Hello world!"
+
+It's recommended that calling programs explicitly require template engine
+libraries (like 'erb' above) at load time. Tilt attempts to lazy require the
+template engine library the first time a template is created but this is
+prone to error in threaded environments.
+
+The `Tilt` module contains generic implementation classes for all supported
+template engines. Each template class adheres to the same interface for
+creation and rendering. In the instant gratification example, we let Tilt
+determine the template implementation class based on the filename, but
+`Tilt::Template` implementations can also be used directly:
+
+ template = Tilt::HamlTemplate.new('templates/foo.haml')
+ output = template.render
+
+The `render` method takes an optional evaluation scope and locals hash
+arguments. Here, the template is evaluated within the context of the
+`Person` object with locals `x` and `y`:
+
+ template = Tilt::ERBTemplate.new('templates/foo.erb')
+ joe = Person.find('joe')
+ output = template.render(joe, :x => 35, :y => 42)
+
+If no scope is provided, the template is evaluated within the context of an
+object created with `Object.new`.
+
+A single `Template` instance's `render` method may be called multiple times
+with different scope and locals arguments. Continuing the previous example,
+we render the same compiled template but this time in jane's scope:
+
+ jane = Person.find('jane')
+ output = template.render(jane, :x => 22, :y => nil)
+
+Blocks can be passed to `render` for templates that support running
+arbitrary ruby code (usually with some form of `yield`). For instance,
+assuming the following in `foo.erb`:
+
+ Hey <%= yield %>!
+
+The block passed to `render` is called on `yield`:
+
+ template = Tilt::ERBTemplate.new('foo.erb')
+ template.render { 'Joe' }
+ # => "Hey Joe!"
+
+Template Mappings
+-----------------
+
+The `Tilt` module includes methods for associating template implementation
+classes with filename patterns and for locating/instantiating template
+classes based on those associations.
+
+The `Tilt::register` method associates a filename pattern with a specific
+template implementation. To use ERB for files ending in a `.bar` extension:
+
+ >> Tilt.register 'bar', Tilt::ERBTemplate
+ >> Tilt.new('views/foo.bar')
+ => #<Tilt::ERBTemplate @file="views/foo.bar" ...>
+
+Retrieving the template class for a file or file extension:
+
+ >> Tilt['foo.bar']
+ => Tilt::ERBTemplate
+ >> Tilt['haml']
+ => Tilt::HamlTemplate
+
+It's also possible to register template file mappings that are more specific
+than a file extension. To use Erubis for `bar.erb` but ERB for all other `.erb`
+files:
+
+ >> Tilt.register 'bar.erb', Tilt::ErubisTemplate
+ >> Tilt.new('views/foo.erb')
+ => Tilt::ERBTemplate
+ >> Tilt.new('views/bar.erb')
+ => Tilt::ErubisTemplate
+
+The template class is determined by searching for a series of decreasingly
+specific name patterns. When creating a new template with
+`Tilt.new('views/foo.html.erb')`, we check for the following template
+mappings:
+
+ 1. `views/foo.html.erb`
+ 2. `foo.html.erb`
+ 3. `html.erb`
+ 4. `erb`
+
+`Tilt::register` can also be used to select between alternative template
+engines. To use Erubis instead of ERB for `.erb` files:
+
+ Tilt.register 'erb', Tilt::ErubisTemplate
+
+Or, use BlueCloth for markdown instead of RDiscount:
+
+ Tilt.register 'markdown', Tilt::BlueClothTemplate
+
+Template Compilation
+--------------------
+
+Tilt can compile generated Ruby source code produced by template engines and
+reuse on subsequent template invocations. Benchmarks show this yields a 5x-10x
+performance increase over evaluating the Ruby source on each invocation.
+
+Template compilation is currently supported for these template engines:
+StringTemplate, ERB, Erubis, Haml, and Builder.
+
+To enable template compilation, the `Tilt::CompileSite` module must be mixed in
+to the scope object passed to the template's `#render` method. This can be
+accomplished by including (with `Module#include`) the module in the class used
+for scope objects or by extending (with `Object#extend`) scope objects before
+passing to `Template#render`:
+
+ require 'tilt'
+
+ template = Tilt::ERBTemplate.new('foo.erb')
+
+ # Slow. Uses Object#instance_eval to process template
+ class Scope
+ end
+ scope = Scope.new
+ template.render(scope)
+
+ # Fast. Uses compiled template and Object#send to process template
+ class Scope
+ include Tilt::CompileSite
+ end
+ scope = Scope.new
+ template.render(scope)
+
+ # Also fast, though a bit a slower due to having to extend each time
+ scope = Object.new
+ scope.extend Tilt::CompileSite
+ template.render(scope)
+
+When the `Tilt::CompileSite` module is not present, template execution falls
+back to evaluating the template from source on each invocation.
+
+LICENSE
+-------
+
+Tilt is Copyright (c) 2010 [Ryan Tomayko](http://tomayko.com/about) and
+distributed under the MIT license. See the `COPYING` file for more info.
View
@@ -0,0 +1,80 @@
+require 'rake/testtask'
+task :default => :test
+
+# SPECS =====================================================================
+
+desc 'Generate test coverage report'
+task :rcov do
+ sh "rcov -Ilib:test test/*_test.rb"
+end
+
+desc 'Run tests (default)'
+Rake::TestTask.new(:test) do |t|
+ t.test_files = FileList['test/*_test.rb']
+ t.ruby_opts = ['-Itest']
+ t.ruby_opts << '-rubygems' if defined? Gem
+end
+
+# PACKAGING =================================================================
+
+begin
+ require 'rubygems'
+rescue LoadError
+end
+
+if defined?(Gem)
+ SPEC = eval(File.read('tilt.gemspec'))
+
+ def package(ext='')
+ "pkg/tilt-#{SPEC.version}" + ext
+ end
+
+ desc 'Build packages'
+ task :package => %w[.gem .tar.gz].map {|e| package(e)}
+
+ desc 'Build and install as local gem'
+ task :install => package('.gem') do
+ sh "gem install #{package('.gem')}"
+ end
+
+ directory 'pkg/'
+
+ file package('.gem') => %w[pkg/ tilt.gemspec] + SPEC.files do |f|
+ sh "gem build tilt.gemspec"
+ mv File.basename(f.name), f.name
+ end
+
+ file package('.tar.gz') => %w[pkg/] + SPEC.files do |f|
+ sh "git archive --format=tar HEAD | gzip > #{f.name}"
+ end
+
+ desc 'Upload gem and tar.gz distributables to rubyforge'
+ task :release => [package('.gem'), package('.tar.gz')] do |t|
+ sh <<-SH
+ rubyforge add_release sinatra tilt #{SPEC.version} #{package('.gem')} &&
+ rubyforge add_file sinatra tilt #{SPEC.version} #{package('.tar.gz')}
+ SH
+ end
+end
+
+# GEMSPEC ===================================================================
+
+file 'tilt.gemspec' => FileList['{lib,test}/**','Rakefile'] do |f|
+ # read version from tilt.rb
+ version = File.read('lib/tilt.rb')[/VERSION = '(.*)'/] && $1
+ # read spec file and split out manifest section
+ spec = File.
+ read(f.name).
+ sub(/s\.version\s*=\s*'.*'/, "s.version = '#{version}'")
+ parts = spec.split(" # = MANIFEST =\n")
+ # determine file list from git ls-files
+ files = `git ls-files`.
+ split("\n").sort.reject{ |file| file =~ /^\./ }.
+ map{ |file| " #{file}" }.join("\n")
+ # piece file back together and write...
+ parts[1] = " s.files = %w[\n#{files}\n ]\n"
+ spec = parts.join(" # = MANIFEST =\n")
+ spec.sub!(/s.date = '.*'/, "s.date = '#{Time.now.strftime("%Y-%m-%d")}'")
+ File.open(f.name, 'w') { |io| io.write(spec) }
+ puts "updated #{f.name}"
+end
Oops, something went wrong.

0 comments on commit f10e56c

Please sign in to comment.