##// END OF EJS Templates
Fixed mardown rendering bug....
Jonathan Frederic -
Show More
@@ -1,458 +1,458 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2012 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // TextCell
10 10 //============================================================================
11 11
12 12
13 13
14 14 /**
15 15 A module that allow to create different type of Text Cell
16 16 @module IPython
17 17 @namespace IPython
18 18 */
19 19 var IPython = (function (IPython) {
20 20 "use strict";
21 21
22 22 // TextCell base class
23 23 var keycodes = IPython.keyboard.keycodes;
24 24 var security = IPython.security;
25 25
26 26 /**
27 27 * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text'
28 28 * cell start as not redered.
29 29 *
30 30 * @class TextCell
31 31 * @constructor TextCell
32 32 * @extend IPython.Cell
33 33 * @param {object|undefined} [options]
34 34 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
35 35 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
36 36 */
37 37 var TextCell = function (options) {
38 38 // in all TextCell/Cell subclasses
39 39 // do not assign most of members here, just pass it down
40 40 // in the options dict potentially overwriting what you wish.
41 41 // they will be assigned in the base class.
42 42
43 43 // we cannot put this as a class key as it has handle to "this".
44 44 var cm_overwrite_options = {
45 45 onKeyEvent: $.proxy(this.handle_keyevent,this)
46 46 };
47 47
48 48 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
49 49
50 50 this.cell_type = this.cell_type || 'text';
51 51
52 52 IPython.Cell.apply(this, [options]);
53 53
54 54 this.rendered = false;
55 55 };
56 56
57 57 TextCell.prototype = new IPython.Cell();
58 58
59 59 TextCell.options_default = {
60 60 cm_config : {
61 61 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
62 62 mode: 'htmlmixed',
63 63 lineWrapping : true,
64 64 }
65 65 };
66 66
67 67
68 68 /**
69 69 * Create the DOM element of the TextCell
70 70 * @method create_element
71 71 * @private
72 72 */
73 73 TextCell.prototype.create_element = function () {
74 74 IPython.Cell.prototype.create_element.apply(this, arguments);
75 75
76 76 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
77 77 cell.attr('tabindex','2');
78 78
79 79 var prompt = $('<div/>').addClass('prompt input_prompt');
80 80 cell.append(prompt);
81 81 var inner_cell = $('<div/>').addClass('inner_cell');
82 82 this.celltoolbar = new IPython.CellToolbar(this);
83 83 inner_cell.append(this.celltoolbar.element);
84 84 var input_area = $('<div/>').addClass('input_area');
85 85 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
86 86 // The tabindex=-1 makes this div focusable.
87 87 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
88 88 addClass('rendered_html').attr('tabindex','-1');
89 89 inner_cell.append(input_area).append(render_area);
90 90 cell.append(inner_cell);
91 91 this.element = cell;
92 92 };
93 93
94 94
95 95 /**
96 96 * Bind the DOM evet to cell actions
97 97 * Need to be called after TextCell.create_element
98 98 * @private
99 99 * @method bind_event
100 100 */
101 101 TextCell.prototype.bind_events = function () {
102 102 IPython.Cell.prototype.bind_events.apply(this);
103 103 var that = this;
104 104
105 105 this.element.dblclick(function () {
106 106 if (that.selected === false) {
107 107 $([IPython.events]).trigger('select.Cell', {'cell':that});
108 108 }
109 109 var cont = that.unrender();
110 110 if (cont) {
111 111 that.focus_editor();
112 112 }
113 113 });
114 114 };
115 115
116 116 // Cell level actions
117 117
118 118 TextCell.prototype.select = function () {
119 119 var cont = IPython.Cell.prototype.select.apply(this);
120 120 if (cont) {
121 121 if (this.mode === 'edit') {
122 122 this.code_mirror.refresh();
123 123 }
124 124 }
125 125 return cont;
126 126 };
127 127
128 128 TextCell.prototype.unrender = function () {
129 129 if (this.read_only) return;
130 130 var cont = IPython.Cell.prototype.unrender.apply(this);
131 131 if (cont) {
132 132 var text_cell = this.element;
133 133 var output = text_cell.find("div.text_cell_render");
134 134 output.hide();
135 135 text_cell.find('div.input_area').show();
136 136 if (this.get_text() === this.placeholder) {
137 137 this.set_text('');
138 138 }
139 139 this.refresh();
140 140 }
141 141 if (this.celltoolbar.ui_controls_list.length) {
142 142 this.celltoolbar.show();
143 143 }
144 144 return cont;
145 145 };
146 146
147 147 TextCell.prototype.execute = function () {
148 148 this.render();
149 149 };
150 150
151 151 /**
152 152 * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}}
153 153 * @method get_text
154 154 * @retrun {string} CodeMirror current text value
155 155 */
156 156 TextCell.prototype.get_text = function() {
157 157 return this.code_mirror.getValue();
158 158 };
159 159
160 160 /**
161 161 * @param {string} text - Codemiror text value
162 162 * @see TextCell#get_text
163 163 * @method set_text
164 164 * */
165 165 TextCell.prototype.set_text = function(text) {
166 166 this.code_mirror.setValue(text);
167 167 this.code_mirror.refresh();
168 168 };
169 169
170 170 /**
171 171 * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}}
172 172 * @method get_rendered
173 173 * @return {html} html of rendered element
174 174 * */
175 175 TextCell.prototype.get_rendered = function() {
176 176 return this.element.find('div.text_cell_render').html();
177 177 };
178 178
179 179 /**
180 180 * @method set_rendered
181 181 */
182 182 TextCell.prototype.set_rendered = function(text) {
183 183 this.element.find('div.text_cell_render').html(text);
184 184 this.celltoolbar.hide();
185 185 };
186 186
187 187
188 188 /**
189 189 * Create Text cell from JSON
190 190 * @param {json} data - JSON serialized text-cell
191 191 * @method fromJSON
192 192 */
193 193 TextCell.prototype.fromJSON = function (data) {
194 194 IPython.Cell.prototype.fromJSON.apply(this, arguments);
195 195 if (data.cell_type === this.cell_type) {
196 196 if (data.source !== undefined) {
197 197 this.set_text(data.source);
198 198 // make this value the starting point, so that we can only undo
199 199 // to this state, instead of a blank cell
200 200 this.code_mirror.clearHistory();
201 201 // TODO: This HTML needs to be treated as potentially dangerous
202 202 // user input and should be handled before set_rendered.
203 203 this.set_rendered(data.rendered || '');
204 204 this.rendered = false;
205 205 this.render();
206 206 }
207 207 }
208 208 };
209 209
210 210 /** Generate JSON from cell
211 211 * @return {object} cell data serialised to json
212 212 */
213 213 TextCell.prototype.toJSON = function () {
214 214 var data = IPython.Cell.prototype.toJSON.apply(this);
215 215 data.source = this.get_text();
216 216 if (data.source == this.placeholder) {
217 217 data.source = "";
218 218 }
219 219 return data;
220 220 };
221 221
222 222
223 223 /**
224 224 * @class MarkdownCell
225 225 * @constructor MarkdownCell
226 226 * @extends IPython.HTMLCell
227 227 */
228 228 var MarkdownCell = function (options) {
229 229 options = this.mergeopt(MarkdownCell, options);
230 230
231 231 this.cell_type = 'markdown';
232 232 TextCell.apply(this, [options]);
233 233 };
234 234
235 235 MarkdownCell.options_default = {
236 236 cm_config: {
237 237 mode: 'gfm'
238 238 },
239 239 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
240 240 };
241 241
242 242 MarkdownCell.prototype = new TextCell();
243 243
244 244 /**
245 245 * @method render
246 246 */
247 247 MarkdownCell.prototype.render = function () {
248 248 var cont = IPython.TextCell.prototype.render.apply(this);
249 249 if (cont) {
250 250 var text = this.get_text();
251 251 var math = null;
252 252 if (text === "") { text = this.placeholder; }
253 253 var text_and_math = IPython.mathjaxutils.remove_math(text);
254 254 text = text_and_math[0];
255 255 math = text_and_math[1];
256 256 var html = marked.parser(marked.lexer(text));
257 257 html = IPython.mathjaxutils.replace_math(html, math);
258 258 html = security.sanitize_html(html);
259 html = $(html);
259 html = $($.parseHTML(html));
260 260 // links in markdown cells should open in new tabs
261 261 html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
262 262 this.set_rendered(html);
263 263 this.element.find('div.input_area').hide();
264 264 this.element.find("div.text_cell_render").show();
265 265 this.typeset();
266 266 }
267 267 return cont;
268 268 };
269 269
270 270
271 271 // RawCell
272 272
273 273 /**
274 274 * @class RawCell
275 275 * @constructor RawCell
276 276 * @extends IPython.TextCell
277 277 */
278 278 var RawCell = function (options) {
279 279
280 280 options = this.mergeopt(RawCell,options);
281 281 TextCell.apply(this, [options]);
282 282 this.cell_type = 'raw';
283 283 // RawCell should always hide its rendered div
284 284 this.element.find('div.text_cell_render').hide();
285 285 };
286 286
287 287 RawCell.options_default = {
288 288 placeholder : "Write raw LaTeX or other formats here, for use with nbconvert. " +
289 289 "It will not be rendered in the notebook. " +
290 290 "When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
291 291 };
292 292
293 293 RawCell.prototype = new TextCell();
294 294
295 295 /** @method bind_events **/
296 296 RawCell.prototype.bind_events = function () {
297 297 TextCell.prototype.bind_events.apply(this);
298 298 var that = this;
299 299 this.element.focusout(function() {
300 300 that.auto_highlight();
301 301 that.render();
302 302 });
303 303
304 304 this.code_mirror.on('focus', function() { that.unrender(); });
305 305 };
306 306
307 307 /**
308 308 * Trigger autodetection of highlight scheme for current cell
309 309 * @method auto_highlight
310 310 */
311 311 RawCell.prototype.auto_highlight = function () {
312 312 this._auto_highlight(IPython.config.raw_cell_highlight);
313 313 };
314 314
315 315 /** @method render **/
316 316 RawCell.prototype.render = function () {
317 317 var cont = IPython.TextCell.prototype.render.apply(this);
318 318 if (cont){
319 319 var text = this.get_text();
320 320 if (text === "") { text = this.placeholder; }
321 321 this.set_text(text);
322 322 this.element.removeClass('rendered');
323 323 }
324 324 return cont;
325 325 };
326 326
327 327
328 328 /**
329 329 * @class HeadingCell
330 330 * @extends IPython.TextCell
331 331 */
332 332
333 333 /**
334 334 * @constructor HeadingCell
335 335 * @extends IPython.TextCell
336 336 */
337 337 var HeadingCell = function (options) {
338 338 options = this.mergeopt(HeadingCell, options);
339 339
340 340 this.level = 1;
341 341 this.cell_type = 'heading';
342 342 TextCell.apply(this, [options]);
343 343
344 344 /**
345 345 * heading level of the cell, use getter and setter to access
346 346 * @property level
347 347 */
348 348 };
349 349
350 350 HeadingCell.options_default = {
351 351 placeholder: "Type Heading Here"
352 352 };
353 353
354 354 HeadingCell.prototype = new TextCell();
355 355
356 356 /** @method fromJSON */
357 357 HeadingCell.prototype.fromJSON = function (data) {
358 358 if (data.level !== undefined){
359 359 this.level = data.level;
360 360 }
361 361 TextCell.prototype.fromJSON.apply(this, arguments);
362 362 };
363 363
364 364
365 365 /** @method toJSON */
366 366 HeadingCell.prototype.toJSON = function () {
367 367 var data = TextCell.prototype.toJSON.apply(this);
368 368 data.level = this.get_level();
369 369 return data;
370 370 };
371 371
372 372 /**
373 373 * can the cell be split into two cells
374 374 * @method is_splittable
375 375 **/
376 376 HeadingCell.prototype.is_splittable = function () {
377 377 return false;
378 378 };
379 379
380 380
381 381 /**
382 382 * can the cell be merged with other cells
383 383 * @method is_mergeable
384 384 **/
385 385 HeadingCell.prototype.is_mergeable = function () {
386 386 return false;
387 387 };
388 388
389 389 /**
390 390 * Change heading level of cell, and re-render
391 391 * @method set_level
392 392 */
393 393 HeadingCell.prototype.set_level = function (level) {
394 394 this.level = level;
395 395 if (this.rendered) {
396 396 this.rendered = false;
397 397 this.render();
398 398 }
399 399 };
400 400
401 401 /** The depth of header cell, based on html (h1 to h6)
402 402 * @method get_level
403 403 * @return {integer} level - for 1 to 6
404 404 */
405 405 HeadingCell.prototype.get_level = function () {
406 406 return this.level;
407 407 };
408 408
409 409
410 410 HeadingCell.prototype.get_rendered = function () {
411 411 var r = this.element.find("div.text_cell_render");
412 412 return r.children().first().html();
413 413 };
414 414
415 415
416 416 HeadingCell.prototype.render = function () {
417 417 var cont = IPython.TextCell.prototype.render.apply(this);
418 418 if (cont) {
419 419 var text = this.get_text();
420 420 var math = null;
421 421 // Markdown headings must be a single line
422 422 text = text.replace(/\n/g, ' ');
423 423 if (text === "") { text = this.placeholder; }
424 424 text = Array(this.level + 1).join("#") + " " + text;
425 425 var text_and_math = IPython.mathjaxutils.remove_math(text);
426 426 text = text_and_math[0];
427 427 math = text_and_math[1];
428 428 var html = marked.parser(marked.lexer(text));
429 429 html = IPython.mathjaxutils.replace_math(html, math);
430 430 html = security.sanitize_html(html);
431 var h = $(html);
431 var h = $($.parseHTML(html));
432 432 // add id and linkback anchor
433 433 var hash = h.text().replace(/ /g, '-');
434 434 h.attr('id', hash);
435 435 h.append(
436 436 $('<a/>')
437 437 .addClass('anchor-link')
438 438 .attr('href', '#' + hash)
439 439 .text('¶')
440 440 );
441 441 this.set_rendered(h);
442 442 this.element.find('div.input_area').hide();
443 443 this.element.find("div.text_cell_render").show();
444 444 this.typeset();
445 445 }
446 446 return cont;
447 447 };
448 448
449 449 IPython.TextCell = TextCell;
450 450 IPython.MarkdownCell = MarkdownCell;
451 451 IPython.RawCell = RawCell;
452 452 IPython.HeadingCell = HeadingCell;
453 453
454 454
455 455 return IPython;
456 456
457 457 }(IPython));
458 458
General Comments 0
You need to be logged in to leave comments. Login now