tilt 1.4.1 → 2.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +1 -1
  3. data/bin/tilt +18 -8
  4. data/lib/tilt/asciidoc.rb +1 -8
  5. data/lib/tilt/babel.rb +16 -0
  6. data/lib/tilt/bluecloth.rb +24 -0
  7. data/lib/tilt/builder.rb +12 -15
  8. data/lib/tilt/coffee.rb +10 -6
  9. data/lib/tilt/commonmarker.rb +88 -0
  10. data/lib/tilt/creole.rb +25 -0
  11. data/lib/tilt/csv.rb +12 -18
  12. data/lib/tilt/dummy.rb +3 -0
  13. data/lib/tilt/erb.rb +9 -56
  14. data/lib/tilt/erubi.rb +32 -0
  15. data/lib/tilt/erubis.rb +43 -0
  16. data/lib/tilt/haml.rb +66 -44
  17. data/lib/tilt/kramdown.rb +25 -0
  18. data/lib/tilt/less.rb +30 -0
  19. data/lib/tilt/liquid.rb +9 -10
  20. data/lib/tilt/livescript.rb +23 -0
  21. data/lib/tilt/mapping.rb +293 -0
  22. data/lib/tilt/markaby.rb +1 -8
  23. data/lib/tilt/maruku.rb +22 -0
  24. data/lib/tilt/nokogiri.rb +1 -8
  25. data/lib/tilt/pandoc.rb +57 -0
  26. data/lib/tilt/plain.rb +0 -4
  27. data/lib/tilt/prawn.rb +43 -0
  28. data/lib/tilt/radius.rb +1 -8
  29. data/lib/tilt/rdiscount.rb +39 -0
  30. data/lib/tilt/rdoc.rb +3 -10
  31. data/lib/tilt/redcarpet.rb +86 -0
  32. data/lib/tilt/{textile.rb → redcloth.rb} +1 -8
  33. data/lib/tilt/rst-pandoc.rb +23 -0
  34. data/lib/tilt/sass.rb +78 -0
  35. data/lib/tilt/sigil.rb +34 -0
  36. data/lib/tilt/string.rb +1 -1
  37. data/lib/tilt/template.rb +121 -105
  38. data/lib/tilt/typescript.rb +26 -0
  39. data/lib/tilt/wikicloth.rb +22 -0
  40. data/lib/tilt/yajl.rb +1 -8
  41. data/lib/tilt.rb +118 -155
  42. metadata +38 -469
  43. data/CHANGELOG.md +0 -44
  44. data/Gemfile +0 -32
  45. data/HACKING +0 -16
  46. data/README.md +0 -232
  47. data/Rakefile +0 -104
  48. data/TEMPLATES.md +0 -516
  49. data/lib/tilt/css.rb +0 -80
  50. data/lib/tilt/markdown.rb +0 -214
  51. data/lib/tilt/wiki.rb +0 -58
  52. data/test/contest.rb +0 -68
  53. data/test/markaby/locals.mab +0 -1
  54. data/test/markaby/markaby.mab +0 -1
  55. data/test/markaby/markaby_other_static.mab +0 -1
  56. data/test/markaby/render_twice.mab +0 -1
  57. data/test/markaby/scope.mab +0 -1
  58. data/test/markaby/yielding.mab +0 -2
  59. data/test/tilt_asciidoctor_test.rb +0 -44
  60. data/test/tilt_blueclothtemplate_test.rb +0 -45
  61. data/test/tilt_buildertemplate_test.rb +0 -59
  62. data/test/tilt_cache_test.rb +0 -32
  63. data/test/tilt_coffeescripttemplate_test.rb +0 -114
  64. data/test/tilt_compilesite_test.rb +0 -51
  65. data/test/tilt_creoletemplate_test.rb +0 -28
  66. data/test/tilt_csv_test.rb +0 -69
  67. data/test/tilt_erbtemplate_test.rb +0 -239
  68. data/test/tilt_erubistemplate_test.rb +0 -151
  69. data/test/tilt_etannitemplate_test.rb +0 -173
  70. data/test/tilt_fallback_test.rb +0 -122
  71. data/test/tilt_hamltemplate_test.rb +0 -144
  72. data/test/tilt_kramdown_test.rb +0 -42
  73. data/test/tilt_lesstemplate_test.less +0 -1
  74. data/test/tilt_lesstemplate_test.rb +0 -42
  75. data/test/tilt_liquidtemplate_test.rb +0 -78
  76. data/test/tilt_markaby_test.rb +0 -88
  77. data/test/tilt_markdown_test.rb +0 -172
  78. data/test/tilt_marukutemplate_test.rb +0 -48
  79. data/test/tilt_nokogiritemplate_test.rb +0 -87
  80. data/test/tilt_radiustemplate_test.rb +0 -75
  81. data/test/tilt_rdiscounttemplate_test.rb +0 -55
  82. data/test/tilt_rdoctemplate_test.rb +0 -31
  83. data/test/tilt_redcarpettemplate_test.rb +0 -71
  84. data/test/tilt_redclothtemplate_test.rb +0 -36
  85. data/test/tilt_sasstemplate_test.rb +0 -41
  86. data/test/tilt_stringtemplate_test.rb +0 -170
  87. data/test/tilt_template_test.rb +0 -323
  88. data/test/tilt_test.rb +0 -65
  89. data/test/tilt_wikiclothtemplate_test.rb +0 -32
  90. data/test/tilt_yajltemplate_test.rb +0 -101
  91. data/tilt.gemspec +0 -120
