includes/wikiengine/Tables.php
changeset 1227 bdac73ed481e
parent 1081 745200a9cc2a
child 1308 f9bee9b125ee
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    28  * @access public
    28  * @access public
    29  */
    29  */
    30  
    30  
    31 function process_tables( $text )
    31 function process_tables( $text )
    32 {
    32 {
    33   // include some globals, do some parser stuff that would normally be done in the parent parser function
    33 	// include some globals, do some parser stuff that would normally be done in the parent parser function
    34   global $mStripState;
    34 	global $mStripState;
    35   $x =& $mStripState;
    35 	$x =& $mStripState;
    36   
    36 	
    37   // parse the text
    37 	// parse the text
    38   $text = doTableStuff($text);
    38 	$text = doTableStuff($text);
    39 
    39 
    40   return $text;
    40 	return $text;
    41 }
    41 }
    42 
    42 
    43 /**
    43 /**
    44  * parse the wiki syntax used to render tables
    44  * parse the wiki syntax used to render tables
    45  *
    45  *
    46  * @param string $t the text to parse
    46  * @param string $t the text to parse
    47  * @return string
    47  * @return string
    48  * @access private
    48  * @access private
    49  */
    49  */
    50 function doTableStuff( $t ) {
    50 function doTableStuff( $t ) {
    51   
    51 	
    52   $t = explode ( "\n" , $t ) ;
    52 	$t = explode ( "\n" , $t ) ;
    53   $td = array () ; # Is currently a td tag open?
    53 	$td = array () ; # Is currently a td tag open?
    54   $ltd = array () ; # Was it TD or TH?
    54 	$ltd = array () ; # Was it TD or TH?
    55   $tr = array () ; # Is currently a tr tag open?
    55 	$tr = array () ; # Is currently a tr tag open?
    56   $ltr = array () ; # tr attributes
    56 	$ltr = array () ; # tr attributes
    57   $has_opened_tr = array(); # Did this table open a <tr> element?
    57 	$has_opened_tr = array(); # Did this table open a <tr> element?
    58   $indent_level = 0; # indent level of the table
    58 	$indent_level = 0; # indent level of the table
    59   foreach ( $t AS $k => $x )
    59 	foreach ( $t AS $k => $x )
    60   {
    60 	{
    61     $x = trim ( $x ) ;
    61 		$x = trim ( $x ) ;
    62     $fc = substr ( $x , 0 , 1 ) ;
    62 		$fc = substr ( $x , 0 , 1 ) ;
    63     if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
    63 		if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
    64       $indent_level = strlen( $matches[1] );
    64 			$indent_level = strlen( $matches[1] );
    65 
    65 
    66       $attributes = unstripForHTML( $matches[2] );
    66 			$attributes = unstripForHTML( $matches[2] );
    67 
    67 
    68       $t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
    68 			$t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
    69         '<table' . fixTagAttributes( $attributes, 'table' ) . '>' ;
    69 				'<table' . fixTagAttributes( $attributes, 'table' ) . '>' ;
    70       array_push ( $td , false ) ;
    70 			array_push ( $td , false ) ;
    71       array_push ( $ltd , '' ) ;
    71 			array_push ( $ltd , '' ) ;
    72       array_push ( $tr , false ) ;
    72 			array_push ( $tr , false ) ;
    73       array_push ( $ltr , '' ) ;
    73 			array_push ( $ltr , '' ) ;
    74       array_push ( $has_opened_tr, false );
    74 			array_push ( $has_opened_tr, false );
    75     }
    75 		}
    76     else if ( count ( $td ) == 0 ) { } # Don't do any of the following
    76 		else if ( count ( $td ) == 0 ) { } # Don't do any of the following
    77     else if ( '|}' == substr ( $x , 0 , 2 ) ) {
    77 		else if ( '|}' == substr ( $x , 0 , 2 ) ) {
    78       $z = "</table>" . substr ( $x , 2);
    78 			$z = "</table>" . substr ( $x , 2);
    79       $l = array_pop ( $ltd ) ;
    79 			$l = array_pop ( $ltd ) ;
    80       if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
    80 			if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
    81       if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    81 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    82       if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    82 			if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    83       array_pop ( $ltr ) ;
    83 			array_pop ( $ltr ) ;
    84       $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
    84 			$t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
    85     }
    85 		}
    86     else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
    86 		else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
    87       $x = substr ( $x , 1 ) ;
    87 			$x = substr ( $x , 1 ) ;
    88       while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
    88 			while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
    89       $z = '' ;
    89 			$z = '' ;
    90       $l = array_pop ( $ltd ) ;
    90 			$l = array_pop ( $ltd ) ;
    91       array_pop ( $has_opened_tr );
    91 			array_pop ( $has_opened_tr );
    92       array_push ( $has_opened_tr , true ) ;
    92 			array_push ( $has_opened_tr , true ) ;
    93       if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    93 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    94       if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    94 			if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    95       array_pop ( $ltr ) ;
    95 			array_pop ( $ltr ) ;
    96       $t[$k] = $z ;
    96 			$t[$k] = $z ;
    97       array_push ( $tr , false ) ;
    97 			array_push ( $tr , false ) ;
    98       array_push ( $td , false ) ;
    98 			array_push ( $td , false ) ;
    99       array_push ( $ltd , '' ) ;
    99 			array_push ( $ltd , '' ) ;
   100       $attributes = unstripForHTML( $x );
   100 			$attributes = unstripForHTML( $x );
   101       array_push ( $ltr , fixTagAttributes( $attributes, 'tr' ) ) ;
   101 			array_push ( $ltr , fixTagAttributes( $attributes, 'tr' ) ) ;
   102     }
   102 		}
   103     else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
   103 		else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
   104       # $x is a table row
   104 			# $x is a table row
   105       if ( '|+' == substr ( $x , 0 , 2 ) ) {
   105 			if ( '|+' == substr ( $x , 0 , 2 ) ) {
   106         $fc = '+' ;
   106 				$fc = '+' ;
   107         $x = substr ( $x , 1 ) ;
   107 				$x = substr ( $x , 1 ) ;
   108       }
   108 			}
   109       $after = substr ( $x , 1 ) ;
   109 			$after = substr ( $x , 1 ) ;
   110       if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
   110 			if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
   111 
   111 
   112       // Split up multiple cells on the same line.
   112 			// Split up multiple cells on the same line.
   113       // FIXME: This can result in improper nesting of tags processed
   113 			// FIXME: This can result in improper nesting of tags processed
   114       // by earlier parser steps, but should avoid splitting up eg
   114 			// by earlier parser steps, but should avoid splitting up eg
   115       // attribute values containing literal "||".
   115 			// attribute values containing literal "||".
   116       $after = wfExplodeMarkup( '||', $after );
   116 			$after = wfExplodeMarkup( '||', $after );
   117 
   117 
   118       $t[$k] = '' ;
   118 			$t[$k] = '' ;
   119 
   119 
   120       # Loop through each table cell
   120 			# Loop through each table cell
   121       foreach ( $after AS $theline )
   121 			foreach ( $after AS $theline )
   122       {
   122 			{
   123         $z = '' ;
   123 				$z = '' ;
   124         if ( $fc != '+' )
   124 				if ( $fc != '+' )
   125         {
   125 				{
   126           $tra = array_pop ( $ltr ) ;
   126 					$tra = array_pop ( $ltr ) ;
   127           if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
   127 					if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
   128           array_push ( $tr , true ) ;
   128 					array_push ( $tr , true ) ;
   129           array_push ( $ltr , '' ) ;
   129 					array_push ( $ltr , '' ) ;
   130           array_pop ( $has_opened_tr );
   130 					array_pop ( $has_opened_tr );
   131           array_push ( $has_opened_tr , true ) ;
   131 					array_push ( $has_opened_tr , true ) ;
   132         }
   132 				}
   133 
   133 
   134         $l = array_pop ( $ltd ) ;
   134 				$l = array_pop ( $ltd ) ;
   135         if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
   135 				if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
   136         if ( $fc == '|' ) $l = 'td' ;
   136 				if ( $fc == '|' ) $l = 'td' ;
   137         else if ( $fc == '!' ) $l = 'th' ;
   137 				else if ( $fc == '!' ) $l = 'th' ;
   138         else if ( $fc == '+' ) $l = 'caption' ;
   138 				else if ( $fc == '+' ) $l = 'caption' ;
   139         else $l = '' ;
   139 				else $l = '' ;
   140         array_push ( $ltd , $l ) ;
   140 				array_push ( $ltd , $l ) ;
   141 
   141 
   142         # Cell parameters
   142 				# Cell parameters
   143         $y = explode ( '|' , $theline , 2 ) ;
   143 				$y = explode ( '|' , $theline , 2 ) ;
   144         # Note that a '|' inside an invalid link should not
   144 				# Note that a '|' inside an invalid link should not
   145         # be mistaken as delimiting cell parameters
   145 				# be mistaken as delimiting cell parameters
   146         if ( strpos( $y[0], '[[' ) !== false ) {
   146 				if ( strpos( $y[0], '[[' ) !== false ) {
   147           $y = array ($theline);
   147 					$y = array ($theline);
   148         }
   148 				}
   149         if ( count ( $y ) == 1 )
   149 				if ( count ( $y ) == 1 )
   150           $y = "{$z}<{$l}>{$y[0]}" ;
   150 					$y = "{$z}<{$l}>{$y[0]}" ;
   151         else {
   151 				else {
   152           $attributes = unstripForHTML( $y[0] );
   152 					$attributes = unstripForHTML( $y[0] );
   153           $y = "{$z}<{$l}".fixTagAttributes($attributes, $l).">{$y[1]}" ;
   153 					$y = "{$z}<{$l}".fixTagAttributes($attributes, $l).">{$y[1]}" ;
   154         }
   154 				}
   155         $t[$k] .= $y ;
   155 				$t[$k] .= $y ;
   156         array_push ( $td , true ) ;
   156 				array_push ( $td , true ) ;
   157       }
   157 			}
   158     }
   158 		}
   159   }
   159 	}
   160 
   160 
   161   # Closing open td, tr && table
   161 	# Closing open td, tr && table
   162   while ( count ( $td ) > 0 )
   162 	while ( count ( $td ) > 0 )
   163   {
   163 	{
   164     $l = array_pop ( $ltd ) ;
   164 		$l = array_pop ( $ltd ) ;
   165     if ( array_pop ( $td ) ) $t[] = '</td>' ;
   165 		if ( array_pop ( $td ) ) $t[] = '</td>' ;
   166     if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
   166 		if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
   167     if ( !array_pop ( $has_opened_tr ) ) $t[] = "<tr><td></td></tr>" ;
   167 		if ( !array_pop ( $has_opened_tr ) ) $t[] = "<tr><td></td></tr>" ;
   168     $t[] = '</table></_paragraph_bypass>' ;
   168 		$t[] = '</table></_paragraph_bypass>' ;
   169   }
   169 	}
   170 
   170 
   171   $t = implode ( "\n" , $t ) ;
   171 	$t = implode ( "\n" , $t ) ;
   172   
   172 	
   173   # special case: don't return empty table
   173 	# special case: don't return empty table
   174   if($t == "<table>\n<tr><td></td></tr>\n</table>")
   174 	if($t == "<table>\n<tr><td></td></tr>\n</table>")
   175     $t = '';
   175 		$t = '';
   176   return $t ;
   176 	return $t ;
   177 }
   177 }
   178 
   178