-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kent Andersen
committed
Jul 7, 2013
1 parent
1139028
commit 9184b80
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var src = path.join(path.dirname(fs.realpathSync(__filename)), '../src'); | ||
require(src + '/main.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "image-inliner", | ||
"version": "0.0.1", | ||
"readme": "empty", | ||
"main": "./src/main", | ||
"bin": { | ||
"imageinliner": "./bin/imageinliner" | ||
}, | ||
"dependencies": { | ||
"mime": "~1.2.9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var fs = require("fs"); | ||
var mime = require("mime"); | ||
var path = require("path"); | ||
|
||
function cleanFileData(fileData) { | ||
// remove linebreaks | ||
fileData = fileData.replace(/(\r\n|\n|\r)/gm,''); | ||
// removes xml comments | ||
fileData = fileData.replace(/<!--[^>]*-->/g, ''); | ||
// remove dubble spaces | ||
fileData = fileData.replace(/ +(?= )/g,''); | ||
// fileData = fileData.replace(/\'/gm,"\'"); | ||
|
||
return fileData; | ||
} | ||
|
||
function createCSSBackgroundImage(mimeType, encoding, fileData) { | ||
var css = ' background-image:'; | ||
css += "url('data:" + mimeType + ";"+ encoding+"," + fileData + "');"; | ||
return css; | ||
} | ||
|
||
function inlineImage(imagePath, cssFileBasePath) { | ||
|
||
imagePath = imagePath.replace(/\"|\'/g, ""); | ||
imagePath = path.join(basePath, cssFileBasePath, imagePath); | ||
var mimeType = mime.lookup(imagePath); | ||
|
||
if(fs.statSync(imagePath).size > fileSizeLimit) { | ||
return; | ||
} | ||
|
||
var cssImage; | ||
if(mimeType === "image/svg+xml") { | ||
var fileData = fs.readFileSync(imagePath, "utf-8"); | ||
cssImage = createCSSBackgroundImage(mimeType, "utf-8", cleanFileData(fileData)); | ||
} else { | ||
var fileData = fs.readFileSync(imagePath).toString('base64'); | ||
cssImage = createCSSBackgroundImage(mimeType, "base64", fileData); | ||
} | ||
|
||
return cssImage; | ||
} | ||
|
||
exports.inline = inlineImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var fs = require("fs"); | ||
var mime = require("mime"); | ||
var path = require("path"); | ||
var inline = require("./inline"); | ||
|
||
|
||
var fileSizeLimit = 61440; | ||
var basePath = process.env.PWD; | ||
|
||
|
||
var listOfFiles = process.argv.splice(2); | ||
if(!listOfFiles) { | ||
return; | ||
} | ||
|
||
listOfFiles.forEach(function (filePath) { | ||
var fileBasePath = path.dirname(filePath); | ||
var reg = /background.*:.*url\((.*)\)/; | ||
|
||
fs.readFile(filePath, "utf-8", function (err, fileData) { | ||
if (err) throw err; | ||
|
||
var fileMimeType = mime.lookup(filePath); | ||
if(fileMimeType !== "text/css") { | ||
throw "Filetype has to be css"; | ||
} | ||
|
||
var outputFilePath = filePath + ".output.css"; | ||
var strArray = fileData.split("\n"); | ||
var modifiedStrArray = strArray.slice(0); | ||
var indexOffset = 0; | ||
|
||
strArray.forEach(function(line, index){ | ||
var extracted = reg.exec(line); | ||
if(extracted && extracted[1]) { | ||
if(cssImage = inline.inlineImage(extracted[1], fileBasePath)) { | ||
modifiedStrArray.splice(++indexOffset + index, 0, cssImage); | ||
} | ||
} | ||
}); | ||
|
||
fs.writeFile(outputFilePath, modifiedStrArray.join("\n"), function (error) { | ||
if (error) throw error; | ||
console.log('File saved: ' + outputFilePath); | ||
}); | ||
}); | ||
}); | ||
|