var xml2js =require('xml2js'); var fs = require('fs'); var Evt = require('evt'); var File = require('file'); var Note = require('note'); var Web = require('web'); var Tag = require('tag'); var async = require('async'); var Common = require('common'); var Import = { /* '\n \n \n
简单简历模板
\n
http://tympanus.net/Development/FullscreenLayoutPageTransitions/
\n
优化, 上线
\n

\n
做一个slide的vcard也行啊, 用slide去做
\n

\n
\n
颜色变换, 几套主题
\n
中间弄个头像
\n
开始的时候, 头像转转, 随后, 4个卡处渐隐出现
\n

\n
\n' 把中间部分抽出 parsedRes = [{FileId: ""}] */ parseEvernoteContent: function(xml, parsedRes) { var me = this; if(!xml) { return ''; } // console.log(xml); var ret = /([\s\S]*)<\/en\-note>/.exec(xml) var content = ''; if(ret && ret.length == 2) { content = ret[1]; } if(!content) { return ''; } var reg = new RegExp("", "g"); // var i = 0; // console.log(content); while(ret = reg.exec(content)) { // ret[1] == type="text/html" style="cursor:pointer;" height="43" hash="bc322a11075e40f3a5b2dce3f2fffcdc" try { var res = parsedRes[i]; i++; var fileId = res['FileId']; if(!fileId) { continue; } if(res.IsImage) { var replace = ''; } else { var replace = '' + res['Title'] + '' } content = content.replace(ret[0], replace); } catch(e) { console.log(e); }; }; // 如果是, 匹配不到 content = content.replace(/<\/en-media>/g, ''); return content; }, writeTest: function() { var path = '/Users/life/Desktop/1428226025985_2.png.txt'; fs.readFile(path, function(err, xml) { File.writeBase64(xml+"", true, 'png', 'a.png', function(file) { }); }); }, parseEachNote: function(note, callback) { var me = this; var jsonNote = { Title: note['title'][0], Tags: note['tag'] || [], Content: note['content'][0] }; // 文件保存之 var resources = note['resource'] || []; var parsedRes = []; var attachs = []; console.log("-----------") console.log(note); console.log(resources); async.eachSeries(resources, function(res, cb) { // console.log(res['data'][0]['$']); /* { data: [ { _: 'base64字符中','$': { encoding: 'base64' } } ], mime: [ 'image/png' ], width: [ '2398' ], height: [ '1466' ], resource-attributes: [ { 'file-name': ['Leanote.html'] } ] } */ try { var base64Str = res['data'][0]['_']; } catch(e) { parsedRes.push({}) return cb(); } try { var filename = res['resource-attributes'][0]['file-name'][0]; } catch(e) {} var isImage = false; var type = ''; var mime = res['mime'][0].toLowerCase(); if(mime.indexOf('image') == 0) { isImage = true; } type = mime.split('/').pop(); if(type == '') { type = 'raw'; } // console.log(base64Str); // console.log(isImage + ""); // console.log(type); // return cb(); File.writeBase64(base64Str, isImage, type, filename, function(file) { if(file) { parsedRes.push(file); if(!isImage) { attachs.push(file); } } else { console.log('文件保存错误!'); } cb(); }); }, function() { // 把content的替换之 // console.log('ok, writeBase64 ok'); try { console.log(parsedRes); jsonNote.Content = me.parseEvernoteContent(jsonNote.Content, parsedRes); jsonNote.Attachs = attachs; } catch(e) { console.log(e); } console.log(jsonNote); return callback && callback(jsonNote); } ); }, // callback 是全局的 // eachFileCallback是每一个文件的 // eachNoteFileCallback是每一个笔记的 importFromEvernote: function(notebookId, filePaths, callback, eachFileCallback, eachNoteCallback) { var me = this; var filePaths = filePaths.split(';'); async.eachSeries(filePaths, function(path, cb) { fs.readFile(path, function(err, xml) { me.parseEvernote(notebookId, xml + '', function(ret) { // 单个文件完成 eachFileCallback(ret, path) cb(); }, // 单个笔记 function(ret) { eachNoteCallback(ret); }); }); }, function() { // 全部完成 callback(true); }); }, // 解析evernote xml parseEvernote: function (notebookId, xml, callback, eachCallback) { var me = this; xml2js.parseString(xml, function (err, result) { // console.log(result); if(err) { return callback(false); } try { var notes = result['en-export']['note']; } catch(e) { return callback(false); } async.eachSeries(notes, function(note, cb) { me.parseEachNote(note, function(jsonNote) { if(jsonNote) { try { // 保存到数据库中 jsonNote.NoteId = Common.objectId(); jsonNote._id = jsonNote.NoteId; jsonNote.IsNew = true; jsonNote.NotebookId = notebookId; jsonNote.Desc = ''; // jsonNote.Content = ""; // jsonNote.Attachs = null; // console.log(jsonNote); // eachCallback && eachCallback(false); // return cb(); // 添加tags if(jsonNote.Tags && jsonNote.Tags.length > 0) { for(var h = 0; h < jsonNote.Tags.length; ++h) { var tagTitle = jsonNote.Tags[h]; Tag.addOrUpdateTag(tagTitle, function(tag) { Web.addTag(tag); }); } } Note.updateNoteOrContent(jsonNote, function(insertedNote) { eachCallback && eachCallback(insertedNote); cb(); }); } catch(e) { console.log(e); } } cb(); }); }, function() { callback && callback(true); }); }); } }; module.exports = Import;