2
2
// by minrk https://github.com/minrk/ipython_extensions
3
3
// See the history of contributions in README.md
4
4
5
-
6
- //define(["require", "jquery", "base/js/namespace", 'services/config',
7
- // 'base/js/utils', "nbextensions/toc2/toc2"], function(require, $, IPython, configmod, utils, toc2) {
8
-
9
- define ( [ "require" , "jquery" , "base/js/namespace" , 'services/config' ,
10
- 'base/js/utils' , 'notebook/js/codecell' , "nbextensions/toc2/toc2" ] , function ( require , $ , IPython , configmod , utils , codecell , toc2 ) {
11
-
12
- var Notebook = require ( 'notebook/js/notebook' ) . Notebook
13
- "use strict" ;
14
-
5
+ define ( [
6
+ 'require' ,
7
+ 'jquery' ,
8
+ 'base/js/namespace' ,
9
+ 'services/config' ,
10
+ 'base/js/utils' ,
11
+ 'notebook/js/codecell' ,
12
+ './toc2'
13
+ ] , function (
14
+ require ,
15
+ $ ,
16
+ IPython ,
17
+ configmod ,
18
+ utils ,
19
+ codecell ,
20
+ toc2
21
+ ) {
22
+ "use strict" ;
23
+
24
+ // imports
25
+ var highlight_toc_item = toc2 . highlight_toc_item ;
26
+ var table_of_contents = toc2 . table_of_contents ;
27
+ var toggle_toc = toc2 . toggle_toc ;
15
28
16
29
// ...........Parameters configuration......................
17
- // define default values for config parameters if they were not present in general settings (notebook.json)
30
+ // default values for system-wide configurable parameters
18
31
var cfg = { 'threshold' :4 ,
19
- 'number_sections' :true ,
20
- 'toc_cell' :false ,
21
- 'toc_window_display' :false ,
22
- "toc_section_display" : "block" ,
23
- 'sideBar' :true ,
24
- 'navigate_menu' :true ,
32
+ 'navigate_menu' :true ,
25
33
'moveMenuLeft' : true ,
26
34
'widenNotebook' : false ,
27
35
'colors' : {
@@ -34,75 +42,74 @@ define(["require", "jquery", "base/js/namespace", 'services/config',
34
42
'navigate_num' : '#000000' ,
35
43
} ,
36
44
collapse_to_match_collapsible_headings : false ,
37
- skip_h1_title : false ,
38
45
}
46
+ // default values for per-notebook configurable parameters
47
+ var metadata_settings = {
48
+ nav_menu : { } ,
49
+ number_sections : true ,
50
+ sideBar : true ,
51
+ skip_h1_title : false ,
52
+ toc_cell : false ,
53
+ toc_position : { } ,
54
+ toc_section_display : 'block' ,
55
+ toc_window_display : false ,
56
+ } ;
57
+ // add per-notebook settings into global config object
58
+ $ . extend ( true , cfg , metadata_settings ) ;
39
59
40
60
//.....................global variables....
41
-
42
- var liveNotebook = ! ( typeof IPython == "undefined" )
43
-
44
61
var st = { }
45
62
st . rendering_toc_cell = false ;
46
- st . config_loaded = false ;
47
- st . extension_initialized = false ;
48
-
49
- st . nbcontainer_marginleft = $ ( '#notebook-container' ) . css ( 'margin-left' )
50
- st . nbcontainer_marginright = $ ( '#notebook-container' ) . css ( 'margin-right' )
51
- st . nbcontainer_width = $ ( '#notebook-container' ) . css ( 'width' )
52
63
st . oldTocHeight = undefined
53
-
54
64
st . cell_toc = undefined ;
55
65
st . toc_index = 0 ;
56
66
57
-
58
-
59
- function read_config ( cfg , callback ) { // read after nb is loaded
60
- // create config object to load parameters
61
- var base_url = utils . get_body_data ( "baseUrl" ) ;
62
- var initial_cfg = $ . extend ( true , { } , cfg ) ;
63
- var config = new configmod . ConfigSection ( 'notebook' , { base_url : base_url } ) ;
64
- config . loaded . then ( function ( ) {
67
+ var read_config = function ( cfg , callback ) {
68
+ IPython . notebook . config . loaded . then ( function ( ) {
65
69
// config may be specified at system level or at document level.
66
70
// first, update defaults with config loaded from server
67
- cfg = $ . extend ( true , cfg , config . data . toc2 ) ;
71
+ $ . extend ( true , cfg , IPython . notebook . config . data . toc2 ) ;
72
+ // ensure notebook metadata has toc object, cache old values
73
+ var md = IPython . notebook . metadata . toc || { } ;
74
+ // reset notebook metadata to remove old values
75
+ IPython . notebook . metadata . toc = { } ;
68
76
// then update cfg with any found in current notebook metadata
69
77
// and save in nb metadata (then can be modified per document)
70
- cfg = IPython . notebook . metadata . toc = $ . extend ( true , cfg ,
71
- IPython . notebook . metadata . toc ) ;
72
- // excepted colors that are taken globally (if defined)
73
- cfg . colors = $ . extend ( true , { } , initial_cfg . colors ) ;
74
- try
75
- { cfg . colors = IPython . notebook . metadata . toc . colors = $ . extend ( true , cfg . colors , config . data . toc2 . colors ) ; }
76
- catch ( e ) { }
77
- // and moveMenuLeft, threshold, wideNotebook, collapse_to_match_collapsible_headings taken globally (if it exists, otherwise default)
78
- cfg . moveMenuLeft = IPython . notebook . metadata . toc . moveMenuLeft = initial_cfg . moveMenuLeft ;
79
- cfg . threshold = IPython . notebook . metadata . toc . threshold = initial_cfg . threshold ;
80
- cfg . widenNotebook = IPython . notebook . metadata . toc . widenNotebook = initial_cfg . widenNotebook ;
81
- cfg . collapse_to_match_collapsible_headings = IPython . notebook . metadata . toc . collapse_to_match_collapsible_headings = initial_cfg . collapse_to_match_collapsible_headings
82
- if ( config . data . toc2 ) {
83
- if ( typeof config . data . toc2 . moveMenuLeft !== "undefined" ) {
84
- cfg . moveMenuLeft = IPython . notebook . metadata . toc . moveMenuLeft = config . data . toc2 . moveMenuLeft ;
85
- }
86
- if ( typeof config . data . toc2 . threshold !== "undefined" ) {
87
- cfg . threshold = IPython . notebook . metadata . toc . threshold = config . data . toc2 . threshold ;
88
- }
89
- if ( typeof config . data . toc2 . widenNotebook !== "undefined" ) {
90
- cfg . widenNotebook = IPython . notebook . metadata . toc . widenNotebook = config . data . toc2 . widenNotebook ;
91
- }
92
- if ( typeof config . data . toc2 . collapse_to_match_collapsible_headings !== "undefined" ) {
93
- cfg . collapse_to_match_collapsible_headings = IPython . notebook . metadata . toc . collapse_to_match_collapsible_headings = config . data . toc2 . collapse_to_match_collapsible_headings ;
94
- }
95
- }
96
- // create highlights style section in document
97
- create_additional_css ( ) ;
98
- // call callbacks
99
- callback && callback ( ) ;
100
- st . config_loaded = true ;
101
- } )
102
- config . load ( ) ;
78
+ Object . keys ( metadata_settings ) . forEach ( function ( key ) {
79
+ cfg [ key ] = IPython . notebook . metadata . toc [ key ] = ( md . hasOwnProperty ( key ) ? md : cfg ) [ key ] ;
80
+ } ) ;
81
+ // create highlights style section in document
82
+ create_additional_css ( ) ;
83
+ // call callbacks
84
+ callback && callback ( ) ;
85
+ } ) ;
103
86
return cfg ;
104
- }
87
+ } ;
105
88
89
+ // extra download as html with toc menu (needs IPython kernel)
90
+ function addSaveAsWithToc ( ) {
91
+ if ( IPython . notebook . metadata . kernelspec . language == 'python' ) {
92
+ if ( $ ( '#save_html_with_toc' ) . length == 0 ) {
93
+ $ ( '#save_checkpoint' ) . after ( "<li id='save_html_with_toc'/>" )
94
+ $ ( '#save_html_with_toc' )
95
+ . append ( $ ( '<a/>' ) . text ( 'Save as HTML (with toc)' ) . attr ( "href" , "#" ) )
96
+ . on ( 'click' , function ( evt ) {
97
+ if ( IPython . notebook . metadata . kernelspec . language == 'python' ) {
98
+ var code = "!jupyter nbconvert '" + IPython . notebook . notebook_name + "' --template toc2" ;
99
+ console . log ( '[toc2] running:' , code ) ;
100
+ IPython . notebook . kernel . execute ( code ) ;
101
+ }
102
+ else {
103
+ alert ( 'Sorry; this only works with a IPython kernel' ) ;
104
+ $ ( '#save_html_with_toc' ) . remove ( ) ;
105
+ }
106
+ } ) ;
107
+ }
108
+ }
109
+ else {
110
+ $ ( '#save_html_with_toc' ) . remove ( )
111
+ }
112
+ }
106
113
107
114
108
115
0 commit comments