Extension:Wiki2LaTeX/Development/w2lTags.php

From MediaWiki.org
Jump to: navigation, search
<?php

/*
 * File:    w2lTags.php
 * Created: 2007-09-01
 * Version: 0.7
 *
 * Purpose:
 * Provides some xml-style Parser-Extension-Tags to Mediawiki and W2L
 *
 * License:
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

if ( !defined('MEDIAWIKI') ) {
        $msg  = 'To install Wiki2LaTeX, put the following line in LocalSettings.php:<br/>';
        $msg .= '<tt>require_once( $IP."/extensions/path_to_Wiki2LaTeX_files/wiki2latex.php" );</tt>';
        echo $msg;
        exit( 1 );
}

if ( !defined('MEDIAWIKI') )
        die();

/* W2l-Parser-Extensions */
// Within the folder, there needs to be geshi... needs to be documented better
if ( !class_exists( "GeSHi" ) AND $w2lConfig['enable_geshi'] ) {
        require_once('geshi.php');
}

class Wiki2LaTeXTags {
        function Setup() {
                global $wgParser, $w2lTags;
                // Register Extension-Tags to Mediawiki...
                
                // LaTeX-commands, which we want to offer to a wiki-article
                $wgParser->setHook("noindent",    array($this, "NoIndent"));
                $wgParser->setHook("newpage",     array($this, "NewPage"));
                $wgParser->setHook("label",       array($this, "Label"));
                $wgParser->setHook("pageref",     array($this, "PageRef"));
                $wgParser->setHook("chapref",     array($this, "ChapRef"));
                
                // Extension-tags, which we need for w2l:
                $wgParser->setHook("templatevar", array($this, "TemplateVar"));
                $wgParser->setHook("latex",       array($this, "Latex"));
                $wgParser->setHook("latexpage",   array($this, "LatexPage"));
                $wgParser->setHook("latexfile",   array($this, "LatexFile"));
                
                // By this one, you can directly input latex to a wiki article. LaTeX code is not interpreted in wiki-mode, though.
                $wgParser->setHook("rawtex",      array($this, "RawTex"));

                // Some default ones
                $w2lTags['math']   = array($this, 'math');
                $w2lTags['pre']    = array($this, 'pre');
                $w2lTags['rawtex'] = array($this, 'rawtex');
                
                // Some Extensions, which return LaTeX-commands
                $w2lTags['newpage'] = array($this, 'NewPage');
                $w2lTags['noindent'] = array($this, 'NoIndent');
                $w2lTags['label']    = array($this, 'Label');
                $w2lTags['pageref']  = array($this, 'PageRef');
                $w2lTags['chapref']  = array($this, 'ChapRef');
                
                // Default-support for some other mediawiki-extensions
                $w2lTags['ref']    = array($this, 'ref');
                $w2lTags['source'] = array($this, 'sourcecode');
                
                // These Tags should not return a value in LaTeX-Mode.
                $w2lTags['references']  = array($this, 'EmptyTag');
                $w2lTags['subpages']    = array($this, 'EmptyTag');
                $w2lTags['templatevar'] = array($this, 'EmptyTag');
                $w2lTags['latexpage']   = array($this, 'EmptyTag');
                $w2lTags['latex']       = array($this, 'EmptyTag');
        }

        function Latex($input, $argv, &$parser, $mode = 'wiki') {
                $input = trim($input);
                $geshi = new GeSHi($input, 'latex');

                $geshi->set_encoding('UTF-8');
                $geshi->set_header_type(GESHI_HEADER_DIV);
                $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
                $geshi->set_line_style('margin:0;padding:0;', 'margin:0;padding:0;', true);

                $output = $geshi->parse_code();
                return $output;
        }

        function LatexPage($input, $argv, &$parser, $mode = 'wiki') {

                switch ($mode) {
                        case 'wiki':
                                $output = "<p><strong>Benoetigte Datei</strong>: ".$input."</p>";
                                $output = "'''Benötigte Datei''': [[".$input."]]";
                                $output = $parser->recursiveTagParse($output);
                                return $output;
                        break;
                        case 'latex': $output = '';
                        break;
                        default: echo $mode;
                }
        }

