A first go at RST cell support in the notebook.

This commit is contained in:
Brian Granger 2012-01-23 18:58:50 -08:00
parent d2467be28b
commit 47e4e1e2c4
4 changed files with 82 additions and 41 deletions

View File

@ -129,6 +129,9 @@ var IPython = (function (IPython) {
this.element.find('#to_markdown').click(function () {
IPython.notebook.to_markdown();
});
this.element.find('#to_rst').click(function () {
IPython.notebook.to_rst();
});
this.element.find('#toggle_output').click(function () {
IPython.notebook.toggle_output();
});

View File

@ -135,6 +135,11 @@ var IPython = (function (IPython) {
that.to_markdown();
that.control_key_active = false;
return false;
} else if (event.which === 82 && that.control_key_active) {
// To RST = r
that.to_rst();
that.control_key_active = false;
return false;
} else if (event.which === 84 && that.control_key_active) {
// Toggle output = t
that.toggle_output();
@ -467,15 +472,17 @@ var IPython = (function (IPython) {
// type = ('code','html','markdown')
// index = cell index or undefined to insert below selected
index = this.index_or_selected(index);
var cell = null;
if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
var cell = null;
if (type === 'code') {
var cell = new IPython.CodeCell(this);
cell = new IPython.CodeCell(this);
cell.set_input_prompt();
} else if (type === 'markdown') {
var cell = new IPython.MarkdownCell(this);
cell = new IPython.MarkdownCell(this);
} else if (type === 'html') {
var cell = new IPython.HTMLCell(this);
cell = new IPython.HTMLCell(this);
} else if (type === 'rst') {
cell = new IPython.RSTCell(this);
};
if (cell !== null) {
if (this.ncells() === 0) {
@ -489,6 +496,7 @@ var IPython = (function (IPython) {
return cell;
};
};
return cell;
};
@ -496,15 +504,17 @@ var IPython = (function (IPython) {
// type = ('code','html','markdown')
// index = cell index or undefined to insert above selected
index = this.index_or_selected(index);
var cell = null;
if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
var cell = null;
if (type === 'code') {
var cell = new IPython.CodeCell(this);
cell = new IPython.CodeCell(this);
cell.set_input_prompt();
} else if (type === 'markdown') {
var cell = new IPython.MarkdownCell(this);
cell = new IPython.MarkdownCell(this);
} else if (type === 'html') {
var cell = new IPython.HTMLCell(this);
cell = new IPython.HTMLCell(this);
} else if (type === 'rst') {
cell = new IPython.RSTCell(this);
};
if (cell !== null) {
if (this.ncells() === 0) {
@ -518,6 +528,7 @@ var IPython = (function (IPython) {
return cell;
};
};
return cell;
};
@ -534,8 +545,8 @@ var IPython = (function (IPython) {
}
target_cell.set_text(text);
source_element.remove();
this.dirty = true;
};
this.dirty = true;
};
};
@ -545,19 +556,16 @@ var IPython = (function (IPython) {
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
var target_cell = null;
if (!(source_cell instanceof IPython.MarkdownCell)) {
target_cell = this.insert_cell_below('markdown',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
};
if (target_cell !== null) {
// The edit must come before the set_text.
target_cell.edit();
target_cell.set_text(text);
source_element.remove();
}
// The edit must come before the set_text.
target_cell.edit();
target_cell.set_text(text);
source_element.remove();
this.dirty = true;
};
};
@ -576,18 +584,37 @@ var IPython = (function (IPython) {
if (text === source_cell.placeholder) {
text = '';
};
if (target_cell !== null) {
// The edit must come before the set_text.
target_cell.edit();
target_cell.set_text(text);
source_element.remove();
}
// The edit must come before the set_text.
target_cell.edit();
target_cell.set_text(text);
source_element.remove();
this.dirty = true;
};
};
};
Notebook.prototype.to_rst = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
var target_cell = null;
if (!(source_cell instanceof IPython.RSTCell)) {
target_cell = this.insert_cell_below('rst',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
};
// The edit must come before the set_text.
target_cell.edit();
target_cell.set_text(text);
source_element.remove();
this.dirty = true;
};
};
};
// Cut/Copy/Paste
Notebook.prototype.enable_paste = function () {

View File

@ -241,6 +241,7 @@ var IPython = (function (IPython) {
var RSTCell = function (notebook) {
this.placeholder = "Type *ReStructured Text* and LaTeX: $\\alpha^2$";
this.code_mirror_mode = 'rst';
IPython.TextCell.apply(this, arguments);
this.cell_type = 'rst';
};
@ -250,29 +251,38 @@ var IPython = (function (IPython) {
RSTCell.prototype.render = function () {
if (this.rendered === false) {
var text = this.get_text();
if (text === "") { text = this.placeholder; }
var settings = {
processData : false,
cache : false,
type : "POST",
data : text,
headers : {'Content-Type': 'application/x-rst'},
success : $.proxy(this.handle_render,this)
};
$.ajax("/rstservice/render", settings);
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
this.set_rendered("Rendering...");
this.rendered = true;
this.edit();
};
RSTCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
// In some cases (inserting a new cell) we need a refresh before and
// after the focus. Not sure why this is the case.
this.code_mirror.refresh();
this.code_mirror.focus();
this.code_mirror.refresh();
};
RSTCell.prototype.at_top = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === 0) {
return true;
} else {
return false;
}
};
RSTCell.prototype.handle_render = function (data, status, xhr) {
this.set_rendered(data);
this.typeset();
this.rendered = true;
RSTCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === (this.code_mirror.lineCount()-1)) {
return true;
} else {
return false;
}
};

View File

@ -119,6 +119,7 @@
<hr/>
<li id="to_code"><a href="#">Code Cell</a></li>
<li id="to_markdown"><a href="#">Markdown Cell</a></li>
<li id="to_rst"><a href="#">RST Cell</a></li>
<hr/>
<li id="toggle_output"><a href="#">Toggle Output</a></li>
<li id="clear_all_output"><a href="#">Clear All Output</a></li>