use WikiFormat to deal with the table format
This commit is contained in:
parent
38d501b74d
commit
ba1898695d
|
@ -189,7 +189,7 @@ class DataController extends Zend_Controller_Action
|
|||
$row=$state->fetchAll();
|
||||
$sum=$row[0]['count'];
|
||||
$select=$db->select();
|
||||
$select->from('metadata','*')->order('title')->limitPage($page,$limit);
|
||||
$select->from('metadata','*')->order('id desc')->limitPage($page,$limit);
|
||||
$this->view->metadata = $db->fetchAll($select);
|
||||
$this->view->page=new Pagination($sum,$page,$limit);
|
||||
}
|
||||
|
@ -1390,7 +1390,8 @@ class DataController extends Zend_Controller_Action
|
|||
$dom = new DOMDocument();
|
||||
$dom->loadXML($row['data']);
|
||||
//提前对表格进行预处理
|
||||
$abs=$this->parseTable($this->view->escape($row["description"]));
|
||||
$wiki=new WikiFormat();
|
||||
$abs=$wiki->parseTable($this->view->escape($row["description"]));
|
||||
//处理外部链接
|
||||
$abs=preg_replace('/\[\s*(http:\/\/.+?)\s+(.*?)\]/m','<a href="$1">$2</a>',$abs);
|
||||
$abs=str_replace(array("\r\n", "\n", "\r"),'</p><p>',$abs);
|
||||
|
@ -1440,190 +1441,5 @@ class DataController extends Zend_Controller_Action
|
|||
->setHeader('Pragma','public')
|
||||
->setBody($content);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* parse the wiki syntax used to render tables, code modified from mediawiki
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function parseTable ( $text ) {
|
||||
$lines = explode ( "\n" , $text );
|
||||
$td_history = array (); // Is currently a td tag open?
|
||||
$last_tag_history = array (); // Save history of last lag activated (td, th or caption)
|
||||
$tr_history = array (); // Is currently a tr tag open?
|
||||
$tr_attributes = array (); // history of tr attributes
|
||||
$has_opened_tr = array(); // Did this table open a <tr> 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( '<dl><dd>' , $indent_level ) . "<table{$attributes}>";
|
||||
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 = '</table>' . substr ( $line , 2 );
|
||||
$last_tag = array_pop ( $last_tag_history );
|
||||
|
||||
if ( !array_pop ( $has_opened_tr ) ) {
|
||||
$line = "<tr><td></td></tr>{$line}";
|
||||
}
|
||||
|
||||
if ( array_pop ( $tr_history ) ) {
|
||||
$line = "</tr>{$line}";
|
||||
}
|
||||
|
||||
if ( array_pop ( $td_history ) ) {
|
||||
$line = "</{$last_tag}>{$line}";
|
||||
}
|
||||
array_pop ( $tr_attributes );
|
||||
$lines[$key] = $line . str_repeat( '</dd></dl>' , $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 = '</tr>';
|
||||
}
|
||||
|
||||
if ( array_pop ( $td_history ) ) {
|
||||
$line = "</{$last_tag}>{$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 = "<tr{$tr_after}>\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 = "</{$last_tag}>{$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[] = '</td>' ;
|
||||
}
|
||||
if ( array_pop ( $tr_history ) ) {
|
||||
$lines[] = '</tr>' ;
|
||||
}
|
||||
if ( !array_pop ( $has_opened_tr ) ) {
|
||||
$lines[] = "<tr><td></td></tr>" ;
|
||||
}
|
||||
|
||||
$lines[] = '</table>' ;
|
||||
}
|
||||
|
||||
$output = implode ( "\n" , $lines ) ;
|
||||
|
||||
//@todo:移除<table></table>中所有的换行符
|
||||
//可能存在问题
|
||||
$output=preg_replace('/([a-zA-Z])>\n/m','$1>',$output);
|
||||
$output=preg_replace('/\n<\//m','</',$output);
|
||||
|
||||
// special case: don't return empty table
|
||||
if( $output == "<table>\n<tr><td></td></tr>\n</table>" ) {
|
||||
$output = '';
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue