diff --git a/cli.js b/cli.js index 07c8875..3b79b3f 100755 --- a/cli.js +++ b/cli.js @@ -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 Delay capturing the screenshot + 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. */})); } diff --git a/fixture/fixture.html b/fixture/fixture.html new file mode 100644 index 0000000..630b772 --- /dev/null +++ b/fixture/fixture.html @@ -0,0 +1 @@ +

unicorns

diff --git a/index.js b/index.js index bf310b8..1a929cd 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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); @@ -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)); diff --git a/readme.md b/readme.md index 29bcfc1..4a2afd6 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # 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) @@ -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 Delay capturing the screenshot + 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. ``` diff --git a/test.js b/test.js index bfce154..974f7f0 100644 --- a/test.js +++ b/test.js @@ -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' }]; @@ -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(); + }); + }); +});