<?php

	## This script adds teaser markup. A line beginning with 'T:WikiWord'
	## gets converted to ':WikiWord:first paragraph of WikiWord'.
	## Lines beginning with T* or T# get converted to list items.
	##
	## [[title:WikiWord]] gets converted to 'first paragraph'
	##
	## and WikiWord#Anchor retrieves a named paragraph
	##
	## Copyright 2002, 2003 John Rankin (john.rankin@affinity.co.nz)
	## Edited by Matthew Shaylor to have UpToAnchor... functionality

	$IdPattern = "[A-Za-z][-.:_A-Za-z0-9]*";
	$PageNamePattern = "($GroupNamePattern)([.\\/])($PageTitlePattern)";
	$GroupLinkPattern = "(($GroupNamePattern)([.\\/]))?($FreeLinkPattern)";
	$PageLinkPattern = 
	"($PageNamePattern)|($GroupLinkPattern)|($WikiWordPattern)";

	$DoubleBrackets["/^T([:*#])((\\[\\[)?($PageLinkPattern)((?:\\s.*?)\\]\\])?)/e"] 
	= 'TeaserText("$1","$2");';
	$DoubleBrackets["/\\[\\[title:($PageLinkPattern)\\]\\]/e"]
	= 'FirstParagraph("$1");';
	$DoubleBrackets["/\\[\\[para:($PageLinkPattern)\\]\\]/e"]
	= 'FirstParagraph("$1");';
	$DoubleBrackets["/\\[\\[para:(($PageLinkPattern)#($IdPattern))\\]\\]/e"]
	= 'AnchorParagraph("$1");';
	$DoubleBrackets["/\\[\\[topara:(($PageLinkPattern)#($IdPattern))\\]\\]/e"]
	= 'UpToAnchorParagraph("$1");';

	function TeaserText($markup,$word) {
		global $PageLinkPattern;
		if (preg_match("/^\\[\\[($PageLinkPattern)/",$word,$match)) {
			$pg = $match[1];
			$word = stripslashes($word);
		} else {
			$pg = $word;
		}
		if ($markup == ":") return ":$word:" . FirstParagraph($pg);
		else
		return "$markup $word: " . FirstParagraph($pg);
	}

	function FirstParagraph($word) {
		global $pagename,$PageLinkPattern,$FreeLinkPattern,$PageTitlePattern;

		#calculate the teaser string...
		$teasername = preg_replace("/($FreeLinkPattern)/e",'Wikify("$1")',$word);
		if (preg_match("/^($PageTitlePattern)$/",$teasername))
		$teasername = FmtPageName("\$Group.$teasername",$pagename);

		$teaserpage = ReadPage($teasername);

		if ($teaserpage) { #the page exists and has content...
			$teasergroup = FmtPageName('$Group',$teasername);
			$teaserpage['text'] .= "\n"; #trailing \n just in case...

			$firstpara = substr($teaserpage['text'],0,strpos($teaserpage['text'],"\n")); #first para is everything up to the 1st \n

			$firstpara = preg_replace("/^[#*!]+\s*/","",$firstpara);
			$firstpara = preg_replace("/^:.*?:/","",$firstpara);
			$firstpara = preg_replace("/\\[\\[#.*?\\]\\]/","",$firstpara);
			$firstpara = preg_replace("/($PageLinkPattern)/e", 
			'QualifyLink($teasergroup,"$1")', $firstpara);
		}
		return $firstpara;
	}

	function UpToAnchorParagraph($wordanchor) {
		#Hacked by Matthew Shaylor for including all news items up to "[[#oldnews]]" or whatever...
		global $pagename,$PageLinkPattern,$FreeLinkPattern,$PageTitlePattern;

		#calculate the teaser string if necessary.
		$teasername= preg_replace("/#.*$/","",$wordanchor);
		$teasername= preg_replace("/($FreeLinkPattern)/e",'Wikify("$1")',$teasername);
		if (preg_match("/^($PageTitlePattern)$/",$teasername))
		$teasername = FmtPageName("\$Group.$teasername",$pagename);

		#and the anchor...
		$anchor = preg_replace("/^.*#/","",$wordanchor);

		$teaserpage = ReadPage($teasername);

		if ($teaserpage) {
			$teasergroup = FmtPageName('$Group',$teasername);
			$teaserpage['text'] .= "\n"; #trailing \n just incase...

			if (preg_match("/(.*?)\\n\\[\\[#$anchor\\]\\]/ms", $teaserpage['text'], $match)) {
				$para = $match[1];
			} else {
				$para = "'''$anchor''' not found in $teasername.";
			}

			#MDS: some of these are a bit conservative.  If included pages miss on some features then try here...
			$firstpara = preg_replace("/^[#*!]+\s*/","",$firstpara); #remove strings of special chars
			$firstpara = preg_replace("/^:.*?:/","",$firstpara); #Remove :stuff: at the begining of the line
			$firstpara = preg_replace("/\\[\\[#.*?\\]\\]/","",$firstpara); #remove anchors
			$firstpara = preg_replace("/\\[\\[topara.*?\\]\\]/","",$firstpara); #recursion is bad.
			$firstpara = preg_replace("/($PageLinkPattern)/e", 
			  'QualifyLink($teasergroup,"$1")', $firstpara); #Do something clever with links and groups
		}
		return PrintText($pagename, $para); #format wikiwise


	}

	function AnchorParagraph($wordanchor) {
		global $pagename,$PageLinkPattern,$FreeLinkPattern,$PageTitlePattern;

		#calculate the teaser string if necessary.
		$teasername= preg_replace("/#.*$/","",$wordanchor);
		$teasername= preg_replace("/($FreeLinkPattern)/e",'Wikify("$1")',$teasername);
		if (preg_match("/^($PageTitlePattern)$/",$teasername))
		$teasername = FmtPageName("\$Group.$teasername",$pagename);

		#and the anchor...
		$anchor = preg_replace("/^.*#/","",$wordanchor);

		$teaserpage = ReadPage($teasername);

		if ($teaserpage) {
			$teasergroup = FmtPageName('$Group',$teasername);
			$teaserpage['text'] .= "\n"; #trailing \n just incase...

			if (preg_match("/\\[\\[#$anchor\\]\\]\\n?(.*?)\\n/", $teaserpage['text'], $match)) {
				$para = $match[1];
			} else {
				$para = "'''$anchor''' not found in $teasername.";
			}

			$para = preg_replace("/^[#*!]+\s*/","",$para);
			$para = preg_replace("/^:.*?:/","",$para);
			$para = preg_replace("/($PageLinkPattern)/e",
			'QualifyLink($teasergroup,"$1")', $para);
		}
		return $para;
	}

	function Wikify($word) {
		global $PageNameSpace;
		$linktext = preg_replace("/{{(.*)}}.*/","$1",str_replace("|","",$word));
		return preg_replace("/\s+/",$PageNameSpace,ucwords($linktext));
	}

	function QualifyLink($thisgroup,$link) {
		if (preg_match("/[.\\/]/",$link)) { return $link; }
		else { return "$thisgroup/$link"; }
	}

	?>