function _toc_filter_process_callback in TOC filter 7
Same name and namespace in other branches
- 6 toc_filter.module \_toc_filter_process_callback()
TOC filter processor callback: Convert's <h2> to a linked table of contents.
1 string reference to '_toc_filter_process_callback'
- toc_filter_filter_info in ./
toc_filter.module - Implements hook_filter_info().
File
- ./
toc_filter.module, line 77 - Converts header tags into a linked table of contents.
Code
function _toc_filter_process_callback($text) {
if (stripos($text, '[toc') === FALSE || !preg_match('/(?:<p>)?\\s*\\[TOC(?::([a-z-]+))?([^\\]]+)?\\]\\s*(?:<\\/p>)?/i', $text, $matches)) {
if (variable_get('toc_filter_default_top', '0') === '0') {
return $text;
}
else {
return _toc_filter_process_callback('[toc:' . variable_get('toc_filter_default_type', 'ul') . "]\n" . $text);
}
}
// Must track nested filter calls which can be created by drupal_get_form('ctools_jump_menu').
static $processing = array();
$md5 = md5($text);
if (isset($processing[$md5])) {
return $text;
}
$processing[$md5] = TRUE;
$match = $matches[0];
$type = !empty($matches[1]) ? drupal_strtolower($matches[1]) : 'ul';
$title = !empty($matches[2]) ? trim($matches[2]) : '';
// Set format based on type.
switch ($type) {
case 'jump-menu':
case 'menu':
$format = 'jump_menu';
break;
case 'faq':
$format = 'faq';
break;
case 'number':
case 'ol':
$format = 'number';
break;
case 'ul':
case 'bullet':
default:
$format = 'bullet';
break;
}
$title = empty($title) ? variable_get('toc_filter_' . $format . '_default_title', '') : $title;
$is_numbered = $format == 'number' || $format == 'faq' ? TRUE : FALSE;
$header_tag = variable_get('toc_filter_header_tag', 'h3');
preg_match_all('/(<' . $header_tag . '[^>]*>)(.*?)(<\\/' . $header_tag . '>)/is', $text, $header_matches);
$targets = array();
$links = array();
for ($i = 0, $len = count($header_matches[0]); $i < $len; $i++) {
$header_match = $header_matches[0][$i];
$open_tag = $header_matches[1][$i];
$header_title = $header_matches[2][$i];
$close_tag = $header_matches[3][$i];
$header_id = preg_replace('/[^-a-z0-9]+/', '-', drupal_strtolower(trim($header_title)));
// Add header class to open tag
$open_tag_attributes = toc_filter_parse_tag_attributes($open_tag) + array(
'class' => '',
);
$open_tag_attributes['class'] .= (empty($open_tag_attributes['class']) ? '' : ' ') . ' toc-header toc-header-' . $format;
$header_replace = '<' . $header_tag . drupal_attributes($open_tag_attributes) . '>' . '<a name="' . $header_id . '" id="' . $header_id . '" class="toc-bookmark" rel="bookmark" title="' . strip_tags($header_title) . '"></a>' . ($is_numbered ? '<span class="toc-number">' . ($i + 1) . ' . </span> ' : '') . $header_title . $close_tag;
// Must manually build each link item since l() function can't generate just an anchor link (aka #anchor).
$targets['#' . $header_id] = strip_tags($header_title);
$links[] = '<a href="#' . $header_id . '">' . strip_tags($header_title, '<i><em><b><strong><br>') . '</a>';
// Add anchor before header
$back_to_top = theme('toc_filter_back_to_top', array(
'class' => $i == 0 ? 'first' : '',
));
$text = str_replace($header_match, $back_to_top . $header_replace, $text);
}
// If no TOC links found then just remove the [toc] tag and return the text.
if (empty($links)) {
return str_replace($match, '', $text);
}
// Theme list
$links_list_type = $is_numbered ? 'ol' : 'ul';
$toc_content = theme('item_list', array(
'items' => $links,
'title' => check_plain($title),
'type' => $links_list_type,
'attributes' => array(
'class' => 'toc-filter-links',
),
));
$output = theme('toc_filter', array(
'type' => $type,
'content' => $toc_content,
));
// Add jump menu with noscript list.
if ($format == 'jump_menu' && module_exists('ctools')) {
$options = array();
if ($title) {
$options['choose'] = check_plain($title);
}
ctools_include('jump-menu');
$jump_menu = drupal_get_form('ctools_jump_menu', $targets, $options);
$output = '<div class="toc-filter-jump-menu">' . drupal_render($jump_menu) . '</div>' . '<noscript>' . $output . '</noscript>';
}
// Prepend #top
$output = '<a name="top"></a>' . $output;
// Replace matched text with output.
$text = str_replace($match, $output, $text);
// Add closing back to top.
$text .= theme('toc_filter_back_to_top', array(
'class' => 'last',
));
unset($processing[$md5]);
return $text;
}