        function LatexFile($input, $argv, &$parser, $mode = 'wiki') {
                $output = '<strong>Dateiname:</strong> '.$argv['name'].'.tex';
                $input = trim($input);
                $geshi = new GeSHi($input, 'latex');
                $geshi->set_encoding('UTF-8');
                $geshi->set_header_type(GESHI_HEADER_DIV);
                $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
                $geshi->set_line_style('margin:0;padding:0;', 'margin:0;padding:0;', true);
                $output.= $geshi->parse_code();
                return $output;
        }

        function TemplateVar($input, $argv, &$parser, $mode = 'wiki') {
                $output = "<p><strong>".$argv['vname']."</strong>: ".$input."</p>";
                return $output;
        }

        // Latex-Befehle f�r Wiki
        function NoIndent($input, $argv, &$parser, $mode = 'wiki') {
                switch($mode) {
                        case 'latex':
                                return '{\noindent}';
                        default:
                                return "";
                }
        }

        function NewPage($input, $argv, &$parser, $mode = 'wiki') {
                switch($mode) {
                        case 'latex':
                                return '\newpage{}';
                        default:
                                return '';
                }
        }

        function Label($input, $argv, &$parser, $mode = 'wiki') {
                switch($mode) {
                        case 'latex':
                                $output = '\label{'.$input.'}';
                        break;
                        default:
                                $output = ' <strong>(Anker: '.$input.'<a id="'.$input.'"></a>)</strong>';
                        break;
                }
                return $output;
        }

        function PageRef($input, $argv, &$parser, $mode = 'wiki') {
                switch($mode) {
                        case 'latex':
                                $output = '\page{'.$input.'}';
                        break;
                        default:
                                $output = '<a href="#'.$input.'" title="'.$input.'">S. XX</a>';
                        break;
                }
                return $output;
        }

        function ChapRef($input, $argv, &$parser, $mode = 'wiki') {
                switch($mode) {
                        case 'latex':
                                $output = '\ref{'.$input.'}';
                        break;
                        default:
                                $output = '<a href="#'.$input.'" title="'.$input.'">X.Y</a>';
                        break;
                }
                return $output;
        }

        function Rawtex($input, $argv, &$parser, $mode = 'wiki') {
                global $w2l_config;
                switch ($mode) {
                        case 'latex': $output = $input;
                        break;
                        case 'wiki':
                                $input = trim($input);
                                if ( $w2l_config['enable_geshi'] == true ) {
                                        $geshi = new GeSHi($input, 'latex');
                                        $geshi->set_encoding('UTF-8');
                                        $geshi->set_header_type(GESHI_HEADER_DIV);
                                        $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
                                        $geshi->set_line_style('margin:0;padding:0;', 'margin:0;padding:0;', true);
                                        $output = $geshi->parse_code();
                                } else {
                        $output = $input;
                                }
                        break;
                        default: $output = $input;
                        break;
                }
                return $output;
        }

        function EmptyTag($input, $argv, &$parser, $mode = 'wiki') {
                if ( $mode == 'latex' ) {
                        return '';
                } else {
                        return $input;
                }
        }


        function ref($input, $argv, &$parser, $mode = 'wiki') {
                $input = $parser->recursiveTagParse($input);
                return '\footnote{'.trim($input).'}';
        }

        function pre($input, $argv, &$parser, $mode = 'wiki') {
                //$input = $parser->recursiveTagParse($input);
                $output  = "\n\begin{verbatim}\n";
                $output .= trim($input)."\n";
                $output .= "\end{verbatim}\n";
                return $output;
        }

        function sourcecode($input, $argv, &$parser, $mode = 'wiki') {
           $parser->addPackageDependency('listings');
                $language = $argv['lang'];
                $output   = "\lstset{language=$language}\n\\begin{lstlisting}\n";
                $output  .= $input;
                $output  .= "\\end{lstlisting}\n";
                return $output;
        }



        function math($input, $argv, &$parser, $mode = 'wiki') {
                $output  = "\n\begin{math}\n";
                $output .= trim($input)."\n";
                $output .= "\end{math}\n";
                return $output;
        }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox