Skip to content

Commit 9228ee0

Browse files
basic repl working
1 parent 80414cb commit 9228ee0

File tree

4 files changed

+232
-24
lines changed

4 files changed

+232
-24
lines changed

bin/x-ray

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

33
var Crawler = require('x-ray-crawler');
44
var Context = require('http-context');
5+
var cheerio = require('cheerio');
56
var pj = require('prettyjson');
7+
var isUrl = require('is-url');
68
var chalk = require('chalk');
79
var repl = require('repl');
810
var Xray = require('../');
11+
var fs = require('fs');
912
var slice = [].slice;
1013
var actions = {};
1114

15+
/**
16+
* Locals
17+
*/
18+
19+
var highlight = require('../lib/highlight');
20+
var isHTML = require('../lib/is-html');
21+
var pretty = require('../lib/pretty');
22+
var params = require('../lib/params');
23+
1224
/**
1325
* Default state
1426
*/
@@ -25,9 +37,10 @@ var help = [
2537
'',
2638
' usage: ',
2739
'',
28-
' load <url>',
29-
' paginate <attr>',
30-
' select ($) <attr>',
40+
' load <url|selector|html>',
41+
' paginate <attribute>',
42+
' select [scope] <selector>',
43+
' html',
3144
''
3245
].join('\n');
3346

@@ -80,38 +93,60 @@ actions.load = function load(url, fn) {
8093
return fn(null, help);
8194
}
8295

83-
request(url, function(err, res) {
84-
if (err) return fn(err);
85-
ctx.status = res.status;
86-
ctx.body = res.body;
87-
success('loaded: %s', res.url);
96+
if (isUrl(url)) {
97+
success('loading: %s', url);
98+
request(url, function(err, res) {
99+
if (err) return fn(err);
100+
ctx.status = res.status;
101+
ctx.body = res.body;
102+
success('loaded: %s', res.url);
103+
console.log(pretty(res.url));
104+
fn(null);
105+
})
106+
} else if (isHTML(url)) {
107+
success('loading: <html string>');
108+
ctx.status = 200;
109+
ctx.body = url;
110+
success('loaded: <html string>');
111+
console.log(pretty(url));
88112
fn(null);
89-
})
113+
} else {
114+
success('loading: %s', url);
115+
fs.readFile(url, 'utf8', function(err, str) {
116+
if (err) return fn(err);
117+
ctx.status = 200;
118+
ctx.body = str;
119+
success('loaded: %s', url);
120+
response(pretty(str));
121+
fn(null);
122+
})
123+
}
90124
}
91125

92126
/**
93127
* Select
94128
*/
95129

