Extension talk:SyntaxHighlight vim

From mediawiki.org
Jump to navigation Jump to search

I had to modify SyntaxHook.php to get it working:

$> diff -u SyntaxHook.php.orig SyntaxHook.php
--- SyntaxHook.php.orig 2007-09-15 21:16:34.000000000 +0200
+++ SyntaxHook.php      2007-09-15 21:45:41.000000000 +0200
@@ -9,6 +9,8 @@
  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  */
 
+class_exists('Syntax') || require 'Syntax.php';
+
 $wgExtensionFunctions[] = 'wfSyntaxHook';
 $wgExtensionCredits['parserhook'][] = array(
        'name' => 'Syntax',

Specifying language[edit]

I have modified SyntaxHook.php and Syntax.php to allow you to specify the language that you want to use with, for example <syntax lang="php"> - ZeBadger

--- Syntax.php.orig     Thu Nov 13 13:35:18 2008
+++ Syntax.php  Thu Nov 13 13:35:17 2008
@@ -22,6 +22,7 @@

     class Syntax {
        private $mIn;
+       private $mFormat;
        private $mInFile, $mOutFile;
        private $mVimrc;

@@ -29,15 +30,16 @@
                $this->mVimrc = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'vimrc.vim';

                $this->mIn = $in;
+               $this->mFormat = $format;
        }

        public function getOut() {
                $this->genTemp();

                if ( ! $handle = fopen( $this->mInFile, 'a' ) )
-                       die( 'AAAAAAA' );
+                       die( 'Unable to create tmpfile' );
                if ( fwrite( $handle, $this->mIn ) === false )
-                       die( 'OOOOOOOOO' );
+                       die( 'Unable to write to tmpfile' );

                $html = $this->run();

@@ -55,7 +57,7 @@
        }

        private static function mktemp() {
-               return rtrim( shell_exec( 'mktemp -u' ), "\n" );
+               return rtrim( shell_exec( 'mktemp -u /tmp/syntax.XXXXXX' ), "\n" );
        }

        private function rmTemp() {
@@ -64,7 +66,15 @@
        }

        private function run() {
-               shell_exec( "vim -u {$this->mVimrc} -e +'run! syntax/2html.vim' +':w {$this->mOutFile}' +':qa!' {$this->mInFile}" );
+               if ($this->mFormat != NULL)
+               {
+                       $lang="+':set syn=".$this->mFormat."'";
+               }
+               else
+               {
+                       $lang="";
+               }
+               shell_exec( "vim -u {$this->mVimrc} -e $lang +'run! syntax/2html.vim' +':w {$this->mOutFile}' +':qa!' {$this->mInFile}" );

                return file_get_contents( $this->mOutFile );
        }
--- SyntaxHook.php.orig Thu Nov 13 13:34:52 2008
+++ SyntaxHook.php      Thu Nov 13 13:34:51 2008
@@ -9,6 +9,8 @@
  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  */

+require 'Syntax.php';
+
 $wgExtensionFunctions[] = 'wfSyntaxHook';
 $wgExtensionCredits['parserhook'][] = array(
     'name' => 'Syntax',
@@ -33,7 +35,14 @@

        public function hook( $in, array $argv ) {
                $in = ltrim( $in, "\n" );
-               $syntax = new Syntax( $in );
+               if (isset($argv['lang']))
+               {
+                       $syntax = new Syntax( $in ,$argv['lang']);
+               }
+               else
+               {
+                       $syntax = new Syntax( $in );
+               }

                return $syntax->getOut();
        }