Difference between revisions of "Document.php"
(revert back to yesterday, somethings broken) |
|||
Line 7: | Line 7: | ||
# On first document.php execution, | # On first document.php execution, | ||
if ( !isset($GLOBALS[$tTitle]) ) { | if ( !isset($GLOBALS[$tTitle]) ) { | ||
− | $GLOBALS[ | + | |
+ | $GLOBALS['document.php'] = true; | ||
+ | |||
# Keep track of include depth for outline numbering | # Keep track of include depth for outline numbering | ||
$GLOBALS['incDepth'] = ''; | $GLOBALS['incDepth'] = ''; | ||
+ | |||
# Add this transform and document.css to the view stack | # Add this transform and document.css to the view stack | ||
− | xwSetProperty( $properties, 'xpath:/properties:view', $tTitle ); | + | xwSetProperty($properties, 'xpath:/properties:view', $tTitle); |
− | xwSetProperty( $properties, 'xpath:/properties:view', 'document.css' ); | + | xwSetProperty($properties, 'xpath:/properties:view', 'document.css'); |
# Declare callback funtion for replacing embeds [[+link]] and outline-number pre-processing | # Declare callback funtion for replacing embeds [[+link]] and outline-number pre-processing | ||
− | function | + | function replace_embed( $matches ) { |
list(, $num, $operator, $embedTitle, $pipe, $anchor ) = $matches; | list(, $num, $operator, $embedTitle, $pipe, $anchor ) = $matches; | ||
$oldDepth = $GLOBALS['incDepth']; | $oldDepth = $GLOBALS['incDepth']; | ||
Line 21: | Line 24: | ||
# Get embed-article content & properties | # Get embed-article content & properties | ||
$embed = xwArticleContent( $embedTitle ); | $embed = xwArticleContent( $embedTitle ); | ||
− | $embedProperties = | + | $embedProperties = xwArticleProperties( $embedTitle ); |
# If embedding, deal with heading, else split pipe list | # If embedding, deal with heading, else split pipe list | ||
− | if ( $operator == '+' ) { if ( !$pipe ) $anchor = $embedTitle; } | + | if ( $operator == '+' ) { |
+ | # If theres no pipe, use the title as the anchor | ||
+ | if ( !$pipe ) $anchor = $embedTitle; | ||
+ | } | ||
else { | else { | ||
# Add transforms from pipe to embed's data-transform stack | # Add transforms from pipe to embed's data-transform stack | ||
− | foreach ( explode('|', $anchor ) as $transformTitle ) | + | foreach ( explode('|', $anchor ) as $transformTitle) |
− | xwSetProperty( $embedProperties, 'xpath:/properties:data', $transformTitle ); | + | xwSetProperty($embedProperties, 'xpath:/properties:data', $transformTitle); |
$anchor = ''; | $anchor = ''; | ||
} | } | ||
Line 44: | Line 50: | ||
# Declare callback function for replacing the tmp elements with final html heading | # Declare callback function for replacing the tmp elements with final html heading | ||
− | function | + | function outline_number( $matches ) { |
global $seq, $xwScript, $toc; | global $seq, $xwScript, $toc; | ||
list(, $num, $title, $anchor ) = $matches; | list(, $num, $title, $anchor ) = $matches; | ||
Line 76: | Line 82: | ||
# If called from data-list, do embeds, applies and outline-preparation | # If called from data-list, do embeds, applies and outline-preparation | ||
if ( $event == 'data' and $language == '' ) { | if ( $event == 'data' and $language == '' ) { | ||
− | # | + | |
− | + | # If article is currently an object, convert to XML before processing | |
− | + | xwUndomificateArticle( $article, $tTitle ); | |
− | + | ||
− | + | # Replace all embeds and applies with transformed content | |
− | + | $article = preg_replace_callback( '/(#*)\\[{2}([+*])(.*?)(\\|(.*?))?\\]{2}/s', 'replace_embed', $article ); | |
− | + | $article = preg_replace( '/^(#+)(.+?)$/m', '<h1><tmp num="'.$GLOBALS['incDepth'].'$1" title="" anchor="$2"/></h1>', $article ); | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
# If called from view-list, do outline numbering and edit links | # If called from view-list, do outline numbering and edit links | ||
if ( $event == 'view' and $language == '' ) { | if ( $event == 'view' and $language == '' ) { | ||
− | + | $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( '/<h1 ><tmp num="(.*?)" title="(.*?)" anchor="(.*?)" *\\/><\\/h1 >/ms', "outline_number", $article ); | |
− | + | # Tidy up source a bit | |
− | + | $article = str_replace( "\n</p>", "</p>\n", $article ); | |
− | + | ||
− | + | # Add table of contents | |
− | + | if ( count($GLOBALS['toc']) > 3 ) { | |
− | + | $tocHTML = '<table border="0" id="toc"><tr id="toctitle"><td align="center">'; | |
− | + | $tocHTML .= '<b>Table of contents</b> '; | |
− | + | $tocHTML .= '<script type="text/javascript">showTocToggle("show","hide")</script>'; | |
− | + | $tocHTML .= '</td></tr><tr id="tocinside"><td>'; | |
− | + | foreach ($GLOBALS['toc'] as $anchor) | |
− | + | $tocHTML .= "<div class=\"tocline\"><a href=\"#$anchor[0]\">$anchor[1]</a><br /></div>"; | |
− | + | $tocHTML .= '</td></tr></table>'; | |
− | + | $article = str_replace( '<!-- start content -->', "<!-- start content -->$tocHTML", $article ); | |
− | |||
} | } | ||
+ | |||
} | } | ||
− | |||
?> | ?> |
Revision as of 06:08, 6 March 2006
<?
- General structured document transform
- TODO: handle external embeds and applies
xwGetProperty( $properties, 'language', $language );
- On first document.php execution,
if ( !isset($GLOBALS[$tTitle]) ) {
$GLOBALS['document.php'] = true;
# Keep track of include depth for outline numbering $GLOBALS['incDepth'] = ;
# Add this transform and document.css to the view stack 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 replace_embed( $matches ) { list(, $num, $operator, $embedTitle, $pipe, $anchor ) = $matches; $oldDepth = $GLOBALS['incDepth']; $num = $GLOBALS['incDepth'] .= $num; # Get embed-article content & properties $embed = xwArticleContent( $embedTitle ); $embedProperties = xwArticleProperties( $embedTitle ); # If embedding, deal with heading, else split pipe list if ( $operator == '+' ) { # If theres no pipe, use the title as the anchor 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 = ; } # Get its properties and merge with default-properties xwMergeDOM( $embedProperties, $GLOBALS['xwDefaultProperties'], $embedTitle, 'DEFAULT-PROPERTIES' ); # 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 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 outline_number( $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
if ( $event == 'data' and $language == ) {
# If article is currently an object, convert to XML before processing xwUndomificateArticle( $article, $tTitle );
# Replace all embeds and applies with transformed content $article = preg_replace_callback( '/(#*)\\[{2}([+*])(.*?)(\\|(.*?))?\\]{2}/s', 'replace_embed', $article );
$article = preg_replace( '/^(#+)(.+?)$/m', '
<tmp num="'.$GLOBALS['incDepth'].'$1" title="" anchor="$2"/>
', $article );
}
- If called from view-list, do outline numbering and edit links
if ( $event == 'view' and $language == ) { $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', "outline_number", $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 ); }
} ?>