Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Last active Dec 19, 2015
Embed
What would you like to do?
node_modules
{
"name": "wp2txt",
"version": "0.0.0",
"description": "",
"main": "wp2txt.js",
"dependencies": {
"mkdirp": "~0.3.5",
"xml2js": "~0.2.8",
"commander": "~1.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/67fa2c68f2626ff4ecb4.git"
},
"author": "",
"license": "MIT",
"gitHead": "06e87e6ca5ddfad460dad11d0fcc292800119757",
"readmeFilename": "README.md"
}
#!/usr/bin/env node
// Node
var fs = require('fs');
var path = require('path');
// NPM
var xml2js = require('xml2js');
var mkdirp = require('mkdirp');
var program = require('commander');
// Local
var pkg = require('./package.json');
program
.version(pkg.version)
.usage('[options] <file>')
.option('-d, --debug', "enable debugger")
.parse(process.argv);
var wpfile = program.args[0];
var json2text = function(item) {
var output = "";
output += '---';
output += '\ntitle: '+item['title'][0];
output += '\npost_name: '+item['wp:post_name'][0];
output += '\nstatus: '+item['wp:status'];
output += '\ndate: \''+item['wp:post_date']+'\'';
// date!!!
output += '\ntags: [ \''+item['category'][0]._+'\' ]';
output += '\n---\n';
output += item['content:encoded'][0];
return output;
};
var parser = new xml2js.Parser();
fs.readFile(wpfile, function(err, data) {
parser.parseString(data);
});
parser.on('end', function(result) {
result.rss.channel[0].item.forEach(function(item) {
var slug = item['wp:post_name'][0];
var relpath = item['link'][0]
.replace('http://blog.hypercubed.com', '')
.replace(slug+'/', '');
var filename = slug+".html";
var dirpath = path.join(__dirname, relpath);
var filepath = path.join(dirpath, filename);
mkdirp(dirpath, function(err) {
if(err) {
console.log(err);
} else {
fs.writeFile(filepath, json2text(item), function(err) {
if(err) {
console.log(err);
} else {
console.log(filepath,"was saved!");
}
});
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment