Document.php
<?
- General structured document transform
- TODO: handle external embeds and applies
xwGetProperty( $properties, 'language', $language );
- On first document.php execution,
if ( !isset( $GLOBALS[$tTitle] ) ) { # Keep track of include depth for outline numbering $GLOBALS['incDepth'] = ; # Add this transform and document.css to the view and data stack xwSetProperty( $properties, 'xpath:/properties:data', $tTitle ); xwSetProperty( $properties, 'xpath:/properties:view', $tTitle ); xwSetProperty( $properties, 'xpath:/properties:view', 'document.css' );
# Declare callback funtion for replacing embeds +link and outline-number pre-processing function xwRE( $matches ) { list(, $num, $operator, $embedTitle, $pipe, $anchor ) = $matches; $oldDepth = $GLOBALS['incDepth']; $num = $GLOBALS['incDepth'] .= $num; # Get embed-article content & properties (merged over defaults) $embed = xwArticleContent( $embedTitle ); $embedProperties = $GLOBALS['xwDefaultPropertiesXML']; xwDomificateArticle( str_replace( 'document.php', , $embedProperties ) ); xwMergeDOM( $embedProperties, xwArticleProperties( $embedTitle, false ) ); # If embedding, deal with heading, else split pipe list if ( $operator == '+' ) { if ( !$pipe ) $anchor = $embedTitle; } else { # Add transforms from pipe to embed's data-transform stack foreach ( explode('|', $anchor ) as $transformTitle ) xwSetProperty( $embedProperties, 'xpath:/properties:data', $transformTitle ); $anchor = ; } # If no language specified in properties, guess from name and content xwGetProperty( $embedProperties, 'language', $embedLang ); if (!$embedLang) xwSetProperty( $embedProperties, 'language', xwArticleType( $embedTitle, $embed ) ); # Transform the content before embedding it xwSetProperty( $embedProperties, 'xpath:/properties:data', 'document.php' ); xwReduceTransformStack( $embed, $embedProperties, $embedTitle, 'data', "EMBED$operator" ); $GLOBALS['incDepth'] = $oldDepth; # If there's a heading, surround it in an element for view-transform to match
if ( $anchor ) $embed = "
<tmp num=\"$num\" title=\"$embedTitle\" anchor=\"$anchor\"/>
\n$embed";
return $embed; }
# Declare callback function for replacing the tmp elements with final html heading function xwOL( $matches ) { global $seq, $xwScript, $toc; list(, $num, $title, $anchor ) = $matches; # Calculate outline numbering string $depth = strlen( $num ); $onum = array(); for ( $i = 1; $i <= $depth; $i++ ) $onum[$i] = $i < $depth ? $seq[$i] : ++$seq[$i] + ($seq[$i+1] = 0); if ( count($onum) == 1 ) array_push( $onum, ); # Add numbering and heading if there's an anchor if ( $anchor ) { if ( $depth ) { $s = ' '; $anchor = join( '.', $onum )."$s$s$anchor"; if ( $depth && $depth < 3 ) $toc[] = array( urlencode($anchor), $depth > 1 ? "$s$s$s$s$s$anchor" : "$anchor" ); } else $depth = 1; $heading = "<h$depth class=\"document\">$anchor</h$depth>"; # Make the heading into an edit link to the source if there's an article title if ( $title ) { $title = str_replace( '&', '%26', $title ); $heading = "<a class=\"document\" href=\"$xwScript?title=$title&action=edit\">$heading</a>"; } if ( $anchor ) $heading = '<a name="'.urlencode($anchor)."\"></a>$heading"; } return "\n$heading"; }
}
- If called from data-list, do embeds, applies and outline-preparation
- - doesn't run on first data call (ie pushed to end of data-stack on first data call)
if ( $GLOBALS[$tTitle] and $event == 'data' and $language == ) { # Render next level or bail out if too deep if ( ++$GLOBALS[$tTitle] < 10 ) { xwUndomificateArticle( $article, $tTitle ); # Replace all embeds and applies with transformed content $article = preg_replace_callback( '/(#*)\\[{2}([+*])(.*?)(\\|(.*?))?\\]{2}/s', 'xwRE', $article );
$article = preg_replace( '/^(#+)(.+?)$/m', '
<tmp num="'.$GLOBALS['incDepth'].'$1" title="" anchor="$2"/>
', $article );
} else $GLOBALS['tooDeep'] = true; --$GLOBALS[$tTitle]; # Clear article content if bailed if ( $GLOBALS[$tTitle] == 1 and $GLOBALS['tooDeep'] ) { xwMessage( 'Structure too deep! (probably circular)', 'red' ); $article = '(Content could not be rendered)'; } }
- If called from view-list, do outline numbering and edit links
if ( $event == 'view' and $language == ) { if ( $GLOBALS[$tTitle] < 10 ) { $GLOBALS['seq'] = array( 0,0,0,0,0,0,0,0,0,0 ); $GLOBALS['toc'] = array(); # Apply view transforms to the tmp elements generated by data iteration
$article = preg_replace_callback( '/
<tmp num="(.*?)" title="(.*?)" anchor="(.*?)" *\\/><\\/h1 >/ms', 'xwOL', $article ); # Tidy up source a bit $article = str_replace( "\n
", "\n", $article );# Add table of contents if ( count($GLOBALS['toc']) > 3 ) {
$tocHTML = '';
$tocHTML .= 'Table of contents '; $tocHTML .= '<script type="text/javascript">showTocToggle("show","hide")</script>'; $tocHTML .= ' |
';
foreach ( $GLOBALS['toc'] as $anchor ) $tocHTML .= "<a href=\"#$anchor[0]\">$anchor[1]</a> ";
$tocHTML .= ' |
$article = str_replace( , "$tocHTML", $article ); } } }
- Ensure declarations don't occur again
if ( !isset( $GLOBALS[$tTitle] ) ) $GLOBALS[$tTitle] = 1; ?>