Document.php

From Organic Design wiki
Revision as of 19:47, 4 March 2006 by Nad (talk | contribs) (Render TOC only for >3 entries like MW TOC)

<?

  1. General structured document transform
  2. TODO: handle external embeds and applies

xwGetProperty($properties, 'language', $language);

  1. On first document.php execution,

if (!isset($GLOBALS['document.php'])) {

$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', 'document.php'); 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) { $anchor = join('.', $onum)."  $anchor"; if ($depth && $depth < 3) $toc[] = array(urlencode($anchor), $depth > 1 ? "     $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"; }

}

  1. 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, 'document.php');

# 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);

}

  1. 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); }

}

?>