-
-
Save Hypercubed/67fa2c68f2626ff4ecb4 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
node_modules |
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
{ | |
"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" | |
} |
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/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