Last active
April 18, 2018 18:24
-
-
Save fgeorges/fc31ca3c5da31ad11878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/fgeorges/' + 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
Used to generate a screenshot of any piece of code, in order to publish code on LinkedIn Pulse articles. Explanations on https://www.linkedin.com/pulse/include-code-snippet-pulse-articles-florent-georges.