Skip to content

Commit

Permalink
Updates the generator functions to adopt the changes introduced to
Browse files Browse the repository at this point in the history
the spriter script. See asciidisco#52

Also did some cleanup to the output string building.
  • Loading branch information
alpadev committed Mar 31, 2014
1 parent 6416c26 commit 85a04f5
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions tasks/sprites.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,31 @@ module.exports = function(grunt) {
});

fileContents += imageClasses + ' {' + '\n' + ' background: url("' + generateBackgroundImagePath() + '") no-repeat;\n' + '}\n\n';
imageData.heights.forEach(function (height, idx) {
fileContents += '.' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') + ' {\n' + ' background-position: 0 ' + -height + ( height === 0 ? "" : 'px') + ';\n' + '}\n\n';
imageData.images.forEach(function (meta, idx) {
fileContents += '.' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') +
' {\n' +
' width: ' + intToPixel(meta.width) + ';\n' +
' height: ' + intToPixel(meta.height) + ';\n' +
' background-position: 0 ' + intToPixel(-meta.offsetY) + ';\n' +
'}\n\n';
});

return fileContents;
}

function generateSASSFile (imageData, images, placeholder, scssSyntax) {
var fileContents = '';
var fileContents = '',
colon = (scssSyntax ? ';' : '') + '\n';

fileContents += "%" + placeholder + (scssSyntax ? ' {' : '') + '\n' + ' background: url("' + generateBackgroundImagePath() + '") no-repeat' + (scssSyntax ? ';\n }' : '') + '\n\n';
imageData.heights.forEach(function (height, idx) {
fileContents += '%' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') + (scssSyntax ? ' {' : '') + '\n @extend ' + '%' + placeholder + (scssSyntax ? ' ;' : '') + '\n' + ' background-position: 0 ' + -height + ( height === 0 ? "" : 'px') + (scssSyntax ? ';\n }' : '') + '\n\n';
imageData.images.forEach(function (meta, idx) {
fileContents += '%' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') +
(scssSyntax ? ' {' : '') + '\n' +
' @extend ' + '%' + placeholder + colon +
' width: ' + intToPixel(meta.width) + colon +
' height: ' + intToPixel(meta.height) + colon +
' background-position: 0 ' + intToPixel(-meta.offsetY) + colon +
(scssSyntax ? '}' : '') + '\n\n';
});

return fileContents;
Expand All @@ -89,8 +101,13 @@ module.exports = function(grunt) {
var fileContents = '';

fileContents += "." + placeholder + ' {\n' + ' background: url("' + generateBackgroundImagePath() + '") no-repeat;\n }' + '\n\n';
imageData.heights.forEach(function (height, idx) {
fileContents += '.' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') + ':extend(.' + placeholder + ') {\n' + ' background-position: 0 ' + -height + ( height === 0 ? "" : 'px') + ';\n' + '}\n\n';
imageData.images.forEach(function (meta, idx) {
fileContents += '.' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') +
':extend(.' + placeholder + ') {\n' +
' width: ' + intToPixel(meta.width) + ';\n' +
' height: ' + intToPixel(meta.height) + ';\n' +
' background-position: 0 ' + intToPixel(-meta.offsetY) + ';\n' +
'}\n\n';
});

return fileContents;
Expand All @@ -100,13 +117,21 @@ module.exports = function(grunt) {
var fileContents = '';

fileContents += '$' + placeholder + '\n background: url("' + generateBackgroundImagePath() + '") no-repeat\n\n';
imageData.heights.forEach(function (height, idx) {
fileContents += '$' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') + '\n @extend $' + placeholder + '\n background-position: 0 ' + ( height > 0 ? -height + 'px' : 0 ) + '\n\n';
imageData.images.forEach(function (meta, idx) {
fileContents += '$' + (classPrefix === '' ? '' : classPrefix + '-') + path.basename(images[idx].file, '.png') +
'\n @extend $' + placeholder +
'\n width: ' + intToPixel(meta.width) +
'\n height: ' + intToPixel(meta.height) +
'\n background-position: 0 ' + intToPixel(-meta.offsetY) + '\n\n';
});

return fileContents;
}

function intToPixel (int) {
return int !== 0 ? int + 'px' : 0;
}

function runSpriteGenerator (images) {
// spawn a phantom js process
var ps = spawn(binPath, ['--web-security=no', path.resolve(__dirname, '../lib/phantomspriter.js')]);
Expand Down Expand Up @@ -153,19 +178,19 @@ module.exports = function(grunt) {

switch (output){
case "scss":
stylesData = generateSASSFile(incomingData, images, placeHolder, true);
stylesData = generateSASSFile(incomingData.meta, images, placeHolder, true);
break;
case "sass":
stylesData = generateSASSFile(incomingData, images, placeHolder);
stylesData = generateSASSFile(incomingData.meta, images, placeHolder);
break;
case "less":
stylesData = generateLESSFile(incomingData, images, placeHolder);
stylesData = generateLESSFile(incomingData.meta, images, placeHolder);
break;
case "stylus":
stylesData = generateStylusFile(incomingData, images, placeHolder);
stylesData = generateStylusFile(incomingData.meta, images, placeHolder);
break;
default:
stylesData = generateCSSFile(incomingData, images);
stylesData = generateCSSFile(incomingData.meta, images);
break;
}

Expand Down

0 comments on commit 85a04f5

Please sign in to comment.