function _tableofcontents_process_text in Table of Contents 7.2
Developer function to apply TOC to any text $body has two assumptions for this function to work 1. It must have [toc] located somewhere in the text 2. It has already been processed by an input filter with toc enabled
1 call to _tableofcontents_process_text()
- youexamplemodule_page in ./
tableofcontents.api.php - Example of how to implement _tableofcontents_process_text In this example, this function would be a menu callback.
File
- ./
tableofcontents.module, line 296 - 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 _tableofcontents_process_text(&$body) {
// store body text in a temp variable for comparison
$text = $body;
// locate the toc settings in the text
$toc = _tableofcontents_toc_extract($text);
// Set the [toc] globally
tableofcontents_toc($toc);
// Process the headers on this page (we have to do this)
module_load_include('inc', 'tableofcontents');
// load the global toc instance
$toc =& tableofcontents_toc();
// add the headers
$text = _tableofcontents_headers($text);
// theme the toc output
$html = theme('tableofcontents_toc', array(
'toc' => $toc,
));
// Insert the rendered [toc] in the right place.
if ($toc['on_off']['automatic'] != 3) {
// Automatic "3" means don't put it on the page (it may go into a block).
$body = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, $html, $text);
}
// Add the styling and controls
$settings = array(
'tableofcontents' => array(
'collapse' => !!$toc['box']['collapsed'],
'scroll' => !!$toc['back_to_top']['scroll'],
),
);
// append settings to the page
drupal_add_js($settings, 'setting');
// add correct js libraries based on settings
$path = drupal_get_path('module', 'tableofcontents');
if (!empty($toc['back_to_top']['scroll'])) {
drupal_add_js($path . '/js/jquery.scrollTo-min.js');
drupal_add_js($path . '/js/jquery.localscroll-min.js');
}
drupal_add_js($path . '/js/tableofcontents.js');
drupal_add_css($path . '/tableofcontents.css');
// Remove any leftover [toc]
$body = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $body);
if (strpos($body, '[toc') !== FALSE) {
$body = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $body . ']');
}
// append the back to top link at the bottom of the page
$body .= theme('tableofcontents_back_to_top', array(
'toc' => $toc,
));
}