You are here

function theme_tableofcontents_toc in Table of Contents 5.2

Same name and namespace in other branches
  1. 6.3 tableofcontents.pages.inc \theme_tableofcontents_toc()
  2. 6.2 tableofcontents.module \theme_tableofcontents_toc()
  3. 7.2 tableofcontents.themes.inc \theme_tableofcontents_toc()
  4. 7 tableofcontents.pages.inc \theme_tableofcontents_toc()

Theme the output of a table of contents.

Parameters

$toc: Array containing the table of contents.

$options: The array of options for the table of contents.

$files: Optional array of files to render in the table of contents.

Return value

Rendered HTML to be displayed.

1 theme call to theme_tableofcontents_toc()
tableofcontents_filter in ./tableofcontents.module
Implementation of hook_filter().

File

./tableofcontents.module, line 360
This is a filter module to generate a collapsible jquery enabled mediawiki style table of contents based on <h[1-6]> tags. Transforms header tags into named anchors.

Code

function theme_tableofcontents_toc($toc, $options, $files = array()) {
  $output = "<div class=\"toc\">\n<div class=\"toc-title\">" . $options["title"] . " [<a href=\"#\" class=\"toc-toggle\">hide</a>]</div>\n<div class=\"toc-list\">\n<" . $options["list"] . ">\n";
  $depth = $options["minlevel"];
  foreach ($toc as $index => $title) {

    // Process nested lists.
    $curdepth = $title['level'];
    if ($curdepth <= $options["maxlevel"]) {

      // Close list items but not before no items have been added.
      if ($curdepth == $depth && $index != 0) {
        $output .= "</li>\n";
      }

      // Be sure to deal with skipping between non-adjacent h levels.
      while ($curdepth != $depth) {
        if ($curdepth > $depth) {
          $output .= "\n<" . $options["list"] . ">\n";
          $depth++;
        }
        else {
          if ($curdepth < $depth) {
            $output .= "</li>\n</" . $options["list"] . ">\n";
            $depth--;
            if ($curdepth == $depth) {
              $output .= "</li>\n";
            }
          }
        }
      }

      // insert the li element
      $output .= "\t<li><a href=\"#" . $title['anchor'] . "\">" . $title['heading'] . "</a>";
    }
  }

  // Did we recurse back out? If not, close open lists.
  while ($depth > $options["minlevel"]) {
    $output .= "</li>\n</" . $options["list"] . ">\n";
    $depth = $depth - 1;
  }
  $output .= "</li>\n";

  // If we've been passed an array of files, add them to the table of contents.
  if (!empty($files)) {
    $output .= '<li><a href="#attachments">' . t("Attachments") . "</a>";
    $output .= "<" . $options["list"] . ">";
    foreach ($files as $file) {
      if ($file->list && !$file->remove) {
        $output .= '<li>';

        // If there is no function providing phptemplate_file_icon in
        // template.php or similar then this will fail gracefully.
        $output .= theme('file_icon', $file);
        $file_href = $href = file_create_url(strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path()));
        $file_text = $file->description ? $file->description : $file->filename;
        $output .= l($file_text, $file_href) . "</li>\n";
      }
    }
    $output .= "</" . $options['list'] . "></li>\n";
  }
  $output .= "</" . $options["list"] . ">\n</div>\n</div>";
  return $output;
}