/*-*- Mode: C++; tab-width: 4; -*-
    abbrtags plugin for Chatzilla. Insert <abbr> tags into Chatzilla's output.
    Copyright (C) 2004  John J Foerch

    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
*/

var State = {
    pluginLocation: String(this.plugin.url).replace(/\/[^\/]*$/,'/'),
    dictionaryFileName: 'abbrtags.txt',
    dictionary: new Object(),
    mungeMatch: '',
    error: false
}


function initPlugin () {
    plugin.id = 'abbrtags';
    plugin.major = 0;
    plugin.minor = 1;
    plugin.version = plugin.major +'.'+ plugin.minor;
    plugin.description = 'Inserts <abbr> tags into Chatzilla\'s output based on entries in the file abbrtags.txt';

    loadDictionary();
}

function enablePlugin () { plugin.enabled = true; }
function disablePlugin () { plugin.enabled = false; }

function loadDictionary () {
    var dictionaryURL = State.pluginLocation + State.dictionaryFileName;

    var localName = getFileFromURLSpec (dictionaryURL);
    var f = new nsLocalFile(localName.path);
    //does the file exist?
    if (! f.exists()) {
	//do error and return
	State.error = true;
	return;
    }
    f = fopen (f.path,'<');
    var content = f.read();
    f.close();

    //line by line
    var content_lines = content.split (/\n+/);
    var re = /^(\S+)\s+(.+)$/;

    for (var i in content_lines) {
	if (content_lines[i].substr(0,1) == '#') { continue; }
	var match;
	if (match = content_lines[i].match(re)) {
	    State.dictionary[match[1]] = match[2];
	}
    }

    //make munger
    var sorted = keys(State.dictionary);
    sorted.sort(function (a,b) {return b.length - a.length;});
    for (v in sorted) {
	if (State.mungeMatch != '') { State.mungeMatch += '|'; }
	State.mungeMatch += regexEscape(sorted[v]);
    }
	
    State.mungeMatch = '(?:^|\\s)(' + State.mungeMatch + ')(?:\\s|\\.|\\?|!|$)';

    //register Munger
    client.munger.addRule('abbrtags', RegExp(State.mungeMatch),mungeAbbreviations);

    function regexEscape (s) {
	var re = /\\|\^|\$|\*|\+|\?|\(|\)|\||\{|\}|\[|\]|\./g;
	s=s.replace(re,"\\$&");
	return s;
    }

}


function mungeAbbreviations (matchText, containerTag) {

    var enabled = plugin.enabled;
    var valid = (! State.error);

    if (enabled && valid) {
	abbreviationTag (State.dictionary[matchText]);
    } else {
	abbreviationText();
    }

    function abbreviationTag (title) {
	var anchor = document.createElementNS("http://www.w3.org/1999/xhtml", "html:abbr");
	anchor.setAttribute('title', title);
	anchor.appendChild (document.createTextNode(matchText));
	containerTag.appendChild(anchor);
    }

    function abbreviationText () {
	containerTag.appendChild(document.createTextNode(matchText));
    }
}
