Permalink
Browse files

Made JavaScript JSLint compliant

  • Loading branch information...
1 parent 31f7a5a commit 14222afc7193e188f598e9306d4bb6ea76052c59 @squashvoice committed Jan 2, 2012
Showing with 74 additions and 71 deletions.
  1. +74 −71 pattern-primer.js
View
145 pattern-primer.js
@@ -1,3 +1,6 @@
+/*jslint nomen: true */
+'use strict';
+
var settings = {
webserverport : '8080',
wwwroot: 'public',
@@ -7,119 +10,119 @@ var settings = {
},
util = require('util'),
connect = require('connect'),
- server = connect.createServer(
- connect.static(__dirname + '/' + settings.wwwroot),
- function (req, resp) {
- if (req.url !== '/') {
- resp.writeHead(404, {
- 'Content-Length': 0,
- 'Content-Type': 'text/plain'
- });
- resp.end();
- return;
- }
-
- primer(resp);
- }
- ),
- primer = function(serverResponse, tofile, tofileCallback) {
- tofile || false;
-
+ primer = function (serverResponse, tofile, tofileCallback) {
+ tofile = tofile || false;
+
var fs = require('fs'),
patternFolder = './' + settings.pattern_path,
simpleEscaper = function (text) {
- return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/"/g,'&quot;').replace(/'/g,'&#039;');
+ return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
},
- beginProcess = function() {
- fs.readdir(patternFolder, function (err, contents) {
- if (err !== null && err.code === 'ENOENT') {
- util.puts('Cannot find patterns folder:', patternFolder);
+ outputPatterns = function (patterns) {
+ fs.readFile(settings.sourcehtmlfile, 'utf-8', function (err, content) {
+ if (err !== null) {
+ util.puts('There was an error when trying to read file:', 'output.html');
return;
}
-
- var files = [],
- i,
- l;
-
- for (i = 0, l = contents.length; i < l; i += 1) {
- if (contents[i].substr(-5) === '.html') {
- files.push(contents[i]);
+
+ var i,
+ l,
+ file;
+
+ for (i = 0, l = patterns.length; i < l; i += 1) {
+ file = patterns[i];
+ content += '<div class="pattern"><div class="display">';
+ content += file.content;
+ content += '</div><div class="source"><textarea rows="6" cols="30">';
+ content += simpleEscaper(file.content);
+ content += '</textarea>';
+ if (!tofile) {
+ content += '<p><a href="patterns/' + file.filename + '">' + file.filename + '</a></p>';
}
+ content += '</div></div>';
+ }
+
+ content += '</body></html>';
+
+ if (tofile) {
+ tofileCallback(content);
+ } else {
+ serverResponse.end(content);
}
-
- handleFiles(files);
});
},
handleFiles = function (files) {
var i,
l,
file,
patterns = [];
-
+
// This was asyncronous, but we need the file names, which we can't get from the callback of 'readFile'
for (i = 0, l = files.length; i < l; i += 1) {
file = {
filename : files[i]
};
-
+
file.content = fs.readFileSync(patternFolder + '/' + file.filename, 'utf-8');
patterns.push(file);
}
-
+
outputPatterns(patterns);
},
- outputPatterns = function (patterns) {
- fs.readFile(settings.sourcehtmlfile, 'utf-8', function(err, content) {
- if (err !== null) {
- util.puts('There was an error when trying to read file:', 'output.html');
+ beginProcess = function () {
+ fs.readdir(patternFolder, function (err, contents) {
+ if (err !== null && err.code === 'ENOENT') {
+ util.puts('Cannot find patterns folder:', patternFolder);
return;
}
-
- var i,
- l,
- file;
-
- for (i = 0, l = patterns.length; i < l; i += 1) {
- file = patterns[i];
- content += '<div class="pattern"><div class="display">';
- content += file.content;
- content += '</div><div class="source"><textarea rows="6" cols="30">';
- content += simpleEscaper(file.content);
- content += '</textarea>';
- if (!tofile) {
- content += '<p><a href="patterns/' + file.filename + '">' + file.filename + '</a></p>';
+
+ var files = [],
+ i,
+ l;
+
+ for (i = 0, l = contents.length; i < l; i += 1) {
+ if (contents[i].substr(-5) === '.html') {
+ files.push(contents[i]);
}
- content += '</div></div>';
- }
-
- content +='</body></html>';
-
- if (tofile) {
- tofileCallback(content);
- } else {
- serverResponse.end(content);
}
+
+ handleFiles(files);
});
};
-
+
beginProcess();
- };
+ },
+ server = connect.createServer(
+ connect.static(__dirname + '/' + settings.wwwroot),
+ function (req, resp) {
+ if (req.url !== '/') {
+ resp.writeHead(404, {
+ 'Content-Length': 0,
+ 'Content-Type': 'text/plain'
+ });
+ resp.end();
+ return;
+ }
+
+ primer(resp);
+ }
+ );
if (process.argv[2] === '--tofile') {
-
+
primer(null, true, function (content) {
var fs = require('fs');
- fs.writeFile('./' + settings.tofile_outputpath + '/index.html', content, 'utf-8', function() {
- util.pump(fs.createReadStream('./' + settings.wwwroot + '/global.css'),
+ fs.writeFile('./' + settings.tofile_outputpath + '/index.html', content, 'utf-8', function () {
+ util.pump(fs.createReadStream('./' + settings.wwwroot + '/global.css'),
fs.createWriteStream('./' + settings.tofile_outputpath + '/global.css'));
util.puts('Stand-alone output can now be found in "' + settings.tofile_outputpath + '/"');
});
});
-
+
} else {
-
+
server.listen(settings.webserverport);
util.puts('You can now visit http://localhost:' + settings.webserverport + '/ to see your patterns.');
util.puts('To kill this server, just press Ctrl + C');
-
+
}

0 comments on commit 14222af

Please sign in to comment.