Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php	(revision 538)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php	(revision 539)
@@ -357,239 +357,2 @@
     }
 }
-
-function wfSajaxSearchImageFCKeditor( $term )
-{
-    global $wgContLang, $wgOut;
-    $limit = 10;
-
-    $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
-    $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
-    $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
-    $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
-    $term = $term1;
-
-    if ( strlen( str_replace( '_', '', $term ) )<3 )
-    return "";
-
-    $db =& wfGetDB( DB_SLAVE );
-    $res = $db->select( 'page', 'page_title',
-    array(  'page_namespace' => NS_IMAGE,
-    "LCASE(page_title) LIKE '%". $db->strencode( $term2 ) ."%'" ),
-    "wfSajaxSearch",
-    array( 'LIMIT' => $limit+1 )
-    );
-
-    $ret = "";
-    $i=0;
-    while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {        
-        $ret .= $row->page_title ."\n";
-    }
-
-    $term = htmlspecialchars( $term );    
-    
-    return $ret;
-}
-
-function wfSajaxSearchArticleFCKeditor( $term )
-{
-    global $wgContLang, $wgOut;
-    $limit = 10;
-
-    $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
-    $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
-    $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
-    $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
-    $term = $term1;
-
-    if ( strlen( str_replace( '_', '', $term ) )<3 )
-    return "";
-
-    $db =& wfGetDB( DB_SLAVE );
-    $res = $db->select( 'page', 'page_title',
-    array(  'page_namespace' => NS_MAIN,
-    "LCASE(page_title) LIKE '%". $db->strencode( $term2 ) ."%'" ),
-    "wfSajaxSearch",
-    array( 'LIMIT' => $limit+1 )
-    );
-
-    $ret = "";
-    $i=0;
-    while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {        
-        $ret .= $row->page_title ."\n";
-    }
-
-    $term = htmlspecialchars( $term );    
-    
-    return $ret;
-}
-
-function wfSajaxWikiToHTML( $wiki )
-{
-        global $wgOut, $wgTitle, $wgScriptPath;
-        global $wgFCKEditorToolbarSet;
-        global $wgFCKEditorDir, $wgFCKEditorHeight, $wgUser;
-        
-        $options = new FCKeditorParserOptions();
-        $options->setTidy(true);
-        $parser = new FCKeditorParser();
-        $parser->setOutputType(OT_HTML);
-        return $parser->parse($wiki, $wgTitle, $options)->getText();
-}
-
-
-class FCKeditorParser extends Parser
-{
-    private $FCKeditorMagicWords = array(
-            '__NOTOC__',
-            '__FORCETOC__',
-            '__NOEDITSECTION__',
-            '__START__',
-            '__NOTITLECONVERT__',
-            '__NOCONTENTCONVERT__',
-            '__END__',
-            '__TOC__',
-            '__NOTC__',
-            '__NOCC__',
-            "__FORCETOC__",
-            "__NEWSECTIONLINK__",
-            "__NOGALLERY__",
-    );
-    
-    function replaceInternalLinks( $text ) {
-        return parent::replaceInternalLinks($text);
-    }	
-    function formatHeadings( $text, $isMain=true ) {
-    	return $text;
-    }
-    function stripNoGallery($text) {}
-    function stripToc( $text ) {
-        $prefix = '<span class="fck_mw_magic">';
-        $suffix = '</span>';
-        
-        $strtr = array();
-        foreach ($this->FCKeditorMagicWords as $word) {
-            $strtr[$word] = $prefix . $word . $suffix;
-        }
-        
-        return strtr( $text, $strtr );
-    }
-    function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
-        $dontstrip[] = "gallery";
-        
-        return parent::strip($text, $state, $stripcomments , $dontstrip );       
-    }       
-    function replaceVariables( $text, $args = array(), $argsOnly = false ) {
-        return preg_replace("/\{\{([^}]+)\}\}(\}*)/", '<span class="fck_mw_template">{{$1}}$2</span>', $text);
-    }
-}
-
-class FCKeditorSkin
-{
-    private $skin;
-
-	function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
-	  $thumb = false, $manual_thumb = '', $valign = '' )
-	{
-		global $wgContLang, $wgUser, $wgThumbLimits;
-		
-		$width = (isset($params['width'])) ? " width=\"{$params['width']}\"" : "";
-		$height = (isset($params['height'])) ? " height=\"{$params['height']}\"" : "";
-		$alt = (!is_null($alt) && strlen($alt)>0) ? "alt=\"$alt\"" : "";
-		
-		return "<img src=\"{$nt->getFullText()}\"{$width}{$height}{$alt} />";
-	}
-    
-    function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' )
-    {
-		$fname = 'FCKeditorSkin::makeKnownLinkObj';
-		wfProfileIn( $fname );
-
-		if ( !is_object( $nt ) ) {
-			wfProfileOut( $fname );
-			return $text;
-		}
-
-		//$u = $nt->escapeLocalURL( $query );
-		$u = $nt->getFullText();
-		
-		if ( $nt->getFragment() != '' ) {
-			if( $nt->getPrefixedDbkey() == '' ) {
-				$u = '';
-				if ( '' == $text ) {
-					$text = htmlspecialchars( $nt->getFragment() );
-				}
-			}
-			$u .= $nt->getFragmentForURL();
-		}
-		if ( $text == '' ) {
-			$text = htmlspecialchars( $nt->getPrefixedText() );
-		}
-
-		list( $inside, $trail ) = Linker::splitTrail( $trail );
-		$r = "<a href=\"{$u}\">{$prefix}{$text}{$inside}</a>{$trail}";
-		wfProfileOut( $fname );
-		return $r;
-    }
-    
-    function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' )
-    {
-		# Fail gracefully
-		if ( ! isset($nt) ) {
-			# throw new MWException();
-			return "<!-- ERROR -->{$prefix}{$text}{$trail}";
-		}
-
-		$fname = 'FCKeditorSkin::makeBrokenLinkObj';
-		wfProfileIn( $fname );
-
-		//$u = $nt->escapeLocalURL( $query );
-		//$u = $nt->getText();
-		//$u = $nt->getEscapedText();
-		$u = $nt->getFullText();
-		//print_r($nt);
-		
-		if ( '' == $text ) {
-			$text = htmlspecialchars( $nt->getPrefixedText() );
-		}
-
-		list( $inside, $trail ) = Linker::splitTrail( $trail );
-		$s = "<a href=\"{$u}\">{$prefix}{$text}{$inside}</a>{$trail}";
-
-		wfProfileOut( $fname );
-		return $s;
-    }    
-    
-	function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {		
-		$url = htmlspecialchars( $url );
-		if( $escape ) {
-			$text = htmlspecialchars( $text );
-		}
-		if ($linktype == 'autonumber') {
-		    return '<a href="'.$url.'">[autonumber]</a>';
-		}
-		return '<a href="'.$url.'">'.$text.'</a>';
-	}
-    
-    function __call( $m, $a)
-    {
-    	return call_user_func_array( array( $this->skin, $m ), $a );
-    }
-    
-    function __construct( &$skin )
-    {
-    	$this->skin = $skin;
-    }
-}
-
-class FCKeditorParserOptions extends ParserOptions 
-{
-    function getNumberHeadings() {return false;}
-    function getEditSection() {return false;}
-    
-	function getSkin() {
-		if ( !isset( $this->mSkin ) ) {
-			$this->mSkin = new FCKeditorSkin( $this->mUser->getSkin() );
-		}
-		return $this->mSkin;
-	}
-}
Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.php	(revision 538)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.php	(revision 539)
@@ -32,4 +32,8 @@
 require_once $IP . "/includes/ParserOptions.php";
 require_once $IP . "/includes/Parser.php";
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditorSajax.body.php";
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditorParser.body.php";
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditorParserOptions.body.php";
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditorSkin.body.php";
 require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditor.body.php";
 
Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParser.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParser.body.php	(revision 539)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParser.body.php	(revision 539)
@@ -0,0 +1,109 @@
+<?php
+
+class FCKeditorParser extends Parser
+{
+    private $FCKeditorMagicWords = array(
+    '__NOTOC__',
+    '__FORCETOC__',
+    '__NOEDITSECTION__',
+    '__START__',
+    '__NOTITLECONVERT__',
+    '__NOCONTENTCONVERT__',
+    '__END__',
+    '__TOC__',
+    '__NOTC__',
+    '__NOCC__',
+    "__FORCETOC__",
+    "__NEWSECTIONLINK__",
+    "__NOGALLERY__",
+    );
+
+    function replaceInternalLinks( $text ) {
+        return parent::replaceInternalLinks($text);
+    }
+    function internalParse ( $text ) {
+        $callback = array('{' =>
+        array(
+        'end'=>'}',
+        'cb' => array(
+        2=>array('FCKeditorParser', 'leaveTemplatesAlone'),
+        3=>array('FCKeditorParser', 'leaveTemplatesAlone'),
+        ),
+        'min' =>2,
+        'max' =>3,
+        )
+        );
+
+        $text = $this->replace_callback($text, $callback);
+
+        $offset=0;
+        $textTmp = $text;
+        while (false !== ($pos = strpos($textTmp, "<!--FCK_SKIP_START-->")))
+        {
+            $tags[abs($pos + $offset)] = 1;
+            $textTmp = substr($textTmp, $pos+21);
+            $offset += $pos + 21;
+        }
+
+        $offset=0;
+        $textTmp = $text;
+        while (false !== ($pos = strpos($textTmp, "<!--FCK_SKIP_END-->")))
+        {
+            $tags[abs($pos + $offset)] = -1;
+            $textTmp = substr($textTmp, $pos+19);
+            $offset += $pos + 19;
+        }
+
+        ksort($tags);
+
+        $strtr = array("<!--FCK_SKIP_START-->" => "", "<!--FCK_SKIP_END-->" => "");
+
+        $sum=0;
+        $lastSum=0;
+        $finalString = false;
+        $startingPos = 0;
+        $inner = "";
+        foreach ($tags as $pos=>$type) {
+            $sum += $type;
+            if ($sum == 1 && $lastSum == 0) {
+                $finalString .= parent::internalParse(strtr(substr($text, $startingPos, $pos - $startingPos), $strtr));
+                $startingPos = $pos;
+            }
+            else if ($sum == 0) {
+                $finalString .= '<span class="fck_template">' . strtr(substr($text, $startingPos, $pos - $startingPos + 19), $strtr) . '</span>';
+                $startingPos = $pos + 19;
+            }
+            $lastSum = $sum;
+        }
+
+        return $finalString;
+    }
+    function leaveTemplatesAlone( $matches ) {
+        return "<!--FCK_SKIP_START-->".$matches['text']."<!--FCK_SKIP_END-->";
+    }
+    function formatHeadings( $text, $isMain=true ) {
+        return $text;
+    }
+    function stripNoGallery($text) {}
+    function stripToc( $text ) {
+        $prefix = '<span class="fck_magic">';
+        $suffix = '</span>';
+
+        $strtr = array();
+        foreach ($this->FCKeditorMagicWords as $word) {
+            $strtr[$word] = $prefix . $word . $suffix;
+        }
+
+        return strtr( $text, $strtr );
+    }
+    function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
+        $dontstrip[] = "gallery";
+
+        return parent::strip($text, $state, $stripcomments , $dontstrip );
+    }
+    /*
+    function replaceVariables( $text, $args = array(), $argsOnly = false ) {
+        return preg_replace("/\{\{([^}]+)\}\}(\}*)/", '<span class="fck_template">{{$1}}$2</span>', $text);
+    }
+    */
+}
Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParserOptions.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParserOptions.body.php	(revision 539)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditorParserOptions.body.php	(revision 539)
@@ -0,0 +1,14 @@
+<?php
+
+class FCKeditorParserOptions extends ParserOptions 
+{
+    function getNumberHeadings() {return false;}
+    function getEditSection() {return false;}
+    
+	function getSkin() {
+		if ( !isset( $this->mSkin ) ) {
+			$this->mSkin = new FCKeditorSkin( $this->mUser->getSkin() );
+		}
+		return $this->mSkin;
+	}
+}
Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSajax.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSajax.body.php	(revision 539)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSajax.body.php	(revision 539)
@@ -0,0 +1,80 @@
+<?php
+
+function wfSajaxSearchImageFCKeditor( $term )
+{
+    global $wgContLang, $wgOut;
+    $limit = 10;
+
+    $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
+    $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
+    $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
+    $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
+    $term = $term1;
+
+    if ( strlen( str_replace( '_', '', $term ) )<3 )
+    return "";
+
+    $db =& wfGetDB( DB_SLAVE );
+    $res = $db->select( 'page', 'page_title',
+    array(  'page_namespace' => NS_IMAGE,
+    "LCASE(page_title) LIKE '%". $db->strencode( $term2 ) ."%'" ),
+    "wfSajaxSearch",
+    array( 'LIMIT' => $limit+1 )
+    );
+
+    $ret = "";
+    $i=0;
+    while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {        
+        $ret .= $row->page_title ."\n";
+    }
+
+    $term = htmlspecialchars( $term );    
+    
+    return $ret;
+}
+
+function wfSajaxSearchArticleFCKeditor( $term )
+{
+    global $wgContLang, $wgOut;
+    $limit = 10;
+
+    $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
+    $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
+    $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
+    $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
+    $term = $term1;
+
+    if ( strlen( str_replace( '_', '', $term ) )<3 )
+    return "";
+
+    $db =& wfGetDB( DB_SLAVE );
+    $res = $db->select( 'page', 'page_title',
+    array(  'page_namespace' => NS_MAIN,
+    "LCASE(page_title) LIKE '%". $db->strencode( $term2 ) ."%'" ),
+    "wfSajaxSearch",
+    array( 'LIMIT' => $limit+1 )
+    );
+
+    $ret = "";
+    $i=0;
+    while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {        
+        $ret .= $row->page_title ."\n";
+    }
+
+    $term = htmlspecialchars( $term );    
+    
+    return $ret;
+}
+
+function wfSajaxWikiToHTML( $wiki )
+{
+        global $wgOut, $wgTitle, $wgScriptPath;
+        global $wgFCKEditorToolbarSet;
+        global $wgFCKEditorDir, $wgFCKEditorHeight, $wgUser;
+        
+        $options = new FCKeditorParserOptions();
+        $options->setTidy(true);
+        $parser = new FCKeditorParser();
+        $parser->setOutputType(OT_HTML);
+        return $parser->parse($wiki, $wgTitle, $options)->getText();
+}
Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSkin.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSkin.body.php	(revision 539)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditorSkin.body.php	(revision 539)
@@ -0,0 +1,99 @@
+<?php
+
+class FCKeditorSkin
+{
+    private $skin;
+
+	function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
+	  $thumb = false, $manual_thumb = '', $valign = '' )
+	{
+		global $wgContLang, $wgUser, $wgThumbLimits;
+		
+		$width = (isset($params['width'])) ? " width=\"{$params['width']}\"" : "";
+		$height = (isset($params['height'])) ? " height=\"{$params['height']}\"" : "";
+		$alt = (!is_null($alt) && strlen($alt)>0) ? "alt=\"$alt\"" : "";
+		
+		return "<img src=\"{$nt->getFullText()}\"{$width}{$height}{$alt} />";
+	}
+    
+    function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' )
+    {
+		$fname = 'FCKeditorSkin::makeKnownLinkObj';
+		wfProfileIn( $fname );
+
+		if ( !is_object( $nt ) ) {
+			wfProfileOut( $fname );
+			return $text;
+		}
+
+		//$u = $nt->escapeLocalURL( $query );
+		$u = $nt->getFullText();
+		
+		if ( $nt->getFragment() != '' ) {
+			if( $nt->getPrefixedDbkey() == '' ) {
+				$u = '';
+				if ( '' == $text ) {
+					$text = htmlspecialchars( $nt->getFragment() );
+				}
+			}
+			$u .= $nt->getFragmentForURL();
+		}
+		if ( $text == '' ) {
+			$text = htmlspecialchars( $nt->getPrefixedText() );
+		}
+
+		list( $inside, $trail ) = Linker::splitTrail( $trail );
+		$r = "<a href=\"{$u}\">{$prefix}{$text}{$inside}</a>{$trail}";
+		wfProfileOut( $fname );
+		return $r;
+    }
+    
+    function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' )
+    {
+		# Fail gracefully
+		if ( ! isset($nt) ) {
+			# throw new MWException();
+			return "<!-- ERROR -->{$prefix}{$text}{$trail}";
+		}
+
+		$fname = 'FCKeditorSkin::makeBrokenLinkObj';
+		wfProfileIn( $fname );
+
+		//$u = $nt->escapeLocalURL( $query );
+		//$u = $nt->getText();
+		//$u = $nt->getEscapedText();
+		$u = $nt->getFullText();
+		//print_r($nt);
+		
+		if ( '' == $text ) {
+			$text = htmlspecialchars( $nt->getPrefixedText() );
+		}
+
+		list( $inside, $trail ) = Linker::splitTrail( $trail );
+		$s = "<a href=\"{$u}\">{$prefix}{$text}{$inside}</a>{$trail}";
+
+		wfProfileOut( $fname );
+		return $s;
+    }    
+    
+	function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {		
+		$url = htmlspecialchars( $url );
+		if( $escape ) {
+			$text = htmlspecialchars( $text );
+		}
+		if ($linktype == 'autonumber') {
+		    return '<a href="'.$url.'">[autonumber]</a>';
+		}
+		return '<a href="'.$url.'">'.$text.'</a>';
+	}
+    
+    function __call( $m, $a)
+    {
+    	return call_user_func_array( array( $this->skin, $m ), $a );
+    }
+    
+    function __construct( &$skin )
+    {
+    	$this->skin = $skin;
+    }
+}
