Browse Source

Merge branch 'teckids-emoji-regex'

pull/650/head
Timothée Jaussoin 8 years ago
parent
commit
36ed608d45
  1. 2
      app/Message.php
  2. 39
      app/helpers/StringHelper.php
  3. 2
      app/widgets/Chat/_chat.tpl
  4. 2
      app/widgets/Notifs/Notifs.php
  5. 2
      app/widgets/Post/Post.php
  6. 1
      composer.json
  7. 46
      composer.lock
  8. 2
      src/Movim/Daemon/Api.php
  9. 100
      src/Movim/Emoji.php
  10. 2663
      src/Movim/Emoji/CompiledEmoji.php
  11. 75
      src/Movim/Emoji/compile-file-list.sh
  12. 19
      src/Movim/Emoji/replace-test.php
  13. 61
      src/Movim/Emoji/test-all-images.sh

2
app/Message.php

@ -259,7 +259,7 @@ class Message extends Model
public function convertEmojis()
{
$emoji = \MovimEmoji::getInstance();
$emoji = \Movim\Emoji::getInstance();
$this->body = addHFR($emoji->replace($this->body));
}

39
app/helpers/StringHelper.php

@ -1,47 +1,8 @@
<?php
use HeyUpdate\Emoji\Emoji;
use HeyUpdate\Emoji\Index\CompiledIndex;
use Movim\Route;
use App\Configuration;
/**
* @desc A singleton wrapper for the Emoji library
*/
class MovimEmoji
{
protected static $instance = null;
private $_emoji;
private $_theme;
protected function __construct()
{
$this->_theme = Configuration::findOrNew(1)->theme;
$this->_emoji = new Emoji(new CompiledIndex, $this->getPath());
}
public function replace($string)
{
$this->_emoji->setImageHtmlTemplate('<img alt="{{name}}" class="emoji" src="'.$this->getPath().'">');
$string = $this->_emoji->replaceEmojiWithImages($string);
return $string;
}
private function getPath()
{
return BASE_URI . 'themes/' . $this->_theme . '/img/emojis/svg/{{unicode}}.svg';
}
public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new MovimEmoji;
}
return static::$instance;
}
}
function addUrls($string, $preview = false)
{
// Add missing links

2
app/widgets/Chat/_chat.tpl

@ -190,7 +190,7 @@
<li class="{if="$muc && !$conference->connected"}disabled{/if}">
{if="!$muc"}
<span class="primary icon gray emojis_open" onclick="Stickers_ajaxShow('{$jid}')">
<img alt=":smiley:" class="emoji large" src="{$c->getSmileyPath('1f603')}">
<img alt="" class="emoji large" src="{$c->getSmileyPath('1f603')}">
</span>
{/if}
{if="$c->getUser()->hasUpload()"}

2
app/widgets/Notifs/Notifs.php

@ -26,7 +26,7 @@ class Notifs extends \Movim\Widget\Base
if (!$since) $since = date(SQL_DATE, 0);
$emoji = \MovimEmoji::getInstance();
$emoji = \Movim\Emoji::getInstance();
$notifs = \App\Post::whereIn('parent_id', function ($query) use ($since) {
$query->select('id')

2
app/widgets/Post/Post.php

@ -160,7 +160,7 @@ class Post extends \Movim\Widget\Base
public function prepareComments(\App\Post $post, $public = false)
{
$emoji = \MovimEmoji::getInstance();
$emoji = \Movim\Emoji::getInstance();
$view = $this->tpl();
$view->assign('post', $post);
$view->assign('public', $public);

1
composer.json

@ -20,7 +20,6 @@
"movim/moxl": "dev-master#bfe3e1d83ef3bbbd270150f2f1164e8a662ef716",
"embed/embed": "^v3.3",
"heyupdate/emoji": "dev-twemoji2#99dda1bed08472046cac1498221f08684ad83d55",
"respect/validation": "^1.1",
"ezyang/htmlpurifier": "^4.9",

46
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "cb3c5cd8b0ebaeee1d6557408bd1198e",
"content-hash": "5f1e42e5e31452e8b3cb6aee677addcb",
"packages": [
{
"name": "cboden/ratchet",
@ -1039,47 +1039,6 @@
],
"time": "2017-03-20T17:10:46+00:00"
},
{
"name": "heyupdate/emoji",
"version": "dev-twemoji2",
"source": {
"type": "git",
"url": "https://github.com/heyupdate/emoji.git",
"reference": "99dda1bed08472046cac1498221f08684ad83d55"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/heyupdate/emoji/zipball/99dda1bed08472046cac1498221f08684ad83d55",
"reference": "99dda1bed08472046cac1498221f08684ad83d55",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"HeyUpdate\\Emoji\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Convert unicode and named (i.e. :smile:) Emoji into images",
"keywords": [
"emoji"
],
"time": "2016-11-15 20:35:47"
},
{
"name": "illuminate/container",
"version": "v5.5.40",
@ -3118,8 +3077,7 @@
"minimum-stability": "stable",
"stability-flags": {
"rain/raintpl": 20,
"movim/moxl": 20,
"heyupdate/emoji": 20
"movim/moxl": 20
},
"prefer-stable": false,
"prefer-lowest": false,

2
src/Movim/Daemon/Api.php

@ -166,7 +166,7 @@ class Api
{
$string = $post['string'];
$emoji = \MovimEmoji::getInstance();
$emoji = \Movim\Emoji::getInstance();
return $emoji->replace($string);
}
}

100
src/Movim/Emoji.php

@ -0,0 +1,100 @@
<?php
/*-
* Copyright © 2018
* mirabilos <thorsten.glaser@teckids.org>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un‐
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person’s immediate fault when using the work as intended.
*/
namespace Movim;
class Emoji
{
protected static $instance = null;
private $_emoji;
private $_regex = [
/* some easy cases first */
'/[#*0-9]\x{20E3}
|\x{1F3F3}(?:\x{FE0F}\x{200D}\x{1F308})?
|\x{1F3F4}(?:\x{200D}\x{2620}\x{FE0F}|\x{E0067}\x{E0062}
(?:\x{E0065}\x{E006E}\x{E0067}|\x{E0073}\x{E0063}\x{E0074}|\x{E0077}\x{E006C}\x{E0073})\x{E007F})?
|\x{1F441}(?:\x{200D}\x{1F5E8})?
/ux',
/* everything starting with 1F468 or 1F469 */
'/[\x{1F468}\x{1F469}]
(?:\x{200D}\x{2764}\x{FE0F}\x{200D}(?:\x{1F48B}\x{200D})?[\x{1F468}\x{1F469}]
|(?:\x{200D}[\x{1F468}\x{1F469}])?
(?:\x{200D}[\x{1F466}\x{1F467}])?
\x{200D}[\x{1F466}\x{1F467}]
|[\x{1F3FB}-\x{1F3FF}]?\x{200D}
(?:[\x{2695}\x{2696}\x{2708}]\x{FE0F}
|[\x{1F33E}\x{1F373}\x{1F393}\x{1F3A4}\x{1F3A8}\x{1F3EB}\x{1F3ED}\x{1F4BB}\x{1F4BC}\x{1F527}\x{1F52C}\x{1F680}\x{1F692}]
)
)/ux',
/* some more combinations (order is important!) */
'/[\x{26F9}\x{1F3C3}-\x{1F3CC}\x{1F46E}\x{1F46F}\x{1F461}-\x{1F477}\x{1F481}-\x{1F487}\x{1F575}\x{1F645}-\x{1F64E}\x{1F6A3}\x{1F6B4}-\x{1F6B6}\x{1F926}\x{1F937}-\x{1F93E}\x{1F9D6}-\x{1F9DF}]
[\x{FE0F}\x{1F3FB}-\x{1F3FF}]?
\x{200D}[\x{2640}\x{2642}]\x{FE0F}
/ux',
'/[\x{261D}\x{26F7}-\x{270D}\x{1F1E6}-\x{1F1FF}\x{1F385}\x{1F3C2}-\x{1F3CC}\x{1F442}-\x{1F487}\x{1F4AA}\x{1F574}-\x{1F596}\x{1F645}-\x{1F6CC}\x{1F918}-\x{1F9DD}]
[\x{1F1E6}-\x{1F1FF}\x{1F3FB}-\x{1F3FF}]/ux',
/* individual codepoints last */
'/[\x{203C}\x{2049}\x{2139}-\x{21AA}\x{231A}-\x{23FA}\x{24C2}\x{25AA}-\x{27BF}\x{2934}-\x{2B55}\x{3030}-\x{3299}\x{1F004}-\x{1F9E6}]/u'
];
protected function __construct()
{
$this->_emoji = require('Emoji/CompiledEmoji.php');
}
public function replace($string)
{
return preg_replace_callback($this->_regex, function ($matches) {
$astext = implode('-',
array_map('dechex',
unpack('N*', mb_convert_encoding($matches[0], 'UCS-4BE', 'UTF-8'))
)
);
/* Do we know this character? */
if (!isset($this->_emoji[$astext])) {
/* No, return match unchanged */
return $matches[0];
}
/* Yes, replace */
return '<img
alt="' . $this->_emoji[$astext] . '"
class="emoji"
src="' .
BASE_URI .
'themes/' .
\App\Configuration::findOrNew(1)->theme .
'/img/emojis/svg/' .
$astext .
'.svg" />';
}, $string);
}
public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new Emoji;
}
return static::$instance;
}
}

2663
src/Movim/Emoji/CompiledEmoji.php
File diff suppressed because it is too large
View File

75
src/Movim/Emoji/compile-file-list.sh

@ -0,0 +1,75 @@
#!/bin/mksh
#-
# Copyright © 2018
# mirabilos <thorsten.glaser@teckids.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
#-
# Needs the Debian packages mksh and unicode-data installed.
cd "$(dirname "$0")"
srcpath=../../../themes/material/img/emojis/svg
cd "$srcpath"
set -A files -- *.svg
cd "$OLDPWD"
cat >CompiledEmoji.php <<\EOF
<?php
/* GENERATED FILE, DO NOT EDIT! */
return [
EOF
php >>CompiledEmoji.php |&
print -pr -- '<?php
$u = array();'
typeset -l codepoint
while IFS=';' read codepoint name rest; do
[[ $name = *\<* ]] && continue
print -pr -- "\$u['${codepoint##*(0)}'] = '$name';"
done </usr/share/unicode/UnicodeData.txt
print -pr -- '
function lookup($name) {
global $u;
$x = "";
$s = "";
foreach (explode("-", $name) as $cp) {
if (isset($u[$cp]))
$x .= $s . $u[$cp];
else
$x .= $x . sprintf("<U%04X>", hexdec($cp));
$s = " + ";
}
return $x;
}
function handle($name) {
printf(" \"%s\" => \"%s\",\n", $name, lookup($name));
}
'
for ff in "${files[@]}"; do
f=${ff%.svg}
if [[ $f != +([0-9a-f-]) ]]; then
print -ru2 -- "W: source file $ff does not match pattern!"
continue
fi
print -pr -- "handle('$f');"
done
exec 3>&p
exec 3>&-
wait
echo "];" >>CompiledEmoji.php

19
src/Movim/Emoji/replace-test.php

@ -0,0 +1,19 @@
<?php
define('BASE_URI', '(base)');
mb_internal_encoding("UTF-8");
class Configuration
{
public $theme = '(theme)';
public static function findOrNew()
{
return new Configuration;
}
}
require_once '../Emoji.php';
$text = file_get_contents('php://stdin');
echo Emoji::getInstance()->replace($text);

61
src/Movim/Emoji/test-all-images.sh

@ -0,0 +1,61 @@
#!/bin/mksh
#-
# Copyright © 2018
# mirabilos <thorsten.glaser@teckids.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
cd "$(dirname "$0")"
srcpath=../../../themes/material/img/emojis/svg
saveIFS=$IFS
cd "$srcpath"
set -A files -- *.svg
cd "$OLDPWD"
if [[ -n $1 ]]; then
set -A files -- "$@"
print -ru2 -- "W: only testing $# files from command line"
fi
for f in "${files[@]}"; do
x=${f%.svg}
IFS=-
set -A y -- $x
IFS=$saveIFS
s='print "'
for z in "${y[@]}"; do
s+="\\x{$z}"
done
print -r -- "$s\\n\";"
done | perl -C7 | php replace-test.php |&
n=-1
rv=0
match=0
mis=0
while IFS= read -pr line; do
if [[ $line = '<img'*"/svg/${files[++n]}\""*\> ]]; then
let ++match
continue
fi
print -ru2 -- "W: file ${files[n]} not matched"
[[ -n $1 ]] && print -ru2 -- "N: line: $line"
rv=1
let ++mis
done
(( rv )) || print -ru2 -- "I: all files matched"
print -ru2 -- "I: $match/${#files[*]} matched, $mis mismatched"
exit $rv
Loading…
Cancel
Save