/*
 * ArraKISS; 🚀 Capsule;
 * tout; log; twtxt; à propos & contact;
 */

smileys.js


Un script js pour remplacer les habituels ":)" et autres en emojis correspondants 😁.


// replace smileys string in to emojis 
// 2016, thuban aka prx, <prx@ybad.name>
// Licence MIT

// Edit this array with regex you like
var strtostr= [
    [/(\s|&nbsp;|^)(:\)|:‑\))/g,' 😊'],
    [/(\s|&nbsp;|^):\(/g,' 😞'],
    [/(\s|&nbsp;|^)(:D|:‑D)/g,' 😃'],
    [/(\s|&nbsp;|^)(X|x)D/g,' 😆'],
    [/(\s|&nbsp;|^):(S|s)/g,' 😖'],
    [/(\s|&nbsp;|^):(P|p)/g,' 😋'],
    [/(\s|&nbsp;|^):(:\'‑\)|:\'\))/g,' 😂'],
    [/(\s|&nbsp;|^)(;\)|;‑\))/g,' 😉'],
    [/(\s|&nbsp;|^);(P|p)/g,' 😜'],
    [/(\s|&nbsp;|^):\//g,' 😕'],
    [/(\s|&nbsp;|^):\|/g,'😒'],
    [/(\s|&nbsp;|^):\'\(/g,' 😢'],
    [/(\s|&nbsp;|^)(oO|:O|:-O)/g,' 😲'],
    [/(\s|&nbsp;|^)(:\*|:-\*)/g,' 😗'],
    [/(\s|&nbsp;|^)x\.x/g,' 😵'],
    [/(\s|&nbsp;|^)O:\)/g,' 😇'],
    [/(\s|&nbsp;|^)\^\^/g,' 😁'],
    [/(\s|&nbsp;|^)=\^-\^=/g,' 🐱'],
    [/(\s|&nbsp;|^)(<|&lt;)3/g,' ♥']
];

// class div where regexp will be applied
var classes_to_replace = ["main"];

// regexp to find tags (no replacement in <pre> and <code>)
var htmlTagRegex =/(<[^>]*>)/g

function convert_smileys()
{
    "use strict";

    // loop in classes
    classes_to_replace.forEach(function (class_) {
    	var tochange = document.getElementsByClassName(class_);
    	var codecnt = 0;

    	var classcnt = 0;
    	var div = "";
    	for (classcnt = 0; classcnt < tochange.length; classcnt++) {
    		div = tochange[classcnt]

    		// check if in <code> or <pre>
    		var tagArray = div.innerHTML.split(htmlTagRegex);
    		var divtxt = "";
    		var tagcnt = 0;
    		var t = "";
    		for (tagcnt = 0; tagcnt < tagArray.length; tagcnt++) {
    			t = tagArray[tagcnt];
    			if (t.toLowerCase() == "<pre>" || t == "<code>") {
    				codecnt++;
    			} else if (t.toLowerCase() == "</pre>" || t == "</code>") {
    				codecnt--;
    			}
    		
    			if (codecnt == 0) {
    				var i;
    				var newtxt = "";
    				for (i = 0; i < strtostr.length; i++) {
    					t = t.replace(strtostr[i][0],strtostr[i][1]);
    				}
    			}
    			divtxt += t;
    		}
    		div.innerHTML = divtxt;
    	}
    });
}

window.addEventListener('load', convert_smileys, false);