Created
September 4, 2018 14:32
-
-
Save redeye86/f9d8102de69606a2984b715cf7537655 to your computer and use it in GitHub Desktop.
Lightweight unicode to emojione converter -- generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This php script generates a simplified javascript library that translates unicode emojis to their emojione graphics. | |
* It uses a space-efficient tree structure. For emojione 4.0 the library has ~38kb / ~5kb gzip compressed. | |
* | |
* | |
* USAGE: | |
* - create a subdirectory containing all emojione images in code point name scheme | |
* - change config in this file (image folder path, file extension, etc.) | |
* - php generate.php > emojione_light.js | |
* - include (<script src="emojione_light.js"></script>) | |
* - convert: ( emojione.convertText("Some Text containing unicode emojis ...") ) | |
*/ | |
//config | |
$file_extension = '.svg'; | |
$emojione_image_folder = 'images/'; | |
$replaced_image_prefix = '<img src="images/'; | |
$replaced_image_postfix = $file_extension . '" class="emoji">'; | |
//config - end | |
function cleaner(&$node){ | |
if(!isset($node['s'])){ | |
unset($node['e']); | |
}else{ | |
foreach($node['s'] AS &$subnode){ | |
cleaner($subnode); | |
} | |
} | |
} | |
$tmpCWD = getcwd(); | |
chdir($emojione_image_folder); | |
$files = glob('*'.$file_extension); | |
chdir($tmpCWD); | |
$files = array_map(function($item) use ($file_extension){ | |
return str_replace($file_extension,'',$item); | |
},$files); | |
usort($files, function($a,$b){ | |
return strlen($a) >= strlen($b) ? 1 : -1; | |
}); | |
$tree = []; | |
$tree['s'] = []; | |
foreach($files AS $file){ | |
$parts = explode('-',$file); | |
$parent = &$tree; | |
$partCount = count($parts); | |
for($i = 0; $i < $partCount; $i++){ | |
$currentCode = $parts[$i]; | |
if(!isset($parent['s'])){ | |
$parent['s'] = []; | |
$parent['s'][$currentCode] = []; | |
} | |
if($i == $partCount - 1){ | |
//we are at the last part for this emoji; add ending marker | |
$parent['s'][$currentCode]['e'] = 1; | |
} | |
$parent = &$parent['s'][$currentCode]; | |
} | |
} | |
cleaner($tree); | |
echo <<< PREFIX | |
(function(ns){ | |
ns.MAX_EMOJI_LENGTH = 7; | |
ns.IMAGE_PREFIX = '{$replaced_image_prefix}'; | |
ns.IMAGE_POSTFIX = '{$replaced_image_postfix}'; | |
ns.emojiMapping = | |
PREFIX; | |
echo '{s:' . str_replace(':[]',':null',preg_replace('/"([a-f][0-9a-f]+|e|s)"/', '$1', json_encode($tree['s']))).'}'; | |
echo <<< POSTFIX | |
; | |
ns.convertText = function(text){ | |
var charCode; | |
var foundLength; | |
var mappingNode; | |
var textArray = Array.from(text); | |
var emojiFile; | |
for(var i = 0; i < textArray.length; i++){ | |
foundLength = 0; | |
mappingNode = ns.emojiMapping; | |
emojiFile = []; | |
for(k = i; k < Math.min(i+ns.MAX_EMOJI_LENGTH, textArray.length); k++){ | |
charCode = textArray[k].codePointAt(0).toString(16); | |
if(charCode == "200d"){ | |
foundLength++; | |
}else if(mappingNode.s.hasOwnProperty(charCode)){ | |
emojiFile.push(charCode); | |
foundLength++; | |
if(mappingNode.s[charCode] != null && mappingNode.s[charCode].hasOwnProperty('s')){ | |
//we found an end of the path | |
mappingNode = mappingNode.s[charCode]; | |
}else{break;} | |
}else{ | |
break; | |
} | |
} | |
if(foundLength > 0){ | |
if(foundLength > 1){ | |
textArray.splice(i+1,foundLength-1); | |
} | |
textArray[i] = ns.IMAGE_PREFIX + emojiFile.join('-') + ns.IMAGE_POSTFIX; | |
} | |
} | |
return textArray.join(''); | |
} | |
}(this.emojione_light = this.emojione_light || {})); | |
POSTFIX; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment