Extension:Wiki2LaTeX/Development/w2lLaTeXUtil.php

<?php /* * Created on 02.03.2007 * * 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. * * This file contains some parserextensions for mediawiki to teach it some LaTeX. */ 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 $w2l_config['enable_geshi'] ) { require_once('geshi.php'); }

$wgExtensionFunctions[] = "w2lParserExtensions";

function w2lParserExtensions { global $wgParser; $wgParser->setHook("templatevar", array('w2l_extensions', "TemplateVar")); $wgParser->setHook("latex",      array('w2l_extensions', "Latex")); $wgParser->setHook("latexpage",  array('w2l_extensions', "LatexPage")); $wgParser->setHook("rawtex",     array('w2l_extensions', "RawTex")); $wgParser->setHook("latexfile",  array('w2l_extensions', "LatexFile")); $wgParser->setHook("noindent",   array('w2l_extensions', "NoIndent")); $wgParser->setHook("newpage",    array('w2l_extensions', "NewPage")); $wgParser->setHook("label",      array('w2l_extensions', "Label")); $wgParser->setHook("pageref",    array('w2l_extensions', "PageRef")); $wgParser->setHook("chapref",    array('w2l_extensions', "ChapRef")); }

class w2l_extensions { 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 = " Benoetigte Datei : ".$input." "; $output = "Ben&ouml;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 = ' Dateiname: '.$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 = " ".$argv['vname']." : ".$input." "; 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 = ' (Anker: '.$input.') '; break; }  		return $output; }	function PageRef($input, $argv, &$parser, $mode = 'wiki') { switch($mode) { case 'latex': $output = '\page{'.$input.'}'; break; default: $output = 'S. XX'; break; }  		return $output; }	function ChapRef($input, $argv, &$parser, $mode = 'wiki') { switch($mode) { case 'latex': $output = '\ref{'.$input.'}'; break; default: $output = 'X.Y'; 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; }	} }

/* * The following extensions are intended only to be used in the w2l-parser. * They try to be behave as expected under latex-circumstances. */ // class w2l_mwext_support {

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') { //$input = $parser->recursiveTagParse($input); $language=$argv['lang']; $output = "\lstset{language=$language}\n\\begin{lstlisting}\n"; $output .= $input; $output .= "\\end{lstlisting}\n"; return $output; }	function references($input, $argv, &$parser, $mode = 'wiki') { return ''; }	function subpages($input, $argv, &$parser, $mode = 'wiki') { return ''; }	function math($input, $argv, &$parser, $mode = 'wiki') { $output = "\n\begin{math}\n"; $output .= trim($input)."\n"; $output .= "\end{math}\n"; return $output; } }