diff --git a/SpecRunner.html b/SpecRunner.html index 55966e5..54ea1a8 100644 --- a/SpecRunner.html +++ b/SpecRunner.html @@ -10,11 +10,11 @@ - - + + - + diff --git a/html2markdown.js b/dist/html2markdown.js similarity index 100% rename from html2markdown.js rename to dist/html2markdown.js diff --git a/dist/html2markdown.min.js b/dist/html2markdown.min.js new file mode 100644 index 0000000..00b7b7f --- /dev/null +++ b/dist/html2markdown.min.js @@ -0,0 +1,4 @@ +(function(name,definition){if(typeof define==="function"){define(definition)}else{if(typeof module!=="undefined"&&module.exports){module.exports=definition()}else{var theModule=definition(),global=this,old=global[name];theModule.noConflict=function(){global[name]=old;return theModule};global[name]=theModule}}})("html2markdown",function(){function trim(value){return value.replace(/^\s+|\s+$/g,"")}function endsWith(value,suffix){return value.match(suffix+"$")==suffix}function startsWith(value,str){return value.indexOf(str)==0}function html2markdown(html,opts){opts=opts||{};var nodeList=[];var listTagStack=[];var linkAttrStack=[];var blockquoteStack=[];var preStack=[];var codeStack=[];var links=[];var inlineStyle=opts["inlineStyle"]||false;var parser=opts["parser"];var markdownTags={"hr":"- - -\n\n","br":" \n","title":"# ","h1":"# ","h2":"## ","h3":"### ","h4":"#### ","h5":"##### ","h6":"###### ","b":"**","strong":"**","i":"_","em":"_","dfn":"_","var":"_","cite":"_","span":" ","ul":"* ","ol":"1. ","dl":"- ","blockquote":"> "};if(!parser&&typeof markdownDOMParser!=="undefined"){parser=markdownDOMParser}function getListMarkdownTag(){var listItem="";if(listTagStack){for(var i=0;i0){return list.slice(-1)[0]}return""}function peekTillNotEmpty(list){if(!list){return""}for(var i=list.length-1;i>=0;i--){if(list[i]!=""){return list[i]}}return""}function removeIfEmptyTag(start){var cleaned=false;if(start==peekTillNotEmpty(nodeList)){while(peek(nodeList)!=start){nodeList.pop()}nodeList.pop();cleaned=true}return cleaned}function sliceText(start){var text=[];while(nodeList.length>0&&peek(nodeList)!=start){var t=nodeList.pop();text.unshift(t)}return text.join("")}function block(isEndBlock){var lastItem=nodeList.pop();if(!lastItem){return +}if(!isEndBlock){var block;if(/\s*\n\n\s*$/.test(lastItem)){lastItem=lastItem.replace(/\s*\n\n\s*$/,"\n\n");block=""}else{if(/\s*\n\s*$/.test(lastItem)){lastItem=lastItem.replace(/\s*\n\s*$/,"\n");block="\n"}else{if(/\s+$/.test(lastItem)){block="\n\n"}else{block="\n\n"}}}nodeList.push(lastItem);nodeList.push(block)}else{nodeList.push(lastItem);if(!endsWith(lastItem,"\n")){nodeList.push("\n\n")}}}function listBlock(){if(nodeList.length>0){var li=peek(nodeList);if(!endsWith(li,"\n")){nodeList.push("\n")}}else{nodeList.push("\n")}}parser(html,{start:function(tag,attrs,unary){tag=tag.toLowerCase();if(unary&&(tag!="br"&&tag!="hr"&&tag!="img")){return}switch(tag){case"br":nodeList.push(markdownTags[tag]);break;case"hr":block();nodeList.push(markdownTags[tag]);break;case"title":case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":block();nodeList.push(markdownTags[tag]);break;case"b":case"strong":case"i":case"em":case"dfn":case"var":case"cite":nodeList.push(markdownTags[tag]);break;case"code":case"span":if(preStack.length>0){break}else{if(!/\s+$/.test(peek(nodeList))){nodeList.push(markdownTags[tag])}}break;case"p":case"div":case"table":case"tbody":case"tr":case"td":block();break;case"ul":case"ol":case"dl":listTagStack.push(markdownTags[tag]);if(listTagStack.length>1){listBlock()}else{block()}break;case"li":case"dt":var li=getListMarkdownTag();nodeList.push(li);break;case"a":var attribs=convertAttrs(attrs);linkAttrStack.push(attribs);nodeList.push("[");break;case"img":var attribs=convertAttrs(attrs);var alt,title,url;attribs["src"]?url=attribs["src"].value:url="";if(!url){break}attribs["alt"]?alt=trim(attribs["alt"].value):alt="";attribs["title"]?title=trim(attribs["title"].value):title="";if(!inlineStyle&&!startsWith(peekTillNotEmpty(nodeList),"[")){var l=links.indexOf(url);if(l==-1){links.push(url);l=links.length-1}block();nodeList.push("![");if(alt!=""){nodeList.push(alt)}else{if(title!=null){nodeList.push(title)}}nodeList.push("]["+l+"]");block()}else{if(!startsWith(peekTillNotEmpty(nodeList),"[")){block() +}nodeList.push("!["+alt+"]("+url+(title?' "'+title+'"':"")+")");if(!startsWith(peekTillNotEmpty(nodeList),"[")){block(true)}}break;case"blockquote":block();blockquoteStack.push(markdownTags[tag]);break;case"pre":block();preStack.push(true);nodeList.push(" ");break;case"table":nodeList.push("");break;case"thead":nodeList.push("");break;case"tbody":nodeList.push("");break;case"tr":nodeList.push("");break;case"td":nodeList.push("
");break}},chars:function(text){if(preStack.length>0){text=text.replace(/\n/g,"\n ")}else{if(trim(text)!=""){text=text.replace(/\s+/g," ");var prevText=peekTillNotEmpty(nodeList);if(/\s+$/.test(prevText)){text=text.replace(/^\s+/g,"")}}else{nodeList.push("");return}}if(blockquoteStack.length>0){nodeList.push(blockquoteStack.join(""))}nodeList.push(text)},end:function(tag){tag=tag.toLowerCase();switch(tag){case"title":case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":if(!removeIfEmptyTag(markdownTags[tag])){block(true)}break;case"p":case"div":case"table":case"tbody":case"tr":case"td":while(nodeList.length>0&&trim(peek(nodeList))==""){nodeList.pop()}block(true);break;case"b":case"strong":case"i":case"em":case"dfn":case"var":case"cite":if(!removeIfEmptyTag(markdownTags[tag])){nodeList.push(trim(sliceText(markdownTags[tag])));nodeList.push(markdownTags[tag])}break;case"a":var text=sliceText("[");text=text.replace(/\s+/g," ");text=trim(text);if(text==""){nodeList.pop();break}var attrs=linkAttrStack.pop();var url;attrs["href"]&&attrs["href"].value!=""?url=attrs["href"].value:url="";if(url==""){nodeList.pop();nodeList.push(text);break}nodeList.push(text);if(!inlineStyle&&!startsWith(peek(nodeList),"!")){var l=links.indexOf(url);if(l==-1){links.push(url);l=links.length-1}nodeList.push("]["+l+"]")}else{if(startsWith(peek(nodeList),"!")){var text=nodeList.pop();text=nodeList.pop()+text;block();nodeList.push(text)}var title=attrs["title"];nodeList.push("]("+url+(title?' "'+trim(title.value).replace(/\s+/g," ")+'"':"")+")"); +if(startsWith(peek(nodeList),"!")){block(true)}}break;case"ul":case"ol":case"dl":listBlock();listTagStack.pop();break;case"li":case"dt":var li=getListMarkdownTag();if(!removeIfEmptyTag(li)){var text=trim(sliceText(li));if(startsWith(text,"[![")){nodeList.pop();block();nodeList.push(text);block(true)}else{nodeList.push(text);listBlock()}}break;case"blockquote":blockquoteStack.pop();break;case"pre":block(true);preStack.pop();break;case"code":case"span":if(preStack.length>0){break}else{if(trim(peek(nodeList))==""){nodeList.pop();nodeList.push(markdownTags[tag])}else{var text=nodeList.pop();nodeList.push(trim(text));nodeList.push(markdownTags[tag])}}break;case"table":nodeList.push("
");break;case"thead":nodeList.push("");break;case"tbody":nodeList.push("");break;case"tr":nodeList.push("");break;case"td":nodeList.push("");break;case"br":case"hr":case"img":break}}},{"nodesToIgnore":["script","noscript","object","iframe","frame","head","style","label"]});if(!inlineStyle){for(var i=0;i";break;case"\r":break}return original}function escapeNBSP(original){var thechar=original.charCodeAt(0);switch(thechar){case 32:return" ";break}return original}function escapeTags(original){var thechar=original.charCodeAt(0);switch(thechar){case 60:return"<";break;case 62:return">";break;case 34:return""";break}return original}function escapeCharOther(original){var found=true;var thechar=original.charCodeAt(0);switch(thechar){case 38:return"&";break;case 198:return"Æ";break;case 193:return"Á";break;case 194:return"Â";break;case 192:return"À";break;case 197:return"Å";break;case 195:return"Ã";break;case 196:return"Ä";break;case 199:return"Ç";break;case 208:return"Ð";break;case 201:return"É";break;case 202:return"Ê";break;case 200:return"È";break;case 203:return"Ë";break;case 205:return"Í";break;case 206:return"Î";break;case 204:return"Ì";break;case 207:return"Ï";break;case 209:return"Ñ";break;case 211:return"Ó";break;case 212:return"Ô";break;case 210:return"Ò";break;case 216:return"Ø"; +break;case 213:return"Õ";break;case 214:return"Ö";break;case 222:return"Þ";break;case 218:return"Ú";break;case 219:return"Û";break;case 217:return"Ù";break;case 220:return"Ü";break;case 221:return"Ý";break;case 225:return"á";break;case 226:return"â";break;case 230:return"æ";break;case 224:return"à";break;case 229:return"å";break;case 227:return"ã";break;case 228:return"ä";break;case 231:return"ç";break;case 233:return"é";break;case 234:return"ê";break;case 232:return"è";break;case 240:return"ð";break;case 235:return"ë";break;case 237:return"í";break;case 238:return"î";break;case 236:return"ì";break;case 239:return"ï";break;case 241:return"ñ";break;case 243:return"ó";break;case 244:return"ô";break;case 242:return"ò";break;case 248:return"ø";break;case 245:return"õ";break;case 246:return"ö";break;case 223:return"ß";break;case 254:return"þ";break;case 250:return"ú";break;case 251:return"û";break;case 249:return"ù";break;case 252:return"ü";break;case 253:return"ý";break;case 255:return"ÿ";break;case 162:return"¢";break;default:found=false;break}if(!found){if(thechar>127){var c=thechar;var a4=c%16;c=Math.floor(c/16);var a3=c%16;c=Math.floor(c/16);var a2=c%16;c=Math.floor(c/16);var a1=c%16;return"&#x"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+";"}else{return original}}}return HTMLParser}); diff --git a/markdown_html_parser.js b/dist/markdown_html_parser.js similarity index 100% rename from markdown_html_parser.js rename to dist/markdown_html_parser.js diff --git a/index.js b/index.js index 5c30639..83c1e30 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -var html2markdown = require('./html2markdown'); -var htmlParser = require('./markdown_html_parser'); +var html2markdown = require('./dist/html2markdown'); +var htmlParser = require('./dist/markdown_html_parser'); module.exports = function(html, opts) { opts = opts || {};