Location: PHPKode > projects > ProNuke. Postnuke To Oracle > postnuke/html/modules/NS-Wiki/transform.inc.php
<?php 
// rcs_id('$Id: transform.inc.php,v 1.12 2001/11/26 12:55:36 voll Exp $');
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Adaptation to bbCode: Patrick Kellum <hide@address.com>
// Copyright (c) 2001 Patrick Kellum (hide@address.com)
// http://quahog-library.com/
//
// Adaptation to WIKI: Sebastien Bernard <hide@address.com>

// ----------------------------------------------------------------------

// I extracted the functions pn_bbencode() and pn_bbdecode() from the
// modules/Messages/functions.php
include('modules/Messages/bbcode.lib.php');

function transform($text, $type = '' ) {
global $cDefaultTypoCode;
// if ($type=='') { 
// $type=substr($text,0,3); 
// }
switch($type) {
		case '_TW':
			$format = "wiki";
			break;
		case '_TB':
			$format = "bbcode";
			break;
		case '_TP':
			$format = "postnuke";
			break;
		default:
			$format = $cDefaultTypoCode;
			break;
	}

    switch($format) {
        case 'postnuke':
			return ($text);
			break;
			
		case 'wiki':
			$text = PNwiki2html($text);		
			return ($text);

		case 'bbcode':	
			// pn_bbencode is in modules/Messages/functions.php
			$text = pn_bbencode($text);
            return ($text);
            break;
			
		default:	
			return ($text);
			break;
    }
}


/*************************** WIKI ****************************************/

function PNwikiInclude($retour) { 
$retour = preg_replace("|(%%%%)(.*?)(%%%%)|", "\\2",  $retour);
$retour = transform( $retour, $this->typeCoding );
$retour = "<table border=0 cellpadding=8 cellspacing=1 width=100%>
<tr><td align=left><font class=\"pn-normal\">" . $retour . "</td></tr></table>";
return $retour;
}

function PNwikiTokenize($str, $pattern, &$orig, &$ntokens) {
      global $PNFieldSeparator;
      // Find any strings in $str that match $pattern and
      // store them in $orig, replacing them with tokens
      // starting at number $ntokens - returns tokenized string
      $new = '';      
      while (preg_match("/^(.*?)($pattern)/", $str, $matches)) {
         $linktoken = $PNFieldSeparator . $PNFieldSeparator . ($ntokens++) . $PNFieldSeparator;
         $new .= $matches[1] . $linktoken;
		 $orig[] = $matches[2];
         $str = substr($str, strlen($matches[0]));
	      }
      $new .= $str;
      return $new;
   }

   // Loop over all lines of the page and apply transformation rules
