diff --git a/checks.json b/checks.json new file mode 100644 index 0000000000..710dcfc1e8 --- /dev/null +++ b/checks.json @@ -0,0 +1,2 @@ +["h1", + ".navigation"] diff --git a/grader.js b/grader.js new file mode 100755 index 0000000000..8d51e4b203 --- /dev/null +++ b/grader.js @@ -0,0 +1,74 @@ +#!/usr/bin/env node +/* +Automatically grade files for the presence of specified HTML tags/attributes. +Uses commander.js and cheerio. Teaches command line application development +and basic DOM parsing. + +References: + + + cheerio + - https://github.com/MatthewMueller/cheerio + - http://encosia.com/cheerio-faster-windows-friendly-alternative-jsdom/ + - http://maxogden.com/scraping-with-node.html + + + commander.js + - https://github.com/visionmedia/commander.js + - http://tjholowaychuk.com/post/9103188408/commander-js-nodejs-command-line-interfaces-made-easy + + + JSON + - http://en.wikipedia.org/wiki/JSON + - https://developer.mozilla.org/en-US/docs/JSON + - https://developer.mozilla.org/en-US/docs/JSON#JSON_in_Firefox_2 +*/ + +var fs = require('fs'); +var program = require('commander'); +var cheerio = require('cheerio'); +var HTMLFILE_DEFAULT = "index.html"; +var CHECKSFILE_DEFAULT = "checks.json"; + +var assertFileExists = function(infile) { + var instr = infile.toString(); + if(!fs.existsSync(instr)) { + console.log("%s does not exist. Exiting.", instr); + process.exit(1); // http://nodejs.org/api/process.html#process_process_exit_code + } + return instr; +}; + +var cheerioHtmlFile = function(htmlfile) { + return cheerio.load(fs.readFileSync(htmlfile)); +}; + +var loadChecks = function(checksfile) { + return JSON.parse(fs.readFileSync(checksfile)); +}; + +var checkHtmlFile = function(htmlfile, checksfile) { + $ = cheerioHtmlFile(htmlfile); + var checks = loadChecks(checksfile).sort(); + var out = {}; + for(var ii in checks) { + var present = $(checks[ii]).length > 0; + out[checks[ii]] = present; + } + return out; +}; + +var clone = function(fn) { + // Workaround for commander.js issue. + // http://stackoverflow.com/a/6772648 + return fn.bind({}); +}; + +if(require.main == module) { + program + .option('-c, --checks ', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT) + .option('-f, --file ', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT) + .parse(process.argv); + var checkJson = checkHtmlFile(program.file, program.checks); + var outJson = JSON.stringify(checkJson, null, 4); + console.log(outJson); +} else { + exports.checkHtmlFile = checkHtmlFile; +} diff --git a/index.html b/index.html new file mode 100644 index 0000000000..3d47db1697 --- /dev/null +++ b/index.html @@ -0,0 +1,103 @@ + + + + + My First Bitstarter + + + + +
+ +
+
+

Bitstarter Title

+
+
+
+
+

This is my first bitstarter project.

+
+
+
+
+ Vidya +
+
+
+ The Meter +
+
+ Order +
+ +
+
+
+
+ Marketing Section 1 +
+
+
+
+ Marketing Section 2 +
+
+
+
+ F.A.Q. +
+
+ +
+ + + diff --git a/web.js b/web.js index 0b54a06391..0b1c587302 100644 --- a/web.js +++ b/web.js @@ -1,12 +1,15 @@ var express = require('express'); +var fs = require('fs'); var app = express.createServer(express.logger()); +var buf = new Buffer(100); +buf = fs.readFileSync("index.html"); app.get('/', function(request, response) { - response.send('Hello World!'); + response.send(buf.toString()); }); var port = process.env.PORT || 5000; app.listen(port, function() { console.log("Listening on " + port); -}); \ No newline at end of file +});