Difference between revisions of "Document.php"
(Render TOC only for >3 entries like MW TOC) |
(Tidy up) |
||
Line 3: | Line 3: | ||
# TODO: handle external embeds and applies | # TODO: handle external embeds and applies | ||
− | xwGetProperty($properties, 'language', $language); | + | xwGetProperty( $properties, 'language', $language ); |
# On first document.php execution, | # On first document.php execution, | ||
− | if (!isset($GLOBALS[ | + | if ( !isset($GLOBALS[$tTitle]) ) { |
$GLOBALS['document.php'] = true; | $GLOBALS['document.php'] = true; | ||
Line 14: | Line 14: | ||
# 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', | + | 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 replace_embed($matches) { | + | function replace_embed( $matches ) { |
− | list(, $num, $operator, $embedTitle, $pipe, $anchor) = $matches; | + | list(, $num, $operator, $embedTitle, $pipe, $anchor ) = $matches; |
$oldDepth = $GLOBALS['incDepth']; | $oldDepth = $GLOBALS['incDepth']; | ||
$num = $GLOBALS['incDepth'] .= $num; | $num = $GLOBALS['incDepth'] .= $num; | ||
# Get embed-article content & properties | # Get embed-article content & properties | ||
− | $embed = xwArticleContent($embedTitle); | + | $embed = xwArticleContent( $embedTitle ); |
− | $embedProperties = xwArticleProperties($embedTitle); | + | $embedProperties = xwArticleProperties( $embedTitle ); |
# If embedding, deal with heading, else split pipe list | # If embedding, deal with heading, else split pipe list | ||
− | if ($operator == '+') { | + | if ( $operator == '+' ) { |
# If theres no pipe, use the title as the anchor | # If theres no pipe, use the title as the anchor | ||
− | if (!$pipe) $anchor = $embedTitle; | + | 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 = ''; | ||
} | } | ||
# Get its properties and merge with default-properties | # Get its properties and merge with default-properties | ||
− | xwMergeDOM($embedProperties, $GLOBALS['xwDefaultProperties'], $embedTitle, 'DEFAULT-PROPERTIES'); | + | xwMergeDOM( $embedProperties, $GLOBALS['xwDefaultProperties'], $embedTitle, 'DEFAULT-PROPERTIES' ); |
# If no language specified in properties, guess from name and content | # If no language specified in properties, guess from name and content | ||
− | xwGetProperty($embedProperties, 'language', $embedLang); | + | xwGetProperty( $embedProperties, 'language', $embedLang ); |
− | if (!$embedLang) xwSetProperty($embedProperties, 'language', xwArticleType($embedTitle, $embed)); | + | if (!$embedLang) xwSetProperty( $embedProperties, 'language', xwArticleType( $embedTitle, $embed ) ); |
# Transform the content before embedding it | # Transform the content before embedding it | ||
− | xwReduceTransformStack($embed, $embedProperties, $embedTitle, 'data', "EMBED$operator"); | + | xwReduceTransformStack( $embed, $embedProperties, $embedTitle, 'data', "EMBED$operator" ); |
$GLOBALS['incDepth'] = $oldDepth; | $GLOBALS['incDepth'] = $oldDepth; | ||
# If there's a heading, surround it in an element for view-transform to match | # If there's a heading, surround it in an element for view-transform to match | ||
− | if ($anchor) $embed = "<h1><tmp num=\"$num\" title=\"$embedTitle\" anchor=\"$anchor\"/></h1>\n$embed"; | + | if ( $anchor ) $embed = "<h1><tmp num=\"$num\" title=\"$embedTitle\" anchor=\"$anchor\"/></h1>\n$embed"; |
return $embed; | return $embed; | ||
} | } | ||
# 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 outline_number($matches) { | + | function outline_number( $matches ) { |
global $seq, $xwScript, $toc; | global $seq, $xwScript, $toc; | ||
− | list(, $num, $title, $anchor) = $matches; | + | list(, $num, $title, $anchor ) = $matches; |
# Calculate outline numbering string | # Calculate outline numbering string | ||
− | $depth = strlen($num); | + | $depth = strlen( $num ); |
$onum = array(); | $onum = array(); | ||
− | for ($i = 1; $i <= $depth; $i++) | + | for ( $i = 1; $i <= $depth; $i++ ) |
$onum[$i] = $i < $depth ? $seq[$i] : ++$seq[$i] + ($seq[$i+1] = 0); | $onum[$i] = $i < $depth ? $seq[$i] : ++$seq[$i] + ($seq[$i+1] = 0); | ||
− | if (count($onum) == 1) array_push($onum, ''); | + | if ( count($onum) == 1 ) array_push( $onum, '' ); |
# Add numbering and heading if there's an anchor | # Add numbering and heading if there's an anchor | ||
− | if ($anchor) { | + | if ( $anchor ) { |
− | if ($depth) { | + | if ( $depth ) { |
− | $anchor = join('.', $onum)." | + | $s = ' '; |
− | if ($depth && $depth < 3) | + | $anchor = join( '.', $onum )."$s$s$anchor"; |
− | $toc[] = array(urlencode($anchor), $depth > 1 ? " | + | if ( $depth && $depth < 3 ) |
+ | $toc[] = array( urlencode($anchor), $depth > 1 ? "$s$s$s$s$s$anchor" : "<b>$anchor</b>" ); | ||
} else $depth = 1; | } else $depth = 1; | ||
$heading = "<h$depth class=\"document\">$anchor</h$depth>"; | $heading = "<h$depth class=\"document\">$anchor</h$depth>"; | ||
# Make the heading into an edit link to the source if there's an article title | # Make the heading into an edit link to the source if there's an article title | ||
− | if ($title) { | + | if ( $title ) { |
− | $title = str_replace('&','%26',$title); | + | $title = str_replace( '&', '%26', $title ); |
$heading = "<a class=\"document\" href=\"$xwScript?title=$title&action=edit\">$heading</a>"; | $heading = "<a class=\"document\" href=\"$xwScript?title=$title&action=edit\">$heading</a>"; | ||
} | } | ||
− | if ($anchor) $heading = '<a name="'.urlencode($anchor)."\"></a>$heading"; | + | if ( $anchor ) $heading = '<a name="'.urlencode($anchor)."\"></a>$heading"; |
} | } | ||
return "\n$heading"; | return "\n$heading"; | ||
Line 80: | Line 81: | ||
# 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 | # If article is currently an object, convert to XML before processing | ||
− | xwUndomificateArticle($article, | + | xwUndomificateArticle( $article, $tTitle ); |
# Replace all embeds and applies with transformed content | # Replace all embeds and applies with transformed content | ||
− | $article = preg_replace_callback('/(#*)\\[{2}([+*])(.*?)(\\|(.*?))?\\]{2}/s', 'replace_embed', $article); | + | $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); | + | $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['seq'] = array( 0,0,0,0,0,0,0,0,0,0 ); |
$GLOBALS['toc'] = array(); | $GLOBALS['toc'] = array(); | ||
# Apply view transforms to the tmp elements generated by data iteration | # 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); | + | $article = preg_replace_callback( '/<h1 ><tmp num="(.*?)" title="(.*?)" anchor="(.*?)" *\\/><\\/h1 >/ms', "outline_number", $article ); |
# Tidy up source a bit | # Tidy up source a bit | ||
− | $article = str_replace("\n</p>", "</p>\n", $article); | + | $article = str_replace( "\n</p>", "</p>\n", $article ); |
# Add table of contents | # Add table of contents | ||
Line 109: | Line 110: | ||
$tocHTML .= "<div class=\"tocline\"><a href=\"#$anchor[0]\">$anchor[1]</a><br /></div>"; | $tocHTML .= "<div class=\"tocline\"><a href=\"#$anchor[0]\">$anchor[1]</a><br /></div>"; | ||
$tocHTML .= '</td></tr></table>'; | $tocHTML .= '</td></tr></table>'; | ||
− | $article = str_replace('<!-- start content -->', "<!-- start content -->$tocHTML", $article); | + | $article = str_replace( '<!-- start content -->', "<!-- start content -->$tocHTML", $article ); |
} | } | ||
} | } | ||
− | |||
?> | ?> |
Revision as of 19:58, 4 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 ); }
} ?>