FCKeditor/function parse

From Meta, a Wikimedia project coordination wiki

	function parse( $text, &$title, $options, $linestart = true, $clearState = true, $revid = null ) {
		/**
		 * First pass--just handle <nowiki> sections, pass the rest off
		 * to internalParse() which does all the real work.
		 */

		global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
		$fname = 'Parser::parse';
		wfProfileIn( $fname );

		if ( $clearState ) {
			$this->clearState();
		}

		$this->mOptions = $options;
		$this->mTitle =& $title;
		$this->mRevisionId = $revid;
		$this->mOutputType = OT_HTML;

		$this->mStripState = NULL;

		//$text = $this->strip( $text, $this->mStripState );
		// VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
		$x =& $this->mStripState;



		# patch fck editor
		global $wgUseEditor, $wgEditorToken;

                if (eregi($wgEditorToken, $text, $eregi_result)) {
			$wgUseEditor = true;
		}


		wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$x ) );
		$text = $this->strip( $text, $x );
		wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$x ) );


		if ($wgUseEditor == true) $text = preg_replace("|$wgEditorToken|i", "", $text);

		# Hook to suspend the parser in this state
		if ( !wfRunHooks( 'ParserBeforeInternalParse', array( &$this, &$text, &$x ) ) ) {
			wfProfileOut( $fname );
			return $text ;
		}

		$text = $this->internalParse( $text );

		$text = $this->unstrip( $text, $this->mStripState );

		# Clean up special characters, only run once, next-to-last before doBlockLevels
		$fixtags = array(
			# french spaces, last one Guillemet-left
			# only if there is something before the space
			'/(.) (?=\\?|:|;|!|\\302\\273)/' => '\\1 \\2',
			# french spaces, Guillemet-right
			'/(\\302\\253) /' => '\\1 ',
			'/<center *>(.*)<\\/center *>/i' => '<div class="center">\\1</div>',
		);
		$text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );

		# only once and last
		if (!$wgUseEditor) $text = $this->doBlockLevels( $text, $linestart ); # patch editor

		$this->replaceLinkHolders( $text );

		# the position of the parserConvert() call should not be changed. it
		# assumes that the links are all replaced and the only thing left
		# is the <nowiki> mark.
		# Side-effects: this calls $this->mOutput->setTitleText()
		$text = $wgContLang->parserConvert( $text, $this );

		$text = $this->unstripNoWiki( $text, $this->mStripState );

		wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );

		$text = Sanitizer::normalizeCharReferences( $text );

		if (($wgUseTidy and $this->mOptions->mTidy) or $wgAlwaysUseTidy) {
			$text = Parser::tidy($text);
		} else {
			# attempt to sanitize at least some nesting problems
			# (bug #2702 and quite a few others)
			$tidyregs = array(	
				# ''Something [http://www.cool.com cool''] --> 
				# <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
				'/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
				'\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
				# fix up an anchor inside another anchor, only
				# at least for a single single nested link (bug 3695)
				'/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
				'\\1\\2</a>\\3</a>\\1\\4</a>',
				# fix div inside inline elements- doBlockLevels won't wrap a line which
				# contains a div, so fix it up here; replace
				# div with escaped text
				'/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
				'\\1\\3<div\\5>\\6</div>\\8\\9',
				# remove empty italic or bold tag pairs, some
				# introduced by rules above
				'/<([bi])><\/\\1>/' => '' 
			);

			$text = preg_replace( 
				array_keys( $tidyregs ),
				array_values( $tidyregs ),
				$text );
		}

		wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );

		$this->mOutput->setText( $text );
		wfProfileOut( $fname );

		return $this->mOutput;
	}

</nowiki>