You are here

function _tableofcontents_replace_toc in Table of Contents 6.3

Same name and namespace in other branches
  1. 7 tableofcontents.pages.inc \_tableofcontents_replace_toc()

This function reokaces the table of contents.

It checks all the current flags such as whether the table should be automatically added if no [toc] tag is found.

@param[in] $delta The filter number (0- full toc, 1- only anchors) @param[in] $text The text to work on. @param[in] $format The format used to do the conversion.

Return value

The $text with the [toc ...] replaced.

1 call to _tableofcontents_replace_toc()
_tableofcontents_process in ./tableofcontents.pages.inc
Transform the [toc ...] tags into the actual table of content.

File

./tableofcontents.pages.inc, line 661
Applies the filter functions.

Code

function _tableofcontents_replace_toc($delta, $format, $text) {

  // we just don't have a way to pass that around our callbacks
  // (PHP is lacking on that one) but it is still a lot faster
  // that way.
  global $_tableofcontents_toc;
  global $_tableofcontents_block_toc;

  // this is a special case... do what it takes at once
  if ($delta == 1) {
    $text = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);

    // note: $_tableofcontents_toc->automatic is only used in this very function and
    //       thus we do not need to define it for _tableofcontents_headers().
    $_tableofcontents_toc = new TOC();

    // pretent that we are on a print page so we do not get back to top links
    $_tableofcontents_toc->on_print_pages = TRUE;
    return _tableofcontents_headers($_tableofcontents_toc, $format, $text);
  }

  // if the user just asked to hide that, hide it and go away
  if (empty($_tableofcontents_block_toc) && variable_get('tableofcontents_hide_table_' . $format, FALSE)) {
    return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
  }

  // keep a copy of the original just in case we bail out as is
  $org_text = $text;
  $_tableofcontents_toc = new TOC();
  $_tableofcontents_toc->id = 'toc';

  // first make sure we want a table of contents
  if (empty($_tableofcontents_block_toc) && strpos($text, "[toc") === FALSE) {

    // no [toc], but a [vtoc], we're good
    if (strpos($text, "[vtoc") !== FALSE) {
      return $org_text;
    }
    $_tableofcontents_toc->automatic = variable_get('tableofcontents_automatic_' . $format, 0);
    switch ($_tableofcontents_toc->automatic) {
      case 1:
      case 2:

        // Avoid the table of contents anywhere else than in regular nodes
        // (which is somewhat equivalent to saying don't add in the teasers)
        if (variable_get('tableofcontents_remove_teaser_' . $format, TRUE)) {
          if (arg(0) != 'node' || !is_numeric(arg(1)) || arg(2) && arg(2) != 'view') {
            return $org_text;
          }
        }
        break;
      default:

        // if not defined as automatic in the filter, check the node and its type
        if (arg(0) == 'node' && is_numeric(arg(1)) && (!arg(2) || arg(2) == 'view')) {
          $node = node_load(arg(1));
          if ($node) {
            if ($node->tableofcontents_toc_automatic) {
              $_tableofcontents_toc->automatic = $node->tableofcontents_toc_automatic;
            }
            else {
              $_tableofcontents_toc->automatic = variable_get('tableofcontents_nodetype_toc_automatic_' . $node->type, 0);
              if ($_tableofcontents_toc->automatic == 99) {
                $_tableofcontents_toc->automatic = 0;
              }
            }
          }
        }
        break;
    }
    switch ($_tableofcontents_toc->automatic) {
      default:

        // case 0
        // if ($_tableofcontents_block_toc) -- cannot happen
        return $org_text;
      case 1:

        // prepend
        $text = '[toc]' . $text;
        break;
      case 2:

        // append
        $text = $text . '[toc]';
        break;
    }
  }
  else {
    $_tableofcontents_toc->automatic = 0;
  }

  // print module running and creating a print version?
  if (arg(0) == 'print' && is_numeric(arg(1)) && module_exists('print')) {

    // Note: not to sure when the build_mode is set, but here it is still undefined
    //       just to make sure, we return only if not defined to something else than
    //       'print'; if undefined, we assume that it is 'print'
    $node = node_load(arg(1));
    if (!isset($node->build_mode) || $node->build_mode == 'print') {
      if (!variable_get('tableofcontents_show_on_print_pages_' . $format, FALSE)) {
        return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $org_text);
      }
      $_tableofcontents_toc->on_print_pages = TRUE;
    }
  }

  // Add an identifier in the existing headers and return info
  // about the tree
  $text = _tableofcontents_headers($_tableofcontents_toc, $format, $text);

  // If we have a block-only table of contents then we do not want to process
  // the body also, just replacing the headers is enough for the body (to get the IDs.)
  if (!$_tableofcontents_block_toc && variable_get('tableofcontents_block_only_table_' . $format, FALSE)) {
    return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
  }

  // Include comments?
  if (variable_get('tableofcontents_comments_' . $format, FALSE) && module_exists('comment') && user_access('access comments')) {
    _tableofcontents_comments($_tableofcontents_toc, $format);
  }

  // Any headers? if not do nothing...
  $max = count($_tableofcontents_toc->headers);
  if ($max == 0) {

    // not even 1 header!!!
    if ($_tableofcontents_block_toc) {
      return '';
    }
    return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $org_text);
  }

  // If automatic verify the limit, if not reached, bail out
  if ($_tableofcontents_toc->automatic) {
    $min_limit = variable_get('tableofcontents_min_limit_' . $format, 5);
    if ($max < $min_limit) {

      // Limit not reached!
      if ($_tableofcontents_block_toc) {
        return '';
      }
      return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $org_text);
    }
  }

  // get the default flags, these apply for each call to the
  // _tableofcontents_replace_toc_tags() function.
  // (i.e. that function should not be able to change $_tableofcontents_toc)
  $_tableofcontents_toc->hidden = FALSE;
  $_tableofcontents_toc->title = variable_get('tableofcontents_title_' . $format, 'Table of contents');
  $_tableofcontents_toc->safe_title = variable_get('tableofcontents_safe_title_' . $format, TRUE);
  $_tableofcontents_toc->minlevel = variable_get('tableofcontents_minlevel_' . $format, 2);
  $_tableofcontents_toc->maxlevel = variable_get('tableofcontents_maxlevel_' . $format, 3);
  $_tableofcontents_toc->attachments = variable_get('tableofcontents_attachments_' . $format, FALSE);
  $_tableofcontents_toc->hideshow = variable_get('tableofcontents_hide_show_' . $format, TRUE);
  $_tableofcontents_toc->collapsed = variable_get('tableofcontents_collapsed_' . $format, FALSE);
  $_tableofcontents_toc->allow_override = variable_get('tableofcontents_allow_override_' . $format, TRUE);
  $_tableofcontents_toc->has_toc = FALSE;
  if ($_tableofcontents_toc->minlevel < $_tableofcontents_toc->header_min) {

    // avoid empty indentations
    $_tableofcontents_toc->minlevel = $_tableofcontents_toc->header_min;
    if ($_tableofcontents_toc->minlevel > $_tableofcontents_toc->maxlevel) {
      $_tableofcontents_toc->maxlevel = $_tableofcontents_toc->minlevel;
    }
  }
  if ($_tableofcontents_toc->maxlevel > $_tableofcontents_toc->header_max) {

    // this is much less important
    if ($_tableofcontents_toc->minlevel > $_tableofcontents_toc->header_max) {
      $_tableofcontents_toc->maxlevel = $_tableofcontents_toc->minlevel;
    }
    else {
      $_tableofcontents_toc->maxlevel = $_tableofcontents_toc->header_max;
    }
  }
  if ($_tableofcontents_block_toc) {
    $text = $_tableofcontents_block_toc;
  }
  $result = preg_replace_callback('/(?:<p(?:\\s[^>]*)?' . '>)?\\[toc(\\s[^]]*?)?\\](?:<\\/p\\s*>)?/', '_tableofcontents_replace_toc_tags', $text);
  if ($_tableofcontents_toc->has_toc) {

    // Add a back to top at the very end too
    if (!$_tableofcontents_toc->first_header && $_tableofcontents_toc->back_to_top && $_tableofcontents_toc->back_to_top_location == 'bottom' && $_tableofcontents_toc->header_min >= $_tableofcontents_toc->back_to_top_minlevel && $_tableofcontents_toc->header_min <= $_tableofcontents_toc->back_to_top_maxlevel) {
      $result .= $_tableofcontents_toc->back_to_top_link;
    }

    // If we have back to top and scroll on, advice script
    if ($_tableofcontents_toc->back_to_top && $_tableofcontents_toc->scroll_back_to_top) {
      $result .= '<script type="text/javascript">toc_scroll_back_to_top = 1;</script>';
    }
  }
  return $result;
}