Skip to content

Commit

Permalink
total refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 9, 2016
1 parent 0a9895d commit 6d30434
Show file tree
Hide file tree
Showing 31 changed files with 827 additions and 823 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "stable"
- "5"
- "4"
- "0.12"
- "0.10"
- "iojs"
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

184 changes: 92 additions & 92 deletions bin/x-ray
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#!/usr/bin/env node

var Crawler = require('x-ray-crawler');
var Context = require('http-context');
var cheerio = require('cheerio');
var pj = require('prettyjson');
var isUrl = require('is-url');
var chalk = require('chalk');
var repl = require('repl');
var Xray = require('../');
var fs = require('fs');
var slice = [].slice;
var actions = {};
var Crawler = require('x-ray-crawler')
var Context = require('http-context')
var cheerio = require('cheerio')
var pj = require('prettyjson')
var isUrl = require('is-url')
var chalk = require('chalk')
var repl = require('repl')
var Xray = require('../')
var fs = require('fs')
var slice = [].slice
var actions = {}

/**
* Locals
*/

var highlight = require('../lib/highlight');
var isHTML = require('../lib/is-html');
var pretty = require('../lib/pretty');
var params = require('../lib/params');
var highlight = require('../lib/highlight')
var isHTML = require('../lib/is-html')
var pretty = require('../lib/pretty')
var params = require('../lib/params')

/**
* Default state
*/

var xray = Xray()
var request = Crawler();
var ctx = Context();
var request = Crawler()
var ctx = Context()

/**
* Help
Expand All @@ -42,13 +42,13 @@ var help = [
' select [scope] <selector>',
' html',
''
].join('\n');
].join('\n')

/**
* Regexps
*/

var rcmd = /^(help|load|paginate|select|\$)/i;
var rcmd = /^(help|load|paginate|select|\$)/i

/**
* Start the repl
Expand All @@ -60,65 +60,65 @@ repl.start({
output: process.stdout,
ignoreUndefined: true,
eval: evaluate
});
})

/**
* Evaluate the input
*/

