Skip to content

Instantly share code, notes, and snippets.

@Swaagie
Last active August 29, 2015 14:02
Show Gist options
  • Save Swaagie/4ce46905d323100d0016 to your computer and use it in GitHub Desktop.
Save Swaagie/4ce46905d323100d0016 to your computer and use it in GitHub Desktop.
var fs = require('fs')
, path = require('path')
, request = require('request')
, cheerio = require('cheerio');
//
// Create target directory
//
if (!fs.existsSync(__dirname + '/target')) fs.mkdirSync(__dirname + '/target');
//
// Read all markdown files.
//
fs.readdirSync(__dirname).forEach(function each(file, i) {
if (path.extname(file) !== '.eco' && path.extname(file) !== '.md') return;
console.log('Processing file: ' + i);
var content = fs.readFileSync(path.join(__dirname, file), 'utf-8')
, github = ''
, author = ''
, module = ''
, license = ''
, found = false
, alternative = false
, original;
content.replace(/\[[^\]]+\]\((https?:\/\/w?w?w?\.?github\.com([^)]+[^\/)#]+\/[^)#]+))\)/i, function replace(full, git, repo) {
found = true;
github = git;
repo = repo.split('/').filter(Boolean);
author = repo[0];
module = repo[1];
});
//
// Check for alternative module linking via @npm()
//
if (!found) {
content.replace(/repo: \&repo ([^\n]+)/i, function replace(full, repo) {
alternative = true;
github = 'https://github.com/' + repo;
repo = repo.split('/').filter(Boolean);
author = repo[0];
});
content.replace(/name: \&name ([^\n]+)/i, function replace(full, mod) {
module = mod;
});
if (!alternative) return console.log('Found nothing to replace in: ' + file);
}
request('https://raw.githubusercontent.com/' + author + '/' + module + '/master/package.json', function done (error, res, body) {
if (res.statusCode == 200) {
try {
body = JSON.parse(body);
if (body.name.toLowerCase() !== module.toLowerCase()) {
original = body.name;
console.log('Module name mismatch, using: ' + module);
}
} catch (e) { }
}
request('http://browsenpm.org/package/' + (original ? original : module).toLowerCase(), function done (error, res, body) {
var $ = cheerio.load(body)
, pkg = $('code[data-pagelet-fragment="package"]')
, target;
if (!!~pkg.html().indexOf('Invalid status code: 404')) return console.log('Skipping module: ' + author + '/' + module);
pkg = cheerio.load(pkg.html().slice(5, -5));
license = pkg('header aside.row .part strong').text();
target = !alternative
? /\[[^\]]+\]\((https?:\/\/w?w?w?\.?github\.com([^)]+[^\/)#]+\/[^)#]+))\)/i
: '<%- @npm() %>';
content = content.replace(target, function replace() {
return [
'[',
module,
'][browsenpm] (Github: [',
author,
'/',
module,
'][github], License: ',
license,
')'
].join('')
}).replace(new RegExp('\\(' + github + '\\)', 'g'), '[github]').replace(/\n+$/, [
'\n\n[browsenpm]: http://browsenpm.org/package/',
original ? original : module,
'\n[github]: https://github.com/',
author,
'/',
module
].join(''));
console.log('Writing file:' + file);
fs.writeFileSync(__dirname + '/target/' + file, content);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment