jader 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/Gemfile +11 -0
- data/LICENSE +22 -0
- data/README.md +356 -0
- data/Rakefile +4 -0
- data/jader.gemspec +28 -0
- data/lib/jader.rb +10 -0
- data/lib/jader/compiler.rb +70 -0
- data/lib/jader/configuration.rb +19 -0
- data/lib/jader/engine.rb +11 -0
- data/lib/jader/renderer.rb +30 -0
- data/lib/jader/serialize.rb +82 -0
- data/lib/jader/source.rb +19 -0
- data/lib/jader/template.rb +29 -0
- data/lib/jader/version.rb +3 -0
- data/spec/compiler_spec.rb +32 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +9 -0
- data/spec/dummy/app/assets/javascripts/includes/util.js +8 -0
- data/spec/dummy/app/assets/javascripts/mixins/application_mixins.jade +3 -0
- data/spec/dummy/app/assets/javascripts/mixins/users_mixins.jade +3 -0
- data/spec/dummy/app/assets/javascripts/sample.jst.jade +5 -0
- data/spec/dummy/app/assets/javascripts/views/users/index.jst.jade +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +10 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/user.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/users/index.jade +4 -0
- data/spec/dummy/app/views/users/show.jade +5 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +65 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +7 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +31 -0
- data/spec/dummy/config/environments/production.rb +64 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/jader.rb +4 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +10 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/db/migrate/20120816043248_user.rb +11 -0
- data/spec/dummy/db/schema.rb +21 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/integration/renderer_spec.rb +59 -0
- data/spec/source_spec.rb +19 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/global.rb +7 -0
- data/spec/support/matchers.rb +17 -0
- data/spec/template_spec.rb +40 -0
- data/vendor/assets/javascripts/jade/jade.js +3168 -0
- data/vendor/assets/javascripts/jade/runtime.js +128 -0
- metadata +278 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2012 Round Lake, inc.,
|
4
|
+
Peter Zotov <whitequark@whitequark.org>.
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,356 @@
|
|
1
|
+
# Jader - Share your Jade templates between client and server
|
2
|
+
|
3
|
+
Jade is a very popular templating engine for Node.js. This gem gives
|
4
|
+
you ability to easily use Jade templates for both server and client side in your Rails project.
|
5
|
+
|
6
|
+
On the client-side your templates should be used with Rails' JST engine. On the server side, you can render your
|
7
|
+
Jade templates as Rails views (similar to how you'd render ERB or HAML templates).
|
8
|
+
|
9
|
+
## Writing your templates
|
10
|
+
|
11
|
+
Lets assume you have a users controller `app/controllers/users_controller.rb` which in turn renders a list of users.
|
12
|
+
We'd like to share that view between the client and the server.
|
13
|
+
|
14
|
+
### Server-Side code
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
class UsersController < ApplicationController
|
18
|
+
|
19
|
+
def index
|
20
|
+
@users = User.all
|
21
|
+
respond_to do |format|
|
22
|
+
format.html
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
To share our template between client and server, we need to place it under `app/assets/javascripts` for Sprockets' JST engine.
|
30
|
+
|
31
|
+
Lets create a views directory for our shared templates `app/assets/javascripts/views` and add our template there, following Rails' naming convensions.
|
32
|
+
|
33
|
+
The full template path should look like this: `app/assets/javascripts/views/users/index.jst.jade`
|
34
|
+
|
35
|
+
### Template code
|
36
|
+
|
37
|
+
The most significant differences between using standard server-side Ruby-based engines like ERB or HAML and using Jader are:
|
38
|
+
|
39
|
+
* No access to server-side view helpers (such as url_for)
|
40
|
+
* No ruby-style instance variable like `@users`
|
41
|
+
* Template code is Javascript, not Ruby and has to follow Jade's syntax rather than embedded Ruby syntax
|
42
|
+
* No partials or includes
|
43
|
+
|
44
|
+
Our template code should look like this:
|
45
|
+
|
46
|
+
```
|
47
|
+
ul.users
|
48
|
+
each user in users
|
49
|
+
li.user= user.name
|
50
|
+
```
|
51
|
+
|
52
|
+
Note that rendering this template server-side, will be done inside your application's layout. You can write your views layout file in ERB / HAML
|
53
|
+
and the call to `=yield` will render your Jade template above.
|
54
|
+
|
55
|
+
### Sharing template code
|
56
|
+
|
57
|
+
Since Rails doesn't expect server-side templates to live under `app/assets` we need to add our client-side views path to Rails views lookup path.
|
58
|
+
|
59
|
+
Inside your `application_controller.rb` add the following:
|
60
|
+
|
61
|
+
```
|
62
|
+
class ApplicationController < ActionController::Base
|
63
|
+
protect_from_forgery
|
64
|
+
|
65
|
+
before_filter :prepend_view_paths
|
66
|
+
|
67
|
+
# .... application controller code
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
# Adds Client-Side views to render path
|
72
|
+
def prepend_view_paths
|
73
|
+
prepend_view_path Rails.root.join('app', 'assets', 'javascripts', , 'views')
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
This is a ad-hoc solution that should change in the near future.
|
81
|
+
|
82
|
+
### Client-side code
|
83
|
+
|
84
|
+
To render the same template from the client, we need to fetch our users list from the server and then call our JST template with that list.
|
85
|
+
|
86
|
+
First, lets change our controller to return a JSON formatted list of users when called from the client:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class UsersController < ApplicationController
|
90
|
+
|
91
|
+
def index
|
92
|
+
@users = User.all
|
93
|
+
respond_to do |format|
|
94
|
+
format.html
|
95
|
+
format.json {
|
96
|
+
render :json => @users.to_jade
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Note the call to `to_jade` on the `@users` collection. This ensures our users are properly serialized for use inside our template.
|
105
|
+
See the [Serialization](https://github.com/zohararad/jader#serialization) section below for more details.
|
106
|
+
|
107
|
+
In our `application.js` file lets write the following:
|
108
|
+
|
109
|
+
```javascript
|
110
|
+
//= require jade/runtime
|
111
|
+
//= require views/users/index
|
112
|
+
|
113
|
+
$.getJSON('/users', function(users){
|
114
|
+
$('body').html(JST['views/users/index']({users:users}));
|
115
|
+
});
|
116
|
+
```
|
117
|
+
|
118
|
+
## Serialization
|
119
|
+
|
120
|
+
To help Jader access Ruby and Rails variables inside the template, we need to employ some sort of JSON serializing before passing
|
121
|
+
these variables to the template. On the server-side, this happens automagically before the template is rendered.
|
122
|
+
|
123
|
+
Internally, Jader will try to call the `to_jade` method on each instance variable that's passed to the template. Ruby's Hash, Array and Object classes have been extended
|
124
|
+
to support this functionality. Arrays and Hashes will attempt to call the `to_jade` method on their members when `to_jade` is invoked on their instances. For
|
125
|
+
other collection-like variables, the `to_jade` method will only be invoked if they respond to a `to_a` method. This allows ActiveModel / ActiveRecord instance variables to
|
126
|
+
automatically serialize their members before rendering.
|
127
|
+
|
128
|
+
### Serializing models
|
129
|
+
|
130
|
+
Jader does not assume your Rails models should be serialized by default. Instead, it expects you to enable serializing on desired models explicitly.
|
131
|
+
|
132
|
+
To enable this behaviour, consider the following example:
|
133
|
+
|
134
|
+
```
|
135
|
+
class User < ActiveRecord::Base
|
136
|
+
|
137
|
+
include Jader::Serialize
|
138
|
+
|
139
|
+
jade_serializable :name, :email, :favorites, :merge => false
|
140
|
+
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
The call to `include Jader::Serialize` mixes Jader::Serializer capabilities into our model class.
|
145
|
+
|
146
|
+
We can then tell the serializer which attributes we'd like to serialize, and how we'd like the serialization to work.
|
147
|
+
|
148
|
+
By default, calling `jade_serializable` with no arguments will serialize all your model attributes. Lets look at two examples:
|
149
|
+
|
150
|
+
Consider the following code:
|
151
|
+
|
152
|
+
```
|
153
|
+
# define our model
|
154
|
+
class User < ActiveRecord::Base
|
155
|
+
|
156
|
+
include Jader::Serialize
|
157
|
+
|
158
|
+
jade_serializable
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
# access in controller
|
163
|
+
class UsersController < ApplicationController
|
164
|
+
|
165
|
+
def index
|
166
|
+
@users = User.all
|
167
|
+
@users.to_jade # => all available user attributes (users table columns) will be serialized
|
168
|
+
end
|
169
|
+
|
170
|
+
def active
|
171
|
+
@users = User.where('active = 1').select('name, email')
|
172
|
+
@users.to_jade # => only name and email attributes are serialized
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
For better control over which attributes are serialized, and when serializing model relationships, we can tell the serializer
|
178
|
+
which attributes should always be serialized, and whether we'd like these attributes to be merged with the default attributes or not.
|
179
|
+
|
180
|
+
Consider the following code:
|
181
|
+
|
182
|
+
|
183
|
+
```
|
184
|
+
# define our models
|
185
|
+
|
186
|
+
class Favorite < ActiveRecord::Base
|
187
|
+
|
188
|
+
include Jader::Serialize
|
189
|
+
|
190
|
+
jade_serializable
|
191
|
+
|
192
|
+
belongs_to :user
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
class User < ActiveRecord::Base
|
197
|
+
|
198
|
+
include Jader::Serialize
|
199
|
+
|
200
|
+
jade_serializable :favorites, :merge => true
|
201
|
+
|
202
|
+
has_many :favorites
|
203
|
+
end
|
204
|
+
|
205
|
+
# access in controller
|
206
|
+
class UsersController < ApplicationController
|
207
|
+
|
208
|
+
def active
|
209
|
+
@users = User.where('active = 1').select('name, email')
|
210
|
+
@users.to_jade # => only name, email and favorites attributes are serialized
|
211
|
+
end
|
212
|
+
end
|
213
|
+
```
|
214
|
+
|
215
|
+
In the above, we defined serialization for the `User` model to include the `:favorites` attribute, which is available because of the `has_many` relationship
|
216
|
+
to the `Favorite` model. Additionally, we specified that serialization should merge model default attributes with the specified attributes, by setting `:merge => true` .
|
217
|
+
|
218
|
+
This will result in merging `self.attributes` and `self.favorites` on any instance of the `User` model when calling the `to_jade` method on it.
|
219
|
+
|
220
|
+
To only serialize the specified attributes, call `jade_serializable` with `:merge => false` .
|
221
|
+
|
222
|
+
Invokation format for `jade_serializable` is:
|
223
|
+
|
224
|
+
```
|
225
|
+
jade_serializable :attr1, :attr2, :attr3 ...., :merge => true/false
|
226
|
+
```
|
227
|
+
|
228
|
+
By default, `jade_serializable` will operate with `:merge => true` and merge instnace attributes with specified attributes.
|
229
|
+
|
230
|
+
## Mixins
|
231
|
+
|
232
|
+
Jade has built in support for template mixins. Jader allows you to define and share mixins inside your templates.
|
233
|
+
|
234
|
+
Following Rails helpers conventions, your Jade mixins can be defined either as application-level mixins or as controller-level mixins.
|
235
|
+
|
236
|
+
Assuming we have a users controller, we can add mixins by creating a mixins folder under `app/assets/javascripts` and adding:
|
237
|
+
|
238
|
+
* `app/assets/javascripts/helpers/application_mixins.jade`
|
239
|
+
* `app/assets/javascripts/helpers/users_mixins.jade`
|
240
|
+
|
241
|
+
Mixins that are defined inside `application_mixins.jade` will be available in all templates. This is a perfect place for example to add a pagination mixin.
|
242
|
+
|
243
|
+
Controller-level mixins are defined inside `CONTROLLER_NAME_mixins.jade` and are available only inside the relevant controller. Since the
|
244
|
+
client-side has no notion of which controller is currently being rendered, we need to strictly follow Rails naming conventions as follows:
|
245
|
+
|
246
|
+
* Client-side views subdirectories are named after their controller. For example, for `UsersController` we will have `app/assets/javascripts/views/users`
|
247
|
+
* Client-side Jade mixins files are resolved based on view name. `mixins/users_mixins.jade` will be available for views inside `views/users` folder
|
248
|
+
|
249
|
+
To enable Jader's mixins capabilities we need to configure Jader and tell it where to look for mixins files:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
Jader.configure do |config|
|
253
|
+
config.mixins_path = Rails.root.join('app','assets','javascripts','mixins')
|
254
|
+
end
|
255
|
+
```
|
256
|
+
|
257
|
+
### Notes on performance
|
258
|
+
|
259
|
+
On the client-side, Jader will add the mixins code into your Jade JST template. This can potentially increase your client-side file size dramatically. Since application-level
|
260
|
+
mixins are included in each and every template, please be sure to keep them to a bare minimum.
|
261
|
+
|
262
|
+
## Javascript inclusion
|
263
|
+
|
264
|
+
Often we'll have additional Javascript that is included client-side and adds more functionality to our client-side application. Two such examples could be
|
265
|
+
using `I18n.js` for internationalization, or `Date.js` for better date handling.
|
266
|
+
|
267
|
+
Since our server-side templates cannot access this code, we need to figure out a way to share arbitrary Javascript from our client in the server.
|
268
|
+
|
269
|
+
This is where inclusions come in.
|
270
|
+
|
271
|
+
We can tell Jader to add arbitrary pieces of raw Javascript to the server-side rendering context before evaluating our template like so:
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
Jader.configure do |config|
|
275
|
+
config.includes << IO.read(Rails.root.join('app','assets','javascripts','includes','util.js'))
|
276
|
+
end
|
277
|
+
```
|
278
|
+
|
279
|
+
`Jader::Configuration.includes` is an array that accepts raw Javascript strings that are, in turn, passed to the server-side template evaluation context.
|
280
|
+
|
281
|
+
To give a more pragmatic example of using Jader inclusions, lets try using `I18n.js` on both server and client.
|
282
|
+
|
283
|
+
For the sake of this example, we assume `gem 'i18n-js'` is installed in our application.
|
284
|
+
|
285
|
+
Our `application.js` file will then include:
|
286
|
+
|
287
|
+
```
|
288
|
+
//= require i18n
|
289
|
+
//= require i18n/translations
|
290
|
+
```
|
291
|
+
|
292
|
+
The first require is made available via `i18n-js` vendorized assets while the second require is a translations file inside our `app/assets/javascripts/i18n` folder.
|
293
|
+
|
294
|
+
To enable I18n support when rendering templates on the server, we configure Jader as follows:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
Jader.configure do |config|
|
298
|
+
# wait for assets to be ready
|
299
|
+
Rails.application.config.after_initialize do
|
300
|
+
# inject assets source into Jader's includes array
|
301
|
+
config.includes << Rails.application.assets['i18n'].source
|
302
|
+
config.includes << Rails.application.assets['i18n/translations'].source
|
303
|
+
config.includes << "I18n.defaultLocale = 'en'; I18n.locale = 'en';"
|
304
|
+
end
|
305
|
+
end
|
306
|
+
```
|
307
|
+
|
308
|
+
## Configuration
|
309
|
+
|
310
|
+
Its recommended to configure Jader inside a Rails initializer so that configuration is defined at boot time.
|
311
|
+
|
312
|
+
Assuming we have an initializer `app/config/initializers/jader.rb` it should include:
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
Jader.configure do |config|
|
316
|
+
config.mixins_path = Rails.root.join('app','assets','javascripts','mixins')
|
317
|
+
# Use some javascript from a file that's not available in the asset pipeline
|
318
|
+
config.includes << IO.read(Rails.root.join('app','assets','javascripts','includes','util.js'))
|
319
|
+
# wait for assets to be ready
|
320
|
+
Rails.application.config.after_initialize do
|
321
|
+
# include javascripts available only from asset pipeline
|
322
|
+
config.includes << Rails.application.assets['util'].source
|
323
|
+
end
|
324
|
+
end
|
325
|
+
```
|
326
|
+
|
327
|
+
## Kudos
|
328
|
+
|
329
|
+
Jader is built upon the wonderful work of [Boris Staal](https://github.com/roundlake/jade/) and draws heavily from:
|
330
|
+
|
331
|
+
- [ice](https://github.com/ludicast/ice)
|
332
|
+
- [ruby-haml-js](https://github.com/dnagir/ruby-haml-js)
|
333
|
+
- [tilt-jade](https://github.com/therabidbanana/tilt-jade)
|
334
|
+
|
335
|
+
Boris Staal's Jade Rubygem It was developed as a successor to tilt-jade to improve following:
|
336
|
+
|
337
|
+
* Add debugging capabilities (slightly different build technique)
|
338
|
+
* Support exact Jade.JS lib without modifications
|
339
|
+
* Do not hold 3 copies of Jade.JS internally
|
340
|
+
* Be well-covered with RSpec
|
341
|
+
|
342
|
+
## Credits
|
343
|
+
|
344
|
+
<img src="http://roundlake.ru/assets/logo.png" align="right" />
|
345
|
+
|
346
|
+
* Boris Staal ([@_inossidabile](http://twitter.com/#!/_inossidabile))
|
347
|
+
|
348
|
+
# License
|
349
|
+
|
350
|
+
Copyright (c) 2012 Zohar Arad <zohar@zohararad.com>
|
351
|
+
|
352
|
+
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:
|
353
|
+
|
354
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
355
|
+
|
356
|
+
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 OR COPYRIGHT HOLDERS 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.
|
data/Rakefile
ADDED
data/jader.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "jader/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "jader"
|
7
|
+
s.version = Jader::VERSION
|
8
|
+
s.authors = ["Zohar Arad", "Boris Staal"]
|
9
|
+
s.email = ["zohar@zohararad.com" "boris@roundlake.ru"]
|
10
|
+
s.homepage = "https://github.com/zohararad/jader"
|
11
|
+
s.summary = %q{JST and Rails views compiler for Jade templates}
|
12
|
+
s.description = %q{Share your Jade views between client and server, eliminate code duplication and make your single-page app SEO friendly}
|
13
|
+
|
14
|
+
s.rubyforge_project = "jader"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'execjs'
|
22
|
+
s.add_dependency 'tilt'
|
23
|
+
s.add_dependency 'sprockets'
|
24
|
+
s.add_development_dependency 'rspec'
|
25
|
+
s.add_development_dependency 'rspec-rails'
|
26
|
+
s.add_development_dependency 'rails', '>= 3.1'
|
27
|
+
s.add_development_dependency 'pry'
|
28
|
+
end
|
data/lib/jader.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'jader/source'
|
2
|
+
require 'jader/compiler'
|
3
|
+
require 'jader/template'
|
4
|
+
require 'jader/renderer'
|
5
|
+
require 'jader/configuration'
|
6
|
+
require 'jader/serialize'
|
7
|
+
require 'jader/engine' if defined?(::Rails)
|
8
|
+
|
9
|
+
ActionView::Template.register_template_handler :jade, Jader::Renderer
|
10
|
+
ActionView::Template.register_template_handler 'jst.jade', Jader::Renderer
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
|
3
|
+
module Jader
|
4
|
+
class Compiler
|
5
|
+
attr_accessor :options
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
@options = {
|
9
|
+
:client => true,
|
10
|
+
:compileDebug => false
|
11
|
+
}.merge options
|
12
|
+
end
|
13
|
+
|
14
|
+
def source
|
15
|
+
@source ||= %{
|
16
|
+
var window = {};
|
17
|
+
#{Jader::Source::jade}
|
18
|
+
var jade = window.jade;
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def context
|
23
|
+
@context ||= ExecJS.compile source
|
24
|
+
end
|
25
|
+
|
26
|
+
def jade_version
|
27
|
+
context.eval("jade.version")
|
28
|
+
end
|
29
|
+
|
30
|
+
def compile(template, file_name = '')
|
31
|
+
template = template.read if template.respond_to?(:read)
|
32
|
+
file_name.match(/views\/([^\/]+)\//)
|
33
|
+
controller_name = $1 || nil
|
34
|
+
combo = (template_mixins(controller_name) << template).join("\n").to_json
|
35
|
+
tmpl = context.eval("jade.precompile(#{combo}, #{@options.to_json})")
|
36
|
+
|
37
|
+
%{
|
38
|
+
function(locals){
|
39
|
+
#{tmpl}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def render(template, controller_name, vars = {})
|
45
|
+
combo = (template_mixins(controller_name) << template).join("\n").to_json
|
46
|
+
tmpl = context.eval("jade.precompile(#{combo}, #{@options.to_json})")
|
47
|
+
context.eval(%{
|
48
|
+
function(locals){
|
49
|
+
#{Jader::Source::runtime}
|
50
|
+
#{Jader.configuration.includes.join("\n")}
|
51
|
+
#{tmpl}
|
52
|
+
}.call(null,#{vars.to_jade.to_json})
|
53
|
+
})
|
54
|
+
end
|
55
|
+
|
56
|
+
def template_mixins(controller_name)
|
57
|
+
mixins = []
|
58
|
+
unless Jader.configuration.mixins_path.nil?
|
59
|
+
Dir["#{Jader.configuration.mixins_path}/*.jade"].each do |f|
|
60
|
+
base_name = File.basename(f)
|
61
|
+
if base_name == '%s_mixins.jade' % controller_name || base_name == 'application_mixins.jade'
|
62
|
+
mixins << IO.read(f)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
mixins
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|