Index: /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php
===================================================================
--- /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php	(revision 502)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php	(revision 503)
@@ -65,5 +65,5 @@
 
         $wgHooks['ParserBeforeStrip'][]                 = $this;
-        $wgHooks['ParserAfterTidy'][]                   = $this;
+        //$wgHooks['ParserAfterTidy'][]                   = $this;
         $wgHooks['UserToggles'][]                       = $this;
         $wgHooks['MessagesPreLoad'][]                   = $this;
@@ -189,48 +189,13 @@
         }
 
-        $fcktext = str_replace('<p class="line867">&nbsp;</p>', "", $fcktext);
+        //$fcktext = str_replace('<p class="line867">&nbsp;</p>', "", $fcktext);
 
         $tagList = array("pre", "math", "gallery", "nowiki", "html");
-        $i=1;
         foreach($tagList as $tag) {
             //while (preg_match("|<($tag.*?)>(.*?)</$tag>|is", $fcktext, $a)) {
             if (preg_match("|<($tag.*?)>(.*?)</$tag>|is", $fcktext, $a)) {
-                if ($i == 1){
-                    /*
-                    print "\n-----\n";
-                    print $fcktext;
-                    print "\n-----\n";
-                    print_r($a);
-                    print "\n-----\n";
-                    print "Tag: $tag";
-                    print "\n-----\n";
-                    */
-                }
-                //print "4";
                 $r = preg_replace("|<br.*?>|i", "", $a[0]);
-                if ($i == 1){
-                    /*
-                    print "\n-----\n";
-                    print $r;
-                    print "\n-----\n";
-                    */
-                }
                 $r = preg_replace("| |i", " ", $r);
-                if ($i == 1){
-                    /*
-                    print "\n-----\n";
-                    print $r;
-                    print "\n-----\n";
-                    */
-                }
                 $fcktext = str_replace($a[0], html_entity_decode($r), $fcktext);
-                if ($i == 1){
-                    /*
-                    print "\n-----\n";
-                    print $fcktext;
-                    print "\n-----\n";
-                    */
-                }
-                $i++;
             }
         }
@@ -239,5 +204,4 @@
             //while (preg_match("|<($tag.*?)>(.*?)</$tag>|is", $fcktext, $a)) {
             if (preg_match("|<($tag.*?)>(.*?)</$tag>|is", $fcktext, $a)) {
-                //print "5";
                 $r = preg_replace("|<br.*?>|i", "", $a[0]);
                 $r = preg_replace("| |i", " ", $r);
@@ -288,8 +252,15 @@
      * @return unknown
      */
-    public function onEditPageShowEditFormInitial( $q ) {
+    public function onEditPageShowEditFormInitial( $form ) {
         global $wgOut, $wgTitle, $wgScriptPath;
         global $wgFCKEditorToolbarSet;
         global $wgFCKEditorDir, $wgFCKEditorHeight, $wgUser;
+        
+        $options = new FCKeditorParserOptions();
+        $options->setTidy(true);
+        $parser = new FCKeditorParser();
+        $parser->setOutputType(OT_HTML);
+        $form->textbox1 = $parser->parse($form->textbox1, $wgTitle, $options)->getText();
+        
 
         if (!$wgUser->getOption( 'showtoolbar' ) || $wgUser->getOption( 'riched_disable' )) {
@@ -314,8 +285,8 @@
         var oDiv=document.getElementById("toolbar"); 
         oDiv.style.cssText = 'display:none;'; 
-    }
+    }    
 } 
 addOnloadHook(onLoadFCK);
-</script>
+</script>        
 HEREDOC;
 
@@ -378,2 +349,104 @@
     return "  <input type=\"hidden\" name=\"wfSajaxSearchImageFCKeditor\" value=\"\"><br />Images containing <b>\"".$term2."\"</b><br /><ul>" .$r ."</ul>";
 }
+
+
+class FCKeditorParser extends Parser
+{
+    function replaceInternalLinks( $text ) {
+        return parent::replaceInternalLinks($text);
+    }
+    function replaceExternalLinks( $text ) {
+        return parent::replaceExternalLinks($text);
+    }    
+    
+    function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
+        $dontstrip[] = "gallery";
+        
+        return parent::strip($text, $state, $stripcomments , $dontstrip );       
+    }       
+}
+
+class FCKeditorSkin
+{
+    private $skin;
+        
+    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->getText();
+		
+		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();
+
+		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 __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 502)
+++ /MediaWiki/trunk/extensions/FCKeditor/FCKeditor.php	(revision 503)
@@ -29,4 +29,7 @@
 ini_set("display_errors", 1);
 
+require_once $IP . "/includes/GlobalFunctions.php";
+require_once $IP . "/includes/ParserOptions.php";
+require_once $IP . "/includes/Parser.php";
 require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "FCKeditor.body.php";
 
