From c729c95f4441f462b9f0cd1a49b590c8a4d4fb6f Mon Sep 17 00:00:00 2001 From: asher Date: Wed, 23 Aug 2023 09:55:18 +0100 Subject: [PATCH] fix multiline jupyter notebook cells --- lib/jupyter.js | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/jupyter.js b/lib/jupyter.js index a2ece38..c906433 100644 --- a/lib/jupyter.js +++ b/lib/jupyter.js @@ -185,33 +185,25 @@ nbNext=async dir=>{ log(newCell.cell_type); switch (newCell.cell_type) { case "code": - // todo, jupyter dinput - var firstChar = cellSource[0].replace(/\s/g, '')[0]; - if (firstChar === "∇" || firstChar === "]") { + let firstLine = cellSource[0].replace(/\s/g, ''); + let isTradfn = firstLine[0] === "∇"; + let hasDinput = !!firstLine.match(/]dinput/i); + if (isTradfn || hasDinput) { log("tradfn or ]dinput here") - // Submit whole definition - if (cellSource[0].replace(/\s/g, '') === "]dinput") { - //dfn - fnDef = replaceAll(cellSource.slice(1,cellSource.length).join(""), "\n", "") + "\n".repeat(cellSource.length - 1); - } else { - // tradfn - fnDef = cellSource.join(""); - } - submitLine(fnDef).then(fn=>{ - // Format definition with line numbers [1] - for (let i = 1; i < cellSource.length; i++) { - log("----------"); - log(cellSource[i]); - cellSource[i] = "[" + i + "]" + "  " + cellSource[i]; - } - newLine = cellSource.join(""); + // if first line is ]dinput, we don't want to send that to the interpreter so skip it + fnDef = cellSource.slice(+hasDinput, cellSource.length); + // all but the first line should start with a tab to not mess up editing in session history + fnDef[0] = " ".repeat(8) + fnDef[0]; + for (let i = 1; i < fnDef.length; i++) { + fnDef[i] = "\t" + fnDef[i]; + } + submitLine(fnDef.join("")).then(fn=>{ + log(fnDef); + newLine = fnDef.join(""); var div = document.createElement("div"); - div.innerHTML = "
" + "      " + newLine + "";
+              div.innerHTML = "
" + newLine + "";
               //div.onclick=e=>{replaceLine(e.target.innerHTML)};
               mdrender.append(div);
-              // Replace session appendage from submitLine
-              session.value = session.value.split("\n").slice(0,-cellSource.length-1).join("\n") + "\n      ";
-              insertLine(replaceAll(newLine + "\n      ", " ", " ")) 
             });
           } else {
             log("ONE LINER");