forked from stylus/stylus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
js-functions.js
55 lines (45 loc) · 1.21 KB
/
js-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Module dependencies.
*/
var css = require('../')
, nodes = css.nodes
, str = require('fs').readFileSync(__dirname + '/js-functions.styl', 'utf8')
, fs = require('fs');
function add(a, b) {
return a.operate('+', b);
}
function sub(a, b) {
return a.operate('-', b);
}
function imageSize(img) {
// assert that the node (img) is a String node, passing
// the param name for error reporting
css.utils.assertType(img, 'string', 'img');
var path = img.val;
// Grab bytes necessary to retrieve dimensions.
// if this was real you would do this per format,
// instead of reading the entire image :)
var data = fs.readFileSync(__dirname + '/' + path);
// GIF
// of course you would support.. more :)
if ('GIF' == data.slice(0, 3).toString()) {
var w = data.slice(6, 8)
, h = data.slice(8, 10);
w = w[1] << 8 | w[0];
h = h[1] << 8 | h[0];
}
// Return (w h)
var expr = new nodes.Expression;
expr.push(new nodes.Unit(w));
expr.push(new nodes.Unit(h));
return expr;
}
css(str)
.set('filename', 'js-functions.styl')
.define('add', add)
.define('sub', sub)
.define('image-size', imageSize)
.render(function(err, css){
if (err) throw err;
console.log(css);
});