@ -20,6 +20,8 @@ var IPython = (function (IPython) {
this . selected = false ;
this . element = null ;
this . metadata = { } ;
// load this from metadata later ?
this . user _highlight == 'auto' ;
this . create _element ( ) ;
if ( this . element !== null ) {
this . element . data ( "cell" , this ) ;
@ -154,6 +156,61 @@ var IPython = (function (IPython) {
this . code _mirror . refresh ( ) ;
} ;
Cell . prototype . force _highlight = function ( mode ) {
this . user _highlight = mode ;
this . auto _highlight ( ) ;
} ;
Cell . prototype . _auto _highlight = function ( modes ) {
//Here we handle manually selected modes
if ( this . user _highlight != undefined && this . user _highlight != 'auto' )
{
var mode = this . user _highlight ;
CodeMirror . autoLoadMode ( this . code _mirror , mode ) ;
this . code _mirror . setOption ( 'mode' , mode ) ;
return ;
}
var first _line = this . code _mirror . getLine ( 0 ) ;
// loop on every pairs
for ( var mode in modes ) {
var regs = modes [ mode ] [ 'reg' ] ;
// only one key every time but regexp can't be keys...
for ( var reg in regs ) {
// here we handle non magic_modes
if ( first _line . match ( regs [ reg ] ) != null ) {
if ( mode . search ( 'magic_' ) != 0 ) {
this . code _mirror . setOption ( 'mode' , mode ) ;
CodeMirror . autoLoadMode ( this . code _mirror , mode ) ;
return ;
}
var open = modes [ mode ] [ 'open' ] || "%%" ;
var close = modes [ mode ] [ 'close' ] || "%%end" ;
var mmode = mode ;
mode = mmode . substr ( 6 ) ;
CodeMirror . autoLoadMode ( this . code _mirror , mode ) ;
// create on the fly a mode that swhitch between
// plain/text and smth else otherwise `%%` is
// source of some highlight issues.
// we use patchedGetMode to circumvent a bug in CM
CodeMirror . defineMode ( mmode , function ( config ) {
return CodeMirror . multiplexingMode (
CodeMirror . patchedGetMode ( config , 'text/plain' ) ,
// always set someting on close
{ open : open , close : close ,
mode : CodeMirror . patchedGetMode ( config , mode ) ,
delimStyle : "delimit"
}
) ;
} ) ;
this . code _mirror . setOption ( 'mode' , mmode ) ;
return ;
}
}
}
// fallback on default (python)
var default _mode = this . default _mode || 'text/plain' ;
this . code _mirror . setOption ( 'mode' , default _mode ) ;
} ;
IPython . Cell = Cell ;