data/lib/tilt.rb CHANGED
@@ -1,132 +1,111 @@
1
+ require 'tilt/mapping'
2
+ require 'tilt/template'
3
+
4
+ # Namespace for Tilt. This module is not intended to be included anywhere.
1
5
  module Tilt
2
- VERSION = '1.4.1'
6
+ # Current version.
7
+ VERSION = '2.0.11'
3
8
 
4
- @preferred_mappings = Hash.new
5
- @template_mappings = Hash.new { |h, k| h[k] = [] }
9
+ @default_mapping = Mapping.new
6
10
 
7
- # Hash of template path pattern => template implementation class mappings.
8
- def self.mappings
9
- @template_mappings
11
+ # @return [Tilt::Mapping] the main mapping object
12
+ def self.default_mapping
13
+ @default_mapping
10
14
  end
11
15
 
12
- def self.normalize(ext)
13
- ext.to_s.downcase.sub(/^\./, '')
16
+ # @private
17
+ def self.lazy_map
18
+ default_mapping.lazy_map
14
19
  end
15
20
 
16
- # Register a template implementation by file extension.
21
+ # @see Tilt::Mapping#register
17
22
  def self.register(template_class, *extensions)
18
- if template_class.respond_to?(:to_str)
19
- # Support register(ext, template_class) too
20
- extensions, template_class = [template_class], extensions[0]
21
- end
23
+ default_mapping.register(template_class, *extensions)
24
+ end
22
25
 
23
- extensions.each do |ext|
24
- ext = normalize(ext)
25
- mappings[ext].unshift(template_class).uniq!
26
- end
26
+ # @see Tilt::Mapping#register_lazy
27
+ def self.register_lazy(class_name, file, *extensions)
28
+ default_mapping.register_lazy(class_name, file, *extensions)
27
29
  end
28
30
 
29
- # Makes a template class preferred for the given file extensions. If you
30
- # don't provide any extensions, it will be preferred for all its already
31
- # registered extensions:
32
- #
33
- # # Prefer RDiscount for its registered file extensions:
34
- # Tilt.prefer(Tilt::RDiscountTemplate)
35
- #
36
- # # Prefer RDiscount only for the .md extensions:
37
- # Tilt.prefer(Tilt::RDiscountTemplate, '.md')
31
+ # @deprecated Use {register} instead.
38
32
  def self.prefer(template_class, *extensions)
