Changeset 614

Show
Ignore:
Timestamp:
2007-08-01 23:43:25 (18 months ago)
Author:
wwalc
Message:

added support in php parser for nowiki, includeonly, noinclude tags

Location:
MediaWiki/trunk/extensions/FCKeditor
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MediaWiki/trunk/extensions/FCKeditor/FCKeditor.body.php

    r593 r614  
    155155        $options->setTidy(true); 
    156156        $parser = new FCKeditorParser(); 
    157         if (in_array("wfCite", $wgExtensionFunctions)) { 
    158             $parser->setHook('ref', array($parser, 'ref')); 
    159             $parser->setHook('references', array($parser, 'references')); 
    160         } 
    161157        $parser->setOutputType(OT_HTML); 
    162158        $form->textbox1 = $parser->parse($form->textbox1, $wgTitle, $options)->getText(); 
  • MediaWiki/trunk/extensions/FCKeditor/FCKeditorParser.body.php

    r611 r614  
    55    public static $fkc_mw_makeImage_options; 
    66    protected $fck_mw_strtr_span; 
     7    protected $fck_mw_taghook; 
     8    protected $fck_internal_parse_text; 
    79 
    810    private $FCKeditorMagicWords = array( 
     
    2224    ); 
    2325 
     26    function __construct() { 
     27        global $wgParser; 
     28        parent::Parser(); 
     29 
     30        foreach ($wgParser->getTags() as $h) { 
     31            if (!in_array($h, array("pre"))) { 
     32                $this->setHook($h, array($this, "fck_genericTagHook")); 
     33            } 
     34        } 
     35    } 
     36 
     37    /** 
     38        * Callback function for custom tags 
     39        * 
     40        * @param string $str Input 
     41        * @param array $argv Arguments 
     42        * @return string 
     43        */ 
     44    function fck_genericTagHook( $str, $argv, $parser ) { 
     45        if (empty($argv)) { 
     46            $ret = "<span class=\"fck_mw_".$this->fck_mw_taghook."\" _fck_mw_customtag=\"true\" _fck_mw_tagname=\"".$this->fck_mw_taghook."\">"; 
     47        } 
     48        else { 
     49            $ret = "<span class=\"fck_mw_".$this->fck_mw_taghook."\" _fck_mw_customtag=\"true\" _fck_mw_tagname=\"".$this->fck_mw_taghook."\""; 
     50            foreach ($argv as $key=>$value) { 
     51                $ret .= " ".$key."=\"".$value."\""; 
     52            } 
     53            $ret .=">"; 
     54        } 
     55        if (is_null($str)) { 
     56            $ret = substr($ret, 0, -1) . " />"; 
     57        } 
     58        else { 
     59            $ret .= htmlspecialchars($str); 
     60            $ret .= "</span>"; 
     61        } 
     62        return $ret; 
     63    } 
     64 
     65    /** 
     66        * Callback function for custom tags 
     67        * 
     68        * @param string $tagName tag name, eg. nowiki, math 
     69        * @param string $str Input 
     70        * @param array $argv Arguments 
     71        * @return string 
     72        */ 
     73    function fck_wikiTag( $tagName, $str, $argv = array()) { 
     74        if (empty($argv)) { 
     75            $ret = "<span class=\"fck_mw_".$tagName."\">"; 
     76        } 
     77        else { 
     78            $ret = "<span class=\"fck_mw_".$tagName."\""; 
     79            foreach ($argv as $key=>$value) { 
     80                $ret .= " ".$key."=\"".$value."\""; 
     81            } 
     82            $ret .=">"; 
     83        } 
     84        if (is_null($str)) { 
     85            $ret = substr($ret, 0, -1) . " />"; 
     86        } 
     87        else { 
     88            $ret .= htmlspecialchars($str); 
     89            $ret .= "</span>"; 
     90        } 
     91        return $ret; 
     92    } 
     93 
     94    /** 
     95         * Strips and renders nowiki, pre, math, hiero 
     96         * If $render is set, performs necessary rendering operations on plugins 
     97         * Returns the text, and fills an array with data needed in unstrip() 
     98         * 
     99         * @param StripState $state 
     100         * 
     101         * @param bool $stripcomments when set, HTML comments <!-- like this --> 
     102         *  will be stripped in addition to other tags. This is important 
     103         *  for section editing, where these comments cause confusion when 
     104         *  counting the sections in the wikisource 
     105         * 
     106         * @param array dontstrip contains tags which should not be stripped; 
     107         *  used to prevent stipping of <gallery> when saving (fixes bug 2700) 
     108         * 
     109         * @private 
     110         */ 
     111    function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) { 
     112        global $wgContLang; 
     113 
     114        wfProfileIn( __METHOD__ ); 
     115        $render = ($this->mOutputType == OT_HTML); 
     116 
     117        $uniq_prefix = $this->mUniqPrefix; 
     118        $commentState = new ReplacementArray; 
     119        $nowikiItems = array(); 
     120        $generalItems = array(); 
     121 
     122        $elements = array_merge( 
     123        array( 'nowiki', 'gallery' ), 
     124        array_keys( $this->mTagHooks ) ); 
     125        global $wgRawHtml; 
     126        if( $wgRawHtml ) { 
     127            $elements[] = 'html'; 
     128        } 
     129        if( $this->mOptions->getUseTeX() ) { 
     130            $elements[] = 'math'; 
     131        } 
     132 
     133        # Removing $dontstrip tags from $elements list (currently only 'gallery', fixing bug 2700) 
     134        foreach ( $elements AS $k => $v ) { 
     135            if ( !in_array ( $v , $dontstrip ) ) continue; 
     136            unset ( $elements[$k] ); 
     137        } 
     138 
     139        $matches = array(); 
     140        $text = Parser::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix ); 
     141 
     142        foreach( $matches as $marker => $data ) { 
     143            list( $element, $content, $params, $tag ) = $data; 
     144            if( $render ) { 
     145                $tagName = strtolower( $element ); 
     146                wfProfileIn( __METHOD__."-render-$tagName" ); 
     147                switch( $tagName ) { 
     148                    case '!--': 
     149                        // Comment 
     150                        if( substr( $tag, -3 ) == '-->' ) { 
     151                            $output = $tag; 
     152                        } else { 
     153                            // Unclosed comment in input. 
     154                            // Close it so later stripping can remove it 
     155                            $output = "$tag-->"; 
     156                        } 
     157                        break; 
     158                    case 'html': 
     159                        if( $wgRawHtml ) { 
     160                            $output = $content; 
     161                            break; 
     162                        } 
     163                        // Shouldn't happen otherwise. :) 
     164                    case 'nowiki': 
     165                        $output = $this->fck_wikiTag('nowiki', $content, $params); //required by FCKeditor 
     166                        break; 
     167                    case 'math': 
     168                        $output = $wgContLang->armourMath( MathRenderer::renderMath( $content ) ); 
     169                        break; 
     170                    case 'gallery': 
     171                        $output = $this->renderImageGallery( $content, $params ); 
     172                        break; 
     173                    default: 
     174                        if( isset( $this->mTagHooks[$tagName] ) ) { 
     175                            $this->fck_mw_taghook = $tagName; //required by FCKeditor 
     176                            $output = call_user_func_array( $this->mTagHooks[$tagName], 
     177                            array( $content, $params, $this ) ); 
     178                        } else { 
     179                            throw new MWException( "Invalid call hook $element" ); 
     180                        } 
     181                } 
     182                wfProfileOut( __METHOD__."-render-$tagName" ); 
     183            } else { 
     184                // Just stripping tags; keep the source 
     185                $output = $tag; 
     186            } 
     187 
     188            // Unstrip the output, to support recursive strip() calls 
     189            $output = $state->unstripBoth( $output ); 
     190 
     191            if( !$stripcomments && $element == '!--' ) { 
     192                $commentState->setPair( $marker, $output ); 
     193            } elseif ( $element == 'html' || $element == 'nowiki' ) { 
     194                $nowikiItems[$marker] = $output; 
     195            } else { 
     196                $generalItems[$marker] = $output; 
     197            } 
     198        } 
     199        # Add the new items to the state 
     200        # We do this after the loop instead of during it to avoid slowing 
     201        # down the recursive unstrip 
     202        $state->nowiki->mergeArray( $nowikiItems ); 
     203        $state->general->mergeArray( $generalItems ); 
     204 
     205        # Unstrip comments unless explicitly told otherwise. 
     206        # (The comments are always stripped prior to this point, so as to 
     207        # not invoke any extension tags / parser hooks contained within 
     208        # a comment.) 
     209        if ( !$stripcomments ) { 
     210            // Put them all back and forget them 
     211            $text = $commentState->replace( $text ); 
     212        } 
     213 
     214        wfProfileOut( __METHOD__ ); 
     215        return $text; 
     216    } 
     217 
    24218    function replaceInternalLinks( $text ) { 
    25219        return parent::replaceInternalLinks($text); 
     
    34228        'end'=>'}', 
    35229        'cb' => array( 
    36         2=>array('FCKeditorParser', 'leaveTemplatesAlone'), 
    37         3=>array('FCKeditorParser', 'leaveTemplatesAlone'), 
     230        2=>array('FCKeditorParser', 'fck_leaveTemplatesAlone'), 
     231        3=>array('FCKeditorParser', 'fck_leaveTemplatesAlone'), 
    38232        ), 
    39233        'min' =>2, 
     
    93287            } 
    94288            $stringToParse .= substr($text, $startingPos); 
    95  
    96             $finalString = parent::internalParse($stringToParse); 
    97         } else { 
    98             $finalString = parent::internalParse($text); 
    99         } 
     289            $text = &$stringToParse; 
     290        } 
     291        $this->fck_internal_parse_text =& $text; 
     292 
     293        $text = StringUtils::delimiterReplaceCallback( '<includeonly>', '</includeonly>', array($this, 'fck_includeonly'), $text ); 
     294        $text = StringUtils::delimiterReplaceCallback( '<noinclude>', '</noinclude>', array($this, 'fck_noinclude'), $text ); 
     295        $text = StringUtils::delimiterReplaceCallback( '<onlyinclude>', '</onlyinclude>', array($this, 'fck_onlyinclude'), $text ); 
     296         
     297        $finalString = parent::internalParse($text); 
    100298 
    101299        return $finalString; 
    102300    } 
    103     function leaveTemplatesAlone( $matches ) { 
     301    function fck_includeonly( $matches ) { 
     302        return $this->fck_wikiTag('includeonly', $matches[1]); 
     303    } 
     304    function fck_noinclude( $matches ) { 
     305        return $this->fck_wikiTag('noinclude', $matches[1]); 
     306    } 
     307    function fck_onlyinclude( $matches ) { 
     308        return $this->fck_wikiTag('onlyinclude', $matches[1]); 
     309    } 
     310    function fck_leaveTemplatesAlone( $matches ) { 
    104311        return "<!--FCK_SKIP_START-->".$matches['text']."<!--FCK_SKIP_END-->"; 
    105312    } 
     
    121328        return strtr( $text, $strtr ); 
    122329    } 
    123     function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) { 
    124         $dontstrip[] = "gallery"; 
    125         //$dontstrip[] = "nowiki"; 
    126         //$dontstrip[] = "noinclude"; 
    127         //$dontstrip[] = "includeonly"; 
    128  
    129         return parent::strip($text, $state, $stripcomments , $dontstrip ); 
    130     } 
    131     /** 
    132         * Callback function for <ref> 
    133         * 
    134         * @param string $str Input 
    135         * @param array $argv Arguments 
    136         * @return string 
    137         */ 
    138     function ref( $str, $argv, $parser ) { 
    139         if (empty($argv)) { 
    140             $ret = "<span class=\"fck_mw_ref\">"; 
    141         } 
    142         else { 
    143             $ret = "<span class=\"fck_mw_ref\""; 
    144             foreach ($argv as $key=>$value) { 
    145                 $ret .= " ".$key."=\"".$value."\""; 
    146             } 
    147             $ret .=">"; 
    148         } 
    149         if (is_null($str)) { 
    150             $ret = substr($ret, 0, -1) . " />"; 
    151         } 
    152         else { 
    153             $ret .= htmlspecialchars($str); 
    154             $ret .= "</span>"; 
    155         } 
    156         return $ret; 
    157     } 
    158     /** 
    159         * Callback function for <references> 
    160         * 
    161         * @param string $str Input 
    162         * @param array $argv Arguments 
    163         * @return string 
    164         */ 
    165     function references( $str, $argv, $parser ) { 
    166         if (empty($argv)) { 
    167             $ret = "<span class=\"fck_mw_references\">"; 
    168         } 
    169         else { 
    170             $ret = "<span class=\"fck_mw_references\""; 
    171             foreach ($argv as $key=>$value) { 
    172                 $ret .= " ".$key."=\"".$value."\""; 
    173             } 
    174             $ret .=">"; 
    175         } 
    176         if (is_null($str)) { 
    177             $ret = substr($ret, 0, -1) . " />"; 
    178         } 
    179         else { 
    180             $ret .= htmlspecialchars($str); 
    181             $ret .= "</span>"; 
    182         } 
    183  
    184         return $ret; 
    185     } 
    186330 
    187331    function parse( $text, &$title, $options, $linestart = true, $clearState = true, $revid = null ) { 
  • MediaWiki/trunk/extensions/FCKeditor/FCKeditorSajax.body.php

    r601 r614  
    124124function wfSajaxWikiToHTML( $wiki ) 
    125125{ 
    126     global $wgOut, $wgTitle, $wgScriptPath; 
    127     global $wgFCKEditorToolbarSet; 
    128     global $wgFCKEditorDir, $wgFCKEditorHeight, $wgUser, $wgParser, $wgExtensionFunctions; 
     126    global $wgTitle; 
    129127 
    130128    $options = new FCKeditorParserOptions(); 
    131129    $options->setTidy(true); 
    132130    $parser = new FCKeditorParser(); 
    133  
    134     if (in_array("wfCite", $wgExtensionFunctions)) { 
    135         $parser->setHook('ref', array($parser, 'ref')); 
    136         $parser->setHook('references', array($parser, 'references')); 
    137     } 
    138131    $parser->setOutputType(OT_HTML); 
    139132