##// END OF EJS Templates
A first go at RST cell support in the notebook.
Brian Granger -
Show More
@@ -129,6 +129,9 b' var IPython = (function (IPython) {'
129 this.element.find('#to_markdown').click(function () {
129 this.element.find('#to_markdown').click(function () {
130 IPython.notebook.to_markdown();
130 IPython.notebook.to_markdown();
131 });
131 });
132 this.element.find('#to_rst').click(function () {
133 IPython.notebook.to_rst();
134 });
132 this.element.find('#toggle_output').click(function () {
135 this.element.find('#toggle_output').click(function () {
133 IPython.notebook.toggle_output();
136 IPython.notebook.toggle_output();
134 });
137 });
@@ -135,6 +135,11 b' var IPython = (function (IPython) {'
135 that.to_markdown();
135 that.to_markdown();
136 that.control_key_active = false;
136 that.control_key_active = false;
137 return false;
137 return false;
138 } else if (event.which === 82 && that.control_key_active) {
139 // To RST = r
140 that.to_rst();
141 that.control_key_active = false;
142 return false;
138 } else if (event.which === 84 && that.control_key_active) {
143 } else if (event.which === 84 && that.control_key_active) {
139 // Toggle output = t
144 // Toggle output = t
140 that.toggle_output();
145 that.toggle_output();
@@ -467,15 +472,17 b' var IPython = (function (IPython) {'
467 // type = ('code','html','markdown')
472 // type = ('code','html','markdown')
468 // index = cell index or undefined to insert below selected
473 // index = cell index or undefined to insert below selected
469 index = this.index_or_selected(index);
474 index = this.index_or_selected(index);
475 var cell = null;
470 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
476 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
471 var cell = null;
472 if (type === 'code') {
477 if (type === 'code') {
473 var cell = new IPython.CodeCell(this);
478 cell = new IPython.CodeCell(this);
474 cell.set_input_prompt();
479 cell.set_input_prompt();
475 } else if (type === 'markdown') {
480 } else if (type === 'markdown') {
476 var cell = new IPython.MarkdownCell(this);
481 cell = new IPython.MarkdownCell(this);
477 } else if (type === 'html') {
482 } else if (type === 'html') {
478 var cell = new IPython.HTMLCell(this);
483 cell = new IPython.HTMLCell(this);
484 } else if (type === 'rst') {
485 cell = new IPython.RSTCell(this);
479 };
486 };
480 if (cell !== null) {
487 if (cell !== null) {
481 if (this.ncells() === 0) {
488 if (this.ncells() === 0) {
@@ -489,6 +496,7 b' var IPython = (function (IPython) {'
489 return cell;
496 return cell;
490 };
497 };
491 };
498 };
499 return cell;
492 };
500 };
493
501
494
502
@@ -496,15 +504,17 b' var IPython = (function (IPython) {'
496 // type = ('code','html','markdown')
504 // type = ('code','html','markdown')
497 // index = cell index or undefined to insert above selected
505 // index = cell index or undefined to insert above selected
498 index = this.index_or_selected(index);
506 index = this.index_or_selected(index);
507 var cell = null;
499 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
508 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
500 var cell = null;
501 if (type === 'code') {
509 if (type === 'code') {
502 var cell = new IPython.CodeCell(this);
510 cell = new IPython.CodeCell(this);
503 cell.set_input_prompt();
511 cell.set_input_prompt();
504 } else if (type === 'markdown') {
512 } else if (type === 'markdown') {
505 var cell = new IPython.MarkdownCell(this);
513 cell = new IPython.MarkdownCell(this);
506 } else if (type === 'html') {
514 } else if (type === 'html') {
507 var cell = new IPython.HTMLCell(this);
515 cell = new IPython.HTMLCell(this);
516 } else if (type === 'rst') {
517 cell = new IPython.RSTCell(this);
508 };
518 };
509 if (cell !== null) {
519 if (cell !== null) {
510 if (this.ncells() === 0) {
520 if (this.ncells() === 0) {
@@ -518,6 +528,7 b' var IPython = (function (IPython) {'
518 return cell;
528 return cell;
519 };
529 };
520 };
530 };
531 return cell;
521 };
532 };
522
533
523
534
@@ -534,8 +545,8 b' var IPython = (function (IPython) {'
534 }
545 }
535 target_cell.set_text(text);
546 target_cell.set_text(text);
536 source_element.remove();
547 source_element.remove();
548 this.dirty = true;
537 };
549 };
538 this.dirty = true;
539 };
550 };
540 };
551 };
541
552
@@ -545,19 +556,16 b' var IPython = (function (IPython) {'
545 if (this.is_valid_cell_index(i)) {
556 if (this.is_valid_cell_index(i)) {
546 var source_element = this.get_cell_element(i);
557 var source_element = this.get_cell_element(i);
547 var source_cell = source_element.data("cell");
558 var source_cell = source_element.data("cell");
548 var target_cell = null;
549 if (!(source_cell instanceof IPython.MarkdownCell)) {
559 if (!(source_cell instanceof IPython.MarkdownCell)) {
550 target_cell = this.insert_cell_below('markdown',i);
560 target_cell = this.insert_cell_below('markdown',i);
551 var text = source_cell.get_text();
561 var text = source_cell.get_text();
552 if (text === source_cell.placeholder) {
562 if (text === source_cell.placeholder) {
553 text = '';
563 text = '';
554 };
564 };
555 if (target_cell !== null) {
565 // The edit must come before the set_text.
556 // The edit must come before the set_text.
566 target_cell.edit();
557 target_cell.edit();
567 target_cell.set_text(text);
558 target_cell.set_text(text);
568 source_element.remove();
559 source_element.remove();
560 }
561 this.dirty = true;
569 this.dirty = true;
562 };
570 };
563 };
571 };
@@ -576,18 +584,37 b' var IPython = (function (IPython) {'
576 if (text === source_cell.placeholder) {
584 if (text === source_cell.placeholder) {
577 text = '';
585 text = '';
578 };
586 };
579 if (target_cell !== null) {
587 // The edit must come before the set_text.
580 // The edit must come before the set_text.
588 target_cell.edit();
581 target_cell.edit();
589 target_cell.set_text(text);
582 target_cell.set_text(text);
590 source_element.remove();
583 source_element.remove();
584 }
585 this.dirty = true;
591 this.dirty = true;
586 };
592 };
587 };
593 };
588 };
594 };
589
595
590
596
597 Notebook.prototype.to_rst = function (index) {
598 var i = this.index_or_selected(index);
599 if (this.is_valid_cell_index(i)) {
600 var source_element = this.get_cell_element(i);
601 var source_cell = source_element.data("cell");
602 var target_cell = null;
603 if (!(source_cell instanceof IPython.RSTCell)) {
604 target_cell = this.insert_cell_below('rst',i);
605 var text = source_cell.get_text();
606 if (text === source_cell.placeholder) {
607 text = '';
608 };
609 // The edit must come before the set_text.
610 target_cell.edit();
611 target_cell.set_text(text);
612 source_element.remove();
613 this.dirty = true;
614 };
615 };
616 };
617
591 // Cut/Copy/Paste
618 // Cut/Copy/Paste
592
619
593 Notebook.prototype.enable_paste = function () {
620 Notebook.prototype.enable_paste = function () {
@@ -241,6 +241,7 b' var IPython = (function (IPython) {'
241
241
242 var RSTCell = function (notebook) {
242 var RSTCell = function (notebook) {
243 this.placeholder = "Type *ReStructured Text* and LaTeX: $\\alpha^2$";
243 this.placeholder = "Type *ReStructured Text* and LaTeX: $\\alpha^2$";
244 this.code_mirror_mode = 'rst';
244 IPython.TextCell.apply(this, arguments);
245 IPython.TextCell.apply(this, arguments);
245 this.cell_type = 'rst';
246 this.cell_type = 'rst';
246 };
247 };
@@ -250,29 +251,38 b' var IPython = (function (IPython) {'
250
251
251
252
252 RSTCell.prototype.render = function () {
253 RSTCell.prototype.render = function () {
253 if (this.rendered === false) {
254 this.rendered = true;
254 var text = this.get_text();
255 this.edit();
255 if (text === "") { text = this.placeholder; }
256 };
256 var settings = {
257
257 processData : false,
258
258 cache : false,
259 RSTCell.prototype.select = function () {
259 type : "POST",
260 IPython.Cell.prototype.select.apply(this);
260 data : text,
261 // In some cases (inserting a new cell) we need a refresh before and
261 headers : {'Content-Type': 'application/x-rst'},
262 // after the focus. Not sure why this is the case.
262 success : $.proxy(this.handle_render,this)
263 this.code_mirror.refresh();
263 };
264 this.code_mirror.focus();
264 $.ajax("/rstservice/render", settings);
265 this.code_mirror.refresh();
265 this.element.find('div.text_cell_input').hide();
266 };
266 this.element.find("div.text_cell_render").show();
267
267 this.set_rendered("Rendering...");
268
269 RSTCell.prototype.at_top = function () {
270 var cursor = this.code_mirror.getCursor();
271 if (cursor.line === 0) {
272 return true;
273 } else {
274 return false;
268 }
275 }
269 };
276 };
270
277
271
278
272 RSTCell.prototype.handle_render = function (data, status, xhr) {
279 RSTCell.prototype.at_bottom = function () {
273 this.set_rendered(data);
280 var cursor = this.code_mirror.getCursor();
274 this.typeset();
281 if (cursor.line === (this.code_mirror.lineCount()-1)) {
275 this.rendered = true;
282 return true;
283 } else {
284 return false;
285 }
276 };
286 };
277
287
278
288
@@ -119,6 +119,7 b''
119 <hr/>
119 <hr/>
120 <li id="to_code"><a href="#">Code Cell</a></li>
120 <li id="to_code"><a href="#">Code Cell</a></li>
121 <li id="to_markdown"><a href="#">Markdown Cell</a></li>
121 <li id="to_markdown"><a href="#">Markdown Cell</a></li>
122 <li id="to_rst"><a href="#">RST Cell</a></li>
122 <hr/>
123 <hr/>
123 <li id="toggle_output"><a href="#">Toggle Output</a></li>
124 <li id="toggle_output"><a href="#">Toggle Output</a></li>
124 <li id="clear_all_output"><a href="#">Clear All Output</a></li>
125 <li id="clear_all_output"><a href="#">Clear All Output</a></li>
General Comments 0
You need to be logged in to leave comments. Login now