Add custom function

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
abhi

Add custom function

Post by abhi »

I post math stuff on my blog, so I have mime tex installed. To post an equation, I have to type in <img src="file.cgi?LATEX CODE . What I want to do is have a custom funtion, so that whenever I type in $LATEX CODE$ ,it is converted to the image tag and show. Can you plz help me?

Thanks.
Abhi.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Add custom function

Post by garvinhicking »

You can use a simple plugin for this. Save the following code as /plugins/serendipity_event_latex/serendipity_event_latex.php.

I needed to modify the code a bit. You need to use $$LATEX
CODE$$ and it will transfer that. The double $$ was used so that you will still be able to use the $ sign in entries.

You may need to edit the string "/cgi-bin/latex.cgi" in the code to point to the right .cgi file!

Code: Select all

<?php # $Id: serendipity_event_latex.php,v 1.13 2005/01/04 10:06:34 garvinhicking Exp $

switch ($serendipity['lang']) {
    case 'en':
    default:
        @define('PLUGIN_EVENT_LATEX_NAME',     'Markup: Latex-Code');
        @define('PLUGIN_EVENT_LATEX_DESC',     'Markup text using LATEX and cgi transcoding');
        break;
}

class serendipity_event_latex extends serendipity_event
{
    var $title = PLUGIN_EVENT_LATEX_NAME;
    function introspect(&$propbag)
    {
        global $serendipity;

        $propbag->add('name',          PLUGIN_EVENT_LATEX_NAME);
        $propbag->add('description',   PLUGIN_EVENT_LATEX_DESC);
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Garvin Hicking');
        $propbag->add('version',       '1.0');
        $propbag->add('cachable_events', array('frontend_display' => true));
        $propbag->add('event_hooks',   array('frontend_display' => true));

        $this->markup_elements = array(
            array(
              'name'     => ENTRY_BODY,
              'element'  => 'body',
            ),
            array(
              'name'     => EXTENDED_BODY,
              'element'  => 'extended',
            ),
            array(
              'name'     => COMMENT,
              'element'  => 'comment',
            ),
            array(
              'name'     => HTML_NUGGET,
              'element'  => 'html_nugget',
            )
        );

        $conf_array = array();
        foreach($this->markup_elements as $element) {
            $conf_array[] = $element['name'];
        }
        $propbag->add('configuration', $conf_array);

    }

    function generate_content(&$title) {
        $title = $this->title;
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            default:
                $propbag->add('type',        'boolean');
                $propbag->add('name',        $name);
                $propbag->add('description', sprintf(APPLY_MARKUP_TO, $name));
                $propbag->add('default', 'true');
                break;
        }
        return true;
    }

    function install() {
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    function uninstall() {
        serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    function event_hook($event, &$bag, &$eventData) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
                case 'frontend_display':

                    foreach ($this->markup_elements as $temp) {
                        if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']])) {
                            $element = $temp['element'];
                            $eventData[$element] = preg_replace('@\$\$(.*)\$\$@', '<img src="/cgi-bin/latex.cgi?\1" alt="\1" />', $eventData[$element]);
                        }
                    }
                    return true;
                    break;

                default:
                  return false;
            }
        } else {
            return false;
        }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
abhi

thanks

Post by abhi »

Thank you :D.
Post Reply