Chatags/code.js

/**************************************************************************/ //* BBCode parser for Wikia chats as part of the ChatTags script. //* //* Copyright 2013 Benjamin Williams <[ Lil' Miss Rarity ]> //* //* Script was created by User:AnimatedCartoons //* //* Version: v0.1.6 //* //* 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 3 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, see . /**************************************************************************/ var chatags = window.chatags || {}; //These should never be touched! chatags.regexp = { master: /\[([^\[\]]{1,})\]([^\[\]]{1,})\[\/([^\[\]]{1,})\]/, split: /(\^\[\{1,}\]|\[\/[^\[\]]{1,}\])/g, endTag: /\[\/([^\[\]]{1,})\]/, beginTagA: /\[(\S{1,})(\s[^\[\]]{1,})\]/, beginTagB: /\[([^\[\]]{1,})\]/ }; chatags.bbCode = { "b": { 0: {           "bbFind": /\[\/b\]/, "bbReplace": " " },       1: {            "bbFind": /\[b\]/, "bbReplace": "" }   },    "bg": { 0: {           "bbFind": /\[\/bg\]/, "bbReplace": " " },       1: {            "bbFind": /\[bg ([^\[\]\\"]{1,})\]/,            "bbReplace": ""        }    },    "big": {        0: {            "bbFind": /\[\/big\]/,            "bbReplace": " "        },        1: {            "bbFind": /\[big\]/,            "bbReplace": ""        }    },    "c": {        0: {            "bbFind": /\[\/c\]/,            "bbReplace": " "        },        1: {            "bbFind": /\[c ([^\[\]\\"]{1,})\]/, "bbReplace": "" }   },    "code": { 0: {           "bbFind": /\[\/code\]/, "bbReplace": " " },       1: {            "bbFind": /\[code\]/, "bbReplace": "" }   },    "i": { 0: {           "bbFind": /\[\/i\]/, "bbReplace": " " },       1: {            "bbFind": /\[i\]/, "bbReplace": "" }   },    "small": { 0: {           "bbFind": /\[\/small\]/, "bbReplace": " " },       1: {            "bbFind": /\[small\]/, "bbReplace": "" }   },    "sub": { 0: {           "bbFind": /\[\/sub\]/, "bbReplace": " " },       1: {            "bbFind": /\[sub\]/, "bbReplace": " " }   },    "s": { 0: {           "bbFind": /\[\/s\]/, "bbReplace": " " },       1: {            "bbFind": /\[s\]/, "bbReplace": "" }   },    "sup": { 0: {           "bbFind": /\[\/sup\]/, "bbReplace": " " },       1: {            "bbFind": /\[sup\]/, "bbReplace": " " }   },    "u": { 0: {           "bbFind": /\[\/u\]/, "bbReplace": " " },       1: {            "bbFind": /\[u\]/, "bbReplace": "" }   } }; chatags.parse = function(string) { if (string.match(chatags.regexp.master)) { var matches = string.match(chatags.regexp.split); var keys = []; var vals = []; //Populates keys and vals with data about the bbcode for (var i = 0; i < matches.length; i++) { var match = matches[i]; if (match.indexOf("/") == 1) { match = match.replace(chatags.regexp.endTag, "$1"); keys.push(match); vals.push(0); } else if (match.indexOf("/") == -1) { if (match.indexOf(" ") > -1) { match = match.replace(chatags.regexp.beginTagA, "$1"); } else { match = match.replace(chatags.regexp.beginTagB, "$1"); }               keys.push(match); vals.push(1); } else { continue; }       }        for (var i = 0; i < keys.length; i++) { if (typeof chatags.bbCode[keys[i]] === "undefined") { continue; } else { var f = chatags.bbCode[keys[i]][vals[i]]["bbFind"]; var r = chatags.bbCode[keys[i]][vals[i]]["bbReplace"]; string = string.replace(f, r); }       }    }    return string; }; //Run parser on new message mainRoom.model.chats.bind("afteradd", function(c) {   var string = $("#Chat_" + roomId + " .message:last").html;        //Run parser        string = chatags.parse(string);    $("#Chat_" + roomId + " .message:last").html(string); }); //