function PNwiki2html( $cContent ) {
	global $PNFieldSeparator,$PNlWithHtml;

	if (strlen($cContent)==0)
		{ return($cContent); }

//	$cContent = str_replace("<br />","",$cContent);
	$aContent  = explode("\n", $cContent);

	
	$aContent = PNCookSpaces($aContent);

   // Loop over all lines of the page and apply transformation rules
   $numlines = count($aContent);
   for ($index = 0; $index < $numlines; $index++) {

      unset($tokens);
      unset($replacements);
      $ntokens = 0;
      $replacements = array();
      
//      $tmpline = stripslashes($aContent[$index]);
      $tmpline = $aContent[$index];

    if (!strlen($tmpline) || $tmpline == "\r") {
         // this is a blank line, send <p>
         $html .= PNSetHTMLOutputMode("p", ZERO_DEPTH, 0);
         continue;
      }

      elseif ($PNlWithHtml and preg_match("/(^\|)(.*)/", $tmpline, $matches)) {

         // HTML mode
         $html .= PNSetHTMLOutputMode("", ZERO_LEVEL, 0);
         $html .= $matches[2];
         continue;
      }

      //////////////////////////////////////////////////////////
      // New linking scheme: links are in brackets. This will
      // emulate typical HTML linking as well as Wiki linking.
	
      // First need to protect [[. 
      $oldn = $ntokens;
      $tmpline = PNwikiTokenize($tmpline, '\[\[', $replacements, $ntokens);
      while ($oldn < $ntokens)
         $replacements[$oldn++] = '[';

      // Now process the [\d+] links which are numeric references	
      $oldn = $ntokens;
      $tmpline = PNwikiTokenize($tmpline, '\[\s*\d+\s*\]', $replacements, $ntokens);
      while ($oldn < $ntokens) {
		 $num = (int) substr($replacements[$oldn], 1);
    	     if (! empty($embedded[$num]))
        	    $replacements[$oldn] = $embedded[$num];
			 $oldn++;
		      }
			  
      // match anything else between brackets 
      $oldn = $ntokens;
      $tmpline = PNwikiTokenize($tmpline, '\[.+?\]', $replacements, $ntokens);
      while ($oldn < $ntokens) {
		$link = PNParseAndLink($replacements[$oldn]);	
		$replacements[$oldn] = $link['link'];
		$oldn++;
    	  }

      //////////////////////////////////////////////////////////
      // replace all URL's with tokens, so we don't confuse them
      // with Wiki words later. Wiki words in URL's break things.
      // URLs preceeded by a '!' are not linked

		global $PNAllowedProtocols;

      $tmpline = PNwikiTokenize($tmpline, "!?\b($PNAllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]", $replacements, $ntokens);

      while ($oldn < $ntokens) {
        	if($replacements[$oldn][0] == '!')
	   			$replacements[$oldn] = substr($replacements[$oldn], 1);
			else
				$replacements[$oldn] = PNLinkURL($replacements[$oldn]);
	        $oldn++;
    	  }

      //////////////////////////////////////////////////////////
      // escape HTML metachars

      $tmpline = str_replace('&', '&amp;', $tmpline);
      $tmpline = str_replace('>', '&gt;', $tmpline);
      $tmpline = str_replace('<', '&lt;', $tmpline);

      // four or more dashes to <hr>
      $tmpline = ereg_replace("^-{4,}", '<hr noshade>', $tmpline);


// %%%% are image blocks
	if ( preg_match("|(%%%%)(.*?)(%%%%)|", $tmpline, $aContenu ))
		{ 	
		$retour = PNwikiInclude(	$aContenu[0] ); 
		$tmpline = $retour.preg_replace("|(%%%%)(.*?)(%%%%)|", "",  $tmpline);
		}

      // %%% are linebreaks
		$tmpline = str_replace('%%%','<br>',$tmpline);

      // bold italics (old way)
      $tmpline = preg_replace("|(''''')(.*?)(''''')|",
                              "<strong><em>\\2</em></strong>", $tmpline);

      // bold (old way)
      $tmpline = preg_replace("|(''')(.*?)(''')|",
                              "<strong>\\2</strong>", $tmpline);

      // italics (old ways)
      $tmpline = preg_replace("|('')(.*?)('')|",
                              "<em>\\2</em>", $tmpline);
							  
      // bold
      $tmpline = preg_replace("|(___)(.*?)(___)|",
                              "<strong>\\2</strong>", $tmpline);

      // italics
      $tmpline = preg_replace("|(__)(.*?)(__)|",
                              "<em>\\2</em>", $tmpline);

      // bold italics 
      $tmpline = preg_replace("|(_____)(.*?)(_____)|", "<strong><em>\\2</em></strong>", $tmpline);

      // center
      $tmpline = preg_replace("|(---)(.*?)(---)|", "<center>\\2</center>", $tmpline);

    	// tag <PUB>
//   	$tmpline = str_replace("<PUB>",  impHtml() , $tmpline );
							  
      //////////////////////////////////////////////////////////
      // unordered, ordered, and dictionary list  (using TAB)

      if (preg_match("/(^\t+)(.*?)(:\t)(.*$)/", $tmpline, $matches)) {
         // this is a dictionary list (<dl>) item
         $numtabs = strlen($matches[1]);
         $html .= PNSetHTMLOutputMode('dl', NESTED_LEVEL, $numtabs);
		 $tmpline = '';
		 if(trim($matches[2]))
            $tmpline = '<dt>' . $matches[2];
		 $tmpline .= '<dd>' . $matches[4];

      } elseif (preg_match("/(^\t+)(\*|\d+|#)/", $tmpline, $matches)) {
         // this is part of a list (<ul>, <ol>)
         $numtabs = strlen($matches[1]);
         if ($matches[2] == '*') {
            $listtag = 'ul';
         } else {
            $listtag = 'ol'; // a rather tacit assumption. oh well.
         }
         $tmpline = preg_replace("/^(\t+)(\*|\d+|#)/", "", $tmpline);
         $html .= PNSetHTMLOutputMode($listtag, NESTED_LEVEL, $numtabs);
         $html .= '<li>';


      //////////////////////////////////////////////////////////
      // tabless markup for unordered, ordered, and dictionary lists
      // ul/ol list types can be mixed, so we only look at the last
      // character. Changes e.g. from "**#*" to "###*" go unnoticed.
      // and wouldn't make a difference to the HTML layout anyway.

      // unordered lists <UL>: "*"
      } elseif (preg_match("/^([#*]*\*)[^#]/", $tmpline, $matches)) {
         // this is part of an unordered list
         $numtabs = strlen($matches[1]);
         $tmpline = preg_replace("/^([#*]*\*)/", '', $tmpline);
         $html .= PNSetHTMLOutputMode('ul', NESTED_LEVEL, $numtabs);
         $html .= '<li>';

      // ordered lists <OL>: "#"
      } elseif (preg_match("/^([#*]*\#)/", $tmpline, $matches)) {
         // this is part of an ordered list
         $numtabs = strlen($matches[1]);
         $tmpline = preg_replace("/^([#*]*\#)/", "", $tmpline);
         $html .= PNSetHTMLOutputMode('ol', NESTED_LEVEL, $numtabs);
         $html .= '<li>';

      // definition lists <DL>: ";text:text"
      } elseif (preg_match("/(^;+)(.*?):(.*$)/", $tmpline, $matches)) {
         // this is a dictionary list item
         $numtabs = strlen($matches[1]);
         $html .= PNSetHTMLOutputMode('dl', NESTED_LEVEL, $numtabs);
		 $tmpline = '';
		 if(trim($matches[2]))
            $tmpline = '<dt>' . $matches[2];
		 $tmpline .= '<dd>' . $matches[3];


      //////////////////////////////////////////////////////////
      // remaining modes: preformatted text, headings, normal text	

      } elseif (preg_match("/^\s+/", $tmpline)) {
         // this is preformatted text, i.e. <pre>
         $html .= PNSetHTMLOutputMode('pre', ZERO_LEVEL, 0);

      } elseif (preg_match("/^(!{1,3})[^!]/", $tmpline, $whichheading)) {
	 // lines starting with !,!!,!!! are headings
		 if($whichheading[1] == '!') $heading = 'h3';
		 elseif($whichheading[1] == '!!') $heading = 'h2';
		 elseif($whichheading[1] == '!!!') $heading = 'h1';
		 $tmpline = preg_replace("/^!+/", '', $tmpline);
		 $html .= PNSetHTMLOutputMode($heading, ZERO_LEVEL, 0);

      } else {
         // it's ordinary output if nothing else
         $html .= PNSetHTMLOutputMode('', ZERO_LEVEL, 0);
      }

      ///////////////////////////////////////////////////////
      // Replace tokens
      for ($i = 0; $i < $ntokens; $i++)
	  	$tmpline = str_replace($PNFieldSeparator.$PNFieldSeparator.$i.$PNFieldSeparator, $replacements[$i], $tmpline);

      $html .= $tmpline . "\n";
	}	  
   $html .= PNSetHTMLOutputMode('', ZERO_LEVEL, 0);

   return($html );
   }
?>
Return current item: ProNuke. Postnuke To Oracle