MediaWiki-Artikel automatisch in LaTeX konvertieren

URL dieses Beitrags: http://blog.stefan-macke.com/2007/07/06/mediawiki-artikel-automatisch-in-latex-konvertieren/

Ich würde unsere Wiki-Artikel gerne automatisiert in LaTeX-Dokumente umwandeln, wenn man mal hübsche Ausdrucke braucht. Gerade bei längeren Artikeln wäre das sehr nützlich...

Dazu habe ich die MediaWiki-Extension Wiki2LaTeX gefunden, die schon eine ganze Menge leistet und sehr variabel erweiterbar ist. Was mir aber noch gefehlt hat, ist die Verarbeitung von Bildern. Die Extension bearbeitet diese nämlich nicht.

Daher habe ich mich einmal rangesetzt und eine kleine Klasse entwickelt, die diese Aufgabe übernimmt. Habe es schon auf der Diskussionsseite der Extension veröffentlicht, aber zur Sicherheit kommt's hier nochmal hin ;)

Image Processing

I have implemented a simple solution for processing of internal images. It searches for the filename in the images/ directory and copies it to an image directory under the tmp/tmp-123... directory. You can add my little helper class with the following steps:

First of all you need to add a few lines to function internalLinkHelper in w2lParser.php before the line // First, check for |:

  1. if ( (stripos($link, "Bild:") === 0)  or (stripos($link, "Image:") === 0) ) {
  2.   $link = str_replace('Bild:', '', $link);
  3.   $link = str_replace('Image:', '', $link);
  4.   return "<IMAGE>" . $link . "</IMAGE>";
  5. }

Then you need to include my class file at the top of w2lExporter.php:

  1. require_once('w2lImages.php');

In w2lExporter.php you need to edit function w2l_unknown_action to process the images. I added the following line

  1. $parsed = w2lImages::processImages($parsed, $mytemp);

to the sections where $action is w2lpdf or w2ltex right behind these lines:

  1. $parsed = $parser-&gt;parse($to_parse);
  2. $mytemp = $helper-&gt;path;

And this is my little class file w2lImages.php:

  1. define('W2L_ImageDir', "Bilder");
  2. define('W2L_ImageTitle', "Abbildung");
  3.  
  4. class w2lImages
  5. {
  6.   public static function processImages($parsed, $mytemp)
  7.   {
  8.     $matches = array();
  9.     $matchCount = preg_match_all('/<IMAGE>(.*)</IMAGE>/', $parsed, $matches);
  10.     if ($matchCount &gt; 0)
  11.     {
  12.       $cntr = 0;
  13.       foreach ($matches[1] as $link)
  14.       {
  15.         $imgTag = $matches[0][$cntr];
  16.         $links = explode("|", $link);
  17.         $imgFileName = $links[0];
  18.         if (strpos($imgFileName, 'jpg') != false)
  19.         {
  20.           $imgFile = shell_exec("find ./images -name " . $imgFileName);
  21.           if ($imgFile)
  22.           {
  23.             $imgFiles = explode("n", $imgFile);
  24.             foreach ($imgFiles as $if)
  25.             {
  26.               if (strlen($if) &gt; 0 &amp;&amp; strpos($if, "thumb") == false)
  27.                 $imgFile = $if;
  28.             }
  29.             if (!file_exists($mytemp . "/" . W2L_ImageDir))
  30.             {
  31.               mkdir($mytemp . "/" . W2L_ImageDir, 0777);
  32.             }
  33.             $copied = copy($imgFile, $mytemp . "/" . W2L_ImageDir . "/" . $imgFileName);
  34.             if ($copied)
  35.             {
  36.               $imgCaption = (isset($links[1])) ? $links[1] : W2L_ImageTitle . " " . $cntr;
  37.               $imgLatex = 'begin{figure}[htb]' . "n";
  38.               $imgLatex .= 'centering' . "n";
  39.               $imgLatex .= 'includegraphics[width=textwidth]{'. $imgFileName . '}' . "n";
  40.               $imgLatex .= 'caption{' . $imgCaption . '}' . "n";
  41.               $imgLatex .= 'label{fig:' . W2L_ImageTitle . $cntr . '}' . "n";
  42.               $imgLatex .= 'end{figure}' . "n";
  43.               $parsed = str_replace($imgTag, $imgLatex, $parsed);
  44.             }
  45.             else
  46.             {
  47.               $parsed = str_replace($imgTag, "Image could not be copied: " . $imgFileName, $parsed);
  48.             }
  49.           }
  50.         }
  51.         else
  52.         {
  53.           $parsed = str_replace($imgTag, "Image is not a JPG: " . $imgFileName, $parsed);
  54.         }
  55.         $cntr++;
  56.       }
  57.       return $parsed;
  58.     }
  59.     else
  60.     {
  61.       return $parsed;
  62.     }
  63.   }
  64. }

This will result in images included like this:

  1. \begin{figure}[htb]
  2. \centering
  3. \includegraphics[width=\textwidth]{ImageName.jpg}
  4. \caption{Abbildung 1}
  5. \label{fig:Abbildung1}
  6. \end{figure}

Any comments are welcome ;)

Füge diesen Artikel zu deinen Bookmarks hinzu Diese Icons verzweigen auf soziale Netzwerke bei denen Nutzer neue Inhalte finden und mit anderen teilen können.
  • del.icio.us
  • bodytext
  • MisterWong
  • Reddit
  • Technorati
  • Spurl
  • description

Einen Kommentar schreiben

XHTML: Diese Tags sind erlaubt: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>