-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
99 lines (83 loc) · 3.32 KB
/
index.coffee
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
os = require 'os'
fs = require 'fs'
path = require 'path'
stylus = require 'stylus'
imagesize = require('imagesize').Parser
lib = require './lib'
exports = module.exports = (cssPath,rootPath,options={})->
if typeof rootPath isnt 'string'
options = rootPath
rootPath = options.dest or options.rootPath or options.documentRoot or ''
debug = options.debug
toURLString = (url)->
unless url.args then return url.val
urlArgs = url.args.nodes[0]
if urlArgs.nodes.length
return (value.string for value in urlArgs.nodes).join ''
return urlArgs.nodes[0]
toAbsolutePath = (url)->
if /^\//i.test url
return path.join rootPath,url
else if /^https?:\/\//i.test url
throw new Error 'sorry. not supported yet.. : ' + url
else
return path.relative '.',path.join cssPath,url
stylsprite = (url,pixelRatio)->
jsonData = do lib.readJSON
url = toURLString url
isRelative = not /^(\/|https?:\/\/)/.test url
targetPath = toAbsolutePath url
if debug
console.log ''
console.log 'url:',url
console.log ' ->',targetPath
pixelRatio = parseFloat(if pixelRatio then pixelRatio.val else options.pixelRatio) or 1
if not pixelRatio or pixelRatio <= 0 then pixelRatio = 1
nodes = @closestBlock.nodes
nodesIndex = @closestBlock.index + 1
backgroundRepeat = new stylus.nodes.Property ['background-repeat'],"no-repeat"
if spriteData = jsonData[targetPath]
spriteWidth = spriteData.dest.width / pixelRatio
spriteHeight = spriteData.dest.height / pixelRatio
x = spriteData.x / pixelRatio
y = spriteData.y / pixelRatio
width = spriteData.width / pixelRatio
height = spriteData.height / pixelRatio
if isRelative
url = path.relative cssPath,spriteData.dest.file
else
url = '/' + path.relative rootPath,spriteData.dest.file
nodes.splice nodesIndex,0,
new stylus.nodes.Property ["width"],"#{width}px"
new stylus.nodes.Property ["height"],"#{height}px"
new stylus.nodes.Property ['background-image'],"url('#{url}')"
new stylus.nodes.Property ['background-position'],"#{-x}px #{-y}px"
new stylus.nodes.Property ['background-size'],"#{spriteWidth}px #{spriteHeight}px"
backgroundRepeat
return null
if fs.existsSync targetPath
# get image size
imageData = fs.readFileSync targetPath
parser = do imagesize
switch parser.parse imageData
when imagesize.EOF or imagesize.INVALID
console.log 'invalid:',targetPath
return null
when imagesize.DONE
rect = do parser.getResult
width = rect.width
height = rect.height
else
console.log 'unknown error:',targetPath
return null
spriteWidth = width / pixelRatio
spriteHeight = height / pixelRatio
nodes.splice nodesIndex,0,
new stylus.nodes.Property ["width"],"#{spriteWidth}px"
new stylus.nodes.Property ["height"],"#{spriteHeight}px"
new stylus.nodes.Property ['background-image'],"url('#{url}')"
new stylus.nodes.Property ['background-size'],"#{spriteWidth}px #{spriteHeight}px"
backgroundRepeat
return null
return new stylus.nodes.Property ['background-image'],"url('#{url}')"
-> (context)-> context.define 'stylsprite',stylsprite