##// END OF EJS Templates
protect highlight.js against unknown languages...
MinRK -
Show More
@@ -1,115 +1,115
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // On document ready
9 // On document ready
10 //============================================================================
10 //============================================================================
11
11
12
12
13 $(document).ready(function () {
13 $(document).ready(function () {
14
14
15 // monkey patch CM to be able to syntax highlight cell magics
15 // monkey patch CM to be able to syntax highlight cell magics
16 // bug reported upstream,
16 // bug reported upstream,
17 // see https://github.com/marijnh/CodeMirror2/issues/670
17 // see https://github.com/marijnh/CodeMirror2/issues/670
18 if(CodeMirror.getMode(1,'text/plain').indent == undefined ){
18 if(CodeMirror.getMode(1,'text/plain').indent == undefined ){
19 console.log('patching CM for undefined indent');
19 console.log('patching CM for undefined indent');
20 CodeMirror.modes.null = function() { return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0}}}
20 CodeMirror.modes.null = function() { return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0}}}
21 }
21 }
22
22
23 CodeMirror.patchedGetMode = function(config, mode){
23 CodeMirror.patchedGetMode = function(config, mode){
24 var cmmode = CodeMirror.getMode(config, mode);
24 var cmmode = CodeMirror.getMode(config, mode);
25 if(cmmode.indent == null)
25 if(cmmode.indent == null)
26 {
26 {
27 console.log('patch mode "' , mode, '" on the fly');
27 console.log('patch mode "' , mode, '" on the fly');
28 cmmode.indent = function(){return 0};
28 cmmode.indent = function(){return 0};
29 }
29 }
30 return cmmode;
30 return cmmode;
31 }
31 }
32 // end monkey patching CodeMirror
32 // end monkey patching CodeMirror
33
33
34 IPython.mathjaxutils.init();
34 IPython.mathjaxutils.init();
35
35
36 IPython.read_only = $('body').data('readOnly') === 'True';
36 IPython.read_only = $('body').data('readOnly') === 'True';
37 $('#ipython-main-app').addClass('border-box-sizing');
37 $('#ipython-main-app').addClass('border-box-sizing');
38 $('div#notebook_panel').addClass('border-box-sizing');
38 $('div#notebook_panel').addClass('border-box-sizing');
39 // The header's bottom border is provided by the menu bar so we remove it.
39 // The header's bottom border is provided by the menu bar so we remove it.
40 $('div#header').css('border-bottom-style','none');
40 $('div#header').css('border-bottom-style','none');
41
41
42 var baseProjectUrl = $('body').data('baseProjectUrl')
42 var baseProjectUrl = $('body').data('baseProjectUrl')
43
43
44 IPython.page = new IPython.Page();
44 IPython.page = new IPython.Page();
45 IPython.layout_manager = new IPython.LayoutManager();
45 IPython.layout_manager = new IPython.LayoutManager();
46 IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter');
46 IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter');
47 IPython.quick_help = new IPython.QuickHelp('span#quick_help_area');
47 IPython.quick_help = new IPython.QuickHelp('span#quick_help_area');
48 IPython.login_widget = new IPython.LoginWidget('span#login_widget',{baseProjectUrl:baseProjectUrl});
48 IPython.login_widget = new IPython.LoginWidget('span#login_widget',{baseProjectUrl:baseProjectUrl});
49 IPython.notebook = new IPython.Notebook('div#notebook',{baseProjectUrl:baseProjectUrl, read_only:IPython.read_only});
49 IPython.notebook = new IPython.Notebook('div#notebook',{baseProjectUrl:baseProjectUrl, read_only:IPython.read_only});
50 IPython.save_widget = new IPython.SaveWidget('span#save_widget');
50 IPython.save_widget = new IPython.SaveWidget('span#save_widget');
51 IPython.menubar = new IPython.MenuBar('#menubar',{baseProjectUrl:baseProjectUrl})
51 IPython.menubar = new IPython.MenuBar('#menubar',{baseProjectUrl:baseProjectUrl})
52 IPython.toolbar = new IPython.MainToolBar('#maintoolbar')
52 IPython.toolbar = new IPython.MainToolBar('#maintoolbar')
53 IPython.tooltip = new IPython.Tooltip()
53 IPython.tooltip = new IPython.Tooltip()
54 IPython.notification_area = new IPython.NotificationArea('#notification_area')
54 IPython.notification_area = new IPython.NotificationArea('#notification_area')
55 IPython.notification_area.init_notification_widgets();
55 IPython.notification_area.init_notification_widgets();
56
56
57 IPython.layout_manager.do_resize();
57 IPython.layout_manager.do_resize();
58
58
59 $('body').append('<div id="fonttest"><pre><span id="test1">x</span>'+
59 $('body').append('<div id="fonttest"><pre><span id="test1">x</span>'+
60 '<span id="test2" style="font-weight: bold;">x</span>'+
60 '<span id="test2" style="font-weight: bold;">x</span>'+
61 '<span id="test3" style="font-style: italic;">x</span></pre></div>')
61 '<span id="test3" style="font-style: italic;">x</span></pre></div>')
62 var nh = $('#test1').innerHeight();
62 var nh = $('#test1').innerHeight();
63 var bh = $('#test2').innerHeight();
63 var bh = $('#test2').innerHeight();
64 var ih = $('#test3').innerHeight();
64 var ih = $('#test3').innerHeight();
65 if(nh != bh || nh != ih) {
65 if(nh != bh || nh != ih) {
66 $('head').append('<style>.CodeMirror span { vertical-align: bottom; }</style>');
66 $('head').append('<style>.CodeMirror span { vertical-align: bottom; }</style>');
67 }
67 }
68 $('#fonttest').remove();
68 $('#fonttest').remove();
69
69
70 if(IPython.read_only){
70 if(IPython.read_only){
71 // hide various elements from read-only view
71 // hide various elements from read-only view
72 $('div#pager').remove();
72 $('div#pager').remove();
73 $('div#pager_splitter').remove();
73 $('div#pager_splitter').remove();
74
74
75 // set the notebook name field as not modifiable
75 // set the notebook name field as not modifiable
76 $('#notebook_name').attr('disabled','disabled')
76 $('#notebook_name').attr('disabled','disabled')
77 }
77 }
78
78
79 IPython.page.show();
79 IPython.page.show();
80
80
81 IPython.layout_manager.do_resize();
81 IPython.layout_manager.do_resize();
82 var first_load = function () {
82 var first_load = function () {
83 IPython.layout_manager.do_resize();
83 IPython.layout_manager.do_resize();
84 var hash = document.location.hash;
84 var hash = document.location.hash;
85 if (hash) {
85 if (hash) {
86 document.location.hash = '';
86 document.location.hash = '';
87 document.location.hash = hash;
87 document.location.hash = hash;
88 }
88 }
89 IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval);
89 IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval);
90 // only do this once
90 // only do this once
91 $([IPython.events]).off('notebook_loaded.Notebook', first_load);
91 $([IPython.events]).off('notebook_loaded.Notebook', first_load);
92 };
92 };
93
93
94 $([IPython.events]).on('notebook_loaded.Notebook', first_load);
94 $([IPython.events]).on('notebook_loaded.Notebook', first_load);
95 IPython.notebook.load_notebook($('body').data('notebookId'));
95 IPython.notebook.load_notebook($('body').data('notebookId'));
96
96
97 if (marked) {
97 if (marked) {
98 marked.setOptions({
98 marked.setOptions({
99 gfm : true,
99 gfm : true,
100 tables: true,
100 tables: true,
101 langPrefix: "language-",
101 langPrefix: "language-",
102 highlight: function(code, lang) {
102 highlight: function(code, lang) {
103 var highlighted;
103 var highlighted;
104 if (lang) {
104 try {
105 highlighted = hljs.highlight(lang, code, false);
105 highlighted = hljs.highlight(lang, code, false);
106 } else {
106 } catch(err) {
107 highlighted = hljs.highlightAuto(code);
107 highlighted = hljs.highlightAuto(code);
108 }
108 }
109 return highlighted.value;
109 return highlighted.value;
110 }
110 }
111 })
111 })
112 }
112 }
113
113
114 });
114 });
115
115
General Comments 0
You need to be logged in to leave comments. Login now