getNamespace() != NS_SPECIAL and $wgUser->getID() != 0 and $action != 'submit' and $action != 'edit') { $content_actions['pdf'] = array( 'class' => ($action == 'latex') ? 'selected' : false, 'text' => "latex+pdf (experimental)", 'href' => $wgTitle->getLocalUrl( 'action=latex' ) ); } } function handle_escape_mathtag($matches) { $needles = array("|-", "|"); $replacements = array("|-", "|"); return str_replace($needles, $replacements, $matches[1]); } function handle_table_cell($matches) { } function handle_table($matches) { $matches = preg_replace_callback("/((\s|.)*?)<\/math>/", 'handle_escape_mathtag', $matches[1]); $offset = 0; $i = 0; $rows = array(); $last = 0; if( ($loc = strpos($matches, '|-')) === false) ;//$rows[$i] = $matches; else { $last = $loc; while( !( $loc = strpos($matches, '|-', $last+2)) === false ) { $rows[$i] = substr($matches, $last+2, $loc-$last-2); $last = $loc; $i++; } } //echo 'Equation : $i = '.$i.'
';
	//print_r($rows);
	//echo '
'; $result = ''; if($i) { $pos = strstr($rows[0], '|'); if(! ($pos===false) ) $rows[0][$pos+1] = ' '; for($r = 0; $r <= $i-1; $r++) $result .= str_replace( '|', '&', $rows[$r] )."\\\\"; if($i > 1) $result .= str_replace( '|', '&', $rows[$i-1] ); } else $result = $matches; $replacements = array("|-", "|"); $needles = array("|-", "|"); return "\begin{align}\r\n" . str_replace($needles, $replacements, $result) . "\r\n\end{align}"; } function getPageParams() { global $wgRequest; $p = array(); if( $wgRequest->getVal( 'action' ) == 'submit') { $p['map'] = trim($wgRequest->getText( 'map' )); $p['start'] = trim($wgRequest->getText( 'start' )); $p['end'] = trim($wgRequest->getText( 'end' )); } return $p; } function create_latex() { global $wgOut; global $wgWikiURL; global $wgArticlePath; global $wgSitename; global $wgRequest; global $wgArticle; global $wgTitle; $thetitle = $wgRequest->getVal( 'title' ); $title = Title::newFromText($thetitle ); $Article = new Article( $title ); $Article->getContent(); $bloodyTempVar = $Article->getTitle(); if ($wgWikiURL == ""){ $wgWikiURL = "Set \\\\$ \!wgWikiURL on LocalSettings.php"; } $content = $Article->getContent( ); $content = preg_replace("/==((\s|.)+?)==/", "\\section{\\1}", $content ); $content = preg_replace("/===((\s|.)+?)===/", "\\subsection{\\1}", $content ); //$content = preg_replace("/((\s|.)+?)<\/math>/", "\\begin{equation} \\1 \\end{equation}", $content ); $content = preg_replace_callback("/{\|((\s|.)+?)\|}/", "handle_table", $content ); // equation on it's own line $content = preg_replace("/:((\s|.)+?)<\/math>((\s|.)+?)[\r\n]+/", "\\begin{equation} \\1 \\2\\end{equation}\r\n", $content ); $content = preg_replace("/((\s|.)+?)<\/math>/", "\\( \\1 \\)", $content ); $bloodyTempVar = $Article->getTitle(); $title = $bloodyTempVar->GetText(); $url = $wgWikiURL.str_replace('$1', $title, $wgArticlePath.$bloodyTempVar->mTitle->mUrlform); $ext_dir = "extensions/wikilatex"; $tmprand = md5(title); $mytemp = $ext_dir."/tmp/".$tmprand; if (is_dir($mytemp ) || @mkdir($mytemp ,0777)) ; //echo '
';
	//print_r($wgArticle);
	//echo '
'; //echo $url; //echo $ext_dir.'/blah.tex'; $latexfile = fopen($mytemp.'/t.tex', 'w'); $preamble = '\documentclass[11pt]{article} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{mathrsfs} \title{'.$title.'} \begin{document} \maketitle Retrieved from '.$wgSitename.'\newline '.$url.' '; fputs($latexfile, $preamble); fputs($latexfile, strip_tags($content).'\end{document}'); fclose($latexfile); $thedir = dirname( __FILE__ )."/tmp/".$tmprand; //$runpdflatex = "TEXMFOUTPUT=".$thedir." TEXINPUTS=extensions/wikipdf/latex:extensions/wikipdf/font:".$mytemp.": TEXFONTS=extensions/wikipdf/font: pdflatex --interaction nonstopmode ".$thedir."/t.tex >".$thedir."/t.log"; $pdflatexcommand = "cd ".$thedir."; pdflatex --interaction nonstopmode ".$thedir ."/t.tex >".$thedir ."/t.log"; //echo "
".$pdflatexcommand ; // Run pdflatex three times so that all references (TOC, etc.) are resolved. shell_exec($pdflatexcommand ).'
'; shell_exec($pdflatexcommand ).'
'; shell_exec($pdflatexcommand ).'
'; //system($runpdflatex); //system($runpdflatex); //system($runpdflatex); $newtitle = escapeshellarg(str_replace('/', '-', $title)); system("mv ".$thedir."/t.pdf ".$thedir."/\"".$newtitle .".pdf\""); system("mv ".$thedir."/t.tex ".$thedir."/\"".$newtitle .".tex\""); system("mv ".$thedir."/t.aux ".$thedir."/\"".$newtitle .".aux\""); system("mv ".$thedir."/t.log ".$thedir."/\"".$newtitle .".log\""); // Just a hack so that non-root can delete stale tempdirs on the server foreach (glob($thedir."/*") as $tfile) chmod($tfile, 0777); $wgOut->addHTML( 'PDF | LaTeX'); // -- pdflatex log -- error log w2latex' ); $wgOut->addHTML('

Wiki to LATEX conversion

Written by Lionel Brits.

');// ' ); } function unknown_action($action, $wgArticle){ global $wgLogo; global $wgSitename; global $wgLanguageCode; global $wgWikiURL; global $wgPDFMessage; global $wgInputEnc; global $wgLicense; global $wgScriptPath; global $wgOut; if($action == 'latex') { $wgOut->setPagetitle( "Wiki to LaTeX conversion" ); create_latex(); } /*if ($action == 'pdf'){ #thanks to Yaroslav Fedevych for his PHP advices $bloodyTempVar = $wgArticle->getTitle(); $title = $bloodyTempVar->GetText(); global $wgOut; $wgOut->setPagetitle( "WikiPDF" ); $wgOut->setSubtitle( $title ); $wgOut->addHTML( '
From article:

Alternative title to be displayed on the document:

Message that will be displayed on the document header:

Select the desired LaTeX template to be used:
'); } if ($action == 'createpdf'){ global $wgRequest; global $wgLaTeXTemplate; global $wgPDFMessage; $wgLaTeXTemplate = $wgRequest->getVal( 'template' ); $wgPDFMessage = $wgRequest->getVal( 'PdfMsg' ); $PDFtitle = $wgRequest->getVal( 'PDFtitle' ); global $wgOut; $wgOut->setPagetitle( "WikiPDF" ); $wgOut->setSubtitle( $PDFtitle ); pdf_create( $PDFtitle ); }*/ } $wgHooks['SkinTemplateContentActions'][] = 'latex_tab'; $wgHooks['UnknownAction'][] = 'unknown_action'; ?>