stylus_assets 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ # StylusAssets helper methods to render Stylus css.
2
+ #
3
+ class window.StylusAssets
4
+
5
+ # Render stylus css with its variables to plain css
6
+ #
7
+ # @param [String] css the stylus css
8
+ # @param [Object] variables the stylus variables
9
+ # @param [HTMLDocument] element a target element to add the styles
10
+ # @return [String] the css styles
11
+ #
12
+ @render: (name, css, variables = {}, doc = undefined) ->
13
+ result = undefined
14
+ newVariables = {}
15
+
16
+ # Replace existing less variables
17
+ #
18
+ for variable, value of variables
19
+ existing = ///#{ variable }\s*=\s*[^;\n]+;?///
20
+
21
+ if existing.test css
22
+ css = css.replace existing, "#{ variable } = #{ value }"
23
+ else
24
+ newVariables[variable] = value
25
+
26
+ # Add bew Stylus variables
27
+ #
28
+ style = ''
29
+ style += "#{ variable } = #{ value }\n" for variable, value of newVariables
30
+ style += css
31
+
32
+ # Compile the Stylus stylesheet
33
+ #
34
+ require('./stylus').render style, {}, (err, compiled) ->
35
+ throw err if err
36
+
37
+ result = compiled
38
+
39
+ # Add a style tag with the css when a document is given
40
+ #
41
+ if doc instanceof HTMLDocument || Object.prototype.toString.call(doc) is '[object HTMLDocument]'
42
+ id = "stylus-asset-#{ name.replace(/[^A-Za-z0-1_/-]/, '').replace(/[/_]/, '-') }"
43
+
44
+ style = doc.getElementById(id) || doc.createElement 'style'
45
+ style.type = 'text/css'
46
+ style.id = id
47
+
48
+ rules = doc.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
+ unless doc.getElementById(id)
59
+ head = doc.getElementsByTagName('head')[0]
60
+ head.appendChild style
61
+
62
+ result
63
+
64
+ # Stylus imports
65
+ #
66
+ class StylusAssets.Imports
67
+
68
+ @functions = '''
69
+ vendors = moz webkit o ms official
70
+
71
+ // stringify the given arg
72
+
73
+ -string(arg)
74
+ type(arg) + ' ' + arg
75
+
76
+ // require a color
77
+
78
+ require-color(color)
79
+ unless color is a 'color'
80
+ error('RGB or HSL value expected, got a ' + -string(color))
81
+
82
+ // require a unit
83
+
84
+ require-unit(n)
85
+ unless n is a 'unit'
86
+ error('unit expected, got a ' + -string(n))
87
+
88
+ // require a string
89
+
90
+ require-string(str)
91
+ unless str is a 'string' or str is a 'ident'
92
+ error('string expected, got a ' + -string(str))
93
+
94
+ // apply js Math function
95
+
96
+ math(n, fn)
97
+ require-unit(n)
98
+ require-string(fn)
99
+ -math(n, fn)
100
+
101
+ // adjust the given color's property by amount
102
+
103
+ adjust(color, prop, amount)
104
+ require-color(color)
105
+ require-string(prop)
106
+ require-unit(amount)
107
+ -adjust(color, prop, amount)
108
+
109
+ // Math functions
110
+
111
+ abs(n) { math(n, 'abs') }
112
+ ceil(n) { math(n, 'ceil') }
113
+ floor(n) { math(n, 'floor') }
114
+ round(n) { math(n, 'round') }
115
+ sin(n) { math(n, 'sin') }
116
+ cos(n) { math(n, 'cos') }
117
+ min(a, b) { a < b ? a : b }
118
+ max(a, b) { a > b ? a : b }
119
+ PI = -math-prop('PI')
120
+
121
+ // return the sum of the given numbers
122
+
123
+ sum(nums)
124
+ sum = 0
125
+ sum += n for n in nums
126
+
127
+ // return the average of the given numbers
128
+
129
+ avg(nums)
130
+ sum(nums) / length(nums)
131
+
132
+ // return a unitless number, or pass through
133
+
134
+ remove-unit(n)
135
+ if typeof(n) is "unit"
136
+ unit(n, "")
137
+ else
138
+ n
139
+
140
+ // convert a percent to a decimal, or pass through
141
+
142
+ percent-to-decimal(n)
143
+ if unit(n) is "%"
144
+ remove-unit(n) / 100
145
+ else
146
+ n
147
+
148
+ // color components
149
+
150
+ alpha(color) { component(hsl(color), 'alpha') }
151
+ hue(color) { component(hsl(color), 'hue') }
152
+ saturation(color) { component(hsl(color), 'saturation') }
153
+ lightness(color) { component(hsl(color), 'lightness') }
154
+
155
+ // check if n is an odd number
156
+
157
+ odd(n)
158
+ 1 == n % 2
159
+
160
+ // check if n is an even number
161
+
162
+ even(n)
163
+ 0 == n % 2
164
+
165
+ // check if color is light
166
+
167
+ light(color)
168
+ lightness(color) >= 50%
169
+
170
+ // check if color is dark
171
+
172
+ dark(color)
173
+ lightness(color) < 50%
174
+
175
+ // desaturate color by amount
176
+
177
+ desaturate(color, amount)
178
+ adjust(color, 'saturation', - amount)
179
+
180
+ // saturate color by amount
181
+
182
+ saturate(color, amount)
183
+ adjust(color, 'saturation', amount)
184
+
185
+ // darken by the given amount
186
+
187
+ darken(color, amount)
188
+ adjust(color, 'lightness', - amount)
189
+
190
+ // lighten by the given amount
191
+
192
+ lighten(color, amount)
193
+ adjust(color, 'lightness', amount)
194
+
195
+ // decerase opacity by amount
196
+
197
+ fade-out(color, amount)
198
+ color - rgba(black, percent-to-decimal(amount))
199
+
200
+ // increase opacity by amount
201
+
202
+ fade-in(color, amount)
203
+ color + rgba(black, percent-to-decimal(amount))
204
+
205
+ // spin hue by a given amount
206
+
207
+ spin(color, amount)
208
+ color + unit(amount, deg)
209
+
210
+ // mix two colors by a given amount
211
+
212
+ mix(color1, color2, weight = 50%)
213
+ unless weight in 0..100
214
+ error("Weight must be between 0% and 100%")
215
+
216
+ if length(color1) == 2
217
+ weight = color1[0]
218
+ color1 = color1[1]
219
+
220
+ else if length(color2) == 2
221
+ weight = 100 - color2[0]
222
+ color2 = color2[1]
223
+
224
+ require-color(color1)
225
+ require-color(color2)
226
+
227
+ p = unit(weight / 100, '')
228
+ w = p * 2 - 1
229
+
230
+ a = alpha(color1) - alpha(color2)
231
+
232
+ w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2
233
+ w2 = 1 - w1
234
+
235
+ channels = (red(color1) red(color2)) (green(color1) green(color2)) (blue(color1) blue(color2))
236
+ rgb = ()
237
+
238
+ for pair in channels
239
+ push(rgb, floor(pair[0] * w1 + pair[1] * w2))
240
+
241
+ a1 = alpha(color1) * p
242
+ a2 = alpha(color1) * (1 - p)
243
+ alpha = a1 + a2
244
+
245
+ rgba(rgb[0], rgb[1], rgb[2], alpha)
246
+
247
+ // invert colors, leave alpha intact
248
+
249
+ invert(color)
250
+ r = 255 - red(color)
251
+ g = 255 - green(color)
252
+ b = 255 - blue(color)
253
+ rgba(r,g,b,alpha(color))
254
+
255
+ // return the last value in the given expr
256
+
257
+ last(expr)
258
+ expr[length(expr) - 1]
259
+
260
+ // return keys in the given pairs
261
+
262
+ keys(pairs)
263
+ ret = ()
264
+ for pair in pairs
265
+ push(ret, pair[0]);
266
+ ret
267
+
268
+ // return values in the given pairs
269
+
270
+ values(pairs)
271
+ ret = ()
272
+ for pair in pairs
273
+ push(ret, pair[1]);
274
+ ret
275
+
276
+ // join values with the given delimiter
277
+
278
+ join(delim, vals...)
279
+ buf = ''
280
+ vals = vals[0] if length(vals) == 1
281
+ for val, i in vals
282
+ buf += i ? delim + val : val
283
+
284
+ // add a css rule to the containing block
285
+
286
+ // - This definition allows add-property to be used as a mixin
287
+ // - It has the same effect as interpolation but allows users
288
+ // to opt for a functional style
289
+
290
+ add-property-function = add-property
291
+ add-property(name, expr)
292
+ if mixin
293
+ {name} expr
294
+ else
295
+ add-property-function(name, expr)
296
+ '''
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stylus_assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Kessler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tilt
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: sprockets
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Stylus JavaScript Templates in the asset pipeline.
63
+ email:
64
+ - michi@netzpiraten.ch
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/stylus_assets/engine.rb
70
+ - lib/stylus_assets/stylus_template.rb
71
+ - lib/stylus_assets/version.rb
72
+ - lib/stylus_assets.rb
73
+ - vendor/assets/javascripts/stylus.js
74
+ - vendor/assets/javascripts/stylus_assets.js.coffee
75
+ - LICENSE
76
+ - README.md
77
+ homepage: https://github.com/netzpirat/stylus_assets
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ segments:
90
+ - 0
91
+ hash: 1828645846812234919
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.6
98
+ requirements: []
99
+ rubyforge_project: stylus_assets
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Stylus JavaScript Templates
104
+ test_files: []
105
+ has_rdoc: