element? $indent_level = 0; // indent level of the table foreach ( $lines as $key => $line ) { $line = trim ( $line ); if( $line == '' ) { // empty line, go to next line continue; } $first_character = $line{0}; $matches = array(); if ( preg_match( '/^(:*)\{\|(.*)$/' , $line , $matches ) ) { // First check if we are starting a new table $indent_level = strlen( $matches[1] ); $lines[$key] = str_repeat( '
' , $indent_level ) . ""; array_push ( $td_history , false ); array_push ( $last_tag_history , '' ); array_push ( $tr_history , false ); array_push ( $tr_attributes , '' ); array_push ( $has_opened_tr , false ); } else if ( count ( $td_history ) == 0 ) { // Don't do any of the following continue; } else if ( substr ( $line , 0 , 2 ) == '|}' ) { // We are ending a table $line = '
' . substr ( $line , 2 ); $last_tag = array_pop ( $last_tag_history ); if ( !array_pop ( $has_opened_tr ) ) { $line = "{$line}"; } if ( array_pop ( $tr_history ) ) { $line = "{$line}"; } if ( array_pop ( $td_history ) ) { $line = "{$line}"; } array_pop ( $tr_attributes ); $lines[$key] = $line . str_repeat( '
' , $indent_level ); } else if ( substr ( $line , 0 , 2 ) == '|-' ) { // Now we have a table row $line = preg_replace( '#^\|-+#', '', $line ); $line = ''; $last_tag = array_pop ( $last_tag_history ); array_pop ( $has_opened_tr ); array_push ( $has_opened_tr , true ); if ( array_pop ( $tr_history ) ) { $line = ''; } if ( array_pop ( $td_history ) ) { $line = "{$line}"; } $lines[$key] = $line; array_push ( $tr_history , false ); array_push ( $td_history , false ); array_push ( $last_tag_history , '' ); } else if ( $first_character == '|' || $first_character == '!' || substr ( $line , 0 , 2 ) == '|+' ) { // This might be cell elements, td, th or captions if ( substr ( $line , 0 , 2 ) == '|+' ) { $first_character = '+'; $line = substr ( $line , 1 ); } $line = substr ( $line , 1 ); if ( $first_character == '!' ) { $line = str_replace ( '!!' , '||' , $line ); } // Split up multiple cells on the same line. // FIXME : This can result in improper nesting of tags processed // by earlier parser steps, but should avoid splitting up eg // attribute values containing literal "||". $cells = explode( '||' , $line ); $lines[$key] = ''; // Loop through each table cell foreach ( $cells as $cell ) { $previous = ''; if ( $first_character != '+' ) { $tr_after = array_pop ( $tr_attributes ); if ( !array_pop ( $tr_history ) ) { $previous = "\n"; } array_push ( $tr_history , true ); array_push ( $tr_attributes , '' ); array_pop ( $has_opened_tr ); array_push ( $has_opened_tr , true ); } $last_tag = array_pop ( $last_tag_history ); if ( array_pop ( $td_history ) ) { $previous = "{$previous}"; } if ( $first_character == '|' ) { $last_tag = 'td'; } else if ( $first_character == '!' ) { $last_tag = 'th'; } else if ( $first_character == '+' ) { $last_tag = 'caption'; } else { $last_tag = ''; } array_push ( $last_tag_history , $last_tag ); // A cell could contain both parameters and data $cell_data = explode ( '|' , $cell , 2 ); // Bug 553: Note that a '|' inside an invalid link should not // be mistaken as delimiting cell parameters if ( strpos( $cell_data[0], '[[' ) !== false ) { $cell = "{$previous}<{$last_tag}>{$cell}"; } else if ( count ( $cell_data ) == 1 ) $cell = "{$previous}<{$last_tag}>{$cell_data[0]}"; else { $cell = "{$previous}<{$last_tag}>{$cell_data[1]}"; } $lines[$key] .= $cell; array_push ( $td_history , true ); } } } // Closing open td, tr && table while ( count ( $td_history ) > 0 ) { if ( array_pop ( $td_history ) ) { $lines[] = '' ; } if ( array_pop ( $tr_history ) ) { $lines[] = '' ; } if ( !array_pop ( $has_opened_tr ) ) { $lines[] = "" ; } $lines[] = '' ; } $output = implode ( "\n" , $lines ) ; //@todo:移除
中所有的换行符 //可能存在问题 $output=preg_replace('/([a-zA-Z])>\n/m','$1>',$output); $output=preg_replace('/\n<\//m','\n\n" ) { $output = ''; } return $output; } }