Skip to content

Commit

Permalink
support local paths - fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 4, 2014
1 parent f9613f8 commit ce39ffd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ function showHelp() {
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 ] [ todomvc.com 1024x768 480x320 ]
pageres --delay 3 1366x768 < urls.txt
pageres unicorn.html 1366x768
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
Options
-d, --delay <seconds> Delay capturing the screenshot
<url> can also be a local file path.
You can also pipe in a newline separated list of urls and screen resolutions which will get merged with the arguments. If no screen resolutions are specified it will fall back to the ten most popular ones according to w3counter.
*/}));
}
Expand Down
1 change: 1 addition & 0 deletions fixture/fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>unicorns</h1>
29 changes: 25 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var urlMod = require('url');
var slugifyUrl = require('slugify-url');
Expand Down Expand Up @@ -37,12 +38,32 @@ function runPhantomjs(options) {
return stream;
}

function toFileUrl(filename) {
var pathname = path.resolve(process.cwd(), filename).replace(/\\/g, '/');

if (pathname[0] !== '/') {
pathname = '/' + pathname;
}

return 'file://' + pathname;
};

function generateSizes(url, size, opts) {
url = url.replace(/^localhost/, 'http://$&');
url = urlMod.parse(url).protocol ? url : 'http://' + url;
var newUrl;

// check whether `url` is a local file since both local
// file `index.html` and url `todomvc.com` are supported
var isFile = fs.existsSync(url);

if (isFile) {
newUrl = toFileUrl(url);
} else {
newUrl = url.replace(/^localhost/, 'http://$&');
newUrl = urlMod.parse(newUrl).protocol ? newUrl : 'http://' + newUrl;
}

// make it a valid filename
var filenameUrl = slugifyUrl(url).replace(/^(?:https?:\/\/)?www\./, '');
var filenameUrl = slugifyUrl(isFile ? url : newUrl).replace(/^(?:https?:\/\/)?www\./, '');

var filename = filenameUrl + '-' + size + '.png';
var dim = size.split(/x/i);
Expand All @@ -52,7 +73,7 @@ function generateSizes(url, size, opts) {
};

var stream = runPhantomjs([defaults, opts, {
url: url,
url: newUrl,
width: dim[0],
height: dim[0]
}].reduce(assign));
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# <img src="logo.png" width="500" alt="pageres">

> Get screenshots of the websites in different resolutions
> Get screenshots of websites in different resolutions.
[![Build Status](https://travis-ci.org/sindresorhus/pageres.svg?branch=master)](https://travis-ci.org/sindresorhus/pageres)

Expand Down Expand Up @@ -40,11 +40,14 @@ Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 ] [ todomvc.com 1024x768 480x320 ]
pageres --delay 3 1366x768 < urls.txt
pageres unicorn.html 1366x768
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
Options
-d, --delay <seconds> Delay capturing the screenshot
<url> can also be a local file path.
You can also pipe in a newline separated list of urls and screen resolutions which will get merged with the arguments. If no screen resolutions are specified it will fall back to the ten most popular ones according to w3counter.
```

Expand Down
20 changes: 19 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ it('should generate screenshots', function (cb) {
});

it('should remove special characters from the URL to create a valid filename', function (cb) {
var items =[{
var items = [{
url: 'http://www.microsoft.com/?query=pageres*|<>:"\\',
sizes: '1024x768'
}];
Expand All @@ -59,3 +59,21 @@ it('should have a `delay` option', function (cb) {
});
});
});

it('should support local relative files', function (cb) {
var items = [{
url: 'fixture/fixture.html',
sizes: ['1024x768']
}];

pageres(items, function (err, streams) {
assert(!err, err);

assert.strictEqual(streams[0].filename, 'fixture!fixture.html-1024x768.png');

streams[0].once('data', function (data) {
assert(data.length > 1000);
cb();
});
});
});

0 comments on commit ce39ffd

Please sign in to comment.