Moves emoji parsers to commons and
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Vitor Pamplona
|
||||
*
|
||||
* 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
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.encoders
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.events.ImmutableListOfLists
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class Nip30CustomEmoji {
|
||||
companion object {
|
||||
val customEmojiPattern: Pattern =
|
||||
Pattern.compile("\\:([A-Za-z0-9_\\-]+)\\:", Pattern.CASE_INSENSITIVE)
|
||||
|
||||
fun fastMightContainEmoji(
|
||||
input: String,
|
||||
allTags: ImmutableListOfLists<String>?,
|
||||
): Boolean {
|
||||
if (allTags == null) return false
|
||||
if (allTags.lists.any { it.size > 2 && it[0] == "emoji" }) return true
|
||||
return input.contains(":")
|
||||
}
|
||||
|
||||
fun fastMightContainEmoji(
|
||||
input: String,
|
||||
emojiPairs: Map<String, String>,
|
||||
): Boolean {
|
||||
if (emojiPairs.isEmpty()) return false
|
||||
return input.contains(":")
|
||||
}
|
||||
|
||||
fun createEmojiMap(tags: ImmutableListOfLists<String>): Map<String, String> {
|
||||
return tags.lists.filter { it.size > 2 && it[0] == "emoji" }.associate { ":${it[1]}:" to it[2] }
|
||||
}
|
||||
|
||||
fun assembleAnnotatedList(
|
||||
input: String,
|
||||
allTags: ImmutableListOfLists<String>?,
|
||||
): ImmutableList<Renderable>? {
|
||||
if (allTags == null || allTags.lists.isEmpty()) return null
|
||||
|
||||
val emojiPairs = createEmojiMap(allTags)
|
||||
|
||||
return assembleAnnotatedList(input, emojiPairs)
|
||||
}
|
||||
|
||||
fun assembleAnnotatedList(
|
||||
input: String,
|
||||
emojiPairs: Map<String, String>,
|
||||
): ImmutableList<Renderable>? {
|
||||
val matcher = customEmojiPattern.matcher(input)
|
||||
val emojiNamesInOrder = mutableListOf<String>()
|
||||
while (matcher.find()) {
|
||||
emojiNamesInOrder.add(matcher.group())
|
||||
}
|
||||
|
||||
if (emojiNamesInOrder.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
val regularCharsInOrder = input.split(customEmojiPattern.toRegex())
|
||||
|
||||
val finalList = mutableListOf<Renderable>()
|
||||
|
||||
// Merge the two lists in Order.
|
||||
var index = 0
|
||||
for (word in regularCharsInOrder) {
|
||||
if (word.isNotEmpty()) {
|
||||
finalList.add(TextType(word))
|
||||
}
|
||||
if (index < emojiNamesInOrder.size) {
|
||||
val url = emojiPairs[emojiNamesInOrder[index]]
|
||||
|
||||
if (url != null) {
|
||||
finalList.add(ImageUrlType(url))
|
||||
} else {
|
||||
if (word.isNotEmpty()) {
|
||||
finalList.add(TextType(word))
|
||||
}
|
||||
}
|
||||
}
|
||||
index++
|
||||
}
|
||||
|
||||
return finalList.toImmutableList()
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable open class Renderable()
|
||||
|
||||
@Immutable class TextType(val text: String) : Renderable()
|
||||
|
||||
@Immutable class ImageUrlType(val url: String) : Renderable()
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Vitor Pamplona
|
||||
*
|
||||
* 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
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.encoders
|
||||
|
||||
import com.vitorpamplona.quartz.events.ImmutableListOfLists
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import junit.framework.TestCase.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class Nip30Test {
|
||||
@Test()
|
||||
fun parseEmoji() {
|
||||
val tags = mapOf(":soapbox:" to "http://soapbox")
|
||||
val input = "Alex Gleason :soapbox:"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertEquals(2, result!!.size)
|
||||
|
||||
assertEquals(
|
||||
"Alex Gleason ",
|
||||
(result!![0] as Nip30CustomEmoji.TextType).text,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"http://soapbox",
|
||||
(result!![1] as Nip30CustomEmoji.ImageUrlType).url,
|
||||
)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseEmojiInverted() {
|
||||
val tags = mapOf(":soapbox:" to "http://soapbox")
|
||||
val input = ":soapbox:Alex Gleason"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertEquals(2, result!!.size)
|
||||
|
||||
assertEquals(
|
||||
"http://soapbox",
|
||||
(result!![0] as Nip30CustomEmoji.ImageUrlType).url,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"Alex Gleason",
|
||||
(result!![1] as Nip30CustomEmoji.TextType).text,
|
||||
)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseEmoji2() {
|
||||
val tags =
|
||||
mapOf(
|
||||
":gleasonator:" to "http://gleasonator",
|
||||
":ablobcatrainbow:" to "http://ablobcatrainbow",
|
||||
":disputed:" to "http://disputed",
|
||||
)
|
||||
val input = "Hello :gleasonator: \uD83D\uDE02 :ablobcatrainbow: :disputed: yolo"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertEquals(7, result!!.size)
|
||||
|
||||
assertEquals("Hello ", (result!![0] as Nip30CustomEmoji.TextType).text)
|
||||
|
||||
assertEquals("http://gleasonator", (result!![1] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
|
||||
assertEquals(" 😂 ", (result!![2] as Nip30CustomEmoji.TextType).text)
|
||||
|
||||
assertEquals("http://ablobcatrainbow", (result!![3] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
|
||||
assertEquals(" ", (result!![4] as Nip30CustomEmoji.TextType).text)
|
||||
|
||||
assertEquals("http://disputed", (result!![5] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
|
||||
assertEquals(" yolo", (result!![6] as Nip30CustomEmoji.TextType).text)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseEmoji3() {
|
||||
val tags = emptyMap<String, String>()
|
||||
val input = "hello vitor: how can I help:"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertNull(result)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseEmoji4() {
|
||||
val tags = mapOf(":vitor:" to "http://vitor")
|
||||
val input = "hello :vitor: how :can I help:"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertEquals(3, result!!.size)
|
||||
|
||||
assertEquals("hello ", (result!![0] as Nip30CustomEmoji.TextType).text)
|
||||
|
||||
assertEquals("http://vitor", (result!![1] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
|
||||
assertEquals(" how :can I help:", (result!![2] as Nip30CustomEmoji.TextType).text)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseJapanese() {
|
||||
val tags = mapOf(":x30EDE:" to "http://x30EDE", ":\uD883\uDEDE:" to "http://\uD883\uDEDE")
|
||||
val input = "\uD883\uDEDE\uD883\uDEDE麺の:x30EDE:。:\uD883\uDEDE:(Violation of NIP-30)"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, tags)
|
||||
|
||||
assertEquals(3, result!!.size)
|
||||
|
||||
assertEquals("\uD883\uDEDE\uD883\uDEDE麺の", (result!![0] as Nip30CustomEmoji.TextType).text)
|
||||
assertEquals("http://x30EDE", (result!![1] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
assertEquals("。:\uD883\uDEDE:(Violation of NIP-30)", (result!![2] as Nip30CustomEmoji.TextType).text)
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun parseJapanese2() {
|
||||
val tags =
|
||||
arrayOf(
|
||||
arrayOf("t", "ioメシヨソイゲーム"),
|
||||
arrayOf("emoji", "_ri", "https://media.misskeyusercontent.com/emoji/_ri.png"),
|
||||
arrayOf("emoji", "petthex_japanesecake", "https://media.misskeyusercontent.com/emoji/petthex_japanesecake.gif"),
|
||||
arrayOf("emoji", "ai_nomming", "https://media.misskeyusercontent.com/misskey/f6294900-f678-43cc-bc36-3ee5deeca4c2.gif"),
|
||||
arrayOf("proxy", "https://misskey.io/notes/9q0x6gtdysir03qh", "activitypub"),
|
||||
)
|
||||
val input =
|
||||
"\u200B:_ri:\u200B\u200B:_ri:\u200Bはベイクドモチョチョ\u200B:petthex_japanesecake:\u200Bを食べました\u200B:ai_nomming:\u200B\n" +
|
||||
"#ioメシヨソイゲーム\n" +
|
||||
"https://misskey.io/play/9g3qza4jow"
|
||||
|
||||
val result = Nip30CustomEmoji.assembleAnnotatedList(input, ImmutableListOfLists(tags))
|
||||
|
||||
assertEquals(9, result!!.size)
|
||||
|
||||
var i = 0
|
||||
assertEquals("\u200B", (result!![i++] as Nip30CustomEmoji.TextType).text)
|
||||
assertEquals("https://media.misskeyusercontent.com/emoji/_ri.png", (result!![i++] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
assertEquals("\u200B\u200B", (result!![i++] as Nip30CustomEmoji.TextType).text)
|
||||
assertEquals("https://media.misskeyusercontent.com/emoji/_ri.png", (result!![i++] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
assertEquals("\u200Bはベイクドモチョチョ\u200B", (result!![i++] as Nip30CustomEmoji.TextType).text)
|
||||
assertEquals("https://media.misskeyusercontent.com/emoji/petthex_japanesecake.gif", (result!![i++] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
assertEquals("\u200Bを食べました\u200B", (result!![i++] as Nip30CustomEmoji.TextType).text)
|
||||
assertEquals("https://media.misskeyusercontent.com/misskey/f6294900-f678-43cc-bc36-3ee5deeca4c2.gif", (result!![i++] as Nip30CustomEmoji.ImageUrlType).url)
|
||||
assertEquals("\u200B\n#ioメシヨソイゲーム\nhttps://misskey.io/play/9g3qza4jow", (result!![i++] as Nip30CustomEmoji.TextType).text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user