function parse_data_process_maketoc( &$data, $options) {
global $prefs;
$need_maketoc = strpos($data, "{maketoc") ;
$need_autonumbering = ( preg_match('/^\!+[\-\+]?#/m', $data) > 0 );
$anch = array();
global $anch;
$pageNum = 1;
// 08-Jul-2003, by zaufi
// HotWords will be replace only in ordinal text
// It looks __really__ goofy in Headers or Titles
if ( $prefs['feature_hotwords'] == 'y' ) {
// Get list of HotWords
$words = $this->get_hotwords();
}
// Now tokenize the expression and process the tokens
// Use tab and newline as tokenizing characters as well ////
$lines = explode("\n", $data);
$data = '';
$listbeg = array();
$divdepth = array();
$hdr_structure = array();
$show_title_level = array();
$last_hdr = array();
$nb_last_hdr = 0;
$nb_hdrs = 0;
$inTable = 0;
$inPre = 0;
$inComment = 0;
$inTOC = 0;
$inScript = 0;
$title_text = '';
// loop: process all lines
$in_paragraph = 0;
foreach ($lines as $line) {
$current_title_num = '';
$numbering_remove = 0;
$line = rtrim($line); // Trim off trailing white space
// Check for titlebars...
// NOTE: that title bar should start at the beginning of the line and
// be alone on that line to be autoaligned... otherwise, it is an old
// styled title bar...
if (substr(ltrim($line), 0, 2) == '-=' && substr($line, -2, 2) == '=-') {
// Close open paragraph and lists, but not div's
$this->close_blocks($data, $in_paragraph, $listbeg, $divdepth, 1, 1, 0);
//
$align_len = strlen($line) - strlen(ltrim($line));
// My textarea size is about 120 space chars.
//define('TEXTAREA_SZ', 120);
// NOTE: That strict math formula (split into 3 areas) gives
// bad visual effects...
// $align = ($align_len < (TEXTAREA_SZ / 3)) ? "left"
// : (($align_len > (2 * TEXTAREA_SZ / 3)) ? "right" : "center");
//
// Going to introduce some heuristic here :)
// Visualy (remember that space char is thin) center starts at 25 pos
// and 'right' from 60 (HALF of full width!) -- thats all :)
//
// NOTE: Guess align only if more than 10 spaces before -=title=-
if ($align_len > 10) {
$align = ($align_len < 25) ? "left" : (($align_len > 60) ? "right" : "center");
$align = ' style="text-align: ' . $align . ';"';
} else {
$align = '';
}
//
$line = trim($line);
$line = '
' . substr($line, 2, strlen($line) - 4). '
';
$data .= $line . "\n";
// TODO: Case is handled ... no need to check other conditions
// (it is apriori known that they are all false, moreover sometimes
// check procedure need > O(0) of compexity)
// -- continue to next line...
// MUST replace all remaining parse blocks to the same logic...
continue;
}
// Replace old styled titlebars
if (strlen($line) != strlen($line = preg_replace("/-=(.+?)=-/", "$1
", $line))) {
// Close open paragraph, but not lists (why not?) or div's
$this->close_blocks($data, $in_paragraph, $listbeg, $divdepth, 1, 0, 0);
$data .= $line . "\n";
continue;
}
// check if we are inside a ~hc~ block and, if so, ignore
// monospaced and do not insert
$inComment += substr_count(strtolower($line), "");
// check if we are inside a ~pre~ block and, if so, ignore
// monospaced and do not insert
$inPre += substr_count(strtolower($line), "
$inTable += substr_count(strtolower($line), "
$inTOC += substr_count(strtolower($line), "
$inScript += substr_count(strtolower($line), "';
array_unshift($divdepth, $hdrlevel);
$addremove += 1;
}
// Generate the final title text
$title_text_base = substr($line, $hdrlevel + $addremove);
$title_text = $current_title_num.$title_text_base;
// create stable anchors for all headers
// use header but replace non-word character sequences
// with one underscore (for XHTML 1.0 compliance)
// Workaround pb with plugin replacement and header id
// first we remove hash from title_text for headings beginning
// with images and HTML tags
$thisid = ereg_replace('§[a-z0-9]{32}§', '', $title_text);
$thisid = ereg_replace('?[^>]+>', '', $thisid);
$thisid = ereg_replace('[^a-zA-Z0-9\:\.\-\_]+', '_', $thisid);
$thisid = ereg_replace('^[^a-zA-Z]*', '', $thisid);
if (empty($thisid)) $thisid = 'a'.md5($title_text);
// Add a number to the anchor if it already exists, to avoid duplicated anchors
if ( isset($all_anchors[$thisid]) ) {
$all_anchors[$thisid]++;
$thisid .= '_'.$all_anchors[$thisid];
} else {
$all_anchors[$thisid] = 1;
}
// Collect TOC entry if any {maketoc} is present on the page
if ( $need_maketoc !== false && strpos($data, title) ){ //=====REMOVE the comment lines - this code is MAKETOC necessary -larry 2009-10-04
$anch[] = array(
'id' => $thisid,
'hdrlevel' => $hdrlevel,
'pagenum' => $pageNum,
'title' => $title_text_base,
'title_displayed_num' => $current_title_num,
'title_real_num' => $current_title_real_num
);
} //=====REMOVE this comment line also - this code is necessary -larry 2009-10-04
global $tiki_p_edit, $section;
if ($prefs['wiki_edit_section'] == 'y' && $section == 'wiki page' && $tiki_p_edit == 'y' and ( $prefs['wiki_edit_section_level'] == 0 or $hdrlevel <= $prefs['wiki_edit_section_level']) && (empty($options['print']) ) ) {
global $smarty;
include_once('lib/smarty_tiki/function.icon.php');
$button = '';
} else {
$button = '';
}
// Use $hdrlevel + 1 because the page title is H1, so none of the other headers should be.
// Except when page title is off
// Or when in wysiwyg mode
$headerInc = 0;
if( $prefs['feature_page_title'] == 'y' && ! $options['noheaderinc'] )
++$headerInc;
if ( $prefs['feature_wiki_show_hide_before'] == 'y' ) {
$line = $button.''.$aclose.' '.$title_text.''.$aclose2;
} else {
$line = $button.''.$title_text.''.$aclose.$aclose2;
}
} elseif (!strcmp($line, $prefs['wiki_page_separator'])) {
// Close open paragraph, lists, and div's
$this->close_blocks($data, $in_paragraph, $listbeg, $divdepth, 1, 1, 1);
// Leave line unchanged... tiki-index.php will split wiki here
$line = $prefs['wiki_page_separator'];
$pageNum += 1;
} else {
/** Usual paragraph.
*
* If the
* $prefs['feature_wiki_paragraph_formatting']
* is on, then consecutive lines of
* text will be gathered into a block
* that is surrounded by HTML
* paragraph tags. One or more blank
* lines, or another special Wiki line
* (e.g., heading, titlebar, etc.)
* signifies the end of the
* paragraph. If the paragraph
* formatting feature is off, the
* original Tikiwiki behavior is used,
* in which each line in the source is
* terminated by an explicit line
* break (br tag).
*
* @since Version 1.9
*/
if ($inTable == 0 && $inPre == 0 && $inComment == 0 && $inTOC == 0 && $inScript == 0
// Don't put newlines at comments' end!
&& ! substr_count(strtolower($line), "-->")
) {
if ($prefs['feature_wiki_paragraph_formatting'] == 'y') {
if ($in_paragraph && ( 0 == strcmp("", trim($line)) || substr(trim($line),0,5) == 'close_blocks($data, $in_paragraph, $listbeg, $divdepth, 1, 0, 0);
} elseif (!$in_paragraph && (0 != strcmp("", trim($line))) && substr(trim($line),0,4) != '' . $line;
} else {
// A normal in-paragraph line or a consecutive blank line.
// Leave it as is.
}
} else {
$line .= '
';
}
}
}
}
}
$data .= $line . "\n";
}
// Close open paragraph, lists, and div's
$this->close_blocks($data, $in_paragraph, $listbeg, $divdepth, 1, 1, 1);
/*
* Replace special "maketoc" plugins
* Valid arguments :
* - type (look of the maketoc),
* - maxdepth (max level displayed),
* - title (replace the default title),
* - showhide (if set to y, add the Show/Hide link)
* - nolinks (if set to y, don't add links on toc entries)
* - nums :
* * 'n' means 'no title autonumbering' in TOC,
* * 'force' means :
* ~ same as 'y' if autonumbering is used in the page,
* ~ 'number each toc entry as if they were all autonumbered'
* * any other value means 'same as page's headings autonumbering',
*
* (Note that title will be translated if a translation is available)
*
* Examples: {maketoc}, {maketoc type=box maxdepth=1 showhide=y}, {maketoc title="Page Content" maxdepth=3}, ...
* Obsolete syntax: {maketoc:box}
*/
$new_data = '';
$search_start = 0;
while ( ($maketoc_start = strpos($data, "{maketoc", $search_start)) !== false ) {
$maketoc_length = strpos($data, "}", $maketoc_start) + 1 - $maketoc_start;
$maketoc_string = substr($data, $maketoc_start, $maketoc_length);
// Handle old type definition for type "box" (and preserve environment for the title also)
if ( $maketoc_length > 12 && strtolower(substr($maketoc_string, 8, 4)) == ':box' ) {
$maketoc_string = "{maketoc type=box showhide=y title='".tra('index', $options['language'], true).'"'.substr($maketoc_string, 12);
}
$maketoc_string = str_replace('"', '"', $maketoc_string);
$maketoc_regs = array();
if ( $maketoc_length == 9 || preg_match_all("/([^\s=\(]+)=([^\"\s=\)\}]+|\"[^\"]*\")/", $maketoc_string, $maketoc_regs) ) {
if ( $maketoc_start > 0 ) {
$new_data .= substr($data, 0, $maketoc_start);
}
// Set maketoc default values
$maketoc_args = array(
'type' => '',
'maxdepth' => 0, // No limit
'title' => tra('Table of contents', $options['language'], true),
'showhide' => '',
'nolinks' => '',
'nums' => ''
);
// Build maketoc arguments list (and remove " chars if they are around the value)
if ( isset($maketoc_regs[1]) ) {
$nb_args = count($maketoc_regs[1]);
for ( $a = 0; $a < $nb_args ; $a++ ) {
$maketoc_args[strtolower($maketoc_regs[1][$a])] = trim($maketoc_regs[2][$a], '"');
}
}
if ( $maketoc_args['title'] != '' ) {
// Translate maketoc title
$maketoc_summary = ' summary="'.tra($maketoc_args['title'], $options['language'], true).'"';
$maketoc_title = "".tra($maketoc_args['title'], $options['language']).'
';
} else {
$maketoc_summary = '';
$maketoc_title = '';
}
// Build maketoc
switch ( $maketoc_args['type'] ) {
case 'box':
$maketoc_header = '';
$maketoc = "\n";
$link_class = 'toclink';
break;
default:
$maketoc = '';
$maketoc_header = "".$maketoc_title;
$maketoc_footer = '';
$link_class = 'link';
}
if ( count($anch) and $need_maketoc !== false) {
foreach ( $anch as $tocentry ) {
if ($maketoc_args['maxdepth'] > 0 && $tocentry['hdrlevel'] > $maketoc_args['maxdepth'] ) {
continue;
}
// Generate the toc entry title (with nums)
if ( $maketoc_args['nums'] == 'n' ) {
$tocentry_title = '';
} elseif ( $maketoc_args['nums'] == 'force' && ! $need_autonumbering ) {
$tocentry_title = $tocentry['title_real_num'];
} else {
$tocentry_title = $tocentry['title_displayed_num'];
}
$tocentry_title .= $tocentry['title'];
// Generate the toc entry link
$tocentry_link = '#'.$tocentry['id'];
if ( $tocentry['pagenum'] > 1 ) {
$tocentry_link = $_SERVER['PHP_SELF'].'?page='.$options['page'].'&pagenum='.$tocentry['pagenum'].$tocentry_link;
}
if ( $maketoc_args['nolinks'] != 'y' ) {
$tocentry_title = "".$tocentry_title.'';
}
if ( $maketoc != '' ) $maketoc.= "\n";
switch ( $maketoc_args['type'] ) {
case 'box':
$maketoc .= "".$tocentry_title."";
break;
default:
$maketoc .= str_repeat('*', $tocentry['hdrlevel']).$tocentry_title;
}
}
$maketoc = $this->parse_data($maketoc);
$maketoc = ereg_replace("^", '', $maketoc);
if ( $link_class != 'link' ) {
$maketoc = ereg_replace("'link'", "'$link_class'", $maketoc);
}
}
global $TOC_newstring, $TOC_oldstring ; //===== create new global variables
$TOC_newstring = $maketoc ; //=====MAKETOC fix, beginning of added code. Get copy of newest TOC -larryg 2009-10-04
if ( strpos($maketoc, $TOC_oldstring) ) //=====if this MAKETOC contains previous chapter's TOC entries, remove that portion of the string
{ $maketoc = substr($maketoc, 0 , strpos($maketoc, $TOC_oldstring)).substr($maketoc, strpos($maketoc, $TOC_oldstring)+ strlen($TOC_oldstring)) ; } //=====concatenate string without old entries
$maketoc = $maketoc_header.$maketoc.$maketoc_footer ; // this line was original to the maketoc function - no changes
//=====prepare this chapter's TOC entry to be compared with the next chapter's string]
$TOC_oldstring = substr($TOC_newstring, strpos($TOC_newstring, '
' )) ; //===== trim the underline stuff from the tail of the string
//=====MAKETOC FIX, end of added code -larryg 2009-10-04
// Add a Show/Hide link //====MAKETOC fix = disable show/hide button in print, added "&& !options['print']" -larryg 2009-10-04
if ( isset($maketoc_args['showhide']) && $maketoc_args['showhide'] == 'y' && !$options['print'] ) {
$maketoc .= "\n";
}
$new_data .= $maketoc;
$data = substr($data, $maketoc_start + $maketoc_length);
$search_start = 0; // Reinitialize search start cursor, since data now begins after the last replaced maketoc
} else {
$search_start = $maketoc_start + $maketoc_length;
}
}
$data = $new_data.$data;
// Add icon to edit the text before the first section (if there is some)
if ($prefs['wiki_edit_section'] == 'y' && isset($section) && $section == 'wiki page' && $tiki_p_edit == 'y' && (empty($options['print']) || !$options['print']) && strpos($data, '') != 0){
global $smarty;
include_once('lib/smarty_tiki/function.icon.php');
$button = '
';
$data = $button.$data;
}
}