Skip to content

Instantly share code, notes, and snippets.

@t-yuki
Last active August 29, 2015 14:21
Show Gist options
  • Save t-yuki/60711b6b99fbaf5a8f42 to your computer and use it in GitHub Desktop.
Save t-yuki/60711b6b99fbaf5a8f42 to your computer and use it in GitHub Desktop.
<html>
<head>
<!--[if lt IE 9]>
<script src="https://raw.github.com/aFarkas/html5shiv/master/src/html5shiv.js"></script>
<![endif]-->
<script language="javascript">
// https://cdn.rawgit.com/chjj/marked/master/marked.min.js
/**
* marked - a markdown parser
* Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
* https://github.com/chjj/marked
*/
(function(){function e(e){this.tokens=[],this.tokens.links={},this.options=e||a.defaults,this.rules=p.normal,this.options.gfm&&(this.rules=this.options.tables?p.tables:p.gfm)}function t(e,t){if(this.options=t||a.defaults,this.links=e,this.rules=u.normal,this.renderer=this.options.renderer||new n,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.gfm?this.rules=this.options.breaks?u.breaks:u.gfm:this.options.pedantic&&(this.rules=u.pedantic)}function n(e){this.options=e||{}}function r(e){this.tokens=[],this.token=null,this.options=e||a.defaults,this.options.renderer=this.options.renderer||new n,this.renderer=this.options.renderer,this.renderer.options=this.options}function s(e,t){return e.replace(t?/&/g:/&(?!#?\w+;)/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}function i(e){return e.replace(/&([#\w]+);/g,function(e,t){return t=t.toLowerCase(),"colon"===t?":":"#"===t.charAt(0)?String.fromCharCode("x"===t.charAt(1)?parseInt(t.substring(2),16):+t.substring(1)):""})}function l(e,t){return e=e.source,t=t||"",function n(r,s){return r?(s=s.source||s,s=s.replace(/(^|[^\[])\^/g,"$1"),e=e.replace(r,s),n):new RegExp(e,t)}}function o(){}function h(e){for(var t,n,r=1;r<arguments.length;r++){t=arguments[r];for(n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}function a(t,n,i){if(i||"function"==typeof n){i||(i=n,n=null),n=h({},a.defaults,n||{});var l,o,p=n.highlight,u=0;try{l=e.lex(t,n)}catch(c){return i(c)}o=l.length;var g=function(e){if(e)return n.highlight=p,i(e);var t;try{t=r.parse(l,n)}catch(s){e=s}return n.highlight=p,e?i(e):i(null,t)};if(!p||p.length<3)return g();if(delete n.highlight,!o)return g();for(;u<l.length;u++)!function(e){return"code"!==e.type?--o||g():p(e.text,e.lang,function(t,n){return t?g(t):null==n||n===e.text?--o||g():(e.text=n,e.escaped=!0,void(--o||g()))})}(l[u])}else try{return n&&(n=h({},a.defaults,n)),r.parse(e.lex(t,n),n)}catch(c){if(c.message+="\nPlease report this to https://github.com/chjj/marked.",(n||a.defaults).silent)return"<p>An error occured:</p><pre>"+s(c.message+"",!0)+"</pre>";throw c}}var p={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:o,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:o,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,blockquote:/^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:/^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,def:/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,table:o,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/};p.bullet=/(?:[*+-]|\d+\.)/,p.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,p.item=l(p.item,"gm")(/bull/g,p.bullet)(),p.list=l(p.list)(/bull/g,p.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+p.def.source+")")(),p.blockquote=l(p.blockquote)("def",p.def)(),p._tag="(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b",p.html=l(p.html)("comment",/<!--[\s\S]*?-->/)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)(/tag/g,p._tag)(),p.paragraph=l(p.paragraph)("hr",p.hr)("heading",p.heading)("lheading",p.lheading)("blockquote",p.blockquote)("tag","<"+p._tag)("def",p.def)(),p.normal=h({},p),p.gfm=h({},p.normal,{fences:/^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,paragraph:/^/}),p.gfm.paragraph=l(p.paragraph)("(?!","(?!"+p.gfm.fences.source.replace("\\1","\\2")+"|"+p.list.source.replace("\\1","\\3")+"|")(),p.tables=h({},p.gfm,{nptable:/^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,table:/^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/}),e.rules=p,e.lex=function(t,n){var r=new e(n);return r.lex(t)},e.prototype.lex=function(e){return e=e.replace(/\r\n|\r/g,"\n").replace(/\t/g," ").replace(/\u00a0/g," ").replace(/\u2424/g,"\n"),this.token(e,!0)},e.prototype.token=function(e,t,n){for(var r,s,i,l,o,h,a,u,c,e=e.replace(/^ +$/gm,"");e;)if((i=this.rules.newline.exec(e))&&(e=e.substring(i[0].length),i[0].length>1&&this.tokens.push({type:"space"})),i=this.rules.code.exec(e))e=e.substring(i[0].length),i=i[0].replace(/^ {4}/gm,""),this.tokens.push({type:"code",text:this.options.pedantic?i:i.replace(/\n+$/,"")});else if(i=this.rules.fences.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"code",lang:i[2],text:i[3]});else if(i=this.rules.heading.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"heading",depth:i[1].length,text:i[2]});else if(t&&(i=this.rules.nptable.exec(e))){for(e=e.substring(i[0].length),h={type:"table",header:i[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:i[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i[3].replace(/\n$/,"").split("\n")},u=0;u<h.align.length;u++)h.align[u]=/^ *-+: *$/.test(h.align[u])?"right":/^ *:-+: *$/.test(h.align[u])?"center":/^ *:-+ *$/.test(h.align[u])?"left":null;for(u=0;u<h.cells.length;u++)h.cells[u]=h.cells[u].split(/ *\| */);this.tokens.push(h)}else if(i=this.rules.lheading.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"heading",depth:"="===i[2]?1:2,text:i[1]});else if(i=this.rules.hr.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"hr"});else if(i=this.rules.blockquote.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"blockquote_start"}),i=i[0].replace(/^ *> ?/gm,""),this.token(i,t,!0),this.tokens.push({type:"blockquote_end"});else if(i=this.rules.list.exec(e)){for(e=e.substring(i[0].length),l=i[2],this.tokens.push({type:"list_start",ordered:l.length>1}),i=i[0].match(this.rules.item),r=!1,c=i.length,u=0;c>u;u++)h=i[u],a=h.length,h=h.replace(/^ *([*+-]|\d+\.) +/,""),~h.indexOf("\n ")&&(a-=h.length,h=this.options.pedantic?h.replace(/^ {1,4}/gm,""):h.replace(new RegExp("^ {1,"+a+"}","gm"),"")),this.options.smartLists&&u!==c-1&&(o=p.bullet.exec(i[u+1])[0],l===o||l.length>1&&o.length>1||(e=i.slice(u+1).join("\n")+e,u=c-1)),s=r||/\n\n(?!\s*$)/.test(h),u!==c-1&&(r="\n"===h.charAt(h.length-1),s||(s=r)),this.tokens.push({type:s?"loose_item_start":"list_item_start"}),this.token(h,!1,n),this.tokens.push({type:"list_item_end"});this.tokens.push({type:"list_end"})}else if(i=this.rules.html.exec(e))e=e.substring(i[0].length),this.tokens.push({type:this.options.sanitize?"paragraph":"html",pre:"pre"===i[1]||"script"===i[1]||"style"===i[1],text:i[0]});else if(!n&&t&&(i=this.rules.def.exec(e)))e=e.substring(i[0].length),this.tokens.links[i[1].toLowerCase()]={href:i[2],title:i[3]};else if(t&&(i=this.rules.table.exec(e))){for(e=e.substring(i[0].length),h={type:"table",header:i[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:i[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:i[3].replace(/(?: *\| *)?\n$/,"").split("\n")},u=0;u<h.align.length;u++)h.align[u]=/^ *-+: *$/.test(h.align[u])?"right":/^ *:-+: *$/.test(h.align[u])?"center":/^ *:-+ *$/.test(h.align[u])?"left":null;for(u=0;u<h.cells.length;u++)h.cells[u]=h.cells[u].replace(/^ *\| *| *\| *$/g,"").split(/ *\| */);this.tokens.push(h)}else if(t&&(i=this.rules.paragraph.exec(e)))e=e.substring(i[0].length),this.tokens.push({type:"paragraph",text:"\n"===i[1].charAt(i[1].length-1)?i[1].slice(0,-1):i[1]});else if(i=this.rules.text.exec(e))e=e.substring(i[0].length),this.tokens.push({type:"text",text:i[0]});else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0));return this.tokens};var u={escape:/^\\([\\`*{}\[\]()#+\-.!_>])/,autolink:/^<([^ >]+(@|:\/)[^ >]+)>/,url:o,tag:/^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,link:/^!?\[(inside)\]\(href\)/,reflink:/^!?\[(inside)\]\s*\[([^\]]*)\]/,nolink:/^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,strong:/^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,em:/^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,code:/^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,br:/^ {2,}\n(?!\s*$)/,del:o,text:/^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/};u._inside=/(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/,u._href=/\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/,u.link=l(u.link)("inside",u._inside)("href",u._href)(),u.reflink=l(u.reflink)("inside",u._inside)(),u.normal=h({},u),u.pedantic=h({},u.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/}),u.gfm=h({},u.normal,{escape:l(u.escape)("])","~|])")(),url:/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,del:/^~~(?=\S)([\s\S]*?\S)~~/,text:l(u.text)("]|","~]|")("|","|https?://|")()}),u.breaks=h({},u.gfm,{br:l(u.br)("{2,}","*")(),text:l(u.gfm.text)("{2,}","*")()}),t.rules=u,t.output=function(e,n,r){var s=new t(n,r);return s.output(e)},t.prototype.output=function(e){for(var t,n,r,i,l="";e;)if(i=this.rules.escape.exec(e))e=e.substring(i[0].length),l+=i[1];else if(i=this.rules.autolink.exec(e))e=e.substring(i[0].length),"@"===i[2]?(n=this.mangle(":"===i[1].charAt(6)?i[1].substring(7):i[1]),r=this.mangle("mailto:")+n):(n=s(i[1]),r=n),l+=this.renderer.link(r,null,n);else if(this.inLink||!(i=this.rules.url.exec(e))){if(i=this.rules.tag.exec(e))!this.inLink&&/^<a /i.test(i[0])?this.inLink=!0:this.inLink&&/^<\/a>/i.test(i[0])&&(this.inLink=!1),e=e.substring(i[0].length),l+=this.options.sanitize?s(i[0]):i[0];else if(i=this.rules.link.exec(e))e=e.substring(i[0].length),this.inLink=!0,l+=this.outputLink(i,{href:i[2],title:i[3]}),this.inLink=!1;else if((i=this.rules.reflink.exec(e))||(i=this.rules.nolink.exec(e))){if(e=e.substring(i[0].length),t=(i[2]||i[1]).replace(/\s+/g," "),t=this.links[t.toLowerCase()],!t||!t.href){l+=i[0].charAt(0),e=i[0].substring(1)+e;continue}this.inLink=!0,l+=this.outputLink(i,t),this.inLink=!1}else if(i=this.rules.strong.exec(e))e=e.substring(i[0].length),l+=this.renderer.strong(this.output(i[2]||i[1]));else if(i=this.rules.em.exec(e))e=e.substring(i[0].length),l+=this.renderer.em(this.output(i[2]||i[1]));else if(i=this.rules.code.exec(e))e=e.substring(i[0].length),l+=this.renderer.codespan(s(i[2],!0));else if(i=this.rules.br.exec(e))e=e.substring(i[0].length),l+=this.renderer.br();else if(i=this.rules.del.exec(e))e=e.substring(i[0].length),l+=this.renderer.del(this.output(i[1]));else if(i=this.rules.text.exec(e))e=e.substring(i[0].length),l+=s(this.smartypants(i[0]));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else e=e.substring(i[0].length),n=s(i[1]),r=n,l+=this.renderer.link(r,null,n);return l},t.prototype.outputLink=function(e,t){var n=s(t.href),r=t.title?s(t.title):null;return"!"!==e[0].charAt(0)?this.renderer.link(n,r,this.output(e[1])):this.renderer.image(n,r,s(e[1]))},t.prototype.smartypants=function(e){return this.options.smartypants?e.replace(/--/g,"?").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…"):e},t.prototype.mangle=function(e){for(var t,n="",r=e.length,s=0;r>s;s++)t=e.charCodeAt(s),Math.random()>.5&&(t="x"+t.toString(16)),n+="&#"+t+";";return n},n.prototype.code=function(e,t,n){if(this.options.highlight){var r=this.options.highlight(e,t);null!=r&&r!==e&&(n=!0,e=r)}return t?'<pre><code class="'+this.options.langPrefix+s(t,!0)+'">'+(n?e:s(e,!0))+"\n</code></pre>\n":"<pre><code>"+(n?e:s(e,!0))+"\n</code></pre>"},n.prototype.blockquote=function(e){return"<blockquote>\n"+e+"</blockquote>\n"},n.prototype.html=function(e){return e},n.prototype.heading=function(e,t,n){return"<h"+t+' id="'+this.options.headerPrefix+n.toLowerCase().replace(/[^\w]+/g,"-")+'">'+e+"</h"+t+">\n"},n.prototype.hr=function(){return this.options.xhtml?"<hr/>\n":"<hr>\n"},n.prototype.list=function(e,t){var n=t?"ol":"ul";return"<"+n+">\n"+e+"</"+n+">\n"},n.prototype.listitem=function(e){return"<li>"+e+"</li>\n"},n.prototype.paragraph=function(e){return"<p>"+e+"</p>\n"},n.prototype.table=function(e,t){return"<table>\n<thead>\n"+e+"</thead>\n<tbody>\n"+t+"</tbody>\n</table>\n"},n.prototype.tablerow=function(e){return"<tr>\n"+e+"</tr>\n"},n.prototype.tablecell=function(e,t){var n=t.header?"th":"td",r=t.align?"<"+n+' style="text-align:'+t.align+'">':"<"+n+">";return r+e+"</"+n+">\n"},n.prototype.strong=function(e){return"<strong>"+e+"</strong>"},n.prototype.em=function(e){return"<em>"+e+"</em>"},n.prototype.codespan=function(e){return"<code>"+e+"</code>"},n.prototype.br=function(){return this.options.xhtml?"<br/>":"<br>"},n.prototype.del=function(e){return"<del>"+e+"</del>"},n.prototype.link=function(e,t,n){if(this.options.sanitize){try{var r=decodeURIComponent(i(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(s){return""}if(0===r.indexOf("javascript:")||0===r.indexOf("vbscript:"))return""}var l='<a href="'+e+'"';return t&&(l+=' title="'+t+'"'),l+=">"+n+"</a>"},n.prototype.image=function(e,t,n){var r='<img src="'+e+'" alt="'+n+'"';return t&&(r+=' title="'+t+'"'),r+=this.options.xhtml?"/>":">"},r.parse=function(e,t,n){var s=new r(t,n);return s.parse(e)},r.prototype.parse=function(e){this.inline=new t(e.links,this.options,this.renderer),this.tokens=e.reverse();for(var n="";this.next();)n+=this.tok();return n},r.prototype.next=function(){return this.token=this.tokens.pop()},r.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},r.prototype.parseText=function(){for(var e=this.token.text;"text"===this.peek().type;)e+="\n"+this.next().text;return this.inline.output(e)},r.prototype.tok=function(){switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,this.token.text);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var e,t,n,r,s,i="",l="";for(n="",e=0;e<this.token.header.length;e++)r={header:!0,align:this.token.align[e]},n+=this.renderer.tablecell(this.inline.output(this.token.header[e]),{header:!0,align:this.token.align[e]});for(i+=this.renderer.tablerow(n),e=0;e<this.token.cells.length;e++){for(t=this.token.cells[e],n="",s=0;s<t.length;s++)n+=this.renderer.tablecell(this.inline.output(t[s]),{header:!1,align:this.token.align[s]});l+=this.renderer.tablerow(n)}return this.renderer.table(i,l);case"blockquote_start":for(var l="";"blockquote_end"!==this.next().type;)l+=this.tok();return this.renderer.blockquote(l);case"list_start":for(var l="",o=this.token.ordered;"list_end"!==this.next().type;)l+=this.tok();return this.renderer.list(l,o);case"list_item_start":for(var l="";"list_item_end"!==this.next().type;)l+="text"===this.token.type?this.parseText():this.tok();return this.renderer.listitem(l);case"loose_item_start":for(var l="";"list_item_end"!==this.next().type;)l+=this.tok();return this.renderer.listitem(l);case"html":var h=this.token.pre||this.options.pedantic?this.token.text:this.inline.output(this.token.text);return this.renderer.html(h);case"paragraph":return this.renderer.paragraph(this.inline.output(this.token.text));case"text":return this.renderer.paragraph(this.parseText())}},o.exec=o,a.options=a.setOptions=function(e){return h(a.defaults,e),a},a.defaults={gfm:!0,tables:!0,breaks:!1,pedantic:!1,sanitize:!1,smartLists:!1,silent:!1,highlight:null,langPrefix:"lang-",smartypants:!1,headerPrefix:"",renderer:new n,xhtml:!1},a.Parser=r,a.parser=r.parse,a.Renderer=n,a.Lexer=e,a.lexer=e.lex,a.InlineLexer=t,a.inlineLexer=t.output,a.parse=a,"undefined"!=typeof module&&"object"==typeof exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):this.marked=a}).call(function(){return this||("undefined"!=typeof window?window:global)}());
/**
* SheetClip - Spreadsheet Clipboard Parser
* version 0.2
*
* This tiny library transforms JavaScript arrays to strings that are pasteable by LibreOffice, OpenOffice,
* Google Docs and Microsoft Excel.
*
* Copyright 2012, Marcin Warpechowski
* Licensed under the MIT license.
* http://github.com/warpech/sheetclip/
*/
!function(n){"use strict";function e(n){return n.split('"').length-1}n.SheetClip={parse:function(n){var t,r,l,i,g,p,s,u=[],f=0;for(l=n.split("\n"),l.length>1&&""===l[l.length-1]&&l.pop(),t=0,r=l.length;r>t;t+=1){for(l[t]=l[t].split(" "),i=0,g=l[t].length;g>i;i+=1)u[f]||(u[f]=[]),p&&0===i?(s=u[f].length-1,u[f][s]=u[f][s]+"\n"+l[t][0],p&&1&e(l[t][0])&&(p=!1,u[f][s]=u[f][s].substring(0,u[f][s].length-1).replace(/""/g,'"'))):i===g-1&&0===l[t][i].indexOf('"')&&1&e(l[t][i])?(u[f].push(l[t][i].substring(1).replace(/""/g,'"')),p=!0):(u[f].push(l[t][i].replace(/""/g,'"')),p=!1);p||(f+=1)}return u},stringify:function(n){var e,t,r,l,i,g="";for(e=0,t=n.length;t>e;e+=1){for(r=0,l=n[e].length;l>r;r+=1)r>0&&(g+=" "),i=n[e][r],g+="string"==typeof i?i.indexOf("\n")>-1?'"'+i.replace(/"/g,'""')+'"':i:null===i||void 0===i?"":i;g+="\n"}return g}}}(window);
// https://raw.githubusercontent.com/leeoniya/reMarked.js/master/reMarked.js
reMarked=function(opts){function extend(t,e){if(!e)return t;for(var n in t)"Object"==typeOf(e[n])?extend(t[n],e[n]):"undefined"!=typeof e[n]&&(t[n]=e[n])}function typeOf(t){return Object.prototype.toString.call(t).slice(8,-1)}function rep(t,e){for(var n="";e-->0;)n+=t;return n}function trim12(t){for(var t=t.replace(/^\s\s*/,""),e=/\s/,n=t.length;e.test(t.charAt(--n)););return t.slice(0,n+1)}function lpad(t,e,n){return rep(e,n-t.length)+t}function rpad(t,e,n){return t+rep(e,n-t.length)}function otag(t,e){if(!t)return"";for(var n,i="<"+t,r=0;r<e.attributes.length;r++)n=e.attributes.item(r),i+=" "+n.nodeName+'="'+n.nodeValue+'"';return i+">"}function ctag(t){return t?"</"+t+">":""}function pfxLines(t,e){return t.replace(/^/gm,e)}function nodeName(t){return("#text"==t.nodeName?"txt":t.nodeName).toLowerCase()}function wrap(t,e){var n,i;return e instanceof Array?(n=e[0],i=e[1]):n=i=e,n=n instanceof Function?n.call(this,t):n,i=i instanceof Function?i.call(this,t):i,n+t+i}function outerHTML(t){return t.outerHTML||function(t){var e,n=document.createElement("div");return n.appendChild(t.cloneNode(!0)),e=n.innerHTML,n=null,e}(t)}var links=[],cfg={link_list:!1,h1_setext:!0,h2_setext:!0,h_atx_suf:!1,gfm_code:["```","~~~"][0],trim_code:!0,li_bullet:"*-+"[0],hr_char:"-_*"[0],indnt_str:[" "," "," "][0],bold_char:"*_"[0],emph_char:"*_"[1],gfm_del:!0,gfm_tbls:!0,tbl_edges:!1,hash_lnks:!1,br_only:!1,col_pre:"col ",nbsp_spc:!1,span_tags:!0,div_tags:!0,unsup_tags:{ignore:"script style noscript",inline:"span sup sub i u b center big",block2:"div form fieldset dl header footer address article aside figure hgroup section",block1c:"dt dd caption legend figcaption output",block2c:"canvas audio video iframe"},tag_remap:{i:"em",b:"strong"}},isIE=eval("/*@cc_on!@*/!1"),docMode=document.documentMode,ieLt9=isIE&&(!docMode||9>docMode),textContProp="textContent"in Element.prototype||!ieLt9?"textContent":"innerText";extend(cfg,opts),this.render=function(t){links=[];var e=document.createElement("div");e.innerHTML="string"==typeof t?t:outerHTML(t);var n=new lib.tag(e,null,0),i=n.rend().replace(/^[\t ]+[\n\r]+/gm,"\n").replace(/^[\n\r]+|[\n\r]+$/g,"");if(cfg.link_list&&links.length>0){i+="\n\n";for(var r=0,l=0;l<links.length;l++)if(links[l].e.title){var s=links[l].e.href.length;s&&s>r&&(r=s)}for(var c=0;c<links.length;c++){var a=links[c].e.title?rep(" ",r+2-links[c].e.href.length)+'"'+links[c].e.title+'"':"";i+=" ["+(+c+1)+"]: "+("a"==nodeName(links[c].e)?links[c].e.href:links[c].e.src)+a+"\n"}}return i.replace(/^[\t ]+\n/gm,"\n")};var lib={};lib.tag=klass({wrap:"",lnPfx:"",lnInd:0,init:function(t,e,n){this.e=t,this.p=e,this.i=n,this.c=[],this.tag=nodeName(t),this.initK()},initK:function(){var t;if(this.e.hasChildNodes()){var e,n,i=cfg.unsup_tags.inline;if("table"==nodeName(this.e)&&this.e.hasChildNodes()&&!this.e.tHead){var r=document.createElement("thead"),l=this.e.tBodies[0],s=l.rows[0],c=s.cells[0];if("th"==nodeName(c))r.appendChild(s);else for(var a,t=0,h=s.cells.length,o=r.insertRow();t++<h;)a=document.createElement("th"),a[textContProp]=cfg.col_pre+t,o.appendChild(a);this.e.insertBefore(r,l)}for(t in this.e.childNodes)if(/\d+/.test(t)&&(e=this.e.childNodes[t],n=nodeName(e),n in cfg.tag_remap&&(n=cfg.tag_remap[n]),!cfg.unsup_tags.ignore.test(n))){if("txt"==n&&!nodeName(this.e).match(i)&&/^\s+$/.test(e[textContProp])){if(0==t||t==this.e.childNodes.length-1)continue;var u=this.e.childNodes[t-1],f=this.e.childNodes[t+1];if(u&&!nodeName(u).match(i)||f&&!nodeName(f).match(i))continue}var d=null;if(!lib[n]){var p=cfg.unsup_tags;p.inline.test(n)?n="span"!=n||cfg.span_tags?"tinl":"inl":p.block2.test(n)?n="div"!=n||cfg.div_tags?"tblk":"blk":p.block1c.test(n)?n="ctblk":p.block2c.test(n)?(n="ctblk",d=["\n\n",""]):n="rawhtml"}var b=new lib[n](e,this,this.c.length);d&&(b.wrap=d),(b instanceof lib.a&&e.href||b instanceof lib.img)&&(b.lnkid=links.length,links.push(b)),this.c.push(b)}}},rend:function(){return this.rendK().replace(/\n{3,}/gm,"\n\n")},rendK:function(){for(var t,e="",n=0;n<this.c.length;n++)t=this.c[n],e+=(t.bef||"")+t.rend()+(t.aft||"");return e.replace(/^\n+|\n+$/,"")}}),lib.blk=lib.tag.extend({wrap:["\n\n",""],wrapK:null,tagr:!1,lnInd:null,init:function(t,e,n){this.supr(t,e,n),null===this.lnInd&&(this.lnInd=this.p&&this.tagr&&this.c[0]instanceof lib.blk?4:0),null===this.wrapK&&(this.wrapK=this.tagr&&this.c[0]instanceof lib.blk?"\n":"")},rend:function(){return wrap.call(this,(this.tagr?otag(this.tag,this.e):"")+wrap.call(this,pfxLines(pfxLines(this.rendK(),this.lnPfx),rep(" ",this.lnInd)),this.wrapK)+(this.tagr?ctag(this.tag):""),this.wrap)},rendK:function(){var t=this.supr();if(this.p instanceof lib.li){var e=null,n=t.match(/^[\t ]+/gm);if(!n)return t;for(var i=0;i<n.length;i++)(null===e||n[i][0].length<e.length)&&(e=n[i][0]);return t.replace(new RegExp("^"+e),"")}return t}}),lib.tblk=lib.blk.extend({tagr:!0}),lib.cblk=lib.blk.extend({wrap:["\n",""]}),lib.ctblk=lib.cblk.extend({tagr:!0}),lib.inl=lib.tag.extend({rend:function(){var t=this.rendK(),e=t.match(/^((?: |\t|&nbsp;)*)(.*?)((?: |\t|&nbsp;)*)$/)||[t,"",t,""];return e[1]+wrap.call(this,e[2],this.wrap)+e[3]}}),lib.tinl=lib.inl.extend({tagr:!0,rend:function(){return otag(this.tag,this.e)+wrap.call(this,this.rendK(),this.wrap)+ctag(this.tag)}}),lib.p=lib.blk.extend({rendK:function(){return this.supr().replace(/^\s+/gm,"")}}),lib.list=lib.blk.extend({wrap:[function(){return this.p instanceof lib.li?"\n":"\n\n"},""]}),lib.ul=lib.list.extend({}),lib.ol=lib.list.extend({}),lib.li=lib.cblk.extend({wrap:["\n",function(t){return this.c[0]&&this.c[0]instanceof lib.p||t.match(/\n{2}/gm)?"\n":""}],wrapK:[function(){return"ul"==this.p.tag?cfg.li_bullet+" ":this.i+1+". "},""],rendK:function(){return this.supr().replace(/\n([^\n])/gm,"\n"+cfg.indnt_str+"$1")}}),lib.hr=lib.blk.extend({wrap:["\n\n",rep(cfg.hr_char,3)]}),lib.h=lib.blk.extend({}),lib.h_setext=lib.h.extend({}),cfg.h1_setext&&(lib.h1=lib.h_setext.extend({wrapK:["",function(t){return"\n"+rep("=",t.length)}]})),cfg.h2_setext&&(lib.h2=lib.h_setext.extend({wrapK:["",function(t){return"\n"+rep("-",t.length)}]})),lib.h_atx=lib.h.extend({wrapK:[function(){return rep("#",this.tag[1])+" "},function(){return cfg.h_atx_suf?" "+rep("#",this.tag[1]):""}]}),!cfg.h1_setext&&(lib.h1=lib.h_atx.extend({})),!cfg.h2_setext&&(lib.h2=lib.h_atx.extend({})),lib.h3=lib.h_atx.extend({}),lib.h4=lib.h_atx.extend({}),lib.h5=lib.h_atx.extend({}),lib.h6=lib.h_atx.extend({}),lib.a=lib.inl.extend({lnkid:null,rend:function(){var t=this.rendK(),e=this.e.getAttribute("href"),n=this.e.title?' "'+this.e.title+'"':"";return!this.e.hasAttribute("href")||e==t||"#"==e[0]&&!cfg.hash_lnks?t:cfg.link_list?"["+t+"] ["+(this.lnkid+1)+"]":"["+t+"]("+e+n+")"}}),lib.img=lib.inl.extend({lnkid:null,rend:function(){var t=this.e.alt,e=this.e.getAttribute("src");if(cfg.link_list)return"!["+t+"] ["+(this.lnkid+1)+"]";var n=this.e.title?' "'+this.e.title+'"':"";return"!["+t+"]("+e+n+")"}}),lib.em=lib.inl.extend({wrap:cfg.emph_char}),lib.del=cfg.gfm_del?lib.inl.extend({wrap:"~~"}):lib.tinl.extend(),lib.br=lib.inl.extend({wrap:["",function(){var t=cfg.br_only?"<br>":" ";return this.p instanceof lib.h?"<br>":t+"\n"}]}),lib.strong=lib.inl.extend({wrap:rep(cfg.bold_char,2)}),lib.blockquote=lib.blk.extend({lnPfx:"> ",rend:function(){return this.supr().replace(/>[ \t]$/gm,">")}}),lib.pre=lib.blk.extend({tagr:!0,wrapK:"\n",lnInd:0}),lib.code=lib.blk.extend({tagr:!1,wrap:"",wrapK:function(t){return-1!==t.indexOf("`")?"``":"`"},lnInd:0,init:function(t,e,n){if(this.supr(t,e,n),this.p instanceof lib.pre)if(this.p.tagr=!1,cfg.gfm_code){var i=this.e.getAttribute("class");i=(i||"").split(" ")[0],0===i.indexOf("lang-")&&(i=i.substr(5)),this.wrapK=[cfg.gfm_code+i+"\n","\n"+cfg.gfm_code]}else this.wrapK="",this.p.lnInd=4},rendK:function(){if(this.p instanceof lib.pre){var t=this.e[textContProp];return cfg.trim_code?t.trim():t}return this.supr()}}),lib.table=cfg.gfm_tbls?lib.blk.extend({cols:[],init:function(t,e,n){this.supr(t,e,n),this.cols=[]},rend:function(){for(var t=0;t<this.c.length;t++)for(var e=0;e<this.c[t].c.length;e++)for(var n=0;n<this.c[t].c[e].c.length;n++)this.c[t].c[e].c[n].prep();return this.supr()}}):lib.tblk.extend(),lib.thead=cfg.gfm_tbls?lib.cblk.extend({wrap:["\n",function(){for(var t="",e=0;e<this.p.cols.length;e++){var n=this.p.cols[e],i="c"==n.a[0]?":":" ",r="r"==n.a[0]||"c"==n.a[0]?":":" ";t+=(0==e&&cfg.tbl_edges?"|":"")+i+rep("-",n.w)+r+(e<this.p.cols.length-1||cfg.tbl_edges?"|":"")}return"\n"+trim12(t)}]}):lib.ctblk.extend(),lib.tbody=cfg.gfm_tbls?lib.cblk.extend():lib.ctblk.extend(),lib.tfoot=cfg.gfm_tbls?lib.cblk.extend():lib.ctblk.extend(),lib.tr=cfg.gfm_tbls?lib.cblk.extend({wrapK:[cfg.tbl_edges?"| ":"",cfg.tbl_edges?" |":""]}):lib.ctblk.extend(),lib.th=cfg.gfm_tbls?lib.inl.extend({guts:null,wrap:[function(){var t,e=this.p.p.p.cols[this.i],n=0==this.i?"":" ",i=e.w-this.guts.length;switch(e.a[0]){case"r":t=rep(" ",i);break;case"c":t=rep(" ",Math.floor(i/2));break;default:t=""}return n+t},function(){var t,e=this.p.p.p.cols[this.i],n=this.i==this.p.c.length-1?"":" |",i=e.w-this.guts.length;switch(e.a[0]){case"r":t="";break;case"c":t=rep(" ",Math.ceil(i/2));break;default:t=rep(" ",i)}return t+n}],prep:function(){this.guts=this.rendK(),this.rendK=function(){return this.guts};var t=this.p.p.p.cols;t[this.i]||(t[this.i]={w:null,a:""});var e=t[this.i];e.w=Math.max(e.w||0,this.guts.length);var n=this.e.align||this.e.style.textAlign;n&&(e.a=n)}}):lib.ctblk.extend(),lib.td=lib.th.extend(),lib.txt=lib.inl.extend({initK:function(){this.c=this.e[textContProp].split(/^/gm)},rendK:function(){var t=this.c.join("").replace(/\r/gm,"");return this.p instanceof lib.code||this.p instanceof lib.pre||(t=t.replace(/^\s*([#*])/gm,function(t,e){return t.replace(e,"\\"+e)})),0==this.i&&(t=t.replace(/^\n+/,"")),this.i==this.p.c.length-1&&(t=t.replace(/\n+$/,"")),t.replace(/\u00a0/gm,cfg.nbsp_spc?" ":"&nbsp;")}}),lib.rawhtml=lib.blk.extend({initK:function(){this.guts=outerHTML(this.e)},rendK:function(){return this.guts}});for(var i in cfg.unsup_tags)cfg.unsup_tags[i]=new RegExp("^(?:"+("inline"==i?"a|em|strong|img|code|del|":"")+cfg.unsup_tags[i].replace(/\s/g,"|")+")$")},!function(t,e,n){"function"==typeof define?define(n):"undefined"!=typeof module?module.exports=n():e[t]=n()}("klass",this,function(){function t(t){return r.call(e(t)?t:function(){},t,1)}function e(t){return typeof t===l}function n(t,e,n){return function(){var i=this.supr;this.supr=n[c][t];var r={}.fabricatedUndefined,l=r;try{l=e.apply(this,arguments)}finally{this.supr=i}return l}}function i(t,i,r){for(var l in i)i.hasOwnProperty(l)&&(t[l]=e(i[l])&&e(r[c][l])&&s.test(i[l])?n(l,i[l],r):i[l])}function r(t,n){function r(){}function l(){this.init?this.init.apply(this,arguments):(n||h&&s.apply(this,arguments),o.apply(this,arguments))}r[c]=this[c];var s=this,a=new r,h=e(t),o=h?t:this,u=h?{}:t;return l.methods=function(t){return i(a,t,s),l[c]=a,this},l.methods.call(l,u).prototype.constructor=l,l.extend=arguments.callee,l[c].implement=l.statics=function(t,e){return t="string"==typeof t?function(){var n={};return n[t]=e,n}():t,i(this,t,s),this},l}var l="function",s=/xyz/.test(function(){xyz})?/\bsupr\b/:/.*/,c="prototype";return t});
/**
* Table to Markdown
*/
function revert() {
tblarea = document.getElementById('table1')
mdarea = document.getElementById('markdown1')
htmlarea = document.getElementById('html1')
marked.setOptions({ gfm: true, tables: true, breaks: false, pedantic: false, sanitize: false, smartLists: true, smartypants: false })
htmlarea.innerHTML = marked(mdarea.value.replace(/^\s*|\s*$/g, ""))
var tables = getElementsByTagName(htmlarea, "table")
for (i = 0; i < tables.length; i++) {
tables[i].setAttribute("contentEditable", true)
var arr = dom2array(tables[i])
tblarea.value = SheetClip.stringify(arr)
}
}
function convert() {
tblarea = document.getElementById('table1')
mdarea = document.getElementById('markdown1')
htmlarea = document.getElementById('html1')
var arr = SheetClip.parse(tblarea.value)
if (arr.length == 0) return
var md = array2md(arr)
mdarea.value = md
marked.setOptions({ gfm: true, tables: true, breaks: false, pedantic: false, sanitize: false, smartLists: true, smartypants: false })
htmlarea.innerHTML = marked(md.replace(/^\s*|\s*$/g, ""))
var tables = getElementsByTagName(htmlarea, "table")
for (i = 0; i < tables.length; i++) {
tables[i].setAttribute("contentEditable", true)
var trs = getElementsByTagName(tables[i], "tr")
for (j = 0; j < trs.length; j++) {
var ths = getElementsByTagName(trs[j], "th")
for (k = 0; k < ths.length; k++) {
ths[k].innerHTML = '<span contentEditable="true">' + ths[k].innerHTML + '</span>'
}
var tds = getElementsByTagName(trs[j], "td")
for (k = 0; k < tds.length; k++) {
tds[k].innerHTML = '<span contentEditable="true">' + tds[k].innerHTML + '</span>'
}
}
}
}
function invert() {
tblarea = document.getElementById('table1')
mdarea = document.getElementById('markdown1')
htmlarea = document.getElementById('html1')
var tables = getElementsByTagName(htmlarea, "table")
for (i = 0; i < tables.length; i++) {
var arr = dom2array(tables[i])
mdarea.value = array2md(arr)
tblarea.value = SheetClip.stringify(arr)
}
}
function array2md(arr) {
var maxlen = {}
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
if (!maxlen[j]) maxlen[j] = 0
if (!arr[i][j]) arr[i][j] = ""
maxlen[j] = Math.max(maxlen[j], arr[i][j].length)
}
}
var md = ""
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
if (j != 0) md += " | "
for (k = 0; k < maxlen[j] - arr[i][j].length; k++) {
md += " "
}
md += arr[i][j]
}
md += "\n"
if (i == 0) {
for (j = 0; j < arr[i].length; j++) {
if (j != 0) md += " | "
for (k = 0; k < maxlen[j]; k++) {
md += "-"
}
}
md += "\n"
}
}
return md
}
function dom2array(table) {
var trs = getElementsByTagName(table, "tr")
var rows = trs.length
var cols = 0
for (i = 0; i < trs.length; i++) {
cols = Math.max(cols, getElementsByTagName(trs[i], "th").length)
cols = Math.max(cols, getElementsByTagName(trs[i], "td").length)
}
// fallback for no header
if (getElementsByTagName(trs[0], "th").length == 0) {
rowOffset = 1
}
var arr = new Array(rows+rowOffset)
for (i = 0; i < rowOffset; i++) {
arr[i] = new Array(cols)
for (j = 0; j < cols; j++) {
arr[i][j] = "&nbsp;"
}
}
var options = {
link_list: false, // render links as references, create link list as appendix
h1_setext: true, // underline h1 headers
h2_setext: true, // underline h2 headers
h_atx_suf: false, // header suffixes (###)
gfm_code: true, // gfm code blocks (```)
li_bullet: "*", // list item bullet style
hr_char: "-", // hr style
indnt_str: " ", // indentation string
bold_char: "*", // char used for strong
emph_char: "_", // char used for em
gfm_del: true, // ~~strikeout~~ for <del>strikeout</del>
gfm_tbls: true, // markdown-extra tables
tbl_edges: false, // show side edges on tables
hash_lnks: false, // anchors w/hash hrefs as links
br_only: false // avoid using " " as line break indicator
};
var reMarker = new reMarked(options);
var arr = new Array(rows)
for (i = 0; i < trs.length; i++) {
arr[i+rowOffset] = new Array(cols)
for (j = 0; j < cols; j++) {
arr[i+rowOffset][j] = "&nbsp;"
}
var ths = getElementsByTagName(trs[i], "th")
for (j = 0; j < ths.length; j++) {
var node = (ths[j].firstChild.firstChild ? ths[j].firstChild : ths[j])
arr[i+rowOffset][j] = reMarker.render(node.innerHTML).replace("&nbsp;", " ")
}
var tds = getElementsByTagName(trs[i], "td")
for (j = 0; j < tds.length; j++) {
var node = (tds[j].firstChild.firstChild ? tds[j].firstChild : tds[j])
arr[i+rowOffset][j] = reMarker.render(node.innerHTML).replace("&nbsp;", " ")
}
}
return arr
}
function getElementsByTagName(node, tagName)
{
var elements = [], i = 0, anyTag = (tagName === "*"), root = node, next = node.firstChild;
tagName = tagName.toUpperCase()
while (node = next)
{
if (node.nodeType === 1 && (anyTag || node.nodeName === tagName)) elements[i++] = node;
if (next = node.firstChild || node.nextSibling) continue;
while((node = node.parentNode) && node !== root && !(next = node.nextSibling)) { /* empty */}
}
return elements;
}
</script>
</head>
<body>
<h2>Table to Markdown and vice versa</h2>
<textarea id="table1" rows="30" cols="80" onkeydown="setTimeout(convert, 10);" onpaste="setTimeout(convert, 33);" onchange="convert();" placeholder="Table paste here"></textarea>
<textarea id="markdown1" rows="30" cols="80" onkeydown="setTimeout(revert, 10);" onpaste="setTimeout(revert, 33);" onchange="setTimeout(revert, 33);" placeholder="Markdown edit here"></textarea>
<div id="html1" onkeydown="setTimeout(invert, 10);"><table contentEditable="true"><thead contentEditable="true"><tr contentEditable="true"><th contentEditable="true">A</th><th contentEditable="true">B</th></tr></thead><tbody contentEditable="true"><tr contentEditable="true"><td contentEditable="true">C</td><td contentEditable="true">D</td></tr></tbody></table></div>
<hr />
<p>Recommend: Chrome or Firefox</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment