You are here

function toc_node_generate in TOC Node 7

Generate the TOC.

Parameters

$title:

$content:

$type:

Return value

string

2 calls to toc_node_generate()
toc_node_block_view in ./toc_node.module
Implements hook_block_view().
toc_node_node_post_render in ./toc_node.module
Post render callback to act on the rendered HTML of the node.

File

./toc_node.module, line 325

Code

function toc_node_generate($content, $style, $heading_levels, $back_to_top_links, $return = 'all') {
  static $toc_list_outputed = FALSE;
  $links = array();
  $anchor_count = 0;
  $dom_document = new DOMDocument('1.0', 'utf-8');
  @$dom_document
    ->loadHTML('<?xml encoding="UTF-8"><div id="toc-node">' . $content . '</div>');

  // Add anchors, and TOC tags which are used to work out the order of headings later.
  for ($level = 2; $level <= $heading_levels; $level++) {
    toc_node_add_anchors($dom_document, $anchor_count, $links, $level);
  }
  if (empty($links)) {
    return $content;
  }
  $title = NULL;

  // Create the TOC links.
  $toc = toc_node_links($dom_document, $links, $style);

  // Remove TOC tags.
  toc_node_remove_toc_tags($dom_document, $heading_levels);

  // Output DOM to a string.
  // Unfortunately below PHP 5.3.6 saveHTML() doesn't expect a parameter.
  $content_updated = $dom_document
    ->saveHTML();

  // So we have to remove wrapping tags ourseleves.
  $content_fragments = explode('<div id="toc-node">', $content_updated);
  $output = str_replace('</div></body></html>', '', $content_fragments[1]);

  // Check if table list is being output in a block.
  if ($return == 'toc_list') {
    $toc_list_outputed = TRUE;
    return $toc;
  }

  // If table list has already been output in a block don't output again.
  if (!$toc_list_outputed) {
    $output = $toc . $output;
  }

  // Back to top links.
  if (!empty($back_to_top_links)) {
    $output = theme('toc_node_move_to_top_link', array(
      'content' => $output,
    ));
  }
  return $output;
}