less_assets 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -87,9 +87,9 @@ By default, `name_filter` strips the leading `templates/`, `stylesheets/` and `s
87
87
 
88
88
  ## Render
89
89
 
90
- When you have a template `app/assets/javascripts/style/header.lesst` with the given content:
90
+ When you have a template named `header` with the given content:
91
91
 
92
- ```scss
92
+ ```SCSS
93
93
  .header (@r) {
94
94
  padding: @r * 2;
95
95
  border-radius: @r;
@@ -121,7 +121,7 @@ which will return in the following CSS
121
121
 
122
122
  whereas rendering
123
123
 
124
- ```javascript
124
+ ```JavaScript
125
125
  JSST['header']({ radius: '20px' })
126
126
  ```
127
127
 
@@ -135,11 +135,71 @@ will result in
135
135
  }
136
136
  ```
137
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.
138
+ ### Default variables
139
+
140
+ You do not need to define the variables in the less stylesheet, as they will be created before compilation, but you
141
+ may want to have them added to provide default variables.
142
+
143
+ Given the following style template named `box`
144
+
145
+ ```SCSS
146
+ @box-margin: 10px;
147
+ @box-padding: 10px;
148
+
149
+ .box {
150
+ margin: @box-margin;
151
+ padding: @box-padding;
152
+ }
153
+ ```
154
+
155
+ Rendered with only some of the variables passed to the template
156
+
157
+ ```JavaScript
158
+ JSST['box']({ 'box-margin': '20px' })
159
+ ```
160
+
161
+ will use the default values that result in
162
+
163
+ ```CSS
164
+ .box {
165
+ margin: 20px;
166
+ padding: 10px;
167
+ }
168
+ ```
169
+
170
+ ### Applying the styles to a document
171
+
172
+ You can let Less Assets to manage the styles on a HTML document by passing the document to the style template.
173
+
174
+ Given the following Less style template named `divider`:
175
+
176
+ ```CSS
177
+ div {
178
+ margin-top: @m;
179
+ }
180
+ ```
181
+
182
+ that is compiled with
183
+
184
+ ```JavaScript
185
+ JSST['header']({ m: '20px' }, document)
186
+ ```
187
+
188
+ will create a new style tag in the head of the document:
189
+
190
+ ```HTML
191
+ <style id="less_asset_header">
192
+ div {
193
+ margin-top: 20px;
194
+ }
195
+ </style>
196
+ ```
197
+
198
+ Re-render the same style template again with other variables will replace the existing styles with the new ones.
139
199
 
140
200
  ## Author
141
201
 
142
- Developed by Michael Kessler, sponsored by [mksoft.ch](https://mksoft.ch).
202
+ Developed by Michael Kessler, [mksoft.ch](https://mksoft.ch).
143
203
 
144
204
  If you like Less Assets, you can watch the repository at [GitHub](https://github.com/netzpirat/less_assets) and
145
205
  follow [@netzpirat](https://twitter.com/#!/netzpirat) on Twitter for project updates.
@@ -57,7 +57,7 @@ module LessAssets
57
57
  <<-JST
58
58
  (function() {
59
59
  #{ self.class.namespace } || (#{ self.class.namespace } = {});
60
- #{ self.class.namespace }['#{ name }'] = function(v) { return LessAssets.render(\"#{ data.gsub(/\n/, "\\n") }\", v); };
60
+ #{ self.class.namespace }['#{ name }'] = function(v, e) { return LessAssets.render('#{ name }', \"#{ data.gsub(/\n/, "\\n") }\", v, e); };
61
61
  }).call(this);
62
62
  JST
63
63
  end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module LessAssets
4
- VERSION = '0.1.0' unless defined?(LessAssets::VERSION)
4
+ VERSION = '0.2.0' unless defined?(LessAssets::VERSION)
5
5
  end
@@ -6,17 +6,56 @@ class window.LessAssets
6
6
  #
7
7
  # @param [String] css the less css
8
8
  # @param [Object] variables the less variables
9
+ # @param [HTMLDocument] element a target element to add the styles
10
+ # @return [String] the css styles
9
11
  #
10
- @render: (css, variables) ->
12
+ @render: (name, css, variables = {}, doc = undefined) ->
11
13
  result = undefined
14
+ newVariables = {}
12
15
 
16
+ # Replace existing less variables
17
+ #
18
+ for variable, value of variables
19
+ existing = ///@#{ variable }:\s*[^;]+;///
20
+
21
+ if existing.test css
22
+ css = css.replace existing, "@#{ variable }: #{ value };"
23
+ else
24
+ newVariables[variable] = value
25
+
26
+ # Add bew less variables
27
+ #
13
28
  style = ''
14
- style += "@#{ variable }: #{ value };\n" for variable, value of variables
29
+ style += "@#{ variable }: #{ value };\n" for variable, value of newVariables
15
30
  style += css
16
31
 
32
+ # Compile the less stylesheet
33
+ #
17
34
  less.Parser().parse style, (err, tree) ->
18
35
  throw err if err
19
36
 
20
37
  result = tree.toCSS()
21
38
 
39
+ # Add a style tag with the css when a document is given
40
+ #
41
+ if doc instanceof HTMLDocument
42
+ id = "less_asset_#{ name.replace(/[^A-Za-z0-1_/-]/, '').replace(/[/-]/, '_') }"
43
+
44
+ style = document.getElementById(id) || document.createElement 'style'
45
+ style.type = 'text/css'
46
+ style.id = id
47
+
48
+ rules = document.createTextNode result
49
+
50
+ if style.styleSheet
51
+ style.styleSheet.cssText = rules.nodeValue
52
+ else
53
+ while style.hasChildNodes()
54
+ style.removeChild style.lastChild
55
+
56
+ style.appendChild rules
57
+
58
+ head = document.getElementsByTagName('head')[0]
59
+ head.appendChild(style);
60
+
22
61
  result
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: less_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-16 00:00:00.000000000 Z
12
+ date: 2012-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tilt
@@ -59,118 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: railties
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '3.1'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '3.1'
78
- - !ruby/object:Gem::Dependency
79
- name: rspec
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: guard-rspec
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- - !ruby/object:Gem::Dependency
111
- name: yard
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: redcarpet
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ! '>='
132
- - !ruby/object:Gem::Version
133
- version: '0'
134
- type: :development
135
- prerelease: false
136
- version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ! '>='
140
- - !ruby/object:Gem::Version
141
- version: '0'
142
- - !ruby/object:Gem::Dependency
143
- name: pry
144
- requirement: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ! '>='
148
- - !ruby/object:Gem::Version
149
- version: '0'
150
- type: :development
151
- prerelease: false
152
- version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
- requirements:
155
- - - ! '>='
156
- - !ruby/object:Gem::Version
157
- version: '0'
158
- - !ruby/object:Gem::Dependency
159
- name: rake
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ! '>='
164
- - !ruby/object:Gem::Version
165
- version: '0'
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ! '>='
172
- - !ruby/object:Gem::Version
173
- version: '0'
174
62
  description: Less JavaScript Style Templates in the asset pipeline.
175
63
  email:
176
64
  - michi@netzpiraten.ch
@@ -201,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
89
  version: '0'
202
90
  segments:
203
91
  - 0
204
- hash: 26994244759746071
92
+ hash: 3705905153218187553
205
93
  required_rubygems_version: !ruby/object:Gem::Requirement
206
94
  none: false
207
95
  requirements: