1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| var xml2js = require('xml2js'), async = require('async'), tomd = require('to-markdown').toMarkdown, request = require('request'), file = require('fs');
var captialize = function(str){ return str[0].toUpperCase() + str.substring(1); }; function replaceTwoBrace(str){ str = str.replace(/{{/g, '{ {'); return str; }; function replaceHTMLEntity(str){ str = str.replace(/amp;/g, ''); str = str.replace(/</g, '<'); str = str.replace(/>/g, '>'); str = str.replace(/"/g, '"'); str = str.replace(/& str = str.replace(/& return str; }; function replaceCodeTag(str){ str = str.replace(/(<pre class=).*?>/g, '<pre>'); str = str.replace(/\<pre\>/gi, '```\r\n'); str = str.replace(/\<\/pre\>/gi, '```'); //str = str.replace(/<pre>(.*)<\/pre>/ig, '```$1```'); //console.log(str); // str = str.replace(/\[python\]/gi, '```'); // str = str.replace(/\[\/python\]/gi, '```'); // str = str.replace(/\[java\]/gi, '```'); // str = str.replace(/\[\/java\]/gi, '```'); // str = str.replace(/\[php\]/gi, '```'); // str = str.replace(/\[\/php\]/gi, '```'); // str = str.replace(/\[c\]/gi, '```'); // str = str.replace(/\[\/c\]/gi, '```'); return str; }; hexo.extend.migrator.register('wordpress', function(args, callback){ var source = args._.shift();
if (!source){ var help = [ 'Usage: hexo migrate wordpress <source>', '', 'For more help, you can check the docs: http://hexo.io/docs/migration.html' ];
console.log(help.join('\n')); return callback(); }
var log = hexo.log, post = hexo.post;
log.i('Analyzing %s...', source);
async.waterfall([ function(next){ // URL regular expression from: http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the if (source.match(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*) request(source, function(err, res, body){ if (err) throw err; if (res.statusCode == 200) next(null, body); }); } else { file.readFile(source, next); } }, function(content, next){ xml2js.parseString(content, next); }, function(xml, next){ var count = 0;
async.each(xml.rss.channel[0].item, function(item, next){ if (!item['wp:post_type']){ return next(); } //console.log(item.title[0]); var title = item.title[0], id = item['wp:post_id'][0], date = item['wp:post_date'][0], slug = item['wp:post_name'][0], content = item['content:encoded'][0], comment = item['wp:comment_status'][0], status = item['wp:status'][0], type = item['wp:post_type'][0], categories = [], tags = [];
if (!title && !slug) return next(); if (type !== 'post' && type !== 'page') return next(); if (typeof content !== 'string') content = ''; content = replaceTwoBrace(content); content = replaceHTMLEntity(content); content = replaceCodeTag(content); content = tomd(content).replace(/\r\n/g, '\n'); count++;
if (item.category){ item.category.forEach(function(category, next){ var name = category._;
switch (category.$.domain){ case 'category': categories.push(name); break;
case 'post_tag': tags.push(name); break; } }); }
var data = { title: title || slug, id: +id, date: date, content: content, layout: status === 'draft' ? 'draft' : 'post', };
if (type === 'page') data.layout = 'page'; if (slug) data.slug = slug.toString(); if (comment === 'closed') data.comment = false; if (categories.length && type === 'post') data.categories = categories; if (tags.length && type === 'post') data.tags = tags; log.i('%s found: %s', captialize(type), title); data.title = data.title.toString(); data.date = data.date.toString(); //console.log(data.slug); //console.log(data.date); post.create(data, next); }, function(err){ if (err) return next(err);
log.i('%d posts migrated.', count); }); } ], callback); });
|