<?php
//
// imgtex.inc.php -- imgtex plugin for pukiwiki
//
// Copyright (C) 2005 Koji Nakamaru
//
// Author: Koji Nakamaru <maru@on.cs.keio.ac.jp>
// Created/Modified: Jan 21 2005
// Keywords: tex, latex, math, web, imgtex
//
// 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, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GNU Emacs; see the file COPYING.  If not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// example usage:
/*

This is an example equation:
&imgtex($ \exp x=1+x+\frac{1}{2!}x^2+\frac{1}{3!}x^3+... $);,
which is inlined.

Centering can be done as follows:
CENTER:&imgtex(\[ \exp x=1+x+\frac{1}{2!}x^2+\frac{1}{3!}x^3+... \]);
CENTER:&imgtex([res=200]{\[ \exp x=1+x+\frac{1}{2!}x^2+\frac{1}{3!}x^3+... \]});

*/
//

define('IMGTEX_PATH', '/~maru/imgtex/imgtex.fcgi');  // please specify your own imgtex.fcgi
define('IMGTEX_RES', '120');

function plugin_imgtex_inline()
{
    $args = func_get_args();
    $args = join($args, ' ');
    $args = preg_replace('/^\s+/', '', $args);
    $args = preg_replace('/\s+$/', '', $args);
    if (preg_match('/^\[/', $args)) {
 	$url = $args;
    } elseif (preg_match('/^{.*}$/', $args)) {
 	$url = '[res=' . IMGTEX_RES . ']' . $args;
    } else {
 	$url = '[res=' . IMGTEX_RES . ']{' . $args . '}';
    }
    preg_match('/^\[res=\d+\]{(.*)}$/', $url, $matches);
    $alt = $matches[1];
    // some preg_replace() seems to have a bug cannot handle
    // backslash-escaping correctly, so we use octal expressions.
    $alt = preg_replace('/^\s*(\134\133|\$+)\s*/', '', $alt);
    $alt = preg_replace('/\s*(\134\135|\$+)\s*$/', '', $alt);
    $url = IMGTEX_PATH . '?' . $url;
    return "<img src=\"$url\" alt=\"$alt\" style=\"text-align:middle;margin:.25em .125em\" />";
}
?>
