Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Last active August 29, 2015 14:04
Show Gist options
  • Save mytharcher/6fadb5f2fc6a39efcdab to your computer and use it in GitHub Desktop.
Save mytharcher/6fadb5f2fc6a39efcdab to your computer and use it in GitHub Desktop.
Node scripts
var fs = require('fs');
var path = require('path');
var ENCODING = 'utf8';
var target = process.argv[2];
fs.readdirSync(target).forEach(function (file) {
var filePath = path.join(__dirname, target, file);
var content = fs.readFileSync(filePath, ENCODING)
.replace(/\[!\[\]\((.+)\)\].+\n([^\s]+)/g, function (matcher, url, caption) {
// console.log('>>>', url);
return '<figure>' +
'<img src="' + url.replace(/\/s\d+\//, '/s400/') + '" height="267" width="400" data-origin-width="600" data-origin-height="400" />' +
'<figcaption>' + caption + '</figcaption>' +
'</figure>';
});
// console.log(content);
fs.writeFileSync(filePath, content);
});
/**
* Usage:
* cat some/file.txt | node unescapeHTML.js > some/file.unescaped.txt
*/
process.stdin.setEncoding('utf8');
var content = '';
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
content += chunk;
}
});
process.stdin.on('end', function() {
process.stdout.write(content.replace(/&#(\d+);/g, function (matcher, code) {
return String.fromCharCode(parseInt(code, 10));
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment