在元数据展示页面,根据mediawiki的表格格式添加表格支持

This commit is contained in:
wlx 2010-06-21 14:52:03 +00:00
parent af3a676625
commit a85e2c48a8
2 changed files with 181 additions and 2 deletions

View File

@ -562,6 +562,8 @@ class DataController extends Zend_Controller_Action
$id=$row->id; $id=$row->id;
$uuid=$row->uuid; $uuid=$row->uuid;
$this->view->metadata=$row; $this->view->metadata=$row;
//提前对表格进行预处理
$this->view->metadata->description=$this->parseTable($this->view->escape($row->description));
if (is_numeric($row->projection)) if (is_numeric($row->projection))
{ {
$sql="select proj4text from spatial_ref_sys where auth_srid=?"; $sql="select proj4text from spatial_ref_sys where auth_srid=?";
@ -1076,5 +1078,182 @@ class DataController extends Zend_Controller_Action
} }
} }
/**
* parse the wiki syntax used to render tables, code 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( trim($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 ) ;
// special case: don't return empty table
if( $output == "<table>\n<tr><td></td></tr>\n</table>" ) {
$output = '';
}
return $output;
}
} }

View File

@ -144,7 +144,7 @@ endforeach;
<div id='map'></div> <div id='map'></div>
<div id="abstract"> <div id="abstract">
<p> <p>
<?php echo str_replace(array("\r\n", "\n", "\r"),'</p><p>',$this->escape($md->description));?> <?php echo str_replace(array("\r\n", "\n", "\r"),'</p><p>',$md->description);?>
</p> </p>
</div> </div>
<hr /> <hr />