Merge pull request #4306 from minrk/raw-cell-metadata

add raw_mimetype metadata to raw cells
Matthias Bussonnier 12 years ago
commit 4039e246fc

@ -33,9 +33,9 @@ var IPython = (function (IPython) {
*/
var Cell = function (options) {
options = this.mergeopt(Cell, options)
options = this.mergeopt(Cell, options);
// superclass default overwrite our default
this.placeholder = options.placeholder || '';
this.read_only = options.cm_config.readOnly;
this.selected = false;
@ -52,7 +52,7 @@ var IPython = (function (IPython) {
// same order. Easiest is to create and set to null in parent class.
this.element = null;
this.cell_type = null;
this.cell_type = this.cell_type || null;
this.code_mirror = null;
@ -80,6 +80,7 @@ var IPython = (function (IPython) {
}
Cell.prototype.mergeopt = function(_class, options, overwrite){
options = options || {};
overwrite = overwrite || {};
return $.extend(true, {}, _class.options_default, options, overwrite)
@ -200,6 +201,7 @@ var IPython = (function (IPython) {
Cell.prototype.toJSON = function () {
var data = {};
data.metadata = this.metadata;
data.cell_type = this.cell_type;
return data;
};

@ -37,37 +37,39 @@ var IPython = (function (IPython) {
this.inner_element = $('<div/>').addClass('celltoolbar')
this.element = $('<div/>').addClass('ctb_hideshow')
.append(this.inner_element);
this.show();
};
// The default css style for the outer celltoolbar div
// (ctb_hideshow) is display: none. We add the ctb_show
// class to either 1) the body to show all cell's toolbars
// or 2) to the individual celltoolbar divs to show just one
// cell's toolbar.
// (ctb_hideshow) is display: none.
// To show the cell toolbar, *both* of the following conditions must be met:
// - A parent container has class `ctb_global_show`
// - The celltoolbar has the class `ctb_show`
// This allows global show/hide, as well as per-cell show/hide.
CellToolbar.global_hide = function () {
$('body').removeClass('ctb_show');
}
$('body').removeClass('ctb_global_show');
};
CellToolbar.global_show = function () {
$('body').addClass('ctb_show');
}
$('body').addClass('ctb_global_show');
};
CellToolbar.prototype.hide = function () {
this.element.removeClass('ctb_show');
}
};
CellToolbar.prototype.show = function () {
this.element.addClass('ctb_show');
}
};
/**
* Class variable that should contain a dict of all availlable callback
* Class variable that should contain a dict of all available callback
* we need to think of wether or not we allow nested namespace
* @property _callback_dict
* @private
@ -89,7 +91,7 @@ var IPython = (function (IPython) {
/**
* Class variable that should contains the CellToolbar instances for each
* Class variable that should contain the CellToolbar instances for each
* cell of the notebook
*
* @private
@ -97,17 +99,17 @@ var IPython = (function (IPython) {
* @static
* @type List
*/
CellToolbar._instances =[]
CellToolbar._instances = [];
/**
* keep a list of all the availlabel presets for the toolbar
* keep a list of all the available presets for the toolbar
* @private
* @property _presets
* @static
* @type Dict
*/
CellToolbar._presets ={}
CellToolbar._presets = {};
// this is by design not a prototype.
@ -180,7 +182,7 @@ var IPython = (function (IPython) {
* CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
*/
CellToolbar.register_preset = function(name, preset_list) {
CellToolbar._presets[name] = preset_list
CellToolbar._presets[name] = preset_list;
$([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
};
@ -217,11 +219,11 @@ var IPython = (function (IPython) {
CellToolbar.activate_preset = function(preset_name){
var preset = CellToolbar._presets[preset_name];
if(preset != undefined){
if(preset !== undefined){
CellToolbar._ui_controls_list = preset;
CellToolbar.rebuild_all();
}
}
};
/**
@ -235,29 +237,37 @@ var IPython = (function (IPython) {
for(var i in CellToolbar._instances){
CellToolbar._instances[i].rebuild();
}
}
};
/**
* Rebuild all the button on the toolbar to update it's state.
* Rebuild all the button on the toolbar to update its state.
* @method rebuild
*/
CellToolbar.prototype.rebuild = function(){
// strip evrything from the div
// which is probabli metainner.
// which is probably inner_element
// or this.element.
this.inner_element.empty();
var cdict = CellToolbar._callback_dict;
var callbacks = CellToolbar._callback_dict;
var preset = CellToolbar._ui_controls_list;
// Yes we iterate on the class varaible, not the instance one.
for ( var index in CellToolbar._ui_controls_list){
// Yes we iterate on the class variable, not the instance one.
for (var index in preset) {
var key = preset[index];
var callback = callbacks[key];
if (!callback) continue;
var local_div = $('<div/>').addClass('button_container');
// Note,
// do this the other way, wrap in try/catch and don't append if any errors.
this.inner_element.append(local_div)
cdict[preset[index]](local_div, this.cell)
try {
callback(local_div, this.cell, this);
} catch (e) {
console.log("Error in cell toolbar callback " + key, e);
continue;
}
// only append if callback succeeded.
this.inner_element.append(local_div);
}
}
};
/**
@ -303,8 +313,8 @@ var IPython = (function (IPython) {
*
*/
CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
return function(div, cell) {
var button_container = $(div)
return function(div, cell, celltoolbar) {
var button_container = $(div);
var chkb = $('<input/>').attr('type', 'checkbox');
var lbl = $('<label/>').append($('<span/>').text(name));
@ -315,11 +325,10 @@ var IPython = (function (IPython) {
var v = getter(cell);
setter(cell, !v);
chkb.attr("checked", !v);
})
button_container.append($('<div/>').append(lbl));
}
}
});
button_container.append($('<div/>').append(lbl));
};
};
/**
@ -365,16 +374,16 @@ var IPython = (function (IPython) {
* CellToolbar.register_callback('slideshow.select', select_type);
*
*/
CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label){
label= label? label: "";
return function(div, cell) {
var button_container = $(div)
CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label, cell_types){
label = label || "";
return function(div, cell, celltoolbar) {
var button_container = $(div);
var lbl = $("<label/>").append($('<span/>').text(label));
var select = $('<select/>').addClass('ui-widget ui-widget-content');
for(var itemn in list_list){
var opt = $('<option/>');
opt.attr('value', list_list[itemn][1])
opt.text(list_list[itemn][0])
var opt = $('<option/>')
.attr('value', list_list[itemn][1])
.text(list_list[itemn][0]);
select.append(opt);
}
select.val(getter(cell));
@ -382,8 +391,13 @@ var IPython = (function (IPython) {
setter(cell, select.val());
});
button_container.append($('<div/>').append(lbl).append(select));
if (cell_types && cell_types.indexOf(cell.cell_type) == -1) {
celltoolbar.hide();
} else {
celltoolbar.show();
}
}
};
};

@ -28,7 +28,7 @@
var button_container = div;
var button = $('<button/>')
.addClass("btn btn-mini")
.text("Raw Edit")
.text("Edit Metadata")
.click( function () {
raw_edit(cell);
return false;
@ -40,7 +40,7 @@
var example_preset = [];
example_preset.push('default.rawedit');
CellToolbar.register_preset('Default', example_preset);
console.log('Default extension for metadata editing loaded.');
CellToolbar.register_preset('Edit Metadata', example_preset);
console.log('Default extension for cell metadata editing loaded.');
}(IPython));

@ -0,0 +1,90 @@
//----------------------------------------------------------------------------
// Copyright (C) 2012 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
// CellToolbar Example
//============================================================================
(function(IPython) {
"use strict";
var CellToolbar = IPython.CellToolbar;
var raw_cell_preset = [];
var utils = IPython.utils;
var select_type = CellToolbar.utils.select_ui_generator([
["None", "-"],
["LaTeX", "text/latex"],
["reST", "text/restructuredtext"],
["HTML", "text/html"],
["Markdown", "text/markdown"],
["Python", "application/x-python"],
["Custom", "dialog"],
],
// setter
function(cell, value) {
if (value === "-") {
delete cell.metadata.raw_mimetype;
} else if (value === 'dialog'){
var dialog = $('<div/>').append(
$("<p/>")
.html("Set the MIME type of the raw cell:")
).append(
$("<br/>")
).append(
$('<input/>').attr('type','text').attr('size','25')
.val(cell.metadata.raw_mimetype || "-")
);
IPython.dialog.modal({
title: "Raw Cell MIME Type",
body: dialog,
buttons : {
"Cancel": {},
"OK": {
class: "btn-primary",
click: function () {
console.log(cell);
cell.metadata.raw_mimetype = $(this).find('input').val();
console.log(cell.metadata);
}
}
},
open : function (event, ui) {
var that = $(this);
// Upon ENTER, click the OK button.
that.find('input[type="text"]').keydown(function (event, ui) {
if (event.which === utils.keycodes.ENTER) {
that.find('.btn-primary').first().click();
return false;
}
});
that.find('input[type="text"]').focus().select();
}
});
} else {
cell.metadata.raw_mimetype = value;
}
},
//getter
function(cell) {
return cell.metadata.raw_mimetype || "";
},
// name
"Raw NBConvert Format",
// cell_types
["raw"]
);
CellToolbar.register_callback('raw_cell.select', select_type);
raw_cell_preset.push('raw_cell.select');
CellToolbar.register_preset('Raw Cell Format', raw_cell_preset);
console.log('Raw Cell Format toolbar preset loaded.');
}(IPython));

@ -445,7 +445,6 @@ var IPython = (function (IPython) {
CodeCell.prototype.toJSON = function () {
var data = IPython.Cell.prototype.toJSON.apply(this);
data.input = this.get_text();
data.cell_type = 'code';
// is finite protect against undefined and '*' value
if (isFinite(this.input_prompt_number)) {
data.prompt_number = this.input_prompt_number;

@ -119,7 +119,7 @@ var IPython = (function (IPython) {
// .addClass('ui-widget-content')
.append($('<option/>').attr('value','code').text('Code'))
.append($('<option/>').attr('value','markdown').text('Markdown'))
.append($('<option/>').attr('value','raw').text('Raw Text'))
.append($('<option/>').attr('value','raw').text('Raw NBConvert'))
.append($('<option/>').attr('value','heading1').text('Heading 1'))
.append($('<option/>').attr('value','heading2').text('Heading 2'))
.append($('<option/>').attr('value','heading3').text('Heading 3'))

@ -46,11 +46,11 @@ var IPython = (function (IPython) {
options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
IPython.Cell.apply(this, [options]);
this.cell_type = this.cell_type || 'text';
IPython.Cell.apply(this, [options]);
this.rendered = false;
this.cell_type = this.cell_type || 'text';
};
TextCell.prototype = new IPython.Cell();
@ -279,8 +279,10 @@ var IPython = (function (IPython) {
*/
TextCell.prototype.toJSON = function () {
var data = IPython.Cell.prototype.toJSON.apply(this);
data.cell_type = this.cell_type;
data.source = this.get_text();
if (data.source == this.placeholder) {
data.source = "";
}
return data;
};
@ -291,12 +293,10 @@ var IPython = (function (IPython) {
* @extends IPython.HTMLCell
*/
var MarkdownCell = function (options) {
var options = options || {};
options = this.mergeopt(MarkdownCell,options);
TextCell.apply(this, [options]);
options = this.mergeopt(MarkdownCell, options);
this.cell_type = 'markdown';
TextCell.apply(this, [options]);
};
MarkdownCell.options_default = {
@ -337,7 +337,7 @@ var IPython = (function (IPython) {
}
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
this.typeset()
this.typeset();
this.rendered = true;
}
};
@ -351,20 +351,21 @@ var IPython = (function (IPython) {
* @extends IPython.TextCell
*/
var RawCell = function (options) {
options = this.mergeopt(RawCell,options)
TextCell.apply(this, [options]);
options = this.mergeopt(RawCell, options);
this.cell_type = 'raw';
TextCell.apply(this, [options]);
var that = this
var that = this;
this.element.focusout(
function() { that.auto_highlight(); }
);
);
};
RawCell.options_default = {
placeholder : "Type plain text and LaTeX: $\\alpha^2$"
placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" +
"It will not be rendered in the notebook.\n" +
"When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
};
@ -382,7 +383,10 @@ var IPython = (function (IPython) {
/** @method render **/
RawCell.prototype.render = function () {
this.rendered = true;
this.edit();
var text = this.get_text();
if (text === "") { text = this.placeholder; }
console.log('rendering', text);
this.set_text(text);
};
@ -415,8 +419,7 @@ var IPython = (function (IPython) {
/** @method select **/
RawCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
this.code_mirror.refresh();
this.code_mirror.focus();
this.edit();
};
/** @method at_top **/
@ -451,16 +454,16 @@ var IPython = (function (IPython) {
* @extends IPython.TextCell
*/
var HeadingCell = function (options) {
options = this.mergeopt(HeadingCell, options);
options = this.mergeopt(HeadingCell,options)
this.level = 1;
this.cell_type = 'heading';
TextCell.apply(this, [options]);
/**
* heading level of the cell, use getter and setter to access
* @property level
*/
this.level = 1;
this.cell_type = 'heading';
};
HeadingCell.options_default = {

@ -1,12 +1,10 @@
/* Css for the metadata edit area */
/* CSS for the cell toolbar */
.celltoolbar {
border: thin solid #CFCFCF;
border-bottom: none;
background : #EEE;
border-top-right-radius: 3px;
border-top-left-radius: 3px;
border-radius : 3px 3px 0px 0px;
width:100%;
-webkit-box-pack: end;
height:22px;
@ -14,20 +12,6 @@
.reverse();
}
.no_input_radius {
border-top-right-radius: 0px;
border-top-left-radius: 0px;
}
.text_cell .ctb_prompt {
display: none;
}
.code_cell .ctb_prompt {
display: block;
}
.ctb_hideshow {
display:none;
vertical-align:bottom;
@ -38,41 +22,21 @@
padding-top: 0px;
}
.ctb_area {
margin:0;
padding:0;
width:100%;
}
/*ctb_show is added to either body or the ctb_hideshow div to show
all or one cell's toolbars.
/* ctb_show is added to the ctb_hideshow div to show the cell toolbar.
Cell toolbars are only shown when the ctb_global_show class is also set.
*/
.ctb_show.ctb_hideshow, .ctb_show .ctb_hideshow {
display:block;
.ctb_global_show .ctb_show.ctb_hideshow {
display: block;
}
.ctb_show .input_area,
.ctb_show .ctb_hideshow + div.text_cell_input {
.ctb_global_show .ctb_show + .input_area,
.ctb_global_show .ctb_show + div.text_cell_input
{
border-top-right-radius: 0px;
border-top-left-radius: 0px;
}
.ctb_show > .celltoolbar {
border-bottom-right-radius: 0px;
border-bottom-left-radius: 0px;
}
.button_container {
margin-top:0;
margin-bottom:0;
}
.ui-button {
min-width:30px;
}
.celltoolbar .button_container select {
margin: 10px;
margin-top: 1px;
@ -108,4 +72,5 @@ all or one cell's toolbars.
border: none;
vertical-align:top;
height:20px;
min-width:30px;
}

@ -1537,23 +1537,16 @@ pre,code,kbd,samp{white-space:pre-wrap;}
#fonttest{font-family:monospace;}
p{margin-bottom:0;}
.end_space{height:200px;}
.celltoolbar{border:thin solid #CFCFCF;border-bottom:none;background:#EEE;border-top-right-radius:3px;border-top-left-radius:3px;width:100%;-webkit-box-pack:end;height:22px;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;-webkit-box-direction:reverse;-moz-box-direction:reverse;box-direction:reverse;}
.no_input_radius{border-top-right-radius:0px;border-top-left-radius:0px;}
.text_cell .ctb_prompt{display:none;}
.code_cell .ctb_prompt{display:block;}
.celltoolbar{border:thin solid #CFCFCF;border-bottom:none;background:#EEE;border-radius:3px 3px 0px 0px;width:100%;-webkit-box-pack:end;height:22px;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;-webkit-box-direction:reverse;-moz-box-direction:reverse;box-direction:reverse;}
.ctb_hideshow{display:none;vertical-align:bottom;padding-right:2px;}
.celltoolbar>div{padding-top:0px;}
.ctb_area{margin:0;padding:0;width:100%;}
.ctb_show.ctb_hideshow,.ctb_show .ctb_hideshow{display:block;}
.ctb_show .input_area,.ctb_show .ctb_hideshow+div.text_cell_input{border-top-right-radius:0px;border-top-left-radius:0px;}
.ctb_show>.celltoolbar{border-bottom-right-radius:0px;border-bottom-left-radius:0px;}
.button_container{margin-top:0;margin-bottom:0;}
.ui-button{min-width:30px;}
.ctb_global_show .ctb_show.ctb_hideshow{display:block;}
.ctb_global_show .ctb_show+.input_area,.ctb_global_show .ctb_show+div.text_cell_input{border-top-right-radius:0px;border-top-left-radius:0px;}
.celltoolbar .button_container select{margin:10px;margin-top:1px;margin-bottom:0px;padding:0;font-size:87%;width:auto;display:inline-block;height:18px;line-height:18px;vertical-align:top;}
.celltoolbar label{display:inline-block;height:15px;line-height:15px;vertical-align:top;}
.celltoolbar label span{font-size:85%;}
.celltoolbar input[type=checkbox]{margin:0px;margin-left:4px;margin-right:4px;}
.celltoolbar .ui-button{border:none;vertical-align:top;height:20px;}
.celltoolbar .ui-button{border:none;vertical-align:top;height:20px;min-width:30px;}
.completions{position:absolute;z-index:10;overflow:hidden;border:1px solid #ababab;border-radius:4px;-webkit-box-shadow:0px 6px 10px -1px #adadad;-moz-box-shadow:0px 6px 10px -1px #adadad;box-shadow:0px 6px 10px -1px #adadad;}
.completions select{background:white;outline:none;border:none;padding:0px;margin:0px;overflow:auto;font-family:monospace;font-size:110%;color:#000000;}
.completions select option.context{color:#0064cd;}

@ -157,8 +157,8 @@ class="notebook_app"
title="Contents will be rendered as HTML and serve as explanatory text">
<a href="#">Markdown</a></li>
<li id="to_raw"
title="Contents will display unmodified in a fixed-width font">
<a href="#">Raw Text</a></li>
title="Contents will pass through nbconvert unmodified">
<a href="#">Raw NBConvert</a></li>
<li id="to_heading1"><a href="#">Heading 1</a></li>
<li id="to_heading2"><a href="#">Heading 2</a></li>
<li id="to_heading3"><a href="#">Heading 3</a></li>
@ -297,6 +297,7 @@ class="notebook_app"
<script src="{{ static_url("notebook/js/contexthint.js") }}" charset="utf-8"></script>
<script src="{{ static_url("notebook/js/celltoolbarpresets/default.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("notebook/js/celltoolbarpresets/rawcell.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("notebook/js/celltoolbarpresets/slideshow.js") }}" type="text/javascript" charset="utf-8"></script>
{% endblock %}

@ -1,6 +1,5 @@
"""
Python exporter which exports Notebook code into a PY file.
"""
"""Python script Exporter class"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
@ -29,3 +28,7 @@ class PythonExporter(TemplateExporter):
file_extension = Unicode(
'py', config=True,
help="Extension of the file that should be written to disk")
def _raw_mimetype_default(self):
return 'application/x-python'

Loading…
Cancel
Save