forked from phlppschrr/gulp-compass-imagehelper
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sass-image-template.mustache
178 lines (153 loc) · 5.08 KB
/
sass-image-template.mustache
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// =================================================================
// This file is dynamically generated by gulp-compass-imagehelper
// Do NOT edit!
// =================================================================
// Returns if the image is accessible for these image-helpers
// Note: Not part of the official compass functions, but still useful
@function image-exists($image) {
@return map-has-key($compass-imagehelper-imagesmap, $image);
}
{{#includeData}}
// Embeds the contents of an image directly inside your stylesheet, eliminating the need for another HTTP request. For small images, this can be a performance
// benefit at the cost of a larger generated CSS file.
@function inline-image($image) {
@if image-exists($image) {
$list: map-get($compass-imagehelper-imagesmap, $image);
@return unquote(map-get($list, 'data'));
}
@warn "Image '#{$image}' not found.";
@return null;
}
{{/includeData}}
// Returns the width of the image found at the path supplied by $image relative to your project's images directory.
@function image-width($image) {
@if image-exists($image) {
$list: map-get($compass-imagehelper-imagesmap, $image);
$width: unquote(map-get($list, 'width'));
@if ($width and str-length($width) > 0) {
@return number($width) * 1px;
}
}
@warn "Image '#{$image}' not found.";
@return null;
}
// Returns the height of the image found at the path supplied by $image relative to your project's images directory.
@function image-height($image) {
@if image-exists($image) {
$list: map-get($compass-imagehelper-imagesmap, $image);
$height: unquote(map-get($list, 'height'));
@if ($height and str-length($height) > 0) {
@return number($height) * 1px;
}
}
@warn "Image '#{$image}' not found.";
@return null;
}
// Generates a path to an asset found relative to the project's images directory.
// Passing a true value as the second argument will cause only the path to be returned instead of a url() function
// The third argument is used to control the cache buster on a per-use basis. When set to false no cache buster will be used.
// When true a md5-hash of the file is appended to the url. When a string, that value will be used as the cache buster.
@function image-url($image, $only-path: false, $cache-buster: false) {
$result: $compass-imagehelper-path-prefix + $image;
@if not(image-exists($image)) {
@warn "Image '#{$image}' not found.";
} @else {
@if (type-of($cache-buster) == string) {
$result: $result + '?' + $cache-buster;
}
@if ($cache-buster == true) {
$list: map-get($compass-imagehelper-imagesmap, $image);
$hash: map-get($list, 'hash');
$result: $result + '?' + $hash;
}
}
$result: quote($result);
@if ($only-path == false) {
$result: url($result);
}
@return $result;
}
// Two helper functions for casting string to number by Hugo Giraudel
// http://hugogiraudel.com/2014/01/15/sass-string-to-number/
// http://sassmeister.com/gist/9647408
@function _length($number, $unit) {
$strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
$units: 1px 1cm 1mm 1% 1ch 1 pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
$index: index($strings, $unit);
@if not ($index) {
@warn "Unknown unit `#{$unit}`.";
@return false;
}
@return $number * nth($units, $index);
}
@function number($string) {
// Matrices
$strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$numbers: 0 1 2 3 4 5 6 7 8 9;
// Result
$result: 0;
$divider: 0;
$minus: false;
// Looping through all characters
@for $i from 1 through str-length($string) {
$character: str-slice($string, $i, $i);
$index: index($strings, $character);
@if $character == '-' {
$minus: true;
} @else if $character == '.' {
$divider: 1;
} @else {
@if not ($index) {
$result: if($minus, $result * -1, $result);
@return _length($result, str-slice($string, $i));
}
$number: nth($numbers, $index);
@if $divider == 0 {
$result: $result * 10;
} @else {
// Move the decimal dot to the left
$divider: $divider * 10;
$number: $number / $divider;
}
$result: $result + $number;
}
}
@return if($minus, $result * -1, $result);
}
$compass-imagehelper-path-prefix: '{{{path_prefix}}}';
$compass-imagehelper-imagesmap: (
{{#items}}
'{{{path}}}': (
width: '{{width}}',
height: '{{height}}',
{{#includeData}}
data: '{{{data}}}',
{{/includeData}}
hash: '{{{hash}}}'
),
{{/items}}
);
{{#createPlaceholder}}
{{#items}}
%{{prefix}}{{fullname}} {
// Available properties:
// prefix: {{prefix}};
// width: {{{width}}};
// height: {{height}};
// filename: {{filename}};
// basename: {{basename}};
// dirname: {{dirname}};
// ext: {{ext}};
// path: {{{path}}};
// fullname: {{{fullname}}};
// mime: {{{mime}}};
// hash: {{{ hash }}};
{{#includeData}}
background-image: inline-image('{{{ path }}}');
{{/includeData}}
{{^includeData}}
background-image: image-url('{{{ path }}}');
{{/includeData}}
}
{{/items}}
{{/createPlaceholder}}