39
- if extensions.empty?
40
- mappings.each do |ext, klasses|
41
- @preferred_mappings[ext] = template_class if klasses.include? template_class
42
- end
43
- else
44
- extensions.each do |ext|
45
- ext = normalize(ext)
46
- register(template_class, ext)
47
- @preferred_mappings[ext] = template_class
48
- end
49
- end
33
+ register(template_class, *extensions)
50
34
  end
51
35
 
52
- # Returns true when a template exists on an exact match of the provided file extension
36
+ # @see Tilt::Mapping#registered?
53
37
  def self.registered?(ext)
54
- mappings.key?(ext.downcase) && !mappings[ext.downcase].empty?
38
+ default_mapping.registered?(ext)
55
39
  end
56
40
 
57
- # Create a new template for the given file using the file's extension
58
- # to determine the the template mapping.
41
+ # @see Tilt::Mapping#new
59
42
  def self.new(file, line=nil, options={}, &block)
60
- if template_class = self[file]
61
- template_class.new(file, line, options, &block)
62
- else
63
- fail "No template engine registered for #{File.basename(file)}"
64
- end
43
+ default_mapping.new(file, line, options, &block)
65
44
  end
66
45
 
67
- # Lookup a template class for the given filename or file
68
- # extension. Return nil when no implementation is found.
46
+ # @see Tilt::Mapping#[]
69
47
  def self.[](file)
70
- pattern = file.to_s.downcase
71
- until pattern.empty? || registered?(pattern)
72
- pattern = File.basename(pattern)
73
- pattern.sub!(/^[^.]*\.?/, '')
74
- end
75
-
76
- # Try to find a preferred engine.
77
- preferred_klass = @preferred_mappings[pattern]
78
- return preferred_klass if preferred_klass
79
-
80
- # Fall back to the general list of mappings.
81
- klasses = @template_mappings[pattern]
82
-
83
- # Try to find an engine which is already loaded.
84
- template = klasses.detect do |klass|
85
- if klass.respond_to?(:engine_initialized?)
86
- klass.engine_initialized?
87
- end
88
- end
89
-
90
- return template if template
91
-
92
- # Try each of the classes until one succeeds. If all of them fails,
93
- # we'll raise the error of the first class.
94
- first_failure = nil
48
+ default_mapping[file]
49
+ end
95
50
 
96
- klasses.each do |klass|
97
- begin
98
- klass.new { '' }
99
- rescue Exception => ex
100
- first_failure ||= ex
101
- next
102
- else
103
- return klass
104
- end
105
- end
51
+ # @see Tilt::Mapping#template_for
52
+ def self.template_for(file)
53
+ default_mapping.template_for(file)
54
+ end
106
55
 
107
- raise first_failure if first_failure
56
+ # @see Tilt::Mapping#templates_for
57
+ def self.templates_for(file)
58
+ default_mapping.templates_for(file)
108
59
  end
109
60
 
110
- # Deprecated module.
111
- module CompileSite
61
+ # @return the template object that is currently rendering.
62
+ #
63
+ # @example
64
+ # tmpl = Tilt['index.erb'].new { '<%= Tilt.current_template %>' }
65
+ # tmpl.render == tmpl.to_s
66
+ #
67
+ # @note This is currently an experimental feature and might return nil
68
+ # in the future.
69
+ def self.current_template
70
+ Thread.current[:tilt_current_template]
112
71
  end
113
72
 
114
73
  # Extremely simple template cache implementation. Calling applications
115
74
  # create a Tilt::Cache instance and use #fetch with any set of hashable
116
75
  # arguments (such as those to Tilt.new):
117
- # cache = Tilt::Cache.new
118
- # cache.fetch(path, line, options) { Tilt.new(path, line, options) }
76
+ #
77
+ # cache = Tilt::Cache.new
78
+ # cache.fetch(path, line, options) { Tilt.new(path, line, options) }
119
79
  #
120
80
  # Subsequent invocations return the already loaded template object.
81
+ #
82
+ # @note
83
+ # Tilt::Cache is a thin wrapper around Hash. It has the following
84
+ # limitations:
85
+ # * Not thread-safe.
86
+ # * Size is unbounded.
87
+ # * Keys are not copied defensively, and should not be modified after
88
+ # being passed to #fetch. More specifically, the values returned by
89
+ # key#hash and key#eql? should not change.
90
+ # If this is too limiting for you, use a different cache implementation.
121
91
  class Cache
