Permalink
Browse files

Markdown cells are now saved and restored in notebooks.

  • Loading branch information...
1 parent 978f742 commit a85c3e74f4a7174d45040c64c9f78943ee1e22be @ellisonbg ellisonbg committed Aug 9, 2011
@@ -624,6 +624,9 @@ var IPython = (function (IPython) {
} else if (cell_data.cell_type === 'html') {
new_cell = this.insert_html_cell_after();
new_cell.fromJSON(cell_data);
+ } else if (cell_data.cell_type === 'markdown') {
+ new_cell = this.insert_markdown_cell_after();
+ new_cell.fromJSON(cell_data);
};
};
};
@@ -132,7 +132,7 @@ var IPython = (function (IPython) {
if (data.cell_type === this.cell_type) {
if (data.source !== undefined) {
this.set_source(data.source);
- this.set_rendered(data.source);
+ this.set_rendered(data.rendered);
};
};
}
@@ -7,7 +7,7 @@
from IPython.nbformat.v2 import (
NotebookNode,
- new_code_cell, new_html_cell, new_notebook, new_output, new_worksheet
+ new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet
)
@@ -1,7 +1,7 @@
from .nbbase import (
NotebookNode,
- new_code_cell, new_html_cell, new_notebook, new_output, new_worksheet
+ new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet
)
from .nbjson import reads as reads_json, writes as writes_json
@@ -1,16 +1,16 @@
from .nbbase import (
- new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output
+ new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
)
def convert_to_this_nbformat(nb, orig_version=1):
if orig_version == 1:
newnb = new_notebook()
ws = new_worksheet()
for cell in nb.cells:
- if cell.cell_type == 'code':
+ if cell.cell_type == u'code':
newcell = new_code_cell(input=cell.get('code'),prompt_number=cell.get('prompt_number'))
- elif cell.cell_type == 'text':
- newcell = new_html_cell(source=cell.get('text'))
+ elif cell.cell_type == u'text':
+ newcell = new_text_cell(u'markdown',source=cell.get('text'))
ws.cells.append(newcell)
newnb.worksheets.append(ws)
return newnb
@@ -65,12 +65,14 @@ def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'pytho
return cell
-def new_html_cell(source=None):
+def new_text_cell(cell_type, source=None, rendered=None):
"""Create a new text cell."""
cell = NotebookNode()
if source is not None:
cell.source = unicode(source)
- cell.cell_type = u'html'
+ if rendered is not None:
+ cell.rendered = unicode(rendered)
+ cell.cell_type = cell_type
return cell
@@ -5,7 +5,7 @@
from .rwbase import NotebookReader, NotebookWriter
from .nbbase import (
- new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output
+ new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
)
def indent(elem, level=0):
@@ -108,7 +108,12 @@ def to_notebook(self, root, **kwargs):
cells.append(cc)
if cell_e.tag == 'htmlcell':
source = _get_text(cell_e,'source')
- cells.append(new_html_cell(source=source))
+ rendered = _get_text(cell_e,'rendered')
+ cells.append(new_text_cell(u'html', source=source, rendered=rendered))
+ if cell_e.tag == 'markdowncell':
+ source = _get_text(cell_e,'source')
+ rendered = _get_text(cell_e,'rendered')
+ cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
ws = new_worksheet(name=wsname,cells=cells)
worksheets.append(ws)
@@ -150,6 +155,11 @@ def writes(self, nb, **kwargs):
elif cell_type == 'html':
cell_e = ET.SubElement(cells_e, 'htmlcell')
_set_text(cell,'source',cell_e,'source')
+ _set_text(cell,'rendered',cell_e,'rendered')
+ elif cell_type == 'markdown':
+ cell_e = ET.SubElement(cells_e, 'markdowncell')
+ _set_text(cell,'source',cell_e,'source')
+ _set_text(cell,'rendered',cell_e,'rendered')
indent(nb_e)
txt = ET.tostring(nb_e, encoding="utf-8")
@@ -1,14 +1,16 @@
from ..nbbase import (
NotebookNode,
- new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output
+ new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
)
ws = new_worksheet(name='worksheet1')
-ws.cells.append(new_html_cell(
- source='Some NumPy Examples'
+ws.cells.append(new_text_cell(
+ u'html',
+ source='Some NumPy Examples',
+ rendered='Some NumPy Examples'
))
@@ -17,6 +19,12 @@
prompt_number=1
))
+ws.cells.append(new_text_cell(
+ u'markdown',
+ source='Some NumPy Examples',
+ rendered='Some NumPy Examples'
+))
+
ws.cells.append(new_code_cell(
input='a = numpy.random.rand(100)',
prompt_number=2
@@ -2,7 +2,7 @@
from ..nbbase import (
NotebookNode,
- new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output
+ new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
)
class TestCell(TestCase):
@@ -26,13 +26,26 @@ def test_code_cell(self):
self.assertEquals(cc.outputs[0].prompt_number, 0)
def test_empty_html_cell(self):
- tc = new_html_cell()
- self.assertEquals(tc.cell_type, 'html')
+ tc = new_text_cell(u'html')
+ self.assertEquals(tc.cell_type, u'html')
self.assertEquals('source' not in tc, True)
+ self.assertEquals('rendered' not in tc, True)
def test_html_cell(self):
- tc = new_html_cell('hi')
+ tc = new_text_cell(u'html', 'hi', 'hi')
self.assertEquals(tc.source, u'hi')
+ self.assertEquals(tc.rendered, u'hi')
+
+ def test_empty_markdown_cell(self):
+ tc = new_text_cell(u'markdown')
+ self.assertEquals(tc.cell_type, u'markdown')
+ self.assertEquals('source' not in tc, True)
+ self.assertEquals('rendered' not in tc, True)
+
+ def test_markdown_cell(self):
+ tc = new_text_cell(u'markdown', 'hi', 'hi')
+ self.assertEquals(tc.source, u'hi')
+ self.assertEquals(tc.rendered, u'hi')
class TestWorksheet(TestCase):
@@ -43,7 +56,7 @@ def test_empty_worksheet(self):
self.assertEquals('name' not in ws, True)
def test_worksheet(self):
- cells = [new_code_cell(), new_html_cell()]
+ cells = [new_code_cell(), new_text_cell(u'html')]
ws = new_worksheet(cells=cells,name='foo')
self.assertEquals(ws.cells,cells)
self.assertEquals(ws.name,u'foo')
@@ -2,7 +2,7 @@
from ..nbbase import (
NotebookNode,
- new_code_cell, new_html_cell, new_worksheet, new_notebook
+ new_code_cell, new_text_cell, new_worksheet, new_notebook
)
from ..nbpy import reads, writes

0 comments on commit a85c3e7

Please sign in to comment.