Newer
Older
scim-wnn / honoka-plugins / jsfilter / src / jsfilter.cpp
/***************************************************************************
 *   Copyright (C) 2006 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   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 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "jsfilter.h"

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif


#ifdef HAVE_GETTEXT
  #include <libintl.h>
  #define _(String) dgettext(GETTEXT_PACKAGE,String)
  #define N_(String) (String)
#else
  #define _(String) (String)
  #define N_(String) (String)
  #define bindtextdomain(Package,Directory)
  #define textdomain(domain)
  #define bind_textdomain_codeset(domain,codeset)
#endif


HonokaPluginRegister(JSFilter);
HonokaPluginSetup(JSFilter);

HonokaSetupCorePage *JSFilter::setup()
{
    bindtextdomain (GETTEXT_PACKAGE, HONOKA_LOCALEDIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    HonokaSetupPage *page = new HonokaSetupPage(_("JSFilter-plugin"),"","");
    page->append(new HonokaSetupFileItem(
        _("JSFilter f_ile: "),
        HONOKA_CONFIG_JSFILTER_FILE,
        _("set JSFilter file in this entry."),
        HONOKA_DEFAULT_JSFILTER_FILE
    ));
    page->append(new HonokaSetupEntryItem(
        _("JSFilter f_unction: "),
        HONOKA_CONFIG_JSFILTER_FUNC,
        "",
        HONOKA_DEFAULT_JSFILTER_FUNC
    ));
    HonokaSetupPage *sc = new HonokaSetupPage(_("shortcut keys: "),"","");
    sc->append(new HonokaSetupKeyItem(
        _("JSFilter Filter: "),
        String(HONOKA_CONFIG_KEY_FILTER_PREFIX) + String("/JSFilter"),
        "",
        ""
    ));
    page->append(sc);
    return page;
};


JSFilter::JSFilter(ConfigPointer cfg) : TextFilter(cfg)
{
    file = cfg->read(String(HONOKA_CONFIG_JSFILTER_FILE),
        String(HONOKA_DEFAULT_JSFILTER_FILE));
    func = cfg->read(String(HONOKA_CONFIG_JSFILTER_FUNC),
        String(HONOKA_DEFAULT_JSFILTER_FUNC));
    jsproc = new JSProc(file);
}


JSFilter::~JSFilter()
{
}


String JSFilter::getName()
{
    return String("JSFilter");
}


String JSFilter::getPropertyName()
{
    return String(_("JSFilter"));
}

const WideString JSFilter::filter(const WideString &text)
{
    String script;
    String sanit;
    //String func = "misakura(\"%%\").toString();";
    String str = utf8_wcstombs(text);
    for(unsigned int i = 0;i < str.length();i ++) {
        if (str[i] == '\"') sanit += "\\\"";
        else sanit += str[i];
    }
    for(unsigned int i = 0;i < (func.length() - 1);i ++) {
        if ((func[i] == '%') && (func[i + 1] == '%')) {
            script += sanit;
            i ++;
        } else script += func[i];
    }
    script += func[func.length() - 1];
    
    string res = jsproc->eval(string(script.c_str(),strlen(script.c_str())));
    if (!res.length()) return text;
    String s;
    for(unsigned int i = 0;i < res.length(); i ++)
        if (res[i] != '\n') s += res[i];
    return utf8_mbstowcs(s);
}