// duckdown (0.0.1) // Simple, lightweight Markdown-like language with extensible grammar. // Christopher Giffard 2012 // // // Package built Tue Sep 11 2012 15:09:22 GMT+1000 (EST) // (function(e){function t(e){return window[{"./ducknode.js":"DuckdownNode","./duckdown.js":"Duckdown","./grammar.js":"DuckdownGrammar","../grammar":"DuckdownGrammar"}[e]]}(function(e){"use strict";var t={};t.wordCharacters=/[a-z0-9 ]/ig,t.escapeCharacters=/[^a-z0-9\s\-\_\:\\\/\=\%\~\`\!\@\#\$\*\(\)\+\[\]\{\}\|\;\,\.\?\']/ig,t.replacer=function(e,t,n){return e==="&"?"&":e==="<"?"<":e===">"?">":e==='"'?""":"&#x"+e.charCodeAt(0).toString(16)+";"},t.tokenMappings={"\n":{wrapper:!0,swallowTokens:!1,exit:/\n/,state:"IMPLICIT_BREAK",semanticLevel:"hybrid"}," ":{wrapper:!0,swallowTokens:!1,exit:/\n/,state:"IMPLICIT_INDENT",semanticLevel:"hybrid",allowSelfNesting:!0}," ":{wrapper:!0,swallowTokens:!1,exit:/\n/,state:"IMPLICIT_INDENT",semanticLevel:"hybrid",allowSelfNesting:!0},"&":{wrapper:!1,exit:/([^#a-z0-9]|;)/i,validIf:/^&#?[a-z0-9]+;$/ig,state:"ENTITY",semanticLevel:"text"},"~":{semanticLevel:"text",wrapper:!0,allowSelfNesting:!1,exit:/[\~\n]/,validIf:/^\~\S[^\n]+\S\~$/,state:"TEXT_EMPHASIS"},"*":{semanticLevel:"text",wrapper:!0,allowSelfNesting:!1,exit:/[\*\n]/,validIf:/^\*\S[^\n]+\S\*$/,state:"TEXT_STRONG"},"-":{state:"TEXT_DEL",wrapper:!0,semanticLevel:"text",exit:/[\-\n]/,validIf:/^\-\S[^\n]+\S\-$/,blankPrevSibling:!0},_:{wrapper:!0,semanticLevel:"text",exit:/[_\n]/,validIf:/^\_\S[^\n]+\S\_$/,state:"TEXT_UNDERLINE"},"<":{wrapper:!1,semanticLevel:"hybrid",exit:/[>\n]/,validIf:/^<[a-z][a-z0-9\-\_]*(\s[^>\n]+)*>$/i,state:"SPECIAL_FEATHER"},"`":{wrapper:!1,semanticLevel:"text",exit:/[`\n]/,state:"CODE_LITERAL"},'"':{wrapper:!1,semanticLevel:"text",exit:/["\n]/,validIf:/^\".*\"$/,state:"TEXT_QUOTE"},"* ":{wrapper:!0,exit:/\n/i,state:"LIST_UNORDERED",semanticLevel:"textblock",swallowTokens:!1},"* ":{wrapper:!0,exit:/\n/i,state:"LIST_UNORDERED",semanticLevel:"textblock",swallowTokens:!1},". ":{wrapper:!0,exit:/\n/i,state:"LIST_ORDERED",semanticLevel:"textblock",swallowTokens:!1},". ":{wrapper:!0,exit:/\n/i,state:"LIST_ORDERED",semanticLevel:"textblock",swallowTokens:!1},">":{wrapper:!0,exit:/\n/i,state:"BLOCKQUOTE",semanticLevel:"block",allowSelfNesting:"true",swallowTokens:!1,mustBeFirstChild:!0},"h1.":{wrapper:!0,exit:/\n/i,state:"HEADING_1",semanticLevel:"textblock"},"h2.":{wrapper:!0,exit:/\n/i,state:"HEADING_2",semanticLevel:"textblock"},"h3.":{wrapper:!0,exit:/\n/i,state:"HEADING_3",semanticLevel:"textblock"},"h4.":{wrapper:!0,exit:/\n/i,state:"HEADING_4",semanticLevel:"textblock"},"h5.":{wrapper:!0,exit:/\n/i,state:"HEADING_5",semanticLevel:"textblock"},"h6.":{wrapper:!0,exit:/\n/i,state:"HEADING_6",semanticLevel:"textblock"},"http://":{state:"AUTO_LINK",wrapper:!1,exit:/[^a-z0-9\-_\.\~\!\*\'\(\)\;\:\@\&\=\+\$\,\/\?\%\#\[\]\#]/i,validIf:/^http[s]?:\/\/[a-z0-9\-\.]+(\:\d+)?.*$/i,swallowTokens:!1,swallowWhitespace:!0,semanticLevel:"text"},"https://":{state:"AUTO_LINK",wrapper:!1,exit:/[^a-z0-9\-_\.\~\!\*\'\(\)\;\:\@\&\=\+\$\,\/\?\%\#\[\]\#]/i,validIf:/^http[s]?:\/\/[a-z0-9\-\.]+(\:\d+)?.*$/i,swallowTokens:!1,swallowWhitespace:!0,semanticLevel:"text"},"(":{state:"PAREN_DESCRIPTOR",wrapper:!0,exit:/(\s\s+|\n|\))/,validIf:/^\([^\n]+\)$/,allowSelfNesting:!0,semanticLevel:"text"},"-- ":{state:"CITATION",exit:/\n/,wrapper:!0,semanticLevel:"text",swallowTokens:!1},"--":{state:"HORIZONTAL_RULE",wrapper:!1,exit:/[^\-]/i,validIf:/^\-\-+\n*$/i,semanticLevel:"block",swallowTokens:!1}},t.stateList={ENTITY:{process:function(){},compile:function(e,t){var n=e.children[e.children.length-1];return e.exitToken===";"?"&"+e.children.join("")+";":typeof n=="string"&&n.match(/\;/)?"&"+e.children.join(""):"&"+t(e)}},IMPLICIT_BREAK:{process:function(e){if(!e.text().length&&!e.blockParent)return!1;for(var t=0;t\n":o,n}return n+"\n"}},IMPLICIT_INDENT:{process:function(e){if(!e.text().length)return!1},compile:function(e,t){var n=!1;if(!e.parent||e.previousSibling||e.parent.state==="IMPLICIT_BREAK"||e.parent.semanticLevel==="block"||e.parent.semanticLevel==="hybrid")e.parent?!e.parent.previousSibling&&!e.previousSibling&&e.parent.state!=="IMPLICIT_INDENT"?n=!0:e.parent.prevSiblingCulled&&e.parent.prevCulledSiblingState==="IMPLICIT_BREAK"&&(n=!0):n=!0;return n?"
"+e.raw(!0).replace(/^\s+/,"")+"
\n":t(e)}},TEXT_EMPHASIS:{compile:function(e,t){return""+t(e)+""}},TEXT_STRONG:{compile:function(e,t){return""+t(e)+""}},TEXT_DEL:{process:function(e){return e.previousSibling?typeof e.previousSibling=="string"&&e.previousSibling.match(/\s$/g)?!0:typeof e.previousSibling=="object"?!0:-1:!0},compile:function(e,t){return""+t(e)+""}},TEXT_QUOTE:{process:function(e){if(e.parent&&!e.parent.prevSiblingCulled&&e.parent.previousSibling&&e.parent.previousSibling.state===e.parent.state)return-1},compile:function(e,t){return""+t(e)+""}},TEXT_UNDERLINE:{compile:function(e,t){return""+t(e)+""}},CODE_LITERAL:{compile:function(e,t){return""+e.text()+""}},LIST_UNORDERED:{process:function(e){if(e.previousSibling)return-1;if(!e.parent)return-1;if(e.parent.previousSibling&&typeof e.parent.previousSibling=="object"&&e.parent.previousSibling.state!==e.parent.state&&!e.parent.prevSiblingCulled)return-1;if(e.rootBlock&&e.rootBlock.previousSibling&&!e.rootBlock.prevSiblingCulled&&e.rootBlock.previousSibling.blockType!==e.state&&e.rootBlock.previousSibling.blockType!=="LIST_ORDERED")return-1;var t=e,n=0;while(t)t.state==="IMPLICIT_INDENT"&&n++,t=t.parent;e.indentation=n;if(e.rootBlock&&e.rootBlock.previousSibling&&e.rootBlock.previousSibling.blockNode&&(e.rootBlock.previousSibling.semanticLevel==="hybrid"||e.rootBlock.previousSibling.semanticLevel==="textblock"||e.rootBlock.previousSibling.semanticLevel==="block")&&e.rootBlock.previousSibling.blockNode.indentation=e.indentation)break;r=r.children[r.children.length-1]}r.children.push(e),r.blockParent=!0,e.rootBlock.remove(),r.updateIndices(),r.children[e.index-1]&&(typeof r.children[e.index-1]!="object"||r.children[e.index-1].state!==e.state)&&(e.breakBefore=!0)}},compile:function(e,t){var n="";e.breakBefore&&(n+="\n");if(!e.parent.previousSibling||e.parent.prevSiblingCulled||!e.parent.previousSibling.blockParent||!e.parent.previousSibling.children.length||e.parent.previousSibling.children[0].state!=="LIST_UNORDERED"&&e.parent.previousSibling.children[0].state!=="IMPLICIT_INDENT")n+="\n";return n}},LIST_ORDERED:{process:function(e){if(e.previousSibling&&(typeof e.previousSibling=="object"||e.previousSibling.match(/[^a-z0-9]/g)))return-1;if(!e.parent)return-1;if(e.parent.previousSibling&&typeof e.parent.previousSibling=="object"&&e.parent.previousSibling.state!==e.parent.state&&!e.parent.prevSiblingCulled)return-1;if(e.rootBlock&&e.rootBlock.previousSibling&&!e.rootBlock.prevSiblingCulled&&e.rootBlock.previousSibling.blockType!==e.state&&e.rootBlock.previousSibling.blockType!=="LIST_UNORDERED")return-1;if(e.previousSibling&&e.previousSibling.match(/^[a-z0-9]+$/)&&e.index<2){e.listQualifier=e.previousSibling,e.parent.removeChild(e.index-1);var t=e,n=0;while(t)t.state==="IMPLICIT_INDENT"&&n++,t=t.parent;e.indentation=n;if(e.rootBlock&&e.rootBlock.previousSibling&&e.rootBlock.previousSibling.blockNode&&(e.rootBlock.previousSibling.semanticLevel==="hybrid"||e.rootBlock.previousSibling.semanticLevel==="textblock"||e.rootBlock.previousSibling.semanticLevel==="block")&&e.rootBlock.previousSibling.blockNode.indentation=e.indentation)break;r=r.children[r.children.length-1]}r.children.push(e),r.blockParent=!0,e.rootBlock.remove(),r.updateIndices(),r.children[e.index-1]&&(typeof r.children[e.index-1]!="object"||r.children[e.index-1].state!==e.state)&&(e.breakBefore=!0)}return!0}return-1},compile:function(e,t){var n="";e.breakBefore&&(n+="\n");if(!e.parent.previousSibling||e.parent.prevSiblingCulled||!e.parent.previousSibling.blockParent||!e.parent.previousSibling.children.length||e.parent.previousSibling.children[0].state!=="LIST_ORDERED"&&e.parent.previousSibling.children[0].state!=="IMPLICIT_INDENT"){var r="";e.listQualifier.match(/[ivxcmd]/ig)?r="lower-roman":e.listQualifier.match(/[a-z]/ig)&&(r="lower-alpha"),r=r.length?' style="list-style: '+r+';"':"",n+="\n"}n+="
  • "+t(e)+(e.blockParent?"\n":"")+"
  • \n";if(!e.parent.nextSibling||e.parent.nextSiblingCulled||!e.parent.nextSibling.blockParent||!e.parent.nextSibling.children.length||e.parent.nextSibling.children[0].state!=="LIST_ORDERED"&&e.parent.nextSibling.children[0].state!=="IMPLICIT_INDENT")n+="\n";return n}},BLOCKQUOTE:{process:function(e){if(e.previousSibling)return-1;var t=0,n=e,r;while(n)n.state===e.state&&t++,n=n.parent;e.indentation=t;if(e.rootBlock&&e.rootBlock.previousSibling&&(!e.rootBlock.prevSiblingCulled||e.rootBlock.prevCulledSiblingState===e.state)&&e.rootBlock.previousSibling.blockType===e.state&&e.rootBlock.previousSibling.blockNode){e.rootBlock.previousSibling.blockNode.indentation\n";var r=t(e);r.length&&(e.blockParent||(n+="

    "),n+=r,e.blockParent||(n+="

    \n"));if(!e.parent.nextSibling||e.parent.nextSiblingCulled||!e.parent.nextSibling.blockParent||!e.parent.nextSibling.children.length||e.parent.nextSibling.children[0].state!=="BLOCKQUOTE"&&e.parent.nextSibling.children[0].state!=="IMPLICIT_INDENT")n+="\n";return n}},CITATION:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return""+t(e)+""}},HEADING_1:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"

    "+t(e)+"

    "}},HEADING_2:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"

    "+t(e)+"

    "}},HEADING_3:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"

    "+t(e)+"

    "}},HEADING_4:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"

    "+t(e)+"

    "}},HEADING_5:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"
    "+t(e)+"
    "}},HEADING_6:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"
    "+t(e)+"
    "}},AUTO_LINK:{compile:function(e,t){var n=e.token+e.text(),r=n;if(e.parent&&e.parent.state==="PAREN_DESCRIPTOR"&&!!e.parent.link)return n;e.linkDetail&&(r=t(e.linkDetail));var i=''+r+"";return e.exitToken.match(/\s+/)&&!e.linkDetail&&(i+=" "),i}},PAREN_DESCRIPTOR:{process:function(e){e.previousSibling&&e.previousSibling.state==="AUTO_LINK"&&(e.previousSibling.linkDetail||(e.previousSibling.linkDetail=e,e.link=e.previousSibling))},compile:function(e,t){return e.link?"":"("+t(e)+")"}},HORIZONTAL_RULE:{process:function(e){if(e.previousSibling)return-1},compile:function(e,t){return"
    "}},SPECIAL_FEATHER:{process:function(e){if(!e.children.length)return-1;var t=e.children.join("").split(/\s+/ig),n=t.shift().replace(/\s+/ig,""),r={};if(!n.length)return-1;if(this.feathers[n]&&this.feathers[n]instanceof Object&&this.feathers[n].handler&&this.feathers[n].handler instanceof Function){e.children=[];var i=0,s=[],o=[];for(var u=0;u0&&this.children[e-1]instanceof r&&(this.children[e-1].nextSibling=this.children[e+1]||null),this.children.splice(e,1),this.updateIndices())},r.prototype.updateIndices=function(){s(this.children)},r.prototype.toString=function(){return"<"+this.state+":"+this.children.length+">"},typeof module!="undefined"&&module.exports?module.exports=r:typeof define!="undefined"?define("DuckdownNode",[],function(){return r}):e.DuckdownNode=r}(this),function(e){"use strict";function a(e){if(!n.stateList[e])return!1;if(!n.stateList[e].tokenGenus){for(var t in n.tokenMappings)if(n.tokenMappings.hasOwnProperty(t)&&n.tokenMappings[t].state===e)return n.stateList[e].tokenGenus=n.tokenMappings[t],n.tokenMappings[t];return!1}return n.stateList[e].tokenGenus}var n=t("./grammar.js"),r=t("./ducknode.js"),i=0,s=1,o=0,u=function(t){this.options=t instanceof Object?t:{},this.clear()};u.prototype.clear=function(){this.currentToken="",this.prevToken="",this.tokenPosition=0,this.parserStates=[],this.parserAST=[],this.parseBuffer=[],this.currentNode=null,this.prevNode=null,this.nodeDepth=0,this.whitespace=!1,this.prevNodeCulled=!1,this.prevCullState=null,this.characterIndex=0,this.tokeniserState=i,this.tokenBuffer="",this.tokens=[],this.curChar="",this.prevChar="",this.feathers={},this.tokenList=function(){var e=[];for(var t in n.tokenMappings)n.tokenMappings.hasOwnProperty(t)&&e.push(t);return e}(),this.longestToken=this.tokenList.sort(function(e,t){return t.length-e.length}).slice(0,1).pop().length,this.emit("clear")},u.prototype.tokenise=function(e){typeof e!="string"&&(e=String(e)),this.emit("tokenisestart"),this.tokens.length||(e="\n"+e);var t=!e||!e.length?0:e.length;this.prevChar="",this.curChar="";for(var r=0;r<=t;r++){this.curChar=e.charAt(r);var o=this.longestToken;o=o>t-r?t-r:o;for(;o>0;o--){var u=e.substr(r,o);if(n.tokenMappings[u]){this.tokenBuffer.length&&(this.tokens.push(this.tokenBuffer),this.tokenBuffer=""),this.tokens.push(u),r+=o-1;break}o===1&&(!this.curChar.match(n.wordCharacters)&&this.tokeniserState===s||this.curChar.match(n.wordCharacters)&&this.tokeniserState===i?this.tokenBuffer+=this.curChar:(this.tokenBuffer.length&&(this.tokens.push(this.tokenBuffer),this.tokenBuffer=""),this.tokenBuffer+=this.curChar,this.tokeniserState=[s,i][this.tokeniserState]))}this.characterIndex=r,this.prevChar=this.curChar}return this.tokenBuffer.length&&(this.tokens.push(this.tokenBuffer),this.tokenBuffer=""),this.emit("tokeniseend",this.tokens),this.tokens},u.prototype.parse=function(e,t){e&&typeof e=="string"&&this.tokenise(e),this.emit("parsestart");for(;this.tokenPositionr[n]?!1:t.semanticLevel==="textblock"&&n==="textblock"?!1:!0}function v(){var t=e.parseBuffer.filter(function(e){return!!e.replace(/\s+/ig,"").length});if(t.length)return t.pop();l=e.currentNode?e.currentNode.children:e.parserAST;if(l.length)return l[l.length-1]}var i,s,o,f,l,c=[],h;this instanceof u&&(e=this),t&&t.length&&(e.tokens.push(t),e.tokenPosition=e.tokens.length-1),e.currentToken=e.tokens[e.tokenPosition],e.emit("parsetoken",e.currentToken);for(var m=e.parserStates.length-1;m>=0;m--){i=e.parserStates[m],f=n.stateList[i];var g=a(i);g&&(f.exitCondition=g.exit);if(!f)throw new Error("State genus for the state "+i+" was not found! ("+e.parserStates.join(",")+")");if(f.exitCondition&&f.exitCondition.exec(e.currentToken)){while(i!==e.currentNode.state)e.currentNode.mismatched=!0,h=e.closeCurrentNode(!0),h&&h.length&&(c=c.concat(h));h=e.closeCurrentNode(),h&&h.length&&(c=c.concat(h))}}if(n.tokenMappings[e.currentToken]&&n.tokenMappings.hasOwnProperty(e.currentToken)){o=n.tokenMappings[e.currentToken],s=o.state,f=n.stateList[o.state];if(e.hasParseState(o.state)&&!o.allowSelfNesting)e.parseBuffer.push(e.currentToken);else{var y=v();if(p()&&d(o)&&(!o.blankPrevSibling||typeof y!="string"||!y.match(/\S$/i))){e.addParseState(o.state);var b=new r(o.state);b.stateStack=e.parserStates.slice(0),b.depth=e.nodeDepth,b.parent=e.currentNode,b.wrapper=o.wrapper,b.token=e.currentToken,b.parser=e,b.prevSiblingCulled=e.prevNodeCulled,b.prevCulledSiblingState=e.prevCullState,o.semanticLevel&&(b.semanticLevel=o.semanticLevel),y&&(b.previousSibling=y),b.previousSibling&&b.previousSibling instanceof r&&(b.previousSibling.nextSibling=b),e.currentNode?(e.currentNode.children.push.apply(e.currentNode.children,e.parseBuffer),e.currentNode.children.push(b)):(e.parserAST.push.apply(e.parserAST,e.parseBuffer),e.parserAST.push(b)),b.index=(e.currentNode?e.currentNode.children.length:e.parserAST.length)-1;if(o.semanticLevel==="block"||o.semanticLevel==="textblock"||o.state==="IMPLICIT_INDENT"){var w=e.currentNode;while(w!==null)w.blockParent=!0,w.blockType=o.state,w.parent||(w.blockNode=b,b.rootBlock=w),w=w.parent}e.parseBuffer=[],e.currentNode=b,e.prevNodeCulled=!1,e.prevCullState=null,e.nodeDepth++}else e.parseBuffer.push(e.currentToken)}}else e.currentToken&&e.currentToken.length&&e.parseBuffer.push(e.currentToken),e.tokenPosition>=e.tokens.length-1&&(e.currentNode?e.currentNode.children.push.apply(e.currentNode.children,e.parseBuffer):e.parserAST.push.apply(e.parserAST,e.parseBuffer),e.parseBuffer=[]);return e.currentToken&&e.currentToken.match(/\s+$/)?e.whitespace=!0:e.whitespace=!1,e.previousToken=e.currentToken,e.parserStates.concat(c).join(" ").toLowerCase().replace(/\_/ig,"-")},u.prototype.compile=function(e){var t=this;e&&this.parse(e),this.emit("compilestart");var i=function s(e){var i=[];i=e;var o="";e instanceof r&&(i=e.children);if(i instanceof Array){for(var u=0;u0&&s.parseBuffer.push(s.currentToken.substr(0,h)),s.currentNode.exitToken=c[0]),s.currentNode.children.push.apply(s.currentNode.children,s.parseBuffer),o&&o.validIf instanceof RegExp&&(o.validIf.exec(s.currentNode.raw())||(s.emit("nodeinvalid",s.currentNode,o.validIf,s.currentNode.raw()),s.currentNode.rootBlock&&(s.currentNode.rootBlock.blockParent=!1,s.currentNode.rootBlock.blockType=null,s.currentNode.rootBlock.blockNode=null),l=!0)),!l&&r.process&&r.process instanceof Function&&(f=r.process.call(s,s.currentNode),f===!1&&(s.emit("nodeselfdestruct",s.currentNode),s.currentNode.culled=!0,s.prevNodeCulled=!0,s.prevCullState=s.currentNode.blockType||s.currentNode.state,s.currentNode.previousSibling&&(s.currentNode.previousSibling.nextSiblingCulled=!0,s.currentNode.previousSibling.nextCulledSiblingState=s.currentNode.state,s.currentNode.previousSibling.nextSibling=null),s.currentNode.rootBlock&&(s.currentNode.rootBlock.blockParent=!1,s.currentNode.rootBlock.blockType=null,s.currentNode.rootBlock.blockNode=null)),f===-1&&(s.emit("nodeinvalid",s.currentNode),s.currentNode.rootBlock&&(s.currentNode.rootBlock.blockParent=!1,s.currentNode.rootBlock.blockType=null,s.currentNode.rootBlock.blockNode=null),l=!0)),s.currentNode.processed=!0;if(f===!1||l)u=s.currentNode.parent?s.currentNode.parent.children:s.parserAST,u.length--,l&&u.push.apply(u,[s.currentNode.token].concat(s.currentNode.children));s.parseBuffer=[],s.emit("nodeclosed",s.currentNode),!l&&f!==!1&&(s.prevNode=s.currentNode),s.currentNode=s.currentNode.parent,s.nodeDepth--,s.nodeDepth0&&(s.currentToken=s.currentToken.substring(h));return i},u.prototype.toString=function(){return this.compile()},u.prototype.registerFeather=function(e,t,n){var r={text:1,textblock:1,block:1,hybrid:1};n=n?n:"block";if(!e.match(/^[a-z0-9]+$/))throw new Error("Feather names must consist of lowercase letters and numbers only.");if(this.feathers[e])throw new Error("A feather with the specified name already exists.");if(!(t&&t instanceof Function))throw new Error("You must provide a function for processing the feather output.");if(!(n in r))throw new Error("Feather semantic level must be one of (text|textblock|block|hybrid)");this.emit("registerfeather",e,t),this.feathers[e]={handler:t,semanticLevel:n}},u.prototype.unregisterFeather=function(e){if(!this.feathers[e])throw new Error("Requested feather does not exist.");this.emit("unregisterfeather",e),delete this.feathers[e]},u.prototype.hasParseState=function(e){for(var t=0;t