122
92
  def initialize
123
93
  @cache = {}
124
94
  end
125
95
 
96
+ # Caches a value for key, or returns the previously cached value.
97
+ # If a value has been previously cached for key then it is
98
+ # returned. Otherwise, block is yielded to and its return value
99
+ # which may be nil, is cached under key and returned.
100
+ # @yield
101
+ # @yieldreturn the value to cache for key
126
102
  def fetch(*key)
127
- @cache[key] ||= yield
103
+ @cache.fetch(key) do
104
+ @cache[key] = yield
105
+ end
128
106
  end
129
107
 
108
+ # Clears the cache.
130
109
  def clear
131
110
  @cache = {}
132
111
  end
@@ -135,70 +114,54 @@ module Tilt
135
114
 
136
115
  # Template Implementations ================================================
137
116
 
138
- require 'tilt/string'
139
- register StringTemplate, 'str'
140
-
141
- require 'tilt/erb'
142
- register ERBTemplate, 'erb', 'rhtml'
143
- register ErubisTemplate, 'erb', 'rhtml', 'erubis'
144
-
145
- require 'tilt/etanni'
146
- register EtanniTemplate, 'etn', 'etanni'
147
-
148
- require 'tilt/haml'
149
- register HamlTemplate, 'haml'
150
-
151
- require 'tilt/css'
152
- register SassTemplate, 'sass'
153
- register ScssTemplate, 'scss'
154
- register LessTemplate, 'less'
155
-
156
- require 'tilt/csv'
157
- register CSVTemplate, 'rcsv'
158
-
159
- require 'tilt/coffee'
160
- register CoffeeScriptTemplate, 'coffee'
161
-
162
- require 'tilt/nokogiri'
163
- register NokogiriTemplate, 'nokogiri'
164
-
165
- require 'tilt/builder'
166
- register BuilderTemplate, 'builder'
167
-
168
- require 'tilt/markaby'
169
- register MarkabyTemplate, 'mab'
170
-
171
- require 'tilt/liquid'
172
- register LiquidTemplate, 'liquid'
173
-
174
- require 'tilt/radius'
175
- register RadiusTemplate, 'radius'
176
-
177
- require 'tilt/markdown'
178
- register MarukuTemplate, 'markdown', 'mkd', 'md'
179
- register KramdownTemplate, 'markdown', 'mkd', 'md'
180
- register BlueClothTemplate, 'markdown', 'mkd', 'md'
181
- register RDiscountTemplate, 'markdown', 'mkd', 'md'
182
- register RedcarpetTemplate::Redcarpet1, 'markdown', 'mkd', 'md'
183
- register RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
184
- register RedcarpetTemplate, 'markdown', 'mkd', 'md'
185
-
186
- require 'tilt/textile'
187
- register RedClothTemplate, 'textile'
188
-
189
- require 'tilt/rdoc'
190
- register RDocTemplate, 'rdoc'
191
-
192
- require 'tilt/wiki'
193
- register CreoleTemplate, 'wiki', 'creole'
194
- register WikiClothTemplate, 'wiki', 'mediawiki', 'mw'
195
-
196
- require 'tilt/yajl'
197
- register YajlTemplate, 'yajl'
198
-
199
- require 'tilt/asciidoc'
200
- register AsciidoctorTemplate, 'ad', 'adoc', 'asciidoc'
201
-
202
- require 'tilt/plain'
203
- register PlainTemplate, 'html'
117
+ # ERB
118
+ register_lazy :ERBTemplate, 'tilt/erb', 'erb', 'rhtml'
119
+ register_lazy :ErubisTemplate, 'tilt/erubis', 'erb', 'rhtml', 'erubis'
120
+ register_lazy :ErubiTemplate, 'tilt/erubi', 'erb', 'rhtml', 'erubi'
121
+
122
+ # Markdown
123
+ register_lazy :BlueClothTemplate, 'tilt/bluecloth', 'markdown', 'mkd', 'md'
124
+ register_lazy :MarukuTemplate, 'tilt/maruku', 'markdown', 'mkd', 'md'
125
+ register_lazy :KramdownTemplate, 'tilt/kramdown', 'markdown', 'mkd', 'md'
126
+ register_lazy :RDiscountTemplate, 'tilt/rdiscount', 'markdown', 'mkd', 'md'
127
+ register_lazy :RedcarpetTemplate, 'tilt/redcarpet', 'markdown', 'mkd', 'md'
128
+ register_lazy :CommonMarkerTemplate, 'tilt/commonmarker', 'markdown', 'mkd', 'md'
129
+ register_lazy :PandocTemplate, 'tilt/pandoc', 'markdown', 'mkd', 'md'
130
+
131
+ # Rest (sorted by name)
132
+ register_lazy :AsciidoctorTemplate, 'tilt/asciidoc', 'ad', 'adoc', 'asciidoc'
133
+ register_lazy :BabelTemplate, 'tilt/babel', 'es6', 'babel', 'jsx'
134
+ register_lazy :BuilderTemplate, 'tilt/builder', 'builder'
135
+ register_lazy :CSVTemplate, 'tilt/csv', 'rcsv'
136
+ register_lazy :CoffeeScriptTemplate, 'tilt/coffee', 'coffee'
137
+ register_lazy :CoffeeScriptLiterateTemplate, 'tilt/coffee', 'litcoffee'
138
+ register_lazy :CreoleTemplate, 'tilt/creole', 'wiki', 'creole'
139
+ register_lazy :EtanniTemplate, 'tilt/etanni', 'etn', 'etanni'
140
+ register_lazy :HamlTemplate, 'tilt/haml', 'haml'
141
+ register_lazy :LessTemplate, 'tilt/less', 'less'
142
+ register_lazy :LiquidTemplate, 'tilt/liquid', 'liquid'
143
+ register_lazy :LiveScriptTemplate, 'tilt/livescript','ls'
144
+ register_lazy :MarkabyTemplate, 'tilt/markaby', 'mab'
145
+ register_lazy :NokogiriTemplate, 'tilt/nokogiri', 'nokogiri'
146
+ register_lazy :PlainTemplate, 'tilt/plain', 'html'
147
+ register_lazy :PrawnTemplate, 'tilt/prawn', 'prawn'
148
+ register_lazy :RDocTemplate, 'tilt/rdoc', 'rdoc'
149
+ register_lazy :RadiusTemplate, 'tilt/radius', 'radius'
150
+ register_lazy :RedClothTemplate, 'tilt/redcloth', 'textile'
151
+ register_lazy :RstPandocTemplate, 'tilt/rst-pandoc', 'rst'
152
+ register_lazy :SassTemplate, 'tilt/sass', 'sass'
153
+ register_lazy :ScssTemplate, 'tilt/sass', 'scss'
154
+ register_lazy :SigilTemplate, 'tilt/sigil', 'sigil'
155
+ register_lazy :StringTemplate, 'tilt/string', 'str'
156
+ register_lazy :TypeScriptTemplate, 'tilt/typescript', 'ts', 'tsx'
157
+ register_lazy :WikiClothTemplate, 'tilt/wikicloth', 'wiki', 'mediawiki', 'mw'
158
+ register_lazy :YajlTemplate, 'tilt/yajl', 'yajl'
159
+
160
+ # External template engines
161
+ register_lazy 'Slim::Template', 'slim', 'slim'
162
+ register_lazy 'Tilt::HandlebarsTemplate', 'tilt/handlebars', 'handlebars', 'hbs'
163
+ register_lazy 'Tilt::OrgTemplate', 'org-ruby', 'org'
164
+ register_lazy 'Tilt::EmacsOrgTemplate', 'tilt/emacs_org', 'org'
165
+ register_lazy 'Opal::Processor', 'opal', 'opal', 'rb'
166
+ register_lazy 'Tilt::JbuilderTemplate', 'tilt/jbuilder', 'jbuilder'
204
167
  end