View source
<?php
class TOC {
}
class TOC_Header {
}
function _tableofcontents_replace_comments($matches) {
return '[toc ' . $matches[1] . ']';
}
function _tableofcontents_replace_headers($matches) {
global $_tableofcontents_toc;
static $toc_translations, $duplicates_error;
$h = new TOC_Header();
$h->level = $matches[2];
$h->attributes = $matches[3];
$h->title = $matches[4];
++$_tableofcontents_toc->counters[$h->level];
for ($l = $h->level + 1; $l <= 6; ++$l) {
$_tableofcontents_toc->counters[$l] = 0;
}
if ($_tableofcontents_toc->level_from > $h->level) {
$_tableofcontents_toc->level_from = $h->level;
}
$_tableofcontents_toc->level_to = $h->level;
if ($_tableofcontents_toc->header_min) {
if ($_tableofcontents_toc->header_min > $h->level) {
$_tableofcontents_toc->header_min = $h->level;
}
}
else {
$_tableofcontents_toc->header_min = $h->level;
}
if ($_tableofcontents_toc->header_max) {
if ($_tableofcontents_toc->header_max < $h->level) {
$_tableofcontents_toc->header_max = $h->level;
}
}
else {
$_tableofcontents_toc->header_max = $h->level;
}
if (preg_match('/\\sid=(?:"([^"]*?)"|\'([^\']*?)\'|([^\\s"\'>]))/i', $h->attributes, $id)) {
$h->identifier = $id[1] ? $id[1] : ($id[2] ? $id[2] : $id[3]);
foreach ($_tableofcontents_toc->headers as $header) {
if ($header->identifier == $h->identifier) {
if (!isset($duplicates_error)) {
$duplicates_error = TRUE;
drupal_set_message(t('Two or more anchor identifiers match each others. One of them will be modified. This error happens when some anchor identifiers are generated automatically and others are predefined. Or both are predefined and the operator made a mistake (maybe a copy & paste?)'), 'warning');
}
$h->identifier .= $_tableofcontents_toc->id_separator . $_tableofcontents_toc->header_id++;
$h->attributes = preg_replace('/\\sid=(?:"([^"]*?)"|\'([^\']*?)\'|([^\\s"\'>]))/i', ' id="' . $h->identifier . '"', $h->attributes);
break;
}
}
}
else {
switch ($_tableofcontents_toc->id_generator) {
case 'random':
do {
$id = user_password(8);
$found = FALSE;
foreach ($_tableofcontents_toc->headers as $header) {
if ($header->identifier == $id) {
$found = TRUE;
break;
}
}
} while ($found);
break;
case 'increment':
$id = $_tableofcontents_toc->identifier_introducer . $_tableofcontents_toc->id_separator . $_tableofcontents_toc->header_id++;
break;
case 'sections':
$id = $_tableofcontents_toc->identifier_introducer;
for ($idx = $_tableofcontents_toc->level_from; $idx <= $_tableofcontents_toc->level_to; ++$idx) {
$id .= $_tableofcontents_toc->id_separator . $_tableofcontents_toc->counters[$idx];
}
break;
case 'custom':
module_invoke_all('anchor_identifier', $_tableofcontents_toc, $h);
$id = '';
break;
default:
if (!isset($toc_translations)) {
$path = drupal_get_path('module', 'tableofcontents');
$toc_translations = parse_ini_file($path . '/i18n-ascii.txt');
}
$title = strtr(strip_tags($h->title, $_tableofcontents_toc->allowed_tags), $toc_translations);
$allowed_chars = '';
if (empty($_tableofcontents_toc->id_stripping['dashes'])) {
$allowed_chars = '-';
}
$allowed_chars .= 'A-Za-z';
if (empty($_tableofcontents_toc->id_stripping['digits'])) {
$allowed_chars .= '0-9';
}
if (empty($_tableofcontents_toc->id_stripping['periods'])) {
$allowed_chars .= '.';
}
if (empty($_tableofcontents_toc->id_stripping['underscores'])) {
$allowed_chars .= '_';
}
if (empty($_tableofcontents_toc->id_stripping['colons'])) {
$allowed_chars .= ':';
}
$id = preg_replace(array(
'/ |\\s/',
'/\'/',
'/—/',
'/–/',
'/&/',
'/&[a-z]+;/',
'/[^' . $allowed_chars . ']/',
'/^[-0-9._:]+/',
'/__+/',
), array(
'_',
'-',
'--',
'--',
'and',
'',
'',
'',
'_',
), strip_tags($title));
if (!$id) {
$id = $_tableofcontents_toc->identifier_introducer . $_tableofcontents_toc->id_separator . $_tableofcontents_toc->header_id++;
}
break;
}
if ($id != '') {
foreach ($_tableofcontents_toc->headers as $header) {
if ($header->identifier == $id) {
$id .= $_tableofcontents_toc->id_separator . $_tableofcontents_toc->header_id++;
break;
}
}
$h->identifier = $id;
$h->attributes .= ' id="' . $id . '"';
}
}
$h->number = theme('tableofcontents_number', $_tableofcontents_toc);
if ($_tableofcontents_toc->number_headers) {
$number = $h->number;
}
else {
$number = '';
}
$result = $matches[1] . '<h' . $h->level . $h->attributes . '>' . $number . $h->title . '</h' . $h->level . '>';
$_tableofcontents_toc->headers[] = $h;
if ($_tableofcontents_toc->back_to_top && $h->level >= $_tableofcontents_toc->back_to_top_minlevel && $h->level <= $_tableofcontents_toc->back_to_top_maxlevel) {
switch ($_tableofcontents_toc->back_to_top_location) {
case 'header':
$result .= $_tableofcontents_toc->back_to_top_link;
break;
default:
if (!$_tableofcontents_toc->first_header) {
$result = $_tableofcontents_toc->back_to_top_link . $result;
}
break;
}
}
$_tableofcontents_toc->first_header = FALSE;
return $result;
}
function _tableofcontents_headers(&$toc, $format, $text) {
$toc->headers = array();
$toc->header_id = 1;
$toc->header_min = 0;
$toc->header_max = 0;
$toc->allowed_tags = variable_get('tableofcontents_allowed_tags_' . $format, TABLEOFCONTENTS_ALLOWED_TAGS);
$toc->id_stripping = variable_get('tableofcontents_id_stripping_' . $format, array());
$toc->identifier_introducer = variable_get('tableofcontents_identifier_introducer_' . $format, 'header');
$toc->id_separator = variable_get('tableofcontents_id_separator_' . $format, '-');
$toc->id_generator = variable_get('tableofcontents_id_generator_' . $format, 'title');
$toc->back_to_top = check_plain(variable_get('tableofcontents_back_to_top_' . $format, ''));
$toc->back_to_top_location = check_plain(variable_get('tableofcontents_back_to_top_location_' . $format, 'bottom'));
$toc->back_to_top_anchor = check_plain(variable_get('tableofcontents_back_to_top_anchor_' . $format, 'toc'));
$toc->scroll_back_to_top = check_plain(variable_get('tableofcontents_scroll_back_to_top_' . $format, FALSE));
$toc->back_to_top_minlevel = variable_get('tableofcontents_back_to_top_minlevel_' . $format, 2);
$toc->back_to_top_maxlevel = variable_get('tableofcontents_back_to_top_maxlevel_' . $format, 4);
$toc->first_header = TRUE;
$toc->numbering = variable_get('tableofcontents_numbering_' . $format, 0);
$toc->number_mode = variable_get('tableofcontents_number_mode_' . $format, 0);
$toc->number_start_letter = check_plain(variable_get('tableofcontents_number_start_letter_' . $format, ''));
$toc->number_separator = check_plain(variable_get('tableofcontents_number_separator_' . $format, '.'));
$toc->number_end_letter = check_plain(variable_get('tableofcontents_number_end_letter_' . $format, '.'));
$toc->number_headers = variable_get('tableofcontents_number_headers_' . $format, FALSE);
$toc->level_from = 6;
$toc->level_to = 6;
$toc->counters = array(
0,
0,
0,
0,
0,
0,
0,
);
if (isset($toc->on_print_pages)) {
$toc->back_to_top_link = '';
}
else {
$toc->back_to_top_link = theme('tableofcontents_back_to_top', $toc);
}
$result = preg_replace_callback('%((?:(?:<p(?:\\s[^>]*)?' . '>\\s*)?\\[/collapse\\](?:</p\\s*>\\s*)?)?' . '(?:<p(?:\\s[^>]*)?' . '>\\s*)?\\[collapse[^]]*?\\](?:</p\\s*>\\s*)?)?' . '<h([1-6])(\\s+[^>]*?)?' . '>(.*?)</h[1-6]\\s*>%si', '_tableofcontents_replace_headers', $text);
return $result;
}
function _tableofcontents_parse_option($opt) {
global $_tableofcontents_toc;
$opt[1] = trim($opt[1]);
$opt[2] = trim($opt[2]);
switch (drupal_strtolower($opt[1])) {
case 'id':
case 'title':
$_tableofcontents_toc->{$opt}[1] = $opt[2];
break;
case 'list':
break;
case 'minlevel':
case 'maxlevel':
case 'back_to_top_minlevel':
case 'back_to_top_maxlevel':
if (!is_numeric($opt[2]) || $opt[2] < 1 || $opt[2] > 6) {
drupal_set_message(t("Table of contents error: @opt is not a valid level. Expected a number from 1 to 6", array(
'@opt' => $opt[2],
)), 'error');
}
else {
$_tableofcontents_toc->{$opt}[1] = $opt[2];
}
break;
case 'hidden':
case 'hide':
case 'attachments':
case 'hideshow':
case 'collapsed':
switch (drupal_strtolower($opt[2])) {
case '0':
case 'false':
$_tableofcontents_toc->{$opt}[1] = FALSE;
break;
case '1':
case 'true':
$_tableofcontents_toc->{$opt}[1] = TRUE;
break;
default:
drupal_set_message(t("Table of contents error: @val is not a valid boolean value for @opt. Expected 0, false, 1, or true.", array(
'@opt' => $opt[1],
'@val' => $opt[2],
)), 'error');
break;
}
break;
default:
drupal_set_message(t("Table of contents error: @opt is not a valid option.", array(
'@opt' => $opt[1],
)), 'error');
break;
}
}
function _tableofcontents_get_options(&$toc, $options) {
static $id_counter = 0;
$toc->id = 'toc';
if ($id_counter) {
$toc->id .= $id_counter;
}
++$id_counter;
if (!$options) {
return;
}
$options .= ';';
preg_replace_callback('/([A-Za-z0-9]+)[:=]\\s*((?:&[^;]+;|[^;])+);/', '_tableofcontents_parse_option', $options);
if ($toc->minlevel > $toc->maxlevel) {
drupal_set_message(t("Table of contents error: minlevel (!minlevel) is larger than maxlevel (!maxlevel), reverting to defaults.", array(
'!minlevel' => $toc->minlevel,
'!maxlevel' => $toc->maxlevel,
)), 'error');
$toc->minlevel = 2;
$toc->maxlevel = 3;
}
if ($toc->minlevel < $toc->header_min) {
$toc->minlevel = $toc->header_min;
if ($toc->minlevel > $toc->maxlevel) {
$toc->maxlevel = $toc->minlevel;
}
}
if ($toc->maxlevel > $toc->header_max) {
$toc->maxlevel = $toc->header_max;
}
}
function _tableofcontents_replace_toc_tags($matches) {
global $_tableofcontents_toc;
$save_toc = drupal_clone($_tableofcontents_toc);
$options = isset($matches[1]) ? $matches[1] : '';
_tableofcontents_get_options($_tableofcontents_toc, $options);
if ($_tableofcontents_toc->hidden) {
$_tableofcontents_toc = $save_toc;
return '';
}
if (!$_tableofcontents_toc->allow_override) {
$_tableofcontents_toc = $save_toc;
}
elseif ($_tableofcontents_toc->safe_title && $_tableofcontents_toc->title != $save_toc->title) {
$_tableofcontents_toc->title = check_plain(strip_tags($_tableofcontents_toc->title));
}
if ($_tableofcontents_toc->attachments && arg(0) == 'node' && is_numeric(arg(1)) && module_exists('upload')) {
$node = node_load(arg(1));
$_tableofcontents_toc->files = $node->files;
}
$result = theme('tableofcontents_toc', $_tableofcontents_toc);
$_tableofcontents_toc = $save_toc;
if ($result) {
$_tableofcontents_toc->has_toc = TRUE;
}
return $result;
}
function _tableofcontents_comments(&$toc, $format) {
if (arg(0) != 'node' || !is_numeric(arg(1))) {
return;
}
$nid = arg(1);
$node = node_load($nid);
$mode = _comment_get_display_setting('mode', $node);
$order = _comment_get_display_setting('sort', $node);
$comments_per_page = _comment_get_display_setting('comments_per_page', $node);
$query = 'SELECT c.cid, c.subject, c.thread, c.status' . ' FROM {comments} c WHERE c.nid = %d AND c.status = %d';
$query_count = 'SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d AND c.status = %d';
$query_args = array(
$nid,
COMMENT_PUBLISHED,
);
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.cid DESC';
}
else {
$query .= ' ORDER BY c.thread DESC';
}
}
elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$query .= ' ORDER BY c.cid';
}
else {
$query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
}
}
$query = db_rewrite_sql($query, 'c', 'cid');
$query_count = db_rewrite_sql($query_count, 'c', 'cid');
$result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
$comment_level = variable_get('tableofcontents_comments_level_' . $format, 3);
while ($comment = db_fetch_object($result)) {
$level = $comment_level;
if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
$level += count(explode('.', $comment->thread)) - 1;
}
if ($level < 1 || $level > 6) {
continue;
}
$h = new TOC_Header();
$h->level = $level;
$h->title = check_plain($comment->subject);
++$toc->counters[$level];
for ($l = $level + 1; $l <= 6; ++$l) {
$toc->counters[$l] = 0;
}
if ($toc->level_from > $level) {
$toc->level_from = $level;
}
$toc->level_to = $level;
if ($toc->header_min) {
if ($toc->header_min > $level) {
$toc->header_min = $level;
}
}
else {
$toc->header_min = $level;
}
if ($toc->header_max) {
if ($toc->header_max < $level) {
$toc->header_max = $level;
}
}
else {
$toc->header_max = $level;
}
$h->identifier = 'comment-' . $comment->cid;
$h->attributes = ' id="' . $h->identifier . '"';
$h->number = theme('tableofcontents_number', $toc);
$toc->headers[] = $h;
}
}
function _tableofcontents_replace_toc($delta, $format, $text) {
global $_tableofcontents_toc;
global $_tableofcontents_block_toc;
if ($delta == 1) {
$text = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
$_tableofcontents_toc = new TOC();
$_tableofcontents_toc->on_print_pages = TRUE;
return _tableofcontents_headers($_tableofcontents_toc, $format, $text);
}
if (empty($_tableofcontents_block_toc) && variable_get('tableofcontents_hide_table_' . $format, FALSE)) {
return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
}
$org_text = $text;
$_tableofcontents_toc = new TOC();
$_tableofcontents_toc->id = 'toc';
if (empty($_tableofcontents_block_toc) && strpos($text, "[toc") === FALSE) {
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:
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 (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:
return $org_text;
case 1:
$text = '[toc]' . $text;
break;
case 2:
$text = $text . '[toc]';
break;
}
}
else {
$_tableofcontents_toc->automatic = 0;
}
if (arg(0) == 'print' && is_numeric(arg(1)) && module_exists('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;
}
}
$text = _tableofcontents_headers($_tableofcontents_toc, $format, $text);
if (!$_tableofcontents_block_toc && variable_get('tableofcontents_block_only_table_' . $format, FALSE)) {
return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
}
if (variable_get('tableofcontents_comments_' . $format, FALSE) && module_exists('comment') && user_access('access comments')) {
_tableofcontents_comments($_tableofcontents_toc, $format);
}
$max = count($_tableofcontents_toc->headers);
if ($max == 0) {
if ($_tableofcontents_block_toc) {
return '';
}
return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $org_text);
}
if ($_tableofcontents_toc->automatic) {
$min_limit = variable_get('tableofcontents_min_limit_' . $format, 5);
if ($max < $min_limit) {
if ($_tableofcontents_block_toc) {
return '';
}
return preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $org_text);
}
}
$_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) {
$_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) {
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) {
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 ($_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;
}
function _tableofcontents_prepare($delta, $format, $text) {
$text = str_replace('[[TOC]]', '[toc]', $text);
return preg_replace_callback(array(
'%<!--\\s*tableofcontents([^>]*?)-->%',
'%\\[\\[TOC(.*?)\\]\\]%',
), '_tableofcontents_replace_comments', $text);
}
function _tableofcontents_process($delta, $format, $text) {
return _tableofcontents_replace_toc($delta, $format, $text);
}
function theme_tableofcontents_toc($toc) {
if ($toc->hideshow && !isset($toc->on_print_pages)) {
$hs = '<span class="toc-toggle-message"> </span>';
$js = '<script type="text/javascript">toc_collapse=' . ($toc->collapsed ? 1 : 0) . ';</script>';
}
else {
$hs = '';
$js = '';
}
if ($toc->id) {
$id = ' id="' . check_plain($toc->id) . '"';
}
else {
$id = '';
}
$output = $js . "<div class=\"toc\"" . $id . ">\n";
if ($toc->title != '<none>') {
$output .= "<div class=\"toc-title\">" . t($toc->title) . $hs . "</div>\n";
}
elseif ($hs) {
$output .= $hs;
}
$list = $toc->numbering == 4 ? 'ol' : 'ul';
$output .= "<div class=\"toc-list\">\n" . "<" . $list . ">\n";
$has_content = FALSE;
$level = $toc->minlevel;
$first = TRUE;
foreach ($toc->headers as $h) {
$cur_level = $h->level;
if ($cur_level >= $toc->minlevel && $cur_level <= $toc->maxlevel) {
$has_content = TRUE;
if ($cur_level > $level) {
do {
$output .= "\n<" . $list . ">\n";
++$level;
} while ($cur_level > $level);
}
elseif ($cur_level < $level) {
do {
$output .= "</li>\n</" . $list . ">\n";
--$level;
} while ($cur_level < $level);
$output .= "</li>\n";
}
elseif (!$first) {
$output .= "</li>\n";
}
if ($h->number) {
$number = '<span class="toc-number">' . $h->number . '</span>';
}
else {
$number = '';
}
$list_class = 'toc-level-' . ($level - $toc->minlevel + 1);
$title = strip_tags($h->title, $toc->allowed_tags);
$output .= "\t<li class=\"{$list_class}\">";
if (isset($toc->on_print_pages)) {
$output .= $number . $title;
}
else {
$output .= "<a href=\"#" . $h->identifier . "\">" . $number . $title . "</a>";
}
$first = FALSE;
}
}
for (; $level > $toc->minlevel; --$level) {
$output .= "</li>\n</" . $list . ">\n";
}
$output .= "</li>\n";
if (!empty($toc->files)) {
$has_files = FALSE;
$files_output = '<li><a href="#attachments">' . t("Attachments") . "</a>";
$files_output .= "<" . $list . ">";
foreach ($toc->files as $file) {
if ($file->list && !$file->remove) {
$has_files = TRUE;
$url = strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path());
$file_href = file_create_url($url);
$file_text = $file->description ? $file->description : $file->filename;
$files_output .= '<li>' . theme('file_icon', $file) . l($file_text, $file_href) . "</li>\n";
}
}
if ($has_files) {
$output .= $files_output . "</" . $list . "></li>\n";
$has_content = TRUE;
}
}
if (!$has_content) {
return '';
}
$output .= "</" . $list . ">\n</div>\n</div>";
return theme('tableofcontents_toc_text', $output);
}
function theme_tableofcontents_toc_text($text) {
return $text;
}
function _tableofcontents_roman($arabic) {
static $ones = array(
"",
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX",
);
static $tens = array(
"",
"X",
"XX",
"XXX",
"XL",
"L",
"LX",
"LXX",
"LXXX",
"XC",
);
static $hundreds = array(
"",
"C",
"CC",
"CCC",
"CD",
"D",
"DC",
"DCC",
"DCCC",
"CM",
);
static $thousands = array(
"",
"M",
"MM",
"MMM",
"MMMM",
);
if ($arabic > 4999) {
return $arabic;
}
elseif ($arabic == 0) {
return "N";
}
else {
$m = fmod($arabic, 1000);
$roman = $thousands[($arabic - $m) / 1000];
$arabic = $m;
$m = fmod($arabic, 100);
$roman .= $hundreds[($arabic - $m) / 100];
$arabic = $m;
$m = fmod($arabic, 10);
$roman .= $tens[($arabic - $m) / 10];
$arabic = $m;
$m = fmod($arabic, 1);
$roman .= $ones[($arabic - $m) / 1];
return $roman;
}
}
function _tableofcontents_letter($number) {
while ($number > 0) {
$result = chr($number % 26 + 64) . $result;
$number = (int) ($number / 26);
}
return $result;
}
function _tableofcontents_convert_number($mode, $number) {
switch ($mode) {
default:
return $number;
case 1:
return _tableofcontents_roman($number);
case 2:
return drupal_strtolower(_tableofcontents_roman($number));
case 3:
return _tableofcontents_letter($number);
case 4:
return drupal_strtolower(_tableofcontents_letter($number));
case 5:
return sprintf("0x%02x", $number);
}
}
function theme_tableofcontents_back_to_top($toc) {
return '<div class="toc-back-to-top"><a href="#' . $toc->back_to_top_anchor . '">' . $toc->back_to_top . '</a></div>';
}
function theme_tableofcontents_number($toc) {
$result = '';
switch ($toc->numbering) {
case 0:
case 4:
return '';
case 1:
$result = _tableofcontents_convert_number($toc->number_mode, $toc->counters[$toc->level_to]);
break;
case 2:
case 3:
$mode = $toc->number_mode;
$result = _tableofcontents_convert_number($mode, $toc->counters[$toc->level_from]);
for ($idx = $toc->level_from + 1; $idx <= $toc->level_to; ++$idx) {
$result .= $toc->number_separator . _tableofcontents_convert_number($mode, $toc->counters[$idx]);
}
if ($toc->numbering == 3 && $toc->level_from == $toc->level_to) {
$result .= $toc->number_separator . _tableofcontents_convert_number($mode, 0);
}
break;
}
$output = $toc->number_start_letter . $result . $toc->number_end_letter . ' ';
return theme('tableofcontents_number_text', $output);
}
function theme_tableofcontents_number_text($text) {
return $text;
}