Commit cf0731f9 authored by Boxiang Sun's avatar Boxiang Sun

erp5_notebook: Process Python code block

We follow the standard jsmd format.
parent aa95e1a0
...@@ -6,12 +6,13 @@ ...@@ -6,12 +6,13 @@
var IODide = function createIODide() { var IODide = function createIODide() {
return; return;
}, },
JSMDCell = function createJSMDCell(type, line_list) { JSMDCell = function createJSMDCell(type, line_list, option) {
this._type = type; this._type = type;
this._line_list = line_list; this._line_list = line_list;
this._option = option;
}, },
split_line_regex = /[\r\n|\n|\r]/, split_line_regex = /[\r\n|\n|\r]/,
cell_type_regexp = /^\%\% (\w+)$/; cell_type_regexp = /^\%\% (\w+)/;
window.iodide = new IODide(); window.iodide = new IODide();
...@@ -67,16 +68,24 @@ ...@@ -67,16 +68,24 @@
var line_list = jsmd.split(split_line_regex), var line_list = jsmd.split(split_line_regex),
i, i,
len = line_list.length, len = line_list.length,
code_type,
current_line, current_line,
current_type, current_type,
current_header,
current_text_list, current_text_list,
option,
next_type, next_type,
cell_list = []; cell_list = [];
function pushNewCell() { function pushNewCell() {
if (current_type !== undefined) { if (current_type !== undefined) {
option = current_header.slice(current_type[0].length);
if (option !== '') {
option = JSON.parse(option);
}
cell_list.push(new JSMDCell(current_type[1], cell_list.push(new JSMDCell(current_type[1],
current_text_list)); current_text_list, option));
} }
} }
...@@ -84,9 +93,10 @@ ...@@ -84,9 +93,10 @@
current_line = line_list[i]; current_line = line_list[i];
next_type = current_line.match(cell_type_regexp); next_type = current_line.match(cell_type_regexp);
if (next_type) { if (next_type) {
// New type detexted // New type detected
pushNewCell(); pushNewCell();
current_type = next_type; current_type = next_type;
current_header = current_line;
current_text_list = []; current_text_list = [];
} else if (current_text_list !== undefined) { } else if (current_text_list !== undefined) {
current_text_list.push(current_line); current_text_list.push(current_line);
...@@ -253,8 +263,17 @@ ...@@ -253,8 +263,17 @@
}); });
} }
function executeCodeCell(line_list, language) {
if (language === 'py') {
console.log(line_list);
console.log("We are processing Python code!!!");
} else {
throw new Error('Unsupported code language: ' + language);
}
}
function executeCell(cell) { function executeCell(cell) {
if (cell._type === 'raw') { if (cell._type === 'raw' || cell._type === 'meta' || cell._type === 'plugin') {
// Do nothing... // Do nothing...
return; return;
} }
...@@ -273,6 +292,9 @@ ...@@ -273,6 +292,9 @@
if (cell._type === 'css') { if (cell._type === 'css') {
return executeCssCell(cell._line_list); return executeCssCell(cell._line_list);
} }
if (cell._type === 'code') {
return executeCodeCell(cell._line_list, cell._option.language);
}
return executeUnknownCellType(cell); return executeUnknownCellType(cell);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment