Skip to content

Instantly share code, notes, and snippets.

@tarokiritani
Created January 22, 2017 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tarokiritani/b1b5fefa5f19e57f107bd1c3b16c742a to your computer and use it in GitHub Desktop.
Save tarokiritani/b1b5fefa5f19e57f107bd1c3b16c742a to your computer and use it in GitHub Desktop.
small script to collapse headings in jupyter notebook after converted to html
<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