Extension talk:AlternateSyntaxParser

From mediawiki.org
Jump to navigation Jump to search

Bug fix[edit]

There's a small bug which causes extraneous error_log output, at least with PHP 5.1.6 and MW1.12. It's fixed with the following patch: --otheus 08:31, 15 July 2008 (UTC)

*** AlternateSyntaxParser.php   2007-06-02 23:15:01.000000000 +0200
--- ../wiki/extensions/AlternateSyntaxParser/AlternateSyntaxParser.php      2008-07-15 10:26:40.000000000 +0200
***************
*** 85,90 ****
--- 85,91 ----
   * Wrapper class for encapsulating AlternateSyntaxParser methods
   */
  class AlternateSyntaxParser {
+     var $mEditPreviewFlag = true;

      /**
       * Setup for AlternateSyntaxParser extension.
***************
*** 188,194 ****
       * @return Boolean Always true.
       */
      function unflagEditPreview( &$editpage, &$out ) {
!         if (isset($this->mEditPreviewFlag)) unset($this->mEditPreviewFlag);
          return true;
      }

--- 189,196 ----
       * @return Boolean Always true.
       */
      function unflagEditPreview( &$editpage, &$out ) {
!         // if (isset($this->mEditPreviewFlag)) unset($this->mEditPreviewFlag);
!         $this->mEditPreviewFlag = false;
          return true;
      }

Questions[edit]

Does specifying a site-wide default alternate syntax reset all articles to be viewed in that syntax? I.e. if we already have a bunch of pages in the standard mediawiki syntax, will setting the site-wide default change these pages over to the new default? — Preceding unsigned comment added by 216.161.249.75 (talkcontribs) 16:56, 18 August 2009 (UTC)

MediaWiki 1.19.1 Compatibility[edit]

The extension does not work out-of-the-box for MW 1.19.1, but it turns out just a few changes are necessary.

This is in addition to the fact that classTextile is not included in the plugin code AND the fact that the original textile parser mentioned in the code is no longer accessible.

Anyway, download the AlternateSyntaxParser.php and classTextile.php. Name classTextile.php textile.php (or change the include line in the AlternateSyntaxParser.php).

Of course, I only tested textile having had to convert from Redmine.

Note that redmine supports an URL syntax classTextile.php doesn't handle (plain http://... form). You need to specify url at least as "$":http://... in order for this plugin to recognize it.

93,98c93,94
<     	# Add system messages
<     	global $wgMessageCache;
<         $wgMessageCache->addMessage(
<             'alternatesyntax-unrecognized-language',
<             'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"'
<         );
---
>     	# PHP complains if the member isn't defined
>         $this->mEditPreviewFlag = false;
156c152
<                 $text = $textile->process($parser->mSwappedOutText);
---
>                 $text = $textile->TextileThis($parser->mSwappedOutText);
163c159
<                         'alternatesyntax-unrecognized-language',
---
>                         'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"',
191c187
<         if (isset($this->mEditPreviewFlag)) unset($this->mEditPreviewFlag);
---
>         $this->mEditPreviewFlag = false;

-- velis74 12:43, 23 August 2012 (UTC)

Wikidot syntax?[edit]

I'm wondering if this extension could be used to change a MediaWiki's syntax to Wikidot syntax. Since Wikidot's open source project is essentially obsolete and utterly dead, I've been trying to find a way to host a wiki environment that would support Wikidot's syntax so that my users don't have to learn new markup and so I have a place to fully backup my own Wikidot sites. Has anyone done this, or would anyone have an idea as to how it could be done? Should I go through the effort of writing the markup myself? NotGaryStu (talk) 22:02, 15 January 2013 (UTC)

Fix apr. 2013, MW 1.20 - v 0.1p1[edit]

Here is my patched version, with commented stuff in respect to original version, so that the patches so far on the talk page are implemented, unnecesarry calls are removed, the new wfMessage is used, there is additional hook for previewing - and also the new way of using the Markdown library is shown. Smd (talk) 20:06, 18 April 2013 (UTC)

<?php //{{MediaWikiExtension}}<source lang="php">
/*
 * AlternateSyntaxParser.php - Conditionally replaces wikitext processing with a alternate syntaxes.
 * mod 2013, tested w/ MW 1.20 and contemporary Markdown (lib) file
 * @author Jim R. Wilson / mod Smd (sdaau)
 * @version 0.1p1
 * @copyright Copyright (C) 2007 Jim R. Wilson
 * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
 * -----------------------------------------------------------------------
 * Description:
 *     This is a MediaWiki (http://www.mediawiki.org/) extension which conditionally replaces
 *     the wikitext parsing engine in MediaWiki with an alternate syntax parser of your
 *     choice.  Several alternate light markup languages are supported out-of-the-box,
 *     and the extension is itself extensible to work with other syntaxes.
 * Requirements:
 *     Mediawiki 1.6.x, 1.9.x, 1.10.x or higher
 *     PHP 4.x, 5.x or higher
 *     PHP Markdown or PHP Markdown Extra library:
 *         http://www.michelf.com/projects/php-markdown/
 * Installation:
 *     1. Create a folder in your $IP/extensions directory called AlternateSyntaxParser.
 *         Note: $IP is your MediaWiki install dir.
 *     2. Drop this script (AlternateSyntaxParser.php) into $IP/extensions/AlternateSyntaxParser
 *     3. Enable the extension by adding this line to your LocalSettings.php:
 *         require_once('extensions/MarkdownSyntax.php');
 *     4. Download required libraries for chosen alternate syntaxes below
 *         Note: If you don't configure this, it won't do anything!
 * To support Markdown Syntax:
 *     1. Download Michel Fortin's PHP Markdown or PHP Markdown Extra library:
 *         http://www.michelf.com/projects/php-markdown/
 *     2. Extract the file 'markdown.php' from the downloaded archive.
 *     3. Drop markdown.php into $IP/extensions/AlternateSyntaxParser/
 * To support Textile Syntax:
 *     1. Download Jim Riggs' TextilePHP library:
 *         http://jimandlissa.com/project/textilephp
 *     2. Extract the file 'Textile.php' from the downloaded archive.
 *     3. Drop Textile.php into $IP/extensions/AlternateSyntaxParser/
 * Usage:
 *     To use an alternate syntax in a page, put the following at the top of the page:
 *         #MARKUP language
 *     Where 'language' is the markup language - examples include 'markdown' and 'textile'.
 *     Alternatively, you may specify a site-wide default alternate language by setting
 *     the $wgAlternateSyntaxParserLanguage variable in your LocalSettings.php.  For
 *     examlpe, to make Markdown the default parsing syntax, you'd add:
 *         $wgAlternateSyntaxParserLanguage = 'markdown';
 * Version Notes:
 *     version 0.1:
 *         Initial release.
 * -----------------------------------------------------------------------
 * Copyright (c) 2007 Jim R. Wilson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * -----------------------------------------------------------------------
 */

# Confirm MediaWiki environment
if (defined('MEDIAWIKI')) {


# Credits
$wgExtensionCredits['parserhook'][] = array(
    'name'=>'AlternateSyntaxParser',
    'author'=>'Jim R. Wilson - wilson.jim.r&lt;at&gt;gmail.com / Smd',
    'url'=>'http://www.mediawiki.org/wiki/Extension:AlternateSyntaxParser',
    'description'=>'Conditionally replaces wikitext processing with a alternate syntaxes',
    'version'=>'0.1p1'
);

// this would be for .i18n.php eventually?
// not needed - onMessagesPreLoad seems to take care of it;
// even if call is wfMessage(...)->inContentLanguage()->text() !!
//~ $messages = array();
//~ $messages['en'] = array(
  //~ 'alternatesyntax-unrecognized-language' => 'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"',
//~ );

/**
 * Wrapper class for encapsulating AlternateSyntaxParser methods
 */
class AlternateSyntaxParser {
     var $mEditPreviewFlag = true;
    /**
     * Setup for AlternateSyntaxParser extension.
     */
    function setup( ) {
      # PHP complains if the member isn't defined
                $this->mEditPreviewFlag = false;
      // note: Manual:$wgMessageCache 	This feature was removed completely in version 1.18.0.
      // now - onMessagesPreLoad
    	# Add system messages
    	//~ global $wgMessageCache;
        //~ $wgMessageCache->addMessage(
            //~ 'alternatesyntax-unrecognized-language',
            //~ 'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"'
        //~ );
    }

    function onMessagesPreLoad( $title, &$message ) {
      if( strtolower($title) == "alternatesyntax-unrecognized-language" ) {
        $message = 'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"';
        return false;
      }
      return true;
    }

    /**
     * Hides all incoming text to the parser for later processing.
     * @param Parser $parser Instance of Parser performing the parse.
     * @param String $text Text to be processed.
     * @return Boolean false if processing suceeded, true otherwise
     */
    function swapOutText( &$parser, &$text ) {

        # Short-circuit if we're not processing main article text
        if (
            !$parser->mRevisionId &&
            !$this->mEditPreviewFlag
        ) return true;

        # Determine the markup language in use.
        # Note: This will either be specified in the text iteslf in the form
        #   "#MARKUP whatever", or will be pulled from the global language setting.
        if (preg_match('/^#MARKUP\s+([a-zA-Z0-9_]+)/', $text, $matches)) {
            $parser->mMarkupLanguage = $matches[1];
            $text = substr($text, strlen($matches[0]));
        } else {
            global $wgAlternateSyntaxParserLanguage;
            if (!$wgAlternateSyntaxParserLanguage) return true;
            $parser->mMarkupLanguage = $wgAlternateSyntaxParserLanguage;
        }

        # An alternate syntax is in use - swap out text
        $parser->mSwappedOutText = $text;
        $text = '';
        return false;
    }

    /**
     * Displays alternatively processed text.
     * @param Parser $parser Instance of Parser performing the parse.
     * @param String $text Text to be processed.
     * @return Boolean false if processing suceeded, true otherwise
     */
    function swapInText( &$parser, &$text ) {

        # Short-circuit if extension has not been configured or there's nothing to process
        if (
            !isset($parser->mMarkupLanguage) ||
            !isset($parser->mSwappedOutText)
        ) return true;

        # Perform processing based on selected language
        switch (strtolower($parser->mMarkupLanguage)) {
            case 'markdown':
                require_once('Markdown.php');//('markdown.php');
                $text = \Michelf\Markdown::defaultTransform($parser->mSwappedOutText); //Markdown($parser->mSwappedOutText);
                break;
            case 'textile':
                require_once('Textile.php');
                $textile = new Textile();
                $text = $textile->TextileThis($parser->mSwappedOutText); //$textile->process($parser->mSwappedOutText);
                break;
            default:
                // there is no 'AlternateSyntaxParser' in global hooks;
                // so the return from wfRunHooks is always 1 - as long as we
                // enter this piece of the code?
                // is we don't enter this portion, wfRunHooks will never run with 'AlternateSyntaxParser' event anyways..
                // so it is unnecesarry (anymore):
                //~ $tret = wfRunHooks( 'AlternateSyntaxParser', array(
                    //~ &$parser, &$text, $parser->mMarkupLanguage
                //~ ) );
                //~ print("wfRunHooks POST tret :".$tret.": \n");
                // as well as the check is unnecesarry:
                //~ if ($tret) {
                      //$text = '<div class="errorbox">'.wfMsgForContent(
                      //    'AlternateSyntaxParser does not recognize the language "<tt>$1</tt>"', //'alternatesyntax-unrecognized-language',
                      //    htmlspecialchars($parser->mMarkupLanguage)
                      //).'</div>';
                      $text = '<div class="errorbox">'. wfMessage(
                        'alternatesyntax-unrecognized-language',
                        array( htmlspecialchars($parser->mMarkupLanguage) )
                      )->inContentLanguage()->text()
                      .'</div>';
                //~ }
                break;
        }
        unset($parser->mSwappedOutText);
        return false;
    }

    /**
     * Flags when an edit preview is taking place.
     * Usage: $wgHooks['EditPage::showEditForm:initial'][] = 'wfAlternateSyntaxFlagEditPreview';
     * @param EditPage $editpage Instance of EditPage performing a preview.
     * @return Boolean Always true.
     */
    function flagEditPreview( &$editpage ) {
        $this->mEditPreviewFlag = true;
        return true;
    }

    function flagEditPreviewPreview( $editPage, &$toparse ) {
        $this->mEditPreviewFlag = true;
        return true;
    }

    /**
     * Unsets edit preview flag
     * @param EditPage $editpage Instance of EditPage performing a preview.
     * @param OutputPage $out Instance of OutputPage during render ($wgOut).
     * @return Boolean Always true.
     */
    function unflagEditPreview( &$editpage, &$out ) {
        // if (isset($this->mEditPreviewFlag)) unset($this->mEditPreviewFlag);
        $this->mEditPreviewFlag = false;
        return true;
    }

}

# Create global instance and wire it up!
$wgAlternateSyntaxParser = new AlternateSyntaxParser();
$wgExtensionFunctions[] = array($wgAlternateSyntaxParser, 'setup');
if (version_compare($wgVersion, '1.6', '>')) {

    # Standard hook mechanism for more recent MW versions
    $wgHooks['ParserBeforeStrip'][] = array($wgAlternateSyntaxParser, 'swapOutText');
    $wgHooks['ParserBeforeTidy'][] = array($wgAlternateSyntaxParser, 'swapInText');
    $wgHooks['EditPage::showEditForm:initial'][] = array($wgAlternateSyntaxParser, 'flagEditPreview');
    $wgHooks['EditPage::showEditForm:fields'][] = array($wgAlternateSyntaxParser, 'unflagEditPreview');
    $wgHooks['EditPageGetPreviewText'][] = array($wgAlternateSyntaxParser, 'flagEditPreviewPreview'); // adding this for preview
    $wgHooks['MessagesPreLoad'][] = array($wgAlternateSyntaxParser, 'onMessagesPreLoad'); //adding this for messages

} else {

    # Hack solution to resolve 1.6 array parameter nullification for hook args
    function wfAlternateSyntaxParserSwapOutText( &$parser, &$text ) {
        global $wgAlternateSyntaxParser;
        return $wgAlternateSyntaxParser->swapOutText( $parser, $text );
    }
    function wfAlternateSyntaxParserSwapInText( &$parser, &$text ) {
        global $wgAlternateSyntaxParser;
        return $wgAlternateSyntaxParser->swapInText( $parser, $text );
    }
    function wfAlternateSyntaxParserFlagEditPreview( &$editpage ) {
        global $wgAlternateSyntaxParser;
        return $wgAlternateSyntaxParser->flagEditPreview( $editpage );
    }
    function wfAlternateSyntaxParserUnflagEditPreview( &$editpage, &$out ) {
        global $wgAlternateSyntaxParser;
        return $wgAlternateSyntaxParser->unflagEditPreview( $editpage, $out );
    }
    $wgHooks['ParserBeforeStrip'][] = 'wfAlternateSyntaxParserSwapOutText';
    $wgHooks['ParserBeforeTidy'][] = 'wfAlternateSyntaxParserSwapInText';
    $wgHooks['EditPage::showEditForm:initial'][] = 'wfAlternateSyntaxParserFlagEditPreview';
    $wgHooks['EditPage::showEditForm:fields'][] = 'wfAlternateSyntaxParserUnflagEditPreview';
}

} # End MW Environment wrapper
//< /source>
?>