Sha256: 388992d71df61013307ea500bad8b0b9ea74138d08e1772c473787c707da94d4

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

/* Code from:
http://stackoverflow.com/questions/5819807/any-good-codes-to-generate-toc-from-html-heading-elements-in-javascript
posted by Japboy, 2011.04.28
*/

window.gen_toc =
  function () {
    var assigned_level = 0,
        current_level = 0,
        id_number = 1,
        parent_node = "article",
        toc_html = '';

    $(parent_node + " *").each(function () {
      if (this.nodeName.length === 2 && this.nodeName.charAt(0) === "H") {
        $(this).attr("class", "heading");
      }
    });

    $(".in_toc").each( function () {
      current_level = this.nodeName.charAt(1);

      $(this).attr('id', "toc-" + id_number);

      // Close a list if a same level list follows.
      if (assigned_level !== current_level - 1) {
        toc_html += "</li>";
      }

      // Open parent lists if a child list follows.
      while (assigned_level < current_level) {
        toc_html += "<ol class=\"toc toc_lvl"+current_level+"\">";
        assigned_level += 1;
      }

      // Close child lists and the parent list if
      // the same level parent list follows.
      while (assigned_level > current_level) {
        toc_html += "</ol></li>";
        assigned_level -= 1;
      }

      toc_html +=
        '<li><a href="#' + this.id + '">' + $(this).html() + "</a>";
      id_number += 1;
    });

    // Close everything
    while (assigned_level > 0) {
      toc_html += "</li></ol>";
      assigned_level -= 1;
    }

    $("#toc").html(
      "<h1 class='toc_heading' />" +
      toc_html
    );
  };

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
any2pdf-1.2.1 data/toc_generator.js
any2pdf-1.1.2 data/toc_generator.js
any2pdf-1.1.1 data/toc_generator.js
any2pdf-1.1.0 data/toc_generator.js
any2pdf-1.0.2 data/toc_generator.js
any2pdf-1.0.1 data/toc_generator.js