-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE-42] - Implement LitEmoji::usePreset() (#43)
* Implemented shortcode presets * Added test setup to ensure consistent results * Misc. fixes * Used LitEmoji::invalidateCache() in usePreset()
- Loading branch information
Showing
10 changed files
with
98 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (C) 2017-2023 Elvanto Pty Ltd <[email protected]> | ||
Copyright (C) 2017-2024 Elvanto Pty Ltd <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,63 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
$presets = [ | ||
'cldr', | ||
'emojibase', | ||
'github', | ||
'iamcal', | ||
'joypixels' | ||
]; | ||
|
||
function normalizeShortcode($shortcode) | ||
{ | ||
return str_replace('-', '_', strtolower($shortcode)); | ||
} | ||
|
||
// Collect available emoji | ||
$data = json_decode(file_get_contents(__DIR__ . '/../vendor/milesj/emojibase/packages/data/en/data.raw.json'), true); | ||
$shortcodes = json_decode(file_get_contents(__DIR__ . '/../vendor/milesj/emojibase/packages/data/en/shortcodes/emojibase.raw.json'), true); | ||
|
||
$emojiList = require(__DIR__ . '/../src/emoji.php'); | ||
$existingShortcodes = array_map('normalizeShortcode', array_keys($emojiList)); | ||
foreach ($presets as $preset) { | ||
$shortcodes = json_decode(file_get_contents(__DIR__ . "/../vendor/milesj/emojibase/packages/data/en/shortcodes/{$preset}.raw.json"), true); | ||
$mapping = __DIR__ . "/../src/{$preset}.php"; | ||
|
||
foreach ($data as $emoji) { | ||
if ( | ||
!isset($shortcodes[$emoji['hexcode']]) || | ||
!array_key_exists('group', $emoji) // Excludes regional indicator emoji that mess with flags | ||
) { | ||
continue; | ||
if (file_exists($mapping)) { | ||
$emojiList = require($mapping); | ||
} else { | ||
$emojiList = []; | ||
} | ||
|
||
if (!is_array($shortcodes[$emoji['hexcode']])) { | ||
$shortcodes[$emoji['hexcode']] = [$shortcodes[$emoji['hexcode']]]; | ||
} | ||
$existingShortcodes = array_map('normalizeShortcode', array_keys($emojiList)); | ||
|
||
foreach ($shortcodes[$emoji['hexcode']] as $shortcode) { | ||
if (in_array(normalizeShortcode($shortcode), $existingShortcodes)) { | ||
foreach ($data as $emoji) { | ||
if ( | ||
!isset($shortcodes[$emoji['hexcode']]) || | ||
!array_key_exists('group', $emoji) // Excludes regional indicator emoji that mess with flags | ||
) { | ||
continue; | ||
} | ||
|
||
$emojiList[(string)$shortcode] = $emoji['hexcode']; | ||
if (!is_array($shortcodes[$emoji['hexcode']])) { | ||
$shortcodes[$emoji['hexcode']] = [$shortcodes[$emoji['hexcode']]]; | ||
} | ||
|
||
foreach ($shortcodes[$emoji['hexcode']] as $shortcode) { | ||
if (in_array(normalizeShortcode($shortcode), $existingShortcodes)) { | ||
continue; | ||
} | ||
|
||
$emojiList[(string)$shortcode] = $emoji['hexcode']; | ||
} | ||
} | ||
} | ||
|
||
// Order by longest codepoint to ensure replacement of ZWJ emoji first | ||
uasort($emojiList, fn ($a, $b) => strlen($b) <=> strlen($a)); | ||
// Order by longest codepoint to ensure replacement of ZWJ emoji first | ||
uasort($emojiList, fn($a, $b) => strlen($b) <=> strlen($a)); | ||
|
||
// Generate cachable PHP code | ||
$output = []; | ||
foreach ($emojiList as $shortcode => $codepoints) { | ||
$output[] = sprintf("'%s'=>'%s'", $shortcode, $codepoints); | ||
}; | ||
// Generate cachable PHP code | ||
$output = []; | ||
foreach ($emojiList as $shortcode => $codepoints) { | ||
$output[] = sprintf("'%s'=>'%s'", $shortcode, $codepoints); | ||
}; | ||
|
||
file_put_contents('src/emoji.php', sprintf('<?php return [%s];', implode(',', $output))); | ||
file_put_contents("src/{$preset}.php", sprintf('<?php return [%s];', implode(',', $output))); | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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