Kalman-and-Bayesian-Filters.../code/secnum.js
Roger Labbe 33c745d997 Edits for reorg
I moved book_format.py to the root directory so that all of the
notebooks do not need to modify the system path to import it. It
modifies the path on import so that all of the code in ./code can
then be accessed.

Altered links to nbviewer to account for no longer using subdirectories.
2015-01-27 15:14:06 -08:00

75 lines
1.6 KiB
JavaScript

console.log("Section numbering...");
function number_sections(threshold) {
var h1_number = 0;
var h2_number = 0;
if (threshold === undefined) {
threshold = 2; // does nothing so far
}
var cells = IPython.notebook.get_cells();
for (var i=0; i < cells.length; i++) {
var cell = cells[i];
if (cell.cell_type !== 'heading') continue;
var level = cell.level;
if (level > threshold) continue;
if (level === 1) {
h1_number ++;
var h1_element = cell.element.find('h1');
var h1_html = h1_element.html();
console.log("h1_html: " + h1_html);
var patt = /^[0-9]+\.\s(.*)/; // section number at start of string
var title = h1_html.match(patt); // just the title
if (title != null) {
h1_element.html(h1_number + ". " + title[1]);
}
else {
h1_element.html(h1_number + ". " + h1_html);
}
h2_number = 0;
}
if (level === 2) {
h2_number ++;
var h2_element = cell.element.find('h2');
var h2_html = h2_element.html();
console.log("h2_html: " + h2_html);
var patt = /^[0-9]+\.[0-9]+\.\s/;
var result = h2_html.match(patt);
if (result != null) {
h2_html = h2_html.replace(result, "");
}
h2_element.html(h1_number + "." + h2_number + ". " + h2_html);
}
}
}
number_sections();
// $([IPython.evnts]).on('create.Cell', number_sections);
$([IPython.events]).on('selected_cell_type_changed.Notebook', number_sections);