Forked from tarokiritani/jupyter_collapse_headings_html
Created
August 30, 2018 22:52
-
-
Save mlgill/a1a0c3d522ec4b9e6241f06145a089f2 to your computer and use it in GitHub Desktop.
small script to collapse headings in jupyter notebook after converted to html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> | |
</script> | |
<script> | |
$(document).ready(function(){ | |
var all_cells = $("div.cell"); | |
$.each(all_cells, function( index, value ) { | |
if ($(value).find("h1").length == 0){ | |
$(value).hide() | |
} else{ | |
var bare_h1 = $(value).find("h1").text() | |
$(value).find("h1").text("+" + bare_h1) | |
$(value).find("h1").css({'color': 'blue', 'text-decoration': 'underline'}) | |
} | |
if ($(value).find("h2").length != 0){ | |
var bare_h2 = $(value).find("h2").text() | |
$(value).find("h2").text("+" + bare_h2) | |
$(value).find("h2").css({'color': 'blue', 'text-decoration': 'underline'}) | |
} | |
if ($(value).find("h3").length != 0){ | |
var bare_h3 = $(value).find("h3").text() | |
$(value).find("h3").text("+" + bare_h3) | |
$(value).find("h3").css({'color': 'blue', 'text-decoration': 'underline'}) | |
} | |
if ($(value).find("h4").length != 0){ | |
var bare_h4 = $(value).find("h4").text() | |
$(value).find("h4").text("+" + bare_h4) | |
$(value).find("h4").css({'color': 'blue', 'text-decoration': 'underline'}) | |
} | |
}); | |
$("h1").click(function(){ | |
var current_cell = $(this).closest("div.cell"); | |
var following_cells = $(current_cell).nextAll(); | |
if ($(this).text().substring(0, 1) == "-"){ | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("-", "+")); | |
$.each(following_cells, function( index, value ) { | |
if ($(value).find("h1").length == 0){ | |
$(value).hide(); | |
if ($(value).find("h2").length != 0) { | |
pre_str = $(value).find("h2").text(); | |
$(value).find("h2").text(pre_str.replace('-','+')) | |
} | |
if ($(value).find("h3").length != 0) { | |
pre_str = $(value).find("h3").text(); | |
$(value).find("h3").text(pre_str.replace('-','+')) | |
} | |
if ($(value).find("h4").length != 0) { | |
pre_str = $(value).find("h4").text(); | |
$(value).find("h4").text(pre_str.replace('-','+')) | |
} | |
} else{ | |
return false; | |
}; | |
}); | |
} else { | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("+", "-")) | |
var see_h2 = false; | |
$.each(following_cells, function( index, value ) { | |
if ($(value).find("h1").length != 0) { | |
return false; | |
} else if ($(value).find("h2").length != 0) { | |
see_h2 = true; | |
$(value).show(); | |
} else { | |
if (see_h2 === false) { | |
$(value).show(); | |
} | |
} | |
}); | |
} | |
}); | |
$("h2").click(function(){ | |
var current_cell = $(this).closest("div.cell"); | |
var following_cells = $(current_cell).nextAll(); | |
if ($(this).text().substring(0, 1) == "-"){ | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("-", "+")); | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length == 0) && ($(value).find("h2").length == 0)){ | |
$(value).hide(); | |
if ($(value).find("h3").length != 0) { | |
pre_str = $(value).find("h3").text(); | |
$(value).find("h3").text(pre_str.replace('-','+')) | |
} | |
if ($(value).find("h4").length != 0) { | |
pre_str = $(value).find("h4").text(); | |
$(value).find("h4").text(pre_str.replace('-','+')); | |
} | |
} else{ | |
return false; | |
}; | |
}); | |
} else { | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("+", "-")); | |
var see_h3 = false; | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length != 0) || ($(value).find("h2").length != 0)) { | |
return false; | |
} else if ($(value).find("h3").length != 0) { | |
see_h3 = true; | |
$(value).show(); | |
} else { | |
if (see_h3 === false) { | |
$(value).show(); | |
} | |
} | |
}); | |
} | |
}); | |
$("h3").click(function(){ | |
var current_cell = $(this).closest("div.cell"); | |
var following_cells = $(current_cell).nextAll(); | |
if ($(this).text().substring(0, 1) == "-"){ | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("-", "+")); | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length == 0) && ($(value).find("h2").length == 0) && ($(value).find("h3").length == 0)){ | |
$(value).hide(); | |
} else{ | |
return false; | |
}; | |
}); | |
} else { | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("+", "-")); | |
var see_h4 = false; | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length != 0) || ($(value).find("h2").length != 0) || ($(value).find("h3").length != 0)) { | |
return false; | |
} else if ($(value).find("h4").length != 0) { | |
see_h4 = true; | |
$(value).show(); | |
} else { | |
if (see_h4 === false) { | |
$(value).show(); | |
} | |
} | |
}); | |
} | |
}); | |
$("h4").click(function(){ | |
var current_cell = $(this).closest("div.cell"); | |
var following_cells = $(current_cell).nextAll(); | |
if ($(this).text().substring(0, 1) == "-"){ | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("-", "+")); | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length == 0) && ($(value).find("h2").length == 0) && ($(value).find("h3").length == 0) && ($(value).find("h4").length == 0)){ | |
$(value).hide(); | |
if ($(value).find("h4").length != 0) { | |
pre_str = $(value).find("h4").text(); | |
$(value).find("h4").text(pre_str.replace('-','+')); | |
} | |
} else{ | |
return false; | |
}; | |
}); | |
} else { | |
orig_string = $(this).text(); | |
$(this).text(orig_string.replace("+", "-")); | |
var see_h5 = false; | |
$.each(following_cells, function( index, value ) { | |
if (($(value).find("h1").length != 0) || ($(value).find("h2").length != 0) || ($(value).find("h3").length != 0) || ($(value).find("h4").length != 0)) { | |
return false; | |
} else if ($(value).find("h5").length != 0) { | |
see_h5 = true; | |
$(value).show(); | |
} else { | |
if (see_h5 === false) { | |
$(value).show(); | |
} | |
} | |
}); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment