Created
September 8, 2015 19:27
-
-
Save liddiard/4a03e9feecda595e11c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var http = require('http'); | |
var fs = require('fs'); | |
var argv = require('yargs').argv; | |
var cheerio = require('cheerio'); | |
var DEFAULT_TEMPLATE = "http://uclabruins.com/ViewArticle.dbml?ATCLID=209624560&DB_OEM_ID=30500"; | |
var template = argv.template || DEFAULT_TEMPLATE; | |
var pageFilename = argv._[0]; | |
if (!pageFilename) { | |
console.error('ERROR: No page specified to preview.'); | |
process.exit(1); | |
} | |
fs.readFile(pageFilename, function(err, page){ | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
getTemplate(page.toString()); | |
}); | |
function getTemplate(page) { | |
http.get(template, function(res){ | |
res.on('data', function(chunk){ | |
replaceArticle(chunk.toString(), page); | |
}); | |
}); | |
} | |
function replaceArticle(template, page) { | |
$ = cheerio.load(template); | |
$('#article-content').empty(); | |
$('#article-content').append(page); | |
servePreview($.html()); | |
} | |
function servePreview(html) { | |
http.createServer(function(req, res){ | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end(html); | |
}).listen(3001, 'localhost'); | |
} |
amoskyler
commented
Sep 8, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment