-
Notifications
You must be signed in to change notification settings - Fork 47
/
notebook.min.js
1 lines (1 loc) · 5.15 KB
/
notebook.min.js
1
(function(){var t=this,e=t.document;if(!e){var r=require("jsdom");e=(new r.JSDOM).window.document}var n,i,a=function(t){return t},s=function(t,r){var n=e.createElement(t);return n.className=(r||[]).map(function(t){return l.prefix+t}).join(" "),n},u=function(t){return t.replace(/</g,"<").replace(/>/g,">")},p=function(t){return t.join?t.map(p).join(""):t},o=function(t){return"function"==typeof require&&require(t)},l={prefix:"nb-",markdown:t.marked||o("marked")||a,ansi:(n=o("ansi_up"),i=t.ansi_up||n,i&&i.ansi_to_html||a),highlighter:a,VERSION:"0.3.0"};l.Input=function(t,e){this.raw=t,this.cell=e},l.Input.prototype.render=function(){if(!this.raw.length)return s("div");var t=s("div",["input"]),e=this.cell;"number"==typeof e.number&&t.setAttribute("data-prompt-number",this.cell.number);var r=s("pre"),n=s("code"),i=e.worksheet.notebook.metadata,a=this.cell.raw.language||i.language||i.language_info.name;return n.setAttribute("data-language",a),n.className="lang-"+a,n.innerHTML=l.highlighter(u(p(this.raw)),r,n,a),r.appendChild(n),t.appendChild(r),this.el=t,t};var d=function(t){return function(e){var r=s("img",["image-output"]);return r.src="data:image/"+t+";base64,"+p(e).replace(/\n/g,""),r}};l.display={},l.display.text=function(t){var e=s("pre",["text-output"]);return e.innerHTML=u(p(t)),e},l.display["text/plain"]=l.display.text,l.display.html=function(t){var e=s("div",["html-output"]);return e.innerHTML=p(t),e},l.display["text/html"]=l.display.html,l.display.marked=function(t){return l.display.html(l.markdown(p(t)))},l.display["text/markdown"]=l.display.marked,l.display.svg=function(t){var e=s("div",["svg-output"]);return e.innerHTML=p(t),e},l.display["text/svg+xml"]=l.display.svg,l.display["image/svg+xml"]=l.display.svg,l.display.latex=function(t){var e=s("div",["latex-output"]);return e.innerHTML=p(t),e},l.display["text/latex"]=l.display.latex,l.display.javascript=function(t){var e=s("script");return e.innerHTML=p(t),e},l.display["application/javascript"]=l.display.javascript,l.display.png=d("png"),l.display["image/png"]=l.display.png,l.display.jpeg=d("jpeg"),l.display["image/jpeg"]=l.display.jpeg,l.display_priority=["png","image/png","jpeg","image/jpeg","svg","image/svg+xml","text/svg+xml","html","text/html","text/markdown","latex","text/latex","javascript","application/javascript","text","text/plain"];var c=function(){var t=this,e=l.display_priority.filter(function(e){return t.raw.data?t.raw.data[e]:t.raw[e]})[0];return e&&l.display[e]?l.display[e](t.raw[e]||t.raw.data[e]):s("div",["empty-output"])},h=function(){var t=s("pre",["pyerr"]),e=this.raw.traceback.join("\n");return t.innerHTML=l.highlighter(l.ansi(u(e)),t),t};l.Output=function(t,e){this.raw=t,this.cell=e,this.type=t.output_type},l.Output.prototype.renderers={display_data:c,execute_result:c,pyout:c,pyerr:h,error:h,stream:function(){var t=s("pre",[this.raw.stream||this.raw.name]),e=p(this.raw.text);return t.innerHTML=l.highlighter(l.ansi(u(e)),t),t}},l.Output.prototype.render=function(){var t=s("div",["output"]);"number"==typeof this.cell.number&&t.setAttribute("data-prompt-number",this.cell.number);var e=this.renderers[this.type].call(this);return t.appendChild(e),this.el=t,t},l.coalesceStreams=function(t){if(!t.length)return t;var e=t[0],r=[e];return t.slice(1).forEach(function(t){"stream"===t.raw.output_type&&"stream"===e.raw.output_type&&t.raw.stream===e.raw.stream?e.raw.text=e.raw.text.concat(t.raw.text):(r.push(t),e=t)}),r},l.Cell=function(t,e){var r=this;if(r.raw=t,r.worksheet=e,r.type=t.cell_type,"code"===r.type){r.number=t.prompt_number>-1?t.prompt_number:t.execution_count;var n=t.input||[t.source];r.input=new l.Input(n,r);var i=(r.raw.outputs||[]).map(function(t){return new l.Output(t,r)});r.outputs=l.coalesceStreams(i)}},l.Cell.prototype.renderers={markdown:function(){var t=s("div",["cell","markdown-cell"]);return t.innerHTML=l.markdown(p(this.raw.source)),t},heading:function(){var t=s("h"+this.raw.level,["cell","heading-cell"]);return t.innerHTML=p(this.raw.source),t},raw:function(){var t=s("div",["cell","raw-cell"]);return t.innerHTML=p(this.raw.source),t},code:function(){var t=s("div",["cell","code-cell"]);t.appendChild(this.input.render());this.outputs.forEach(function(e){t.appendChild(e.render())});return t}},l.Cell.prototype.render=function(){var t=this.renderers[this.type].call(this);return this.el=t,t},l.Worksheet=function(t,e){var r=this;this.raw=t,this.notebook=e,this.cells=t.cells.map(function(t){return new l.Cell(t,r)}),this.render=function(){var t=s("div",["worksheet"]);return r.cells.forEach(function(e){t.appendChild(e.render())}),this.el=t,t}},l.Notebook=function(t,e){var r=this;this.raw=t,this.config=e;var n=this.metadata=t.metadata;this.title=n.title||n.name;var i=t.worksheets||[{cells:t.cells}];this.worksheets=i.map(function(t){return new l.Worksheet(t,r)}),this.sheet=this.worksheets[0]},l.Notebook.prototype.render=function(){var t=s("div",["notebook"]);return this.worksheets.forEach(function(e){t.appendChild(e.render())}),this.el=t,t},l.parse=function(t,e){return new l.Notebook(t,e)},"function"==typeof define&&define.amd&&define(function(){return l}),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=l),exports.nb=l):t.nb=l}).call(this);