/*
 * https ; gemini ;
 * tout ; log ; twtxt ;
 * Γ  propos ; βˆ€ ;
 */

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@si3t.ch>
// 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);