2014-09-21 22:09:54 +08:00
|
|
|
(function(){var util={},position={},ui={},doc=window.document,re=window.RegExp,nav=window.navigator,SETTINGS={lineLength:72},uaSniffed={isIE:/msie/.test(nav.userAgent.toLowerCase()),isIE_5or6:/msie 6/.test(nav.userAgent.toLowerCase())||/msie 5/.test(nav.userAgent.toLowerCase()),isOpera:/opera/.test(nav.userAgent.toLowerCase())};var defaultsStrings={bold:"Strong <strong> Ctrl+B",boldexample:"strong text",italic:"Emphasis <em> Ctrl+I",italicexample:"emphasized text",link:"Hyperlink <a> Ctrl+L",linkdescription:"enter link description here",linkdialog:'<p><b>Insert Hyperlink</b></p><p>http://example.com/ "optional title"</p>',quote:"Blockquote <blockquote> Ctrl+Q",quoteexample:"Blockquote",code:"Code Sample <pre><code> Ctrl+K",codeexample:"enter code here",image:"Image <img> Ctrl+G",imagedescription:"enter image description here",imagedialog:"<p><b>Insert Image</b></p><p>http://example.com/images/diagram.jpg \"optional title\"<br><br>Need <a href='http://www.google.com/search?q=free+image+hosting' target='_blank'>free image hosting?</a></p>",olist:"Numbered List <ol> Ctrl+O",ulist:"Bulleted List <ul> Ctrl+U",litem:"List item",heading:"Heading <h1>/<h2> Ctrl+H",headingexample:"Heading",hr:"Horizontal Rule <hr> Ctrl+R",undo:"Undo - Ctrl+Z",redo:"Redo - Ctrl+Y",redomac:"Redo - Ctrl+Shift+Z",help:"Markdown Editing Help"};var imageDefaultText="http://";var linkDefaultText="http://";Markdown.Editor=function(markdownConverter,idPostfix,options){options=options||{};if(typeof options.handler==="function"){options={helpButton:options}}options.strings=options.strings||{};if(options.helpButton){options.strings.help=options.strings.help||options.helpButton.title}var getString=function(identifier){return options.strings[identifier]||defaultsStrings[identifier]};idPostfix=idPostfix||"";var hooks=this.hooks=new Markdown.HookCollection;hooks.addNoop("onPreviewRefresh");hooks.addNoop("postBlockquoteCreation");hooks.addFalse("insertImageDialog");hooks.addFalse("insertLinkDialog");this.getConverter=function(){return markdownConverter};var that=this,panels;this.run=function(previewWrapper){if(panels)return;panels=new PanelCollection(idPostfix);var commandManager=new CommandManager(hooks,getString);var previewManager=new PreviewManager(markdownConverter,panels,function(){hooks.onPreviewRefresh()},previewWrapper);var undoManager,uiManager;if(!/\?noundo/.test(doc.location.href)){undoManager=new UndoManager(function(){previewManager.refresh();if(uiManager)uiManager.setUndoRedoButtonStates()},panels);this.textOperation=function(f){undoManager.setCommandMode();f();that.refreshPreview()}}uiManager=new UIManager(idPostfix,panels,undoManager,previewManager,commandManager,options.helpButton,getString);uiManager.setUndoRedoButtonStates();var forceRefresh=that.refreshPreview=function(){previewManager.refresh(true)};forceRefresh()}};function Chunks(){}Chunks.prototype.findTags=function(startRegex,endRegex){var chunkObj=this;var regex;if(startRegex){regex=util.extendRegExp(startRegex,"","$");this.before=this.before.replace(regex,function(match){chunkObj.startTag=chunkObj.startTag+match;return""});regex=util.extendRegExp(startRegex,"^","");this.selection=this.selection.replace(regex,function(match){chunkObj.startTag=chunkObj.startTag+match;return""})}if(endRegex){regex=util.extendRegExp(endRegex,"","$");this.selection=this.selection.replace(regex,function(match){chunkObj.endTag=match+chunkObj.endTag;return""});regex=util.extendRegExp(endRegex,"^","");this.after=this.after.replace(regex,function(match){chunkObj.endTag=match+chunkObj.endTag;return""})}};Chunks.prototype.trimWhitespace=function(remove){var beforeReplacer,afterReplacer,that=this;if(remove){beforeReplacer=afterReplacer=""}else{beforeReplacer=function(s){that.before+=s;return""};afterReplacer=function(s){that.after=s+that.after;return""}}this.selection=this.selection.replace(/^(\s*)/,beforeReplacer).replace(/(\s*)$/,afterReplacer)};Chunks.prototype.skipLines=function(nLinesBefore,nLinesAfter,findExtraNewlines){if(nLinesBefore===undefined){nLinesBefore=1}if(nLinesAfter===undefined){nLinesAfter=1}nLi
|
|
|
|
chunk.before=chunk.before.replace(/(\n|^)[ \t]+\n$/,"\n\n");if(!chunk.selection&&!/^[ \t]*(?:\n|$)/.test(chunk.after)){chunk.after=chunk.after.replace(/^[^\n]*/,function(wholeMatch){chunk.selection=wholeMatch;return""});fakeSelection=true}if(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(chunk.before)){if(commandMgr.doList){commandMgr.doList(chunk)}}if(/(\n|^)[ ]{0,3}>[ \t]+.*\n$/.test(chunk.before)){if(commandMgr.doBlockquote){commandMgr.doBlockquote(chunk)}}if(/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)){if(commandMgr.doCode){commandMgr.doCode(chunk)}}if(fakeSelection){chunk.after=chunk.selection+chunk.after;chunk.selection=""}};commandProto.doBlockquote=function(chunk,postProcessing){chunk.selection=chunk.selection.replace(/^(\n*)([^\r]+?)(\n*)$/,function(totalMatch,newlinesBefore,text,newlinesAfter){chunk.before+=newlinesBefore;chunk.after=newlinesAfter+chunk.after;return text});chunk.before=chunk.before.replace(/(>[ \t]*)$/,function(totalMatch,blankLine){chunk.selection=blankLine+chunk.selection;return""});chunk.selection=chunk.selection.replace(/^(\s|>)+$/,"");chunk.selection=chunk.selection||this.getString("quoteexample");var match="",leftOver="",line;if(chunk.before){var lines=chunk.before.replace(/\n$/,"").split("\n");var inChain=false;for(var i=0;i<lines.length;i++){var good=false;line=lines[i];inChain=inChain&&line.length>0;if(/^>/.test(line)){good=true;if(!inChain&&line.length>1)inChain=true}else if(/^[ \t]*$/.test(line)){good=true}else{good=inChain}if(good){match+=line+"\n"}else{leftOver+=match+line;match="\n"}}if(!/(^|\n)>/.test(match)){leftOver+=match;match=""}}chunk.startTag=match;chunk.before=leftOver;if(chunk.after){chunk.after=chunk.after.replace(/^\n?/,"\n")}chunk.after=chunk.after.replace(/^(((\n|^)(\n[ \t]*)*>(.+\n)*.*)+(\n[ \t]*)*)/,function(totalMatch){chunk.endTag=totalMatch;return""});var replaceBlanksInTags=function(useBracket){var replacement=useBracket?"> ":"";if(chunk.startTag){chunk.startTag=chunk.startTag.replace(/\n((>|\s)*)\n$/,function(totalMatch,markdown){return"\n"+markdown.replace(/^[ ]{0,3}>?[ \t]*$/gm,replacement)+"\n"})}if(chunk.endTag){chunk.endTag=chunk.endTag.replace(/^\n((>|\s)*)\n/,function(totalMatch,markdown){return"\n"+markdown.replace(/^[ ]{0,3}>?[ \t]*$/gm,replacement)+"\n"})}};if(/^(?![ ]{0,3}>)/m.test(chunk.selection)){this.wrap(chunk,SETTINGS.lineLength-2);chunk.selection=chunk.selection.replace(/^/gm,"> ");replaceBlanksInTags(true);chunk.skipLines()}else{chunk.selection=chunk.selection.replace(/^[ ]{0,3}> ?/gm,"");this.unwrap(chunk);replaceBlanksInTags(false);if(!/^(\n|^)[ ]{0,3}>/.test(chunk.selection)&&chunk.startTag){chunk.startTag=chunk.startTag.replace(/\n{0,2}$/,"\n\n")}if(!/(\n|^)[ ]{0,3}>.*$/.test(chunk.selection)&&chunk.endTag){chunk.endTag=chunk.endTag.replace(/^\n{0,2}/,"\n\n")}}chunk.selection=this.hooks.postBlockquoteCreation(chunk.selection);if(!/\n/.test(chunk.selection)){chunk.selection=chunk.selection.replace(/^(> *)/,function(wholeMatch,blanks){chunk.startTag+=blanks;return""})}};commandProto.doCode=function(chunk,postProcessing){var hasTextBefore=/\S[ ]*$/.test(chunk.before);var hasTextAfter=/^[ ]*\S/.test(chunk.after);if(!hasTextAfter&&!hasTextBefore||/\n/.test(chunk.selection)){chunk.before=chunk.before.replace(/[ ]{4}$/,function(totalMatch){chunk.selection=totalMatch+chunk.selection;return""});var nLinesBack=1;var nLinesForward=1;if(/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)){nLinesBack=0}if(/^\n(\t|[ ]{4,})/.test(chunk.after)){nLinesForward=0}chunk.skipLines(nLinesBack,nLinesForward);if(!chunk.selection){chunk.startTag=" ";chunk.selection=this.getString("codeexample")}else{if(/^[ ]{0,3}\S/m.test(chunk.selection)){if(/\n/.test(chunk.selection))chunk.selection=chunk.selection.replace(/^/gm," ");else chunk.before+=" "}else{chunk.selection=chunk.selection.replace(/^(?:[ ]{4}|[ ]{0,3}\t)/gm,"")}}}else{chunk.trimWhitespace();chunk.findTags(/`/,/`/);if(!chunk.startTag&&!chunk.endTag){chunk.startTag=chunk.endTag="`";if(!chunk.selection){chunk.selection=this.getString("codeexample")}}else if(chunk.endTag&&!chunk.startTag){chunk.before+=chun
|