function evaluate(cmd, ctx, filename, fn) {
cmd = cmd.replace(/[\(\n\)]/g, '');
if (!cmd) return fn(null, undefined);
var args = cmd.split(' ');
cmd = args.shift().toLowerCase();
function evaluate (cmd, ctx, filename, fn) {
cmd = cmd.replace(/[\(\n\)]/g, '')
if (!cmd) return fn(null, undefined)
var args = cmd.split(' ')
cmd = args.shift().toLowerCase()
if ('help' == cmd || !rcmd.test(cmd)) {
console.log(help);
return fn(null, undefined);
console.log(help)
return fn(null, undefined)
}

actions[cmd].apply(null, args.concat(function(err, val) {
if (err) return fn(err);
else if (val) console.log(val);
fn(null, undefined);
}));
actions[cmd].apply(null, args.concat(function (err, val) {
if (err) return fn(err)
else if (val) console.log(val)
fn(null, undefined)
}))
}

/**
* Load
*/

actions.load = function load(url, fn) {
actions.load = function load (url, fn) {
if (arguments.length == 1) {
fn = url;
return fn(null, help);
fn = url
return fn(null, help)
}

if (isUrl(url)) {
success('loading: %s', url);
request(url, function(err, res) {
if (err) return fn(err);
ctx.status = res.status;
ctx.body = res.body;
success('loaded: %s', res.url);
console.log(pretty(res.url));
fn(null);
success('loading: %s', url)
request(url, function (err, res) {
if (err) return fn(err)
ctx.status = res.status
ctx.body = res.body
success('loaded: %s', res.url)
console.log(pretty(res.url))
fn(null)
})
} else if (isHTML(url)) {
success('loading: <html string>');
ctx.status = 200;
ctx.body = url;
success('loaded: <html string>');
console.log(pretty(url));
fn(null);
success('loading: <html string>')
ctx.status = 200
ctx.body = url
success('loaded: <html string>')
console.log(pretty(url))
fn(null)
} else {
success('loading: %s', url);
fs.readFile(url, 'utf8', function(err, str) {
if (err) return fn(err);
ctx.status = 200;
ctx.body = str;
success('loaded: %s', url);
response(pretty(str));
fn(null);
success('loading: %s', url)
fs.readFile(url, 'utf8', function (err, str) {
if (err) return fn(err)
ctx.status = 200
ctx.body = str
success('loaded: %s', url)
response(pretty(str))
fn(null)
})
}
}
Expand All @@ -128,73 +128,73 @@ actions.load = function load(url, fn) {
*/

actions.select =
actions.$ = function select(scope, selector, fn) {
if (arguments.length == 1) {
response(ctx.body);
fn(null);
return;
} else if (arguments.length == 2) {
fn = selector;
selector = coerce(scope);
scope = null;
} else {
scope = coerce(scope);
selector = coerce(selector);
}

var $ = cheerio.load(ctx.body);
$ = highlight($, scope, selector);
var html = $.html();
response(html);
fn(null);
actions.$ = function select (scope, selector, fn) {
if (arguments.length == 1) {
response(ctx.body)
fn(null)
return
} else if (arguments.length == 2) {
fn = selector
selector = coerce(scope)
scope = null
} else {
scope = coerce(scope)
selector = coerce(selector)
}

var $ = cheerio.load(ctx.body)
$ = highlight($, scope, selector)
var html = $.html()
response(html)
fn(null)
}

/**
* string(...)
*/

function string(str) {
str = unstring(str);
return '"' + str + '"';
function string (str) {
str = unstring(str)
return '"' + str + '"'
}

/**
* unstring(str)
*/

function unstring(str) {
return str.replace(/^['"]|['"]$/g, '');
function unstring (str) {
return str.replace(/^['"]|['"]$/g, '')
}

/**
* coerce
*/

function coerce(str) {
function coerce (str) {
return '[' == str[0] && ']' == str[str.length - 1]
? [str.slice(1, -1)]
: str;
: str
}

/**
* Success
*/

function success(str) {
var args = slice.call(arguments).slice(1);
console.error.apply(console.error, [chalk.green(' ' + str)].concat(args));
function success (str) {
var args = slice.call(arguments).slice(1)
console.error.apply(console.error, [chalk.green(' ' + str)].concat(args))
}

function response(str) {
var args = slice.call(arguments).slice(1);
console.error.apply(console.error, [chalk.gray(' ' + str)].concat(args));
function response (str) {
var args = slice.call(arguments).slice(1)
console.error.apply(console.error, [chalk.gray(' ' + str)].concat(args))
}

/**
* Error
*/

function error(str) {
var args = slice.call(arguments).slice(1);
console.error.apply(console.error, [chalk.red(' ' + str)].concat(args));
function error (str) {
var args = slice.call(arguments).slice(1)
console.error.apply(console.error, [chalk.red(' ' + str)].concat(args))
}
8 changes: 0 additions & 8 deletions examples/array-of-arrays.js

This file was deleted.

File renamed without changes.
8 changes: 8 additions & 0 deletions examples/array-of-arrays/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var read = require('fs').readFileSync
var html = read(__dirname + '/index.html')
var Xray = require('..')
var x = Xray()

x(html, '.tags', [['li']])(function (err, arr) {
console.log(arr)
})
8 changes: 0 additions & 8 deletions examples/arrays.js

This file was deleted.

File renamed without changes.
8 changes: 8 additions & 0 deletions examples/arrays/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var read = require('fs').readFileSync
var html = read(__dirname + '/index.html')
var Xray = require('..')
var x = Xray()

x(html, ['a'])(function (err, arr) {
console.log(arr)
})
11 changes: 0 additions & 11 deletions examples/collection-of-collections.js

This file was deleted.

File renamed without changes.
11 changes: 11 additions & 0 deletions examples/collection-of-collections/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var read = require('fs').readFileSync
var html = read(__dirname + '/index.html')
var Xray = require('..')
var x = Xray()

x(html, '.item', [{
title: 'h2',
tags: x('.tags', ['li'])
}])(function (err, arr) {
console.log(arr)
})
File renamed without changes.
6 changes: 3 additions & 3 deletions examples/collections.js → examples/collections/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var read = require('fs').readFileSync
var html = read(__dirname + '/collections.html')
var html = read(__dirname + '/index.html')
var Xray = require('..')
var x = Xray()

x(html, {
title: '.title',
image: 'img@src',
tags: ['li']
})(function(err, obj) {
console.log(obj);
})(function (err, obj) {
console.log(obj)
})
6 changes: 3 additions & 3 deletions test.js → examples/example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var Xray = require('./');
var x = Xray();
var Xray = require('./')
var x = Xray()

x('http://google.com', {
main: 'title',
image: x('https://images.google.com', 'title')
})(function(err, obj) {
})(function (err, obj) {
console.log(obj); // => { main: 'Google', image: 'Google Images' }
})
8 changes: 0 additions & 8 deletions examples/selector.js

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 6d30434

Please sign in to comment.