Skip to content

Instantly share code, notes, and snippets.

@GregHilston
Forked from fgeorges/blog--gist-to-png-01.js
Last active April 18, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GregHilston/2d848165b936a79c20a2424b75890940 to your computer and use it in GitHub Desktop.
Save GregHilston/2d848165b936a79c20a2424b75890940 to your computer and use it in GitHub Desktop.
This Gist assists you in creating a screen shot of a Gist. I use it to post screen shots on LinkedIn.
#!/usr/local/bin/phantomjs
var page = require('webpage').create();
var system = require('system');
// ***** Argument handling
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
if ( system.args.length !== 4 ) {
console.log('Usage: gist-to-png.js <gist-ID> <lines> <filename>');
console.log(' <gist-ID> - the ID of the Gist, from the URL, e.g. 2ab741a36788a38b459e');
console.log(' <lines> - number of lines in the Gist');
console.log(' <filename> - output file');
phantom.exit();
}
var id = system.args[1];
var lines = system.args[2];
var output = system.args[3];
var url = 'https://gist.github.com/GregHilston/' + id;
if ( ! isInt(lines) ) {
console.log('The number of lines is not an integer: ' + lines);
phantom.exit();
}
// ***** Processing
// the portion of the page to take a screenshot of
page.clipRect = {
top: 275,
left: 10,
width: 790,
height: ( 18 * parseInt(lines, 10) ) + 2
};
page.open(url, function(status) {
if (status !== 'success') {
console.log('FAIL to load the URL: ' + url);
}
else {
page.render(output);
phantom.exit();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment