From 054a1a2ac8a74b963f718976fbe915b551f18202 2014-10-16 22:28:02 From: Thomas Kluyver Date: 2014-10-16 22:28:02 Subject: [PATCH] Add pygments_lexer key to kernelspec --- diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index 8f03eb3..b99d116 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -208,9 +208,9 @@ define([ this.events.on('spec_changed.Kernel', function(event, data) { that.set_kernelspec_metadata(data); - if (data.codemirror_mode) { - that.set_codemirror_mode(data.codemirror_mode); - } + // Mode 'null' should be plain, unhighlighted text. + cm_mode = data.codemirror_mode || data.language || 'null' + that.set_codemirror_mode(cm_mode); }); var collapse_time = function (time) { diff --git a/IPython/kernel/kernelspec.py b/IPython/kernel/kernelspec.py index a0a81a1..9ba9e26 100644 --- a/IPython/kernel/kernelspec.py +++ b/IPython/kernel/kernelspec.py @@ -38,11 +38,15 @@ class KernelSpec(HasTraits): display_name = Unicode() language = Unicode() codemirror_mode = Any() # can be unicode or dict + pygments_lexer = Unicode() env = Dict() resource_dir = Unicode() def _codemirror_mode_default(self): return self.language + + def _pygments_lexer_default(self): + return self.language @classmethod def from_resource_dir(cls, resource_dir): @@ -56,12 +60,17 @@ class KernelSpec(HasTraits): return cls(resource_dir=resource_dir, **kernel_dict) def to_dict(self): - return dict(argv=self.argv, - env=self.env, - display_name=self.display_name, - language=self.language, - codemirror_mode=self.codemirror_mode, - ) + d = dict(argv=self.argv, + env=self.env, + display_name=self.display_name, + language=self.language, + ) + if self.codemirror_mode != self.language: + d['codemirror_mode'] = self.codemirror_mode + if self.pygments_lexer != self.language: + d['pygments_lexer'] = self.pygment_lexer + + return d def to_json(self): return json.dumps(self.to_dict()) @@ -114,6 +123,7 @@ class KernelSpecManager(HasTraits): 'language': 'python', 'codemirror_mode': {'name': 'ipython', 'version': sys.version_info[0]}, + 'pygments_lexer': 'ipython' } @property diff --git a/docs/source/development/kernels.rst b/docs/source/development/kernels.rst index 4077d3b..b9eedae 100644 --- a/docs/source/development/kernels.rst +++ b/docs/source/development/kernels.rst @@ -120,6 +120,9 @@ JSON serialised dictionary containing the following keys and values: to use for code in this language. This can be a string or a dictionary, as passed to codemirror config. The string from *language* will be used if this is not provided. +- **pygments_lexer** (optional): The name of a `Pygments lexer `_ + to use for code in this language, as a string. The value of *language* will be + used if this is not provided. - **env** (optional): A dictionary of environment variables to set for the kernel. These will be added to the current environment variables before the kernel is started.