less_assets 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +194 -0
- data/lib/less_assets/engine.rb +21 -0
- data/lib/less_assets/less_template.rb +67 -0
- data/lib/less_assets/version.rb +5 -0
- data/lib/less_assets.rb +18 -0
- data/lib/tasks/less_assets.tasks +4 -0
- data/vendor/assets/javascripts/less.js +3478 -0
- data/vendor/assets/javascripts/less_assets.js.coffee +22 -0
- metadata +218 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011-2012 Michael Kessler <michi@netzpiraten.ch>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# Less Assets [![Build Status](https://secure.travis-ci.org/netzpirat/less_assets.png)](http://travis-ci.org/netzpirat/less_assets)
|
2
|
+
|
3
|
+
[Less](http://lesscss.org/) JavaScript Style Templates (JSST) in the Rails asset pipeline or as Tilt template.
|
4
|
+
It's like a JavaScript template, but for generating dynamic CSS styles instead of HTML.
|
5
|
+
|
6
|
+
If you just want to render your Less stylesheets in the Rails backend and deliver the CSS through the asset pipeline,
|
7
|
+
have a look at [Less Rails](https://github.com/metaskills/less-rails).
|
8
|
+
|
9
|
+
Tested on MRI Ruby 1.8.7, 1.9.2, 1.9.3, REE and the latest version of JRuby.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
The simplest way to install Less Assets is to use [Bundler](http://gembundler.com/).
|
14
|
+
Add `less_assets` to your `Gemfile`:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
group :assets do
|
18
|
+
gem 'less_assets'
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
And require the `less` and `less_assets` in your `app/assets/javascripts/application.js.coffee`:
|
23
|
+
|
24
|
+
```coffeescript
|
25
|
+
#= require less
|
26
|
+
#= require less_assets
|
27
|
+
```
|
28
|
+
|
29
|
+
This provides the less.js parser to parse your stylesheets in the browser and the LessAssets renderer for adding the
|
30
|
+
variables at render time.
|
31
|
+
|
32
|
+
Please have a look at the [CHANGELOG](https://github.com/netzpirat/less_assets/blob/master/CHANGELOG.md) when
|
33
|
+
upgrading to a newer Less Assets version.
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
You can place all your Less templates in the `app/assets/javascripts/styles` directory and include them from your
|
38
|
+
`app/assets/javascripts/application.js.coffee`:
|
39
|
+
|
40
|
+
```coffeescript
|
41
|
+
#= require_tree ./styles
|
42
|
+
```
|
43
|
+
|
44
|
+
Because Less Assets provides a default template name filter, the `styles/`, `stylesheet/` and `templates/` prefix will
|
45
|
+
be automatically removed.
|
46
|
+
|
47
|
+
## Configuration
|
48
|
+
|
49
|
+
Sprockets will cache your templates after compiling and will only recompile them when the content of the template has
|
50
|
+
changed, thus if you change to your configuration, the new settings will not be applied to templates already compiled.
|
51
|
+
You can clear the Sprockets cache with:
|
52
|
+
|
53
|
+
```Bash
|
54
|
+
rake assets:clean
|
55
|
+
```
|
56
|
+
|
57
|
+
### Template namespace
|
58
|
+
|
59
|
+
By default all Less templates are registered under the `JSST` namespace, which stands for JavaScript style template.
|
60
|
+
If you prefer another namespace, you can set it in an initializer:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
LessAssets::LessTemplate.namespace = `window.Styles`
|
64
|
+
```
|
65
|
+
|
66
|
+
### Template name
|
67
|
+
|
68
|
+
The name under which the template can be addressed in the namespace depends not only from the filename, but also on
|
69
|
+
the directory name by default.
|
70
|
+
|
71
|
+
The following examples assumes a configured namespace `window.JSST` and the asset template directory
|
72
|
+
`app/assets/javascripts/styles`:
|
73
|
+
|
74
|
+
* `app/assets/javascripts/styles/document.lesst` will become `JSST['document']`
|
75
|
+
* `app/assets/javascripts/styles/editor/zone.lesst` will become `JSST['editor/zone']`
|
76
|
+
* `app/assets/javascripts/styles/shared/general/headers.lesst` will become `JSST['shared/general/headers']`
|
77
|
+
|
78
|
+
#### Template name filter
|
79
|
+
|
80
|
+
If you wish to put the templates in a different location, you may want to modify `name_filter` in an initializer.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
LessAssets::LessTemplate.name_filter = lambda { |n| n.sub /^(templates|styles|stylesheets)\//, '' }
|
84
|
+
```
|
85
|
+
|
86
|
+
By default, `name_filter` strips the leading `templates/`, `stylesheets/` and `styles/` directory off of the name.
|
87
|
+
|
88
|
+
## Render
|
89
|
+
|
90
|
+
When you have a template `app/assets/javascripts/style/header.lesst` with the given content:
|
91
|
+
|
92
|
+
```scss
|
93
|
+
.header (@r) {
|
94
|
+
padding: @r * 2;
|
95
|
+
border-radius: @r;
|
96
|
+
}
|
97
|
+
|
98
|
+
.header (@r) when (@r > 10) {
|
99
|
+
margin-top: 3 * @r;
|
100
|
+
}
|
101
|
+
|
102
|
+
#header {
|
103
|
+
.header(@radius);
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
107
|
+
You can render the style template and pass the variables to be used:
|
108
|
+
|
109
|
+
```javascript
|
110
|
+
JSST['header']({ radius: '10px' })
|
111
|
+
```
|
112
|
+
|
113
|
+
which will return in the following CSS
|
114
|
+
|
115
|
+
```CSS
|
116
|
+
#header {
|
117
|
+
margin: 20px;
|
118
|
+
border-radius: 10px;
|
119
|
+
}
|
120
|
+
```
|
121
|
+
|
122
|
+
whereas rendering
|
123
|
+
|
124
|
+
```javascript
|
125
|
+
JSST['header']({ radius: '20px' })
|
126
|
+
```
|
127
|
+
|
128
|
+
will result in
|
129
|
+
|
130
|
+
```CSS
|
131
|
+
#header {
|
132
|
+
padding: 40px;
|
133
|
+
border-radius: 20px;
|
134
|
+
margin-top: 60px;
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
It's up to you to apply the generated CSS to a style element, Less Assets doesn't provide any helper methods yet.
|
139
|
+
|
140
|
+
## Author
|
141
|
+
|
142
|
+
Developed by Michael Kessler, sponsored by [mksoft.ch](https://mksoft.ch).
|
143
|
+
|
144
|
+
If you like Less Assets, you can watch the repository at [GitHub](https://github.com/netzpirat/less_assets) and
|
145
|
+
follow [@netzpirat](https://twitter.com/#!/netzpirat) on Twitter for project updates.
|
146
|
+
|
147
|
+
## Development
|
148
|
+
|
149
|
+
* Issues and feature request hosted at [GitHub Issues](https://github.com/netzpirat/less_assets/issues).
|
150
|
+
* Documentation hosted at [RubyDoc](http://rubydoc.info/github/netzpirat/less_assets/master/frames).
|
151
|
+
* Source hosted at [GitHub](https://github.com/netzpirat/less_assets).
|
152
|
+
|
153
|
+
Pull requests are very welcome! Please try to follow these simple rules if applicable:
|
154
|
+
|
155
|
+
* Please create a topic branch for every separate change you make.
|
156
|
+
* Make sure your patches are well tested. All specs must pass.
|
157
|
+
* Update the [Yard](http://yardoc.org/) documentation.
|
158
|
+
* Update the README.
|
159
|
+
* Update the CHANGELOG for noteworthy changes.
|
160
|
+
* Please **do not change** the version number.
|
161
|
+
|
162
|
+
## Contributors
|
163
|
+
|
164
|
+
See the [CHANGELOG](https://github.com/netzpirat/less_assets/blob/master/CHANGELOG.md) and the GitHub list of
|
165
|
+
[contributors](https://github.com/netzpirat/less_assets/contributors).
|
166
|
+
|
167
|
+
## Acknowledgement
|
168
|
+
|
169
|
+
* [Alexis Sellier](http://twitter.com/cloudhead) for creating Less, the dynamic stylesheet language.
|
170
|
+
|
171
|
+
## License
|
172
|
+
|
173
|
+
(The MIT License)
|
174
|
+
|
175
|
+
Copyright (c) 2012 Michael Kessler
|
176
|
+
|
177
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
178
|
+
a copy of this software and associated documentation files (the
|
179
|
+
'Software'), to deal in the Software without restriction, including
|
180
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
181
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
182
|
+
permit persons to whom the Software is furnished to do so, subject to
|
183
|
+
the following conditions:
|
184
|
+
|
185
|
+
The above copyright notice and this permission notice shall be
|
186
|
+
included in all copies or substantial portions of the Software.
|
187
|
+
|
188
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
189
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
190
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
191
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
192
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
193
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
194
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
module LessAssets
|
4
|
+
|
5
|
+
# Hook the less template into a Rails app.
|
6
|
+
#
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
|
9
|
+
config.less_assets = ActiveSupport::OrderedOptions.new
|
10
|
+
|
11
|
+
# Initialize Haml Coffee Assets after Sprockets
|
12
|
+
#
|
13
|
+
initializer 'sprockets.lessassets', :group => :all, :after => 'sprockets.environment' do |app|
|
14
|
+
next unless app.assets
|
15
|
+
|
16
|
+
# Register tilt template
|
17
|
+
app.assets.register_engine '.lesst', LessTemplate
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require 'tilt'
|
4
|
+
|
5
|
+
module LessAssets
|
6
|
+
|
7
|
+
# Less template implementation for Tilt.
|
8
|
+
#
|
9
|
+
class LessTemplate < Tilt::Template
|
10
|
+
|
11
|
+
class << self
|
12
|
+
# A proc that is called to modify the template name used as the
|
13
|
+
# JST key. The proc is passed the name as an argument and should
|
14
|
+
# return the modified name (or unmodified) name.
|
15
|
+
attr_accessor :name_filter
|
16
|
+
|
17
|
+
# The JavaScript Style template namespace
|
18
|
+
attr_accessor :namespace
|
19
|
+
end
|
20
|
+
|
21
|
+
# By default the namespace is JSST (JavaScript Style template)
|
22
|
+
self.namespace = 'window.JSST'
|
23
|
+
|
24
|
+
# By default, remove any leading `templates/`, `styles/` and `stylesheets/` in the name
|
25
|
+
self.name_filter = lambda { |n| n.sub /^(templates|styles|stylesheets)\//, '' }
|
26
|
+
|
27
|
+
# The default mime type of the tilt template
|
28
|
+
self.default_mime_type = 'application/javascript'
|
29
|
+
|
30
|
+
# Test if the compiler is initialized.
|
31
|
+
#
|
32
|
+
# @return [Boolean] the initialization status
|
33
|
+
#
|
34
|
+
def self.engine_initialized?
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
# Initialize the template engine.
|
39
|
+
#
|
40
|
+
def initialize_engine
|
41
|
+
end
|
42
|
+
|
43
|
+
# Prepare the template
|
44
|
+
#
|
45
|
+
def prepare
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generates the Less template wrapped in a JavaScript template
|
49
|
+
#
|
50
|
+
# @param [String] name the template name
|
51
|
+
# @return [String] the less JavaScript template
|
52
|
+
#
|
53
|
+
def evaluate(scope, locals = { }, &block)
|
54
|
+
name = scope.logical_path
|
55
|
+
name = self.class.name_filter.call(name) if self.class.name_filter
|
56
|
+
|
57
|
+
<<-JST
|
58
|
+
(function() {
|
59
|
+
#{ self.class.namespace } || (#{ self.class.namespace } = {});
|
60
|
+
#{ self.class.namespace }['#{ name }'] = function(v) { return LessAssets.render(\"#{ data.gsub(/\n/, "\\n") }\", v); };
|
61
|
+
}).call(this);
|
62
|
+
JST
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/lib/less_assets.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
require 'tilt'
|
6
|
+
require 'sprockets'
|
7
|
+
|
8
|
+
require 'less_assets/version'
|
9
|
+
require 'less_assets/less_template'
|
10
|
+
|
11
|
+
if defined?(Rails)
|
12
|
+
require 'rails'
|
13
|
+
require 'less_assets/engine'
|
14
|
+
else
|
15
|
+
require 'sprockets'
|
16
|
+
require 'sprockets/engines'
|
17
|
+
Sprockets.register_engine '.lesst', LessAssets::LessTemplate
|
18
|
+
end
|