includes/wikiengine/Tables.php
changeset 1308 f9bee9b125ee
parent 1227 bdac73ed481e
equal deleted inserted replaced
1307:5d9f6ecf748e 1308:f9bee9b125ee
    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 	$row_count = 0;
       
    60 	foreach ( $t AS $k => $line )
    60 	{
    61 	{
    61 		$x = trim ( $x ) ;
    62 		$line = trim ( $line ) ;
    62 		$fc = substr ( $x , 0 , 1 ) ;
    63 		$first_char = substr ( $line , 0 , 1 ) ;
    63 		if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
    64 		if ( preg_match( '/^(:*)\{\|(.*)$/', $line, $matches ) ) {
    64 			$indent_level = strlen( $matches[1] );
    65 			$indent_level = strlen( $matches[1] );
    65 
    66 
    66 			$attributes = unstripForHTML( $matches[2] );
    67 			$attributes = unstripForHTML( $matches[2] );
       
    68 			$styled_table = false;
       
    69 			if ( trim($attributes) == "styled" )
       
    70 			{
       
    71 				$attributes='cellspacing="1" cellpadding="4"';
       
    72 				$styled_table = true;
       
    73 			}
    67 
    74 
    68 			$t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
    75 			$t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
       
    76 				( $styled_table ? '<div class="tblholder">' : '' ) .
    69 				'<table' . fixTagAttributes( $attributes, 'table' ) . '>' ;
    77 				'<table' . fixTagAttributes( $attributes, 'table' ) . '>' ;
    70 			array_push ( $td , false ) ;
    78 			array_push ( $td , false ) ;
    71 			array_push ( $ltd , '' ) ;
    79 			array_push ( $ltd , '' ) ;
    72 			array_push ( $tr , false ) ;
    80 			array_push ( $tr , false ) ;
    73 			array_push ( $ltr , '' ) ;
    81 			array_push ( $ltr , '' ) ;
    74 			array_push ( $has_opened_tr, false );
    82 			array_push ( $has_opened_tr, false );
    75 		}
    83 		}
    76 		else if ( count ( $td ) == 0 ) { } # Don't do any of the following
    84 		else if ( count ( $td ) == 0 ) { } # Don't do any of the following
    77 		else if ( '|}' == substr ( $x , 0 , 2 ) ) {
    85 		else if ( '|}' == substr ( $line , 0 , 2 ) ) {
    78 			$z = "</table>" . substr ( $x , 2);
    86 			$z = "</table>" . ( $styled_table ? '</div>' : '' ) . substr ( $line , 2);
    79 			$l = array_pop ( $ltd ) ;
    87 			$l = array_pop ( $ltd ) ;
    80 			if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
    88 			if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
    81 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    89 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    82 			if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    90 			if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
    83 			array_pop ( $ltr ) ;
    91 			array_pop ( $ltr ) ;
    84 			$t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
    92 			$t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
    85 		}
    93 		}
    86 		else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
    94 		else if ( '|-' == substr ( $line , 0 , 2 ) ) { # Allows for |---------------
    87 			$x = substr ( $x , 1 ) ;
    95 			$row_count++;
    88 			while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
    96 			$line = substr ( $line , 1 ) ;
       
    97 			while ( $line != '' && substr ( $line , 0 , 1 ) == '-' ) $line = substr ( $line , 1 ) ;
    89 			$z = '' ;
    98 			$z = '' ;
    90 			$l = array_pop ( $ltd ) ;
    99 			$l = array_pop ( $ltd ) ;
    91 			array_pop ( $has_opened_tr );
   100 			array_pop ( $has_opened_tr );
    92 			array_push ( $has_opened_tr , true ) ;
   101 			array_push ( $has_opened_tr , true ) ;
    93 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
   102 			if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
    95 			array_pop ( $ltr ) ;
   104 			array_pop ( $ltr ) ;
    96 			$t[$k] = $z ;
   105 			$t[$k] = $z ;
    97 			array_push ( $tr , false ) ;
   106 			array_push ( $tr , false ) ;
    98 			array_push ( $td , false ) ;
   107 			array_push ( $td , false ) ;
    99 			array_push ( $ltd , '' ) ;
   108 			array_push ( $ltd , '' ) ;
   100 			$attributes = unstripForHTML( $x );
   109 			$attributes = unstripForHTML( $line );
   101 			array_push ( $ltr , fixTagAttributes( $attributes, 'tr' ) ) ;
   110 			array_push ( $ltr , fixTagAttributes( $attributes, 'tr' ) ) ;
   102 		}
   111 		}
   103 		else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
   112 		else if ( '|' == $first_char || '!' == $first_char || '|+' == substr ( $line , 0 , 2 ) ) { # Caption
   104 			# $x is a table row
   113 			# $line is a table row
   105 			if ( '|+' == substr ( $x , 0 , 2 ) ) {
   114 			if ( '|+' == substr ( $line , 0 , 2 ) ) {
   106 				$fc = '+' ;
   115 				$first_char = '+' ;
   107 				$x = substr ( $x , 1 ) ;
   116 				$line = substr ( $line , 1 ) ;
   108 			}
   117 			}
   109 			$after = substr ( $x , 1 ) ;
   118 			$after = substr ( $line , 1 ) ;
   110 			if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
   119 			if ( $first_char == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
   111 
   120 
   112 			// Split up multiple cells on the same line.
   121 			// Split up multiple cells on the same line.
   113 			// FIXME: This can result in improper nesting of tags processed
   122 			// FIXME: This can result in improper nesting of tags processed
   114 			// by earlier parser steps, but should avoid splitting up eg
   123 			// by earlier parser steps, but should avoid splitting up eg
   115 			// attribute values containing literal "||".
   124 			// attribute values containing literal "||".
   119 
   128 
   120 			# Loop through each table cell
   129 			# Loop through each table cell
   121 			foreach ( $after AS $theline )
   130 			foreach ( $after AS $theline )
   122 			{
   131 			{
   123 				$z = '' ;
   132 				$z = '' ;
   124 				if ( $fc != '+' )
   133 				if ( $first_char != '+' )
   125 				{
   134 				{
   126 					$tra = array_pop ( $ltr ) ;
   135 					$tra = array_pop ( $ltr ) ;
   127 					if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
   136 					if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
   128 					array_push ( $tr , true ) ;
   137 					array_push ( $tr , true ) ;
   129 					array_push ( $ltr , '' ) ;
   138 					array_push ( $ltr , '' ) ;
   131 					array_push ( $has_opened_tr , true ) ;
   140 					array_push ( $has_opened_tr , true ) ;
   132 				}
   141 				}
   133 
   142 
   134 				$l = array_pop ( $ltd ) ;
   143 				$l = array_pop ( $ltd ) ;
   135 				if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
   144 				if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
   136 				if ( $fc == '|' ) $l = 'td' ;
   145 				if ( $first_char == '|' ) $l = 'td' ;
   137 				else if ( $fc == '!' ) $l = 'th' ;
   146 				else if ( $first_char == '!' ) $l = 'th' ;
   138 				else if ( $fc == '+' ) $l = 'caption' ;
   147 				else if ( $first_char == '+' ) $l = 'caption' ;
   139 				else $l = '' ;
   148 				else $l = '' ;
   140 				array_push ( $ltd , $l ) ;
   149 				array_push ( $ltd , $l ) ;
   141 
   150 
   142 				# Cell parameters
   151 				# Cell parameters
   143 				$y = explode ( '|' , $theline , 2 ) ;
   152 				$y = explode ( '|' , $theline , 2 ) ;
   144 				# Note that a '|' inside an invalid link should not
   153 				# Note that a '|' inside an invalid link should not
   145 				# be mistaken as delimiting cell parameters
   154 				# be mistaken as delimiting cell parameters
   146 				if ( strpos( $y[0], '[[' ) !== false ) {
   155 				if ( strpos( $y[0], '[[' ) !== false ) {
   147 					$y = array ($theline);
   156 					$y = array ($theline);
   148 				}
   157 				}
       
   158 				$attr_append = '';
       
   159 				if ( $styled_table && $l == 'td' )
       
   160 				{
       
   161 					$rowclass = 1 + ($row_count % 2);
       
   162 					$attr_append .= ' class="row' . $rowclass . '"';
       
   163 				}
   149 				if ( count ( $y ) == 1 )
   164 				if ( count ( $y ) == 1 )
   150 					$y = "{$z}<{$l}>{$y[0]}" ;
   165 				{
   151 				else {
   166 					$y = "{$z}<{$l}{$attr_append}>{$y[0]}" ;
       
   167 				}
       
   168 				else
       
   169 				{
   152 					$attributes = unstripForHTML( $y[0] );
   170 					$attributes = unstripForHTML( $y[0] );
       
   171 					if ( !strstr($attributes, "class=") )
       
   172 						$attributes .= $attr_append;
   153 					$y = "{$z}<{$l}".fixTagAttributes($attributes, $l).">{$y[1]}" ;
   173 					$y = "{$z}<{$l}".fixTagAttributes($attributes, $l).">{$y[1]}" ;
   154 				}
   174 				}
   155 				$t[$k] .= $y ;
   175 				$t[$k] .= $y ;
   156 				array_push ( $td , true ) ;
   176 				array_push ( $td , true ) ;
   157 			}
   177 			}