forked from jsvine/notebookjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
notebook.min.js
1 lines (1 loc) · 5.41 KB
/
notebook.min.js
1
(function(){var t=this,e="0.2.7",r=t.document||require("jsdom").jsdom(),n=function(t){return t},a=function(t,e){var n=r.createElement(t);return n.className=(e||[]).map(function(t){return l.prefix+t}).join(" "),n},i=function(t){var e=t.replace(/</g,"<").replace(/>/g,">");return e},s=function(t){return t.join?t.map(s).join(""):t},o=function(t){return"function"==typeof require&&require(t)},u=function(){return t.marked||o("marked")},p=function(){var e=o("ansi_up"),r=t.ansi_up||e;return r&&r.ansi_to_html},l={prefix:"nb-",markdown:u()||n,ansi:p()||n,VERSION:e};l.Input=function(t,e){this.raw=t,this.cell=e},l.Input.prototype.render=function(){if(!this.raw.length)return a("div");var t=a("div",["input"]),e=this.cell;"number"==typeof e.number&&t.setAttribute("data-prompt-number",this.cell.number);var r=a("pre"),n=a("code"),o=e.worksheet.notebook,u=o.metadata,p=this.cell.raw.language||u.language||u.language_info&&u.language_info.name||u.kernelspec&&u.kernelspec.name||"python";return n.setAttribute("data-language",p),n.className="lang-"+p,n.innerHTML=i(s(this.raw)),r.appendChild(n),t.appendChild(r),this.el=t,t};var c=function(t){return function(e){var r=a("img",["image-output"]);return r.src="data:image/"+t+";base64,"+s(e).replace(/\n/g,""),r}};l.display={},l.display.text=function(t){var e=a("pre",["text-output"]);return e.innerHTML=i(s(t)),e},l.display["text/plain"]=l.display.text,l.display.html=function(t){var e=a("div",["html-output"]);return e.innerHTML=s(t),e},l.display["text/html"]=l.display.html,l.display.md_html=function(t){var e=a("div",["html-output"]);return e.innerHTML=l.markdown(s(t)),e},l.display.marked=function(t){return l.display.html(l.markdown(s(t)))},l.display["text/markdown"]=l.display.marked,l.display.svg=function(t){var e=a("div",["svg-output"]);return e.innerHTML=s(t),e},l.display["text/svg+xml"]=l.display.svg,l.display.latex=function(t){var e=a("div",["latex-output"]);return e.innerHTML=s(t),e},l.display["text/latex"]=l.display.latex,l.display.javascript=function(t){var e=a("script");return e.innerHTML=t,e},l.display["application/javascript"]=l.display.javascript,l.display.png=c("png"),l.display["image/png"]=l.display.png,l.display.jpeg=c("jpeg"),l.display["image/jpeg"]=l.display.jpeg,l.display_priority=["png","image/png","jpeg","image/jpeg","svg","text/svg+xml","html","text/html","text/markdown","latex","text/latex","javascript","application/javascript","text","text/plain"];var d=function(){var t=this,e=l.display_priority.filter(function(e){return t.raw.data?t.raw.data[e]:t.raw[e]}),r=e[0];return r&&l.display[r]?l.display[r](t.raw[r]||t.raw.data[r]):a("div",["empty-output"])},h=function(){var t=a("pre",["pyerr"]),e=this.raw.traceback.join("\n");return t.innerHTML=l.ansi(i(e)),t};l.Output=function(t,e){this.raw=t,this.cell=e,this.type=t.output_type},l.Output.prototype.renderers={display_data:d,execute_result:d,pyout:d,pyerr:h,error:h,stream:function(){var t=a("pre",[this.raw.stream||this.raw.name]),e=s(this.raw.text);return t.innerHTML=l.ansi(i(e)),t}},l.Output.prototype.render=function(){var t=a("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 a=(r.raw.outputs||[]).map(function(t){return new l.Output(t,r)});r.outputs=l.coalesceStreams(a)}},l.Cell.prototype.renderers={markdown:function(){var t=a("div",["cell","markdown-cell"]);return t.innerHTML=l.markdown(s(this.raw.source)),t},heading:function(){var t=a("h"+this.raw.level,["cell","heading-cell"]);return t.innerHTML=s(this.raw.source),t},raw:function(){var t=a("div",["cell","raw-cell"]);return t.innerHTML=s(this.raw.source),t},code:function(){var t=a("div",["cell","code-cell"]);t.appendChild(this.input.render());this.outputs.forEach(function(e){try{t.appendChild(e.render())}catch(e){console.log(e.stack),t.appendChild(l.display.text("There was an error rendering this output"))}});return t}},l.Cell.prototype.render=function(){try{var t=this.renderers[this.type].call(this)}catch(t){console.log(t.stack);var t=l.display.text("There was an error rendering this cell")}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=a("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 a=t.worksheets||[{cells:t.cells}];this.worksheets=a.map(function(t){return new l.Worksheet(t,r)}),this.sheet=this.worksheets[0]},l.Notebook.prototype.render=function(){var t=a("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);