Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implement lean packaging of tilt #160

Closed
wants to merge 5 commits into from

3 participants

@ujifgc

Gemfile:

# register with all available template engines, default option
gem 'tilt'

Gemfile:

# register with no loaded templates, expert option
gem 'tilt', :require => 'tilt/lean'

boot.rb:

# require only needed template engines, no bloating, fast load
require 'tilt/haml'
require 'tilt/sass'
@rkh
Collaborator

Thanks for the pull request, I think this is the right direction. Though I think this should be arranged slightly differently: What's currently in tilt should move to tilt/lean, dropping the flag. What's currently in tilt/all should move to tilt (requiring tilt/lean first), tilt-lean should be dropped. Otherwise you mess with anyone requiring tilt and assuming to have all templates available.

@ujifgc

On it.

@ujifgc ujifgc referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
ujifgc added some commits
@ujifgc ujifgc Implement lean packaging of tilt
    Gemfile:
    gem 'tilt' # with all available template engines
    gem 'tilt', :require => ['tilt/lean', 'tilt/haml] # only with haml
5629203
@ujifgc ujifgc Merge branch 'master' into mean
Restructure new templates to conform with lean packaging

Conflicts:
	lib/tilt.rb
06d48ca
@ujifgc

Restructured new template engines to conform with lean packaging.

Could I please have some clever comments to know if I'm on the right path?

@judofyr
Collaborator

I wrote some notes on making it more general in #178.

I'm going to look at this (and #178) after 1.4 is released.

@ujifgc

Merged current master

@ujifgc ujifgc Merge branch 'master' into mean
Conflicts:
	CHANGELOG.md
	lib/tilt.rb
	tilt.gemspec
ee6dcec
@judofyr judofyr referenced this pull request from a commit
@judofyr judofyr Refactor into Tilt::Mapping:
- Allow multiple mappings with Tilt::Mapping
- Replace global state with a Tilt::Mapping-instance
- Implement lazy loading (#160, #178)
- Remove initialize_template_engine and engine_initialized?
432ae80
@judofyr
Collaborator

Thanks for your ideas and code. Tilt 2.0 will be lean by default (require 'tilt' will not load any template classes) :)

I'm not going to merge this specific pull request because we want to solve this together with #178. Follow that issue for updates.

See also #187 for the current proposal for how to solve #178.

@judofyr judofyr closed this
@ujifgc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 15, 2013
  1. @ujifgc

    Implement lean packaging of tilt

    ujifgc authored
        Gemfile:
        gem 'tilt' # with all available template engines
        gem 'tilt', :require => ['tilt/lean', 'tilt/haml] # only with haml
Commits on Mar 3, 2013
  1. @ujifgc

    Merge branch 'master' into mean

    ujifgc authored
    Restructure new templates to conform with lean packaging
    
    Conflicts:
    	lib/tilt.rb
Commits on Mar 17, 2013
  1. @ujifgc

    Merge with master

    ujifgc authored
Commits on Apr 7, 2013
  1. @ujifgc

    Merge master branch

    ujifgc authored
Commits on Apr 9, 2013
  1. @ujifgc

    Merge branch 'master' into mean

    ujifgc authored
    Conflicts:
    	CHANGELOG.md
    	lib/tilt.rb
    	tilt.gemspec
This page is out of date. Refresh to see the latest.
View
227 lib/tilt.rb
@@ -1,204 +1,23 @@
-module Tilt
- VERSION = '1.3.7'
-
- @preferred_mappings = Hash.new
- @template_mappings = Hash.new { |h, k| h[k] = [] }
-
- # Hash of template path pattern => template implementation class mappings.
- def self.mappings
- @template_mappings
- end
-
- def self.normalize(ext)
- ext.to_s.downcase.sub(/^\./, '')
- end
-
- # Register a template implementation by file extension.
- def self.register(template_class, *extensions)
- if template_class.respond_to?(:to_str)
- # Support register(ext, template_class) too
- extensions, template_class = [template_class], extensions[0]
- end
-
- extensions.each do |ext|
- ext = normalize(ext)
- mappings[ext].unshift(template_class).uniq!
- end
- end
-
- # Makes a template class preferred for the given file extensions. If you
- # don't provide any extensions, it will be preferred for all its already
- # registered extensions:
- #
- # # Prefer RDiscount for its registered file extensions:
- # Tilt.prefer(Tilt::RDiscountTemplate)
- #
- # # Prefer RDiscount only for the .md extensions:
- # Tilt.prefer(Tilt::RDiscountTemplate, '.md')
- def self.prefer(template_class, *extensions)
- if extensions.empty?
- mappings.each do |ext, klasses|
- @preferred_mappings[ext] = template_class if klasses.include? template_class
- end
- else
- extensions.each do |ext|
- ext = normalize(ext)
- register(template_class, ext)
- @preferred_mappings[ext] = template_class
- end
- end
- end
-
- # Returns true when a template exists on an exact match of the provided file extension
- def self.registered?(ext)
- mappings.key?(ext.downcase) && !mappings[ext.downcase].empty?
- end
-
- # Create a new template for the given file using the file's extension
- # to determine the the template mapping.
- def self.new(file, line=nil, options={}, &block)
- if template_class = self[file]
- template_class.new(file, line, options, &block)
- else
- fail "No template engine registered for #{File.basename(file)}"
- end
- end
-
- # Lookup a template class for the given filename or file
- # extension. Return nil when no implementation is found.
- def self.[](file)
- pattern = file.to_s.downcase
- until pattern.empty? || registered?(pattern)
- pattern = File.basename(pattern)
- pattern.sub!(/^[^.]*\.?/, '')
- end
-
- # Try to find a preferred engine.
- preferred_klass = @preferred_mappings[pattern]
- return preferred_klass if preferred_klass
-
- # Fall back to the general list of mappings.
- klasses = @template_mappings[pattern]
-
- # Try to find an engine which is already loaded.
- template = klasses.detect do |klass|
- if klass.respond_to?(:engine_initialized?)
- klass.engine_initialized?
- end
- end
-
- return template if template
-
- # Try each of the classes until one succeeds. If all of them fails,
- # we'll raise the error of the first class.
- first_failure = nil
-
- klasses.each do |klass|
- begin
- klass.new { '' }
- rescue Exception => ex
- first_failure ||= ex
- next
- else
- return klass
- end
- end
-
- raise first_failure if first_failure
- end
-
- # Deprecated module.
- module CompileSite
- end
-
- # Extremely simple template cache implementation. Calling applications
- # create a Tilt::Cache instance and use #fetch with any set of hashable
- # arguments (such as those to Tilt.new):
- # cache = Tilt::Cache.new
- # cache.fetch(path, line, options) { Tilt.new(path, line, options) }
- #
- # Subsequent invocations return the already loaded template object.
- class Cache
- def initialize
- @cache = {}
- end
-
- def fetch(*key)
- @cache[key] ||= yield
- end
-
- def clear
- @cache = {}
- end
- end
-
-
- # Template Implementations ================================================
-
- require 'tilt/string'
- register StringTemplate, 'str'
-
- require 'tilt/erb'
- register ERBTemplate, 'erb', 'rhtml'
- register ErubisTemplate, 'erb', 'rhtml', 'erubis'
-
- require 'tilt/etanni'
- register EtanniTemplate, 'etn', 'etanni'
-
- require 'tilt/haml'
- register HamlTemplate, 'haml'
-
- require 'tilt/css'
- register SassTemplate, 'sass'
- register ScssTemplate, 'scss'
- register LessTemplate, 'less'
-
- require 'tilt/csv'
- register CSVTemplate, 'rcsv'
-
- require 'tilt/coffee'
- register CoffeeScriptTemplate, 'coffee'
-
- require 'tilt/nokogiri'
- register NokogiriTemplate, 'nokogiri'
-
- require 'tilt/builder'
- register BuilderTemplate, 'builder'
-
- require 'tilt/markaby'
- register MarkabyTemplate, 'mab'
-
- require 'tilt/liquid'
- register LiquidTemplate, 'liquid'
-
- require 'tilt/radius'
- register RadiusTemplate, 'radius'
-
- require 'tilt/markdown'
- register MarukuTemplate, 'markdown', 'mkd', 'md'
- register KramdownTemplate, 'markdown', 'mkd', 'md'
- register BlueClothTemplate, 'markdown', 'mkd', 'md'
- register RDiscountTemplate, 'markdown', 'mkd', 'md'
- register RedcarpetTemplate::Redcarpet1, 'markdown', 'mkd', 'md'
- register RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
- register RedcarpetTemplate, 'markdown', 'mkd', 'md'
-
- require 'tilt/textile'
- register RedClothTemplate, 'textile'
-
- require 'tilt/rdoc'
- register RDocTemplate, 'rdoc'
-
- require 'tilt/wiki'
- register CreoleTemplate, 'wiki', 'creole'
- register WikiClothTemplate, 'wiki', 'mediawiki', 'mw'
-
- require 'tilt/yajl'
- register YajlTemplate, 'yajl'
-
- require 'tilt/asciidoc'
- register AsciidoctorTemplate, 'ad', 'adoc', 'asciidoc'
-
- require 'tilt/plain'
- register PlainTemplate, 'html'
-end
+require 'tilt/lean'
+require 'tilt/template'
+
+# templates
+require 'tilt/string'
+require 'tilt/erb'
+require 'tilt/haml'
+require 'tilt/css'
+require 'tilt/coffee'
+require 'tilt/nokogiri'
+require 'tilt/builder'
+require 'tilt/markaby'
+require 'tilt/liquid'
+require 'tilt/radius'
+require 'tilt/markdown'
+require 'tilt/textile'
+require 'tilt/rdoc'
+require 'tilt/wiki'
+require 'tilt/yajl'
+require 'tilt/plain'
+require 'tilt/etanni'
+require 'tilt/csv'
+require 'tilt/asciidoc'
View
4 lib/tilt/asciidoc.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
# AsciiDoc see: http://asciidoc.org/
module Tilt
# Asciidoctor implementation for AsciiDoc see:
@@ -31,4 +29,6 @@ def allows_script?
false
end
end
+
+ register AsciidoctorTemplate, 'ad', 'adoc', 'asciidoc'
end
View
7 lib/tilt/builder.rb
@@ -1,7 +1,5 @@
-require 'tilt/template'
-
module Tilt
- # Builder template implementation. See:
+ # Builder template implementation. See:
# http://builder.rubyforge.org/
class BuilderTemplate < Template
self.default_mime_type = 'text/xml'
@@ -36,5 +34,6 @@ def precompiled_template(locals)
data.to_str
end
end
-end
+ register BuilderTemplate, 'builder'
+end
View
5 lib/tilt/coffee.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# CoffeeScript template implementation. See:
# http://coffeescript.org/
@@ -50,5 +48,6 @@ def allows_script?
false
end
end
-end
+ register CoffeeScriptTemplate, 'coffee'
+end
View
9 lib/tilt/css.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Sass template implementation. See:
# http://haml.hamptoncatlin.com/
@@ -44,7 +42,7 @@ def sass_options
end
end
- # Lessscss template implementation. See:
+ # Lessscss template implementation. See:
# http://lesscss.org/
#
# Less templates do not support object scopes, locals, or yield.
@@ -76,5 +74,8 @@ def allows_script?
false
end
end
-end
+ register SassTemplate, 'sass'
+ register ScssTemplate, 'scss'
+ register LessTemplate, 'less'
+end
View
6 lib/tilt/csv.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# CSV Template implementation. See:
@@ -68,4 +66,6 @@ def precompiled(locals)
end
end
-end
+
+ register CSVTemplate, 'rcsv'
+end
View
6 lib/tilt/erb.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# ERB template implementation. See:
# http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
@@ -106,5 +104,7 @@ def precompiled(locals)
end
end
end
-end
+ register ERBTemplate, 'erb', 'rhtml'
+ register ErubisTemplate, 'erb', 'rhtml', 'erubis'
+end
View
4 lib/tilt/etanni.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
class EtanniTemplate < Template
def prepare
@@ -24,4 +22,6 @@ def precompiled(locals)
[source, offset + 1]
end
end
+
+ register EtanniTemplate, 'etn', 'etanni'
end
View
5 lib/tilt/haml.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Haml template implementation. See:
# http://haml.hamptoncatlin.com/
@@ -60,5 +58,6 @@ def precompiled_postamble(locals)
end
end
end
-end
+ register HamlTemplate, 'haml'
+end
View
134 lib/tilt/lean.rb
@@ -0,0 +1,134 @@
+module Tilt
+ VERSION = '1.3.7'
+
+ @preferred_mappings = Hash.new
+ @template_mappings = Hash.new { |h, k| h[k] = [] }
+
+ # Hash of template path pattern => template implementation class mappings.
+ def self.mappings
+ @template_mappings
+ end
+
+ def self.normalize(ext)
+ ext.to_s.downcase.sub(/^\./, '')
+ end
+
+ # Register a template implementation by file extension.
+ def self.register(template_class, *extensions)
+ if template_class.respond_to?(:to_str)
+ # Support register(ext, template_class) too
+ extensions, template_class = [template_class], extensions[0]
+ end
+
+ extensions.each do |ext|
+ ext = normalize(ext)
+ mappings[ext].unshift(template_class).uniq!
+ end
+ end
+
+ # Makes a template class preferred for the given file extensions. If you
+ # don't provide any extensions, it will be preferred for all its already
+ # registered extensions:
+ #
+ # # Prefer RDiscount for its registered file extensions:
+ # Tilt.prefer(Tilt::RDiscountTemplate)
+ #
+ # # Prefer RDiscount only for the .md extensions:
+ # Tilt.prefer(Tilt::RDiscountTemplate, '.md')
+ def self.prefer(template_class, *extensions)
+ if extensions.empty?
+ mappings.each do |ext, klasses|
+ @preferred_mappings[ext] = template_class if klasses.include? template_class
+ end
+ else
+ extensions.each do |ext|
+ ext = normalize(ext)
+ register(template_class, ext)
+ @preferred_mappings[ext] = template_class
+ end
+ end
+ end
+
+ # Returns true when a template exists on an exact match of the provided file extension
+ def self.registered?(ext)
+ mappings.key?(ext.downcase) && !mappings[ext.downcase].empty?
+ end
+
+ # Create a new template for the given file using the file's extension
+ # to determine the the template mapping.
+ def self.new(file, line=nil, options={}, &block)
+ if template_class = self[file]
+ template_class.new(file, line, options, &block)
+ else
+ fail "No template engine registered for #{File.basename(file)}"
+ end
+ end
+
+ # Lookup a template class for the given filename or file
+ # extension. Return nil when no implementation is found.
+ def self.[](file)
+ pattern = file.to_s.downcase
+ until pattern.empty? || registered?(pattern)
+ pattern = File.basename(pattern)
+ pattern.sub!(/^[^.]*\.?/, '')
+ end
+
+ # Try to find a preferred engine.
+ preferred_klass = @preferred_mappings[pattern]
+ return preferred_klass if preferred_klass
+
+ # Fall back to the general list of mappings.
+ klasses = @template_mappings[pattern]
+
+ # Try to find an engine which is already loaded.
+ template = klasses.detect do |klass|
+ if klass.respond_to?(:engine_initialized?)
+ klass.engine_initialized?
+ end
+ end
+
+ return template if template
+
+ # Try each of the classes until one succeeds. If all of them fails,
+ # we'll raise the error of the first class.
+ first_failure = nil
+
+ klasses.each do |klass|
+ begin
+ klass.new { '' }
+ rescue Exception => ex
+ first_failure ||= ex
+ next
+ else
+ return klass
+ end
+ end
+
+ raise first_failure if first_failure
+ end
+
+ # Deprecated module.
+ module CompileSite
+ end
+
+ # Extremely simple template cache implementation. Calling applications
+ # create a Tilt::Cache instance and use #fetch with any set of hashable
+ # arguments (such as those to Tilt.new):
+ # cache = Tilt::Cache.new
+ # cache.fetch(path, line, options) { Tilt.new(path, line, options) }
+ #
+ # Subsequent invocations return the already loaded template object.
+ class Cache
+ def initialize
+ @cache = {}
+ end
+
+ def fetch(*key)
+ @cache[key] ||= yield
+ end
+
+ def clear
+ @cache = {}
+ end
+ end
+end
View
4 lib/tilt/liquid.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Liquid template implementation. See:
# http://liquid.rubyforge.org/
@@ -42,4 +40,6 @@ def allows_script?
false
end
end
+
+ register LiquidTemplate, 'liquid'
end
View
5 lib/tilt/markaby.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Markaby
# http://github.com/markaby/markaby
@@ -48,5 +46,6 @@ def __run_markaby_tilt__
builder.to_s
end
end
-end
+ register MarkabyTemplate, 'mab'
+end
View
11 lib/tilt/markdown.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Discount Markdown implementation. See:
# http://github.com/rtomayko/rdiscount
@@ -210,5 +208,12 @@ def allows_script?
false
end
end
-end
+ register MarukuTemplate, 'markdown', 'mkd', 'md'
+ register KramdownTemplate, 'markdown', 'mkd', 'md'
+ register BlueClothTemplate, 'markdown', 'mkd', 'md'
+ register RDiscountTemplate, 'markdown', 'mkd', 'md'
+ register RedcarpetTemplate::Redcarpet1, 'markdown', 'mkd', 'md'
+ register RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
+ register RedcarpetTemplate, 'markdown', 'mkd', 'md'
+end
View
5 lib/tilt/nokogiri.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Nokogiri template implementation. See:
# http://nokogiri.org/
@@ -39,5 +37,6 @@ def precompiled_template(locals)
data.to_str
end
end
-end
+ register NokogiriTemplate, 'nokogiri'
+end
View
5 lib/tilt/plain.rb
@@ -1,6 +1,3 @@
-require 'tilt/template'
-
-
module Tilt
# Raw text (no template functionality).
class PlainTemplate < Template
@@ -17,4 +14,6 @@ def evaluate(scope, locals, &block)
@output ||= data
end
end
+
+ register PlainTemplate, 'html'
end
View
4 lib/tilt/radius.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Radius Template
# http://github.com/jlong/radius/
@@ -52,4 +50,6 @@ def allows_script?
false
end
end
+
+ register RadiusTemplate, 'radius'
end
View
4 lib/tilt/rdoc.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# RDoc template. See:
# http://rdoc.rubyforge.org/
@@ -44,4 +42,6 @@ def allows_script?
false
end
end
+
+ register RDocTemplate, 'rdoc'
end
View
4 lib/tilt/string.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# The template source is evaluated as a Ruby string. The #{} interpolation
# syntax can be used to generated dynamic output.
@@ -18,4 +16,6 @@ def precompiled(locals)
[source, offset + 1]
end
end
+
+ register StringTemplate, 'str'
end
View
5 lib/tilt/textile.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# RedCloth implementation. See:
# http://redcloth.org/
@@ -26,5 +24,6 @@ def allows_script?
false
end
end
-end
+ register RedClothTemplate, 'textile'
+end
View
5 lib/tilt/wiki.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Creole implementation. See:
# http://www.wikicreole.org/
@@ -55,4 +53,7 @@ def allows_script?
false
end
end
+
+ register CreoleTemplate, 'wiki', 'creole'
+ register WikiClothTemplate, 'wiki', 'mediawiki', 'mw'
end
View
3  lib/tilt/yajl.rb
@@ -1,5 +1,3 @@
-require 'tilt/template'
-
module Tilt
# Yajl Template implementation
@@ -91,4 +89,5 @@ def decorate(json)
end
end
+ register YajlTemplate, 'yajl'
end
Something went wrong with that request. Please try again.