Personal tools


Document.php

From OrganicDesign Wiki

Jump to: navigation, search
Image:legacy.png
Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.


<?
# 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 );
 
	# 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)
		$apply = $operator == '*';
		$embed = ($apply && !$pipe) ? '' : $embedTitle ? xwArticleContent($embedTitle) : '';
		$embedProperties = str_replace('categories.php','',$GLOBALS['xwDefaultPropertiesXML']);
		xwDomificateArticle($embedProperties);
		if ($embedTitle) xwMergeDOM($embedProperties, xwArticleProperties($embedTitle, false));
		# If embedding, deal with heading, else split pipe list
		if ($apply) {
			# Add transforms from pipe to embed's data-transform stack
			$transforms = $pipe ? explode('|', $anchor) : array($embedTitle);
			foreach ($transforms as $transformTitle)
				xwSetProperty($embedProperties, 'xpath:/properties:data', $transformTitle);
			$anchor = '';
			}
		else if (!$pipe) $anchor = $embedTitle;
		# 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 = "<h1><tmp num=\"$num\" title=\"$embedTitle\" anchor=\"$anchor\"/></h1>\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 = '&nbsp;';
				$anchor = join( '.', $onum )."$s$s$anchor";
				if ( $depth && $depth < 3 )
					$toc[] = array( urlencode($anchor), $depth > 1 ? "$s$s$s$s$s$anchor" : "<b>$anchor</b>" );
				} 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( '&amp;', '%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', '<h1><tmp num="'.$GLOBALS['incDepth'].'$1" title="" anchor="$2"/></h1>', $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( '/<h1 ><tmp num="(.*?)" title="(.*?)" anchor="(.*?)" *\\/><\\/h1 >/ms', 'xwOL', $article );
		# Tidy up source a bit
		$article = str_replace( "\n</p>", "</p>\n", $article );
		# Add table of contents
		if ((count($GLOBALS['toc'])>3) && !ereg('__NOTOC__',$article)) {
			$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 );
			}
		}
	}
 
# Ensure declarations don't occur again
if ( !isset( $GLOBALS[$tTitle] ) ) $GLOBALS[$tTitle] = 1;
?>

The GNU Project Debian Linux Ubuntu Linux Wikipedia online encycopedia MediaWiki