96130
actions.select =
97-
actions.$ = function select(selector, fn) {
131+
actions.$ = function select(scope, selector, fn) {
98132
if (arguments.length == 1) {
99-
fn = url;
100-
return fn(null, help);
101-
} else if (!ctx.body) {
102-
error('nothing to select, try load <url> first');
103-
return fn(null);
133+
response(ctx.body);
134+
fn(null);
135+
return;
136+
} else if (arguments.length == 2) {
137+
fn = selector;
138+
selector = coerce(scope);
139+
scope = null;
104140
} else {
105-
xray(ctx.body, selector)(function(err, obj) {
106-
if (err) {
107-
error(err.toString())
108-
return fn(err);
109-
}
110-
111-
console.log(unstring(JSON.stringify(obj, true, 2)));
112-
fn(null);
113-
});
141+
scope = coerce(scope);
142+
selector = coerce(selector);
114143
}
144+
145+
var $ = cheerio.load(ctx.body);
146+
$ = highlight($, scope, selector);
147+
var html = $.html();
148+
response(html);
149+
fn(null);
115150
}
116151

117152
/**
@@ -131,6 +166,16 @@ function unstring(str) {
131166
return str.replace(/^['"]|['"]$/g, '');
132167
}
133168

169+
/**
170+
* coerce
171+
*/
172+
173+
function coerce(str) {
174+
return '[' == str[0] && ']' == str[str.length - 1]
175+
? [str.slice(1, -1)]
176+
: str;
177+
}
178+
134179
/**
135180
* Success
136181
*/
@@ -140,6 +185,11 @@ function success(str) {
140185
console.error.apply(console.error, [chalk.green(' ' + str)].concat(args));
141186
}
142187

188+
function response(str) {
189+
var args = slice.call(arguments).slice(1);
190+
console.error.apply(console.error, [chalk.gray(' ' + str)].concat(args));
191+
}
192+
143193
/**
144194
* Error
145195
*/

lib/highlight.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Module Dependencies
3+
*/
4+
5+
var debug = require('debug')('highlight');
6+
var parse = require('x-ray-parse');
7+
var chalk = require('chalk');
8+
var isArray = Array.isArray;
9+
10+
/**
11+
* Export `highlight`
12+
*/
13+
14+
module.exports = highlight;
15+
16+
/**
17+
* Initialize `highlight`
18+
*
19+
* @param {Cheerio} $
20+
* @param {String} scope
21+
* @param {String} selector
22+
* @return {String}
23+
* @api public
24+
*/
25+
26+
function highlight($, scope, selector) {
27+
var array = isArray(selector)
28+
var obj = parse(array ? selector[0] : selector);
29+
obj.attribute = obj.attribute || 'text';
30+
31+
if (!obj.selector) {
32+
obj.selector = scope;
33+
scope = null;
34+
}
35+
36+
return find($, scope, array ? [obj.selector] : obj.selector, obj.attribute)
37+
}
38+
39+
/**
40+
* Find the node(s)
41+
*
42+
* @param {Cheerio} $
43+
* @param {String} scope
44+
* @param {String|Array} selector
45+
* @param {String} attr
46+
* @return {Array|String}
47+
*/
48+
49+
function find($, scope, selector, attr) {
50+
if (scope && isArray(selector)) {
51+
var $scope = select($, scope);
52+
$scope.map(function(i) {
53+
var $el = $scope.eq(i);
54+
var $children = select($el, selector[0]);
55+
$children.map(function(i) {
56+
attribute($children.eq(i), attr);
57+
});
58+
});
59+
return $;
60+
} else if (scope) {
61+
var $scope = select($, scope);
62+
attribute($scope.find(selector).eq(0), attr);
63+
return $;
64+
} else {
65+
if (isArray(selector)) {
66+
var $selector = select($, selector[0]);
67+
$selector.map(function(i) {
68+
attribute($selector.eq(i), attr);
69+
})
70+
return $;
71+
} else {
72+
var $selector = select($, selector);
73+
attribute($selector.eq(0), attr);
74+
return $;
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Selector abstraction, deals
81+
* with various instances of $
82+
*
83+
* @param {Cheerio} $
84+
* @param {String} selector
85+
* @return {Cheerio}
86+
*/
87+
88+
function select($, selector) {
89+
if ($.is && $.is(selector)) return $;
90+
return $.find ? $.find(selector) : $(selector);
91+
}
92+
93+
/**
94+
* Select the attribute based on `attr`
95+
*
96+
* @param {Cheerio} $
97+
* @param {String} attr
98+
* @return {String}
99+
*/
100+
101+
function attribute($el, attr) {
102+
switch(attr) {
103+
case 'html':
104+
var html = $el.html();
105+
html = chalk.red(html);
106+
$el.html(html);
107+
break;
108+
case 'text':
109+
var text = $el.text();
110+
text = chalk.red(text);
111+
$el.text(text);
112+
break;
113+
default:
114+
var val = $el.attr(attr);
115+
$el.attr(attr, chalk.red(val));
116+
break;
117+
}
118+
}

lib/pretty.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Module Dependencies
3+
*/
4+
5+
var pd = require('pretty-data').pd;
6+
7+
/**
8+
* Export `pretty`
9+
*/
10+
11+
module.exports = pretty;
12+
13+
/**
14+
* Parse the pretty data
15+
*/
16+
17+
function pretty(html) {
18+
return pd.xml(html).replace(/\n/g, '\n ');
19+
}

test.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>X-ray Test</title>
5+
</head>
6+
<body>
7+
<h1>X-ray</h1>
8+
<p>
9+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur voluptatem atque eveniet adipisci eligendi aperiam, ducimus voluptate magnam, asperiores, quas ex debitis unde porro, sunt quasi! Porro reiciendis quasi omnis?
10+
</p>
11+
<h2>Subheader</h2>
12+
<ul class="list">
13+
<li>A</li>
14+
<li>B</li>
15+
<li>C</li>
16+
</ul>
17+
<p>
18+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque commodi similique facilis molestias quasi, quaerat totam temporibus numquam eligendi enim voluptatum recusandae, amet voluptate possimus. Quas iure sequi molestias nisi?
19+
</p>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)