Active
Project:
Tweakbox : drupal tweaking modules
Version:
master
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
8 Nov 2006 at 22:27 UTC
Updated:
17 Dec 2006 at 10:22 UTC
This isn't a request so much as suggestion, with code. I dig the TOC filter, but wanted an (admin) option to change it from a numbered list to an unordered one, with bullets. Here's the code to do that, feel free to use it in future releases.
I did this in the CVS release from 2/10/06, 19:47
At the top of toc.module, add these lines:
define('FILTER_MARKER_NUMBERS', 1);
define('FILTER_MARKER_BULLETS', 2);
In the toc_filter function, add the following to the bottom of the case 'settings' statement (before the return):
$form['toc_settings']["line_markers"] = array(
'#type' => 'radios', '#title' => t('TOC Line Markers'), '#default_value' => variable_get("line_markers", FILTER_MARKERS_NUMBERS),
'#options' => array(FILTER_MARKERS_NUMBERS => t('Numbers'), FILTER_MARKERS_BULLETS => t('Bullets')),
'#description' => t('Line marker to use on each line in the TOC.'));
Finally, replace the _toc_replace_callback function with the following:
function _toc_replace_callback( $matches, $op = '' ) {
static $n = 0;
static $store_matches = array();
$max_depth = variable_get('toc_max_depth', 3);
$exceptions = explode(',', variable_get('toc_exceptions', ''));
$line_markers = variable_get('line_markers', FILTER_MARKER_NUMBERS);
if( $op == 'output header' ) {
// Return if no table of contents to show
if (!$n)
return;
$last_index = array();
$cur_depth = 0;
$str = '<div class="toc">';
for( $m = 1; $m <= $n; $m++ ){
$temp = end($last_index);
$concat_str = '';
if ($store_matches[$m - 1][0] > $temp) {
array_push($last_index, $store_matches[$m - 1][0]);
$cur_depth++;
if ($line_markers == FILTER_MARKER_NUMBERS) {
$concat_str .= ($cur_depth > 1 ? '<ol class="toc">' : '<ol class="toc">');
} else {
$concat_str .= ($cur_depth > 1 ? '<ul class="toc">' : '<ul class="toc">');
}
}
elseif ($store_matches[$m - 1][0] < $temp) {
while ( $store_matches[$m - 1][0] != end($last_index) ) {
$temp = array_pop($last_index);
$cur_depth--;
if ($cur_depth < $max_depth) {
if ($line_markers == FILTER_MARKER_NUMBERS) {
$concat_str .= ($cur_depth > 0 ? '</li></ol></li>' : '</ol>');
} else {
$concat_str .= ($cur_depth > 0 ? '</li></ul></li>' : '</ul>');
}
}
}
}
else
$concat_str .= '</li>';
$concat_str .= '<li id="toc_elt' . $m . '">' . '<a href="#toc' . $m . '">' .
$store_matches[ $m - 1 ][1] . '</a>' . "\n\n";
if ($cur_depth <= $max_depth)
$str .= $concat_str;
}
$n = 0;
$store_matches = array();
while ($cur_depth-- > 0) {
if ($line_markers == FILTER_MARKER_NUMBERS) {
$str .= ($cur_depth > 0 ? '</ol></li>' : '</li></ol>');
} else {
$str .= ($cur_depth > 0 ? '</ul></li>' : '</li></ul>');
}
}
$str .= '</div>';
return $str;
}
//default op: act as called by preg_replace_callback()
//echo 'Testing : "'.$matches[1].'" with "'.$exceptions[0].'" -> '.array_search(intval($matches[1]), $exceptions);
if (array_search(intval($matches[1]), $exceptions) === FALSE) {
array_push( $store_matches, array($matches[1], $matches[3]));
$n++;
// Removing any id attribute present, in order to stay w3c compliant
$matches[2] = preg_replace('|id= *(.)[^\1]*\1|', '', $matches[2]);
return '<h' . $matches[1] .
' id="toc' . $n . '" ' .
$matches[2] . '>' .
$matches[3] .
'</h' . $matches[1] . '>';
}
else {
return '<h' . $matches[1] . '>' .
$matches[3] .
'</h' . $matches[1] . '>';
}
}
I only added a few things in that function, I'm just including it all for simplicity's sake. Also attaching the modified .module if it helps.
| Comment | File | Size | Author |
|---|---|---|---|
| toc.module | 6.45 KB | Scourge |
Comments
Comment #1
Scourge commentedHmm, I should figure out how to make patches.
Comment #2
acp commentedHi there... I never saw your post until today, wonder how I didn't get an email about it...
Anyway, sorry for the delay. I'll have a look at your suggestions soon enough. For the moment, my drupal contributions are rather stalled (am currently working on something else), but I should get some time by January...
Btw, some info regarding creation of patches: http://drupal.org/diffandpatch