Skip to content

Commit

Permalink
add /home with home/index.html for nelsonic/nelsonic.github.io#511
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 16, 2018
1 parent 5bfb50b commit 9131058
Show file tree
Hide file tree
Showing 5 changed files with 868 additions and 0 deletions.
26 changes: 26 additions & 0 deletions home/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var fs = require('fs');
var http_request = require('./http_request');
var cheerio = require('cheerio');

// Adding Colors to Terminal *Without* a Library/Module
var bgRedBlack = '\x1b[41m\x1b[30m';
var bgGreenBlack = '\x1b[42m\x1b[30m';
var RESET = '\x1b[0m'; // see: https://stackoverflow.com/a/41407246/1148249
var URL = 'https://github.com/nelsonic/home/blob/master/README.md';

http_request(URL, function (status, html) {
if (status !== 200 || !html) {
console.log(bgRedBlack,
" - - - GitHub Scraper FAIL >> " + URL + " - - - ", RESET);
process.exit();
}
else {
var $ = cheerio.load(html);
var body = $('#readme').html()
var css = fs.readFileSync('./style.css', 'utf8');
var template = fs.readFileSync('./template.html', 'utf8');
var out = template.replace('{css}', css).replace('{content}', body);
fs.writeFileSync('./index.html', out, 'utf8');
console.log(bgGreenBlack, 'done.', RESET);
}
});
48 changes: 48 additions & 0 deletions home/http_request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var http = require('https'); // ALWAYS use TLS over the internets!
var bgRedBlack = '\x1b[41m\x1b[30m';
var RESET = '\x1b[0m'; // see: https://stackoverflow.com/a/41407246/1148249
/**
* simple_http_request is a bare-bones http request using node.js core http
* see: https://nodejs.org/api/http.html#http_http_request_options_callback
* the NPM request module is 3.6 Megabytes and offers v. little benefit ...
* This code achieves the same in less than 1kb. less code = faster response.
* @param {Object} path - the path (on GitHub) we want to "view"
* @param {Function} callback - a standard callback with error & response args
* response is a JSON Object unless there is an error.
*/

module.exports = function simple_http_request (path, callback) {

var options = {
headers: {
'Accept': 'text/html',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36'
},
hostname: 'github.com',
port: '443',
path: path
}

http.request(options, function (res) {
var resStr = '';
var response;
// console.log(res.statusCode);
if (res.statusCode !== 200) {
console.log(bgRedBlack, ' GOT ', res.statusCode, ' for ', options, RESET);
return callback(res.statusCode);
}

res.setEncoding('utf8');
res.on('data', function (chunk) {
// console.log(chunk);
resStr += chunk;
}).on('end', function () {
return callback(res.statusCode, resStr); // return response as HTML!
});

return true;
}).end();

};
Loading

0 comments on commit 9131058

Please sign in to comment.