Migrates rich text parser from jvm to commons
This commit is contained in:
@@ -30,8 +30,8 @@ import coil3.fetch.Fetcher
|
|||||||
import coil3.fetch.ImageFetchResult
|
import coil3.fetch.ImageFetchResult
|
||||||
import coil3.key.Keyer
|
import coil3.key.Keyer
|
||||||
import coil3.request.Options
|
import coil3.request.Options
|
||||||
import com.vitorpamplona.amethyst.commons.base64Image.Base64Image
|
|
||||||
import com.vitorpamplona.amethyst.commons.base64Image.toBitmap
|
import com.vitorpamplona.amethyst.commons.base64Image.toBitmap
|
||||||
|
import com.vitorpamplona.amethyst.commons.richtext.Base64Image
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||||
|
|
||||||
|
|||||||
+1
@@ -24,6 +24,7 @@ import android.graphics.Bitmap
|
|||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import com.vitorpamplona.amethyst.commons.blurhash.PlatformImage
|
import com.vitorpamplona.amethyst.commons.blurhash.PlatformImage
|
||||||
import com.vitorpamplona.amethyst.commons.blurhash.toPlatformImage
|
import com.vitorpamplona.amethyst.commons.blurhash.toPlatformImage
|
||||||
|
import com.vitorpamplona.amethyst.commons.richtext.Base64Image
|
||||||
import java.util.Base64
|
import java.util.Base64
|
||||||
|
|
||||||
fun Base64Image.toBitmap(content: String): Bitmap {
|
fun Base64Image.toBitmap(content: String): Bitmap {
|
||||||
|
|||||||
+9
-6
@@ -20,8 +20,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.commons.richtext
|
package com.vitorpamplona.amethyst.commons.richtext
|
||||||
|
|
||||||
import java.util.regex.Pattern
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pattern constants for email and phone validation.
|
* Pattern constants for email and phone validation.
|
||||||
* These replace android.util.Patterns for KMP compatibility.
|
* These replace android.util.Patterns for KMP compatibility.
|
||||||
@@ -30,16 +28,21 @@ object Patterns {
|
|||||||
/**
|
/**
|
||||||
* Email address pattern from RFC 5322... From android.util.Patterns.
|
* Email address pattern from RFC 5322... From android.util.Patterns.
|
||||||
*/
|
*/
|
||||||
val EMAIL_ADDRESS: Pattern =
|
val EMAIL_ADDRESS: Regex =
|
||||||
Pattern.compile(
|
Regex(
|
||||||
"[a-zA-Z0-9+._%-]{1,256}@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+",
|
"[a-zA-Z0-9+._%-]{1,256}@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+",
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phone number pattern - matches common phone formats.
|
* Phone number pattern - matches common phone formats.
|
||||||
*/
|
*/
|
||||||
val PHONE: Pattern =
|
val PHONE: Regex =
|
||||||
Pattern.compile(
|
Regex(
|
||||||
"^[+]?[(]?[0-9]{1,4}[)]?[-\\s./0-9]*\$",
|
"^[+]?[(]?[0-9]{1,4}[)]?[-\\s./0-9]*\$",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val BASE64_IMAGE: Regex =
|
||||||
|
Regex(
|
||||||
|
"data:image/(${RichTextParser.imageExtensions.joinToString(separator = "|")});base64,([a-zA-Z0-9+/]+={0,2})",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
+35
-35
@@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.commons.richtext
|
|||||||
|
|
||||||
import com.linkedin.urls.detection.UrlDetector
|
import com.linkedin.urls.detection.UrlDetector
|
||||||
import com.linkedin.urls.detection.UrlDetectorOptions
|
import com.linkedin.urls.detection.UrlDetectorOptions
|
||||||
import com.vitorpamplona.amethyst.commons.base64Image.Base64Image
|
|
||||||
import com.vitorpamplona.amethyst.commons.emojicoder.EmojiCoder
|
import com.vitorpamplona.amethyst.commons.emojicoder.EmojiCoder
|
||||||
import com.vitorpamplona.quartz.experimental.inlineMetadata.Nip54InlineMetadata
|
import com.vitorpamplona.quartz.experimental.inlineMetadata.Nip54InlineMetadata
|
||||||
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
|
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
|
||||||
@@ -45,8 +44,8 @@ import kotlinx.collections.immutable.toPersistentList
|
|||||||
import java.net.MalformedURLException
|
import java.net.MalformedURLException
|
||||||
import java.net.URISyntaxException
|
import java.net.URISyntaxException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.regex.Pattern
|
|
||||||
import kotlin.coroutines.cancellation.CancellationException
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
|
import kotlin.text.iterator
|
||||||
|
|
||||||
class RichTextParser {
|
class RichTextParser {
|
||||||
fun createMediaContent(
|
fun createMediaContent(
|
||||||
@@ -108,7 +107,7 @@ class RichTextParser {
|
|||||||
|
|
||||||
return urls.mapNotNullTo(LinkedHashSet(urls.size)) {
|
return urls.mapNotNullTo(LinkedHashSet(urls.size)) {
|
||||||
if (it.originalUrl.contains("@")) {
|
if (it.originalUrl.contains("@")) {
|
||||||
if (Patterns.EMAIL_ADDRESS.matcher(it.originalUrl).matches()) {
|
if (Patterns.EMAIL_ADDRESS.matches(it.originalUrl)) {
|
||||||
null
|
null
|
||||||
} else {
|
} else {
|
||||||
it.originalUrl
|
it.originalUrl
|
||||||
@@ -201,7 +200,7 @@ class RichTextParser {
|
|||||||
}.toImmutableList()
|
}.toImmutableList()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isNumber(word: String) = numberPattern.matcher(word).matches()
|
private fun isNumber(word: String) = numberPattern.matches(word)
|
||||||
|
|
||||||
private fun isPhoneNumberChar(c: Char): Boolean =
|
private fun isPhoneNumberChar(c: Char): Boolean =
|
||||||
when (c) {
|
when (c) {
|
||||||
@@ -225,7 +224,7 @@ class RichTextParser {
|
|||||||
return isPotentialNumber
|
return isPotentialNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isDate(word: String): Boolean = shortDatePattern.matcher(word).matches() || longDatePattern.matcher(word).matches()
|
fun isDate(word: String): Boolean = shortDatePattern.matches(word) || longDatePattern.matches(word)
|
||||||
|
|
||||||
private fun isArabic(text: String): Boolean = text.any { it in '\u0600'..'\u06FF' || it in '\u0750'..'\u077F' }
|
private fun isArabic(text: String): Boolean = text.any { it in '\u0600'..'\u06FF' || it in '\u0750'..'\u077F' }
|
||||||
|
|
||||||
@@ -239,7 +238,9 @@ class RichTextParser {
|
|||||||
): Segment {
|
): Segment {
|
||||||
if (word.isEmpty()) return RegularTextSegment(word)
|
if (word.isEmpty()) return RegularTextSegment(word)
|
||||||
|
|
||||||
if (word.startsWith("data:image/") && Base64Image.isBase64(word)) return Base64Segment(word)
|
if (word.startsWith("data:image/")) {
|
||||||
|
if (Patterns.BASE64_IMAGE.matches(word)) return Base64Segment(word)
|
||||||
|
}
|
||||||
|
|
||||||
if (images.contains(word)) return ImageSegment(word)
|
if (images.contains(word)) return ImageSegment(word)
|
||||||
|
|
||||||
@@ -260,25 +261,22 @@ class RichTextParser {
|
|||||||
if (EmojiCoder.isCoded(word)) return SecretEmoji(word)
|
if (EmojiCoder.isCoded(word)) return SecretEmoji(word)
|
||||||
|
|
||||||
if (word.contains("@")) {
|
if (word.contains("@")) {
|
||||||
if (Patterns.EMAIL_ADDRESS.matcher(word).matches()) return EmailSegment(word)
|
if (Patterns.EMAIL_ADDRESS.matches(word)) return EmailSegment(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startsWithNIP19Scheme(word)) return BechSegment(word)
|
if (startsWithNIP19Scheme(word)) return BechSegment(word)
|
||||||
|
|
||||||
if (isPotentialPhoneNumber(word) && !isDate(word)) {
|
if (isPotentialPhoneNumber(word) && !isDate(word)) {
|
||||||
if (Patterns.PHONE.matcher(word).matches()) return PhoneSegment(word)
|
if (Patterns.PHONE.matches(word)) return PhoneSegment(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
val indexOfPeriod = word.indexOf(".")
|
val indexOfPeriod = word.indexOf(".")
|
||||||
if (indexOfPeriod > 0 && indexOfPeriod < word.length - 1) { // periods cannot be the last one
|
if (indexOfPeriod > 0 && indexOfPeriod < word.length - 1) { // periods cannot be the last one
|
||||||
val schemelessMatcher = noProtocolUrlValidator.matcher(word)
|
val schemelessMatcher = noProtocolUrlValidator.find(word)
|
||||||
if (schemelessMatcher.find()) {
|
if (schemelessMatcher != null) {
|
||||||
val url = schemelessMatcher.group(1) // url
|
val url = schemelessMatcher.groups[1]?.value // url
|
||||||
val additionalChars = schemelessMatcher.group(4).ifEmpty { null } // additional chars
|
val additionalChars = schemelessMatcher.groups[4]?.value?.ifEmpty { null } // additional chars
|
||||||
val pattern =
|
if (additionalUrlSchema.find(word) != null && url != null) {
|
||||||
"""^([A-Za-z0-9-_]+(\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\?[^#]*)?(#.*)?"""
|
|
||||||
.toRegex(RegexOption.IGNORE_CASE)
|
|
||||||
if (pattern.find(word) != null && url != null) {
|
|
||||||
return SchemelessUrlSegment(word, url, additionalChars)
|
return SchemelessUrlSegment(word, url, additionalChars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -292,12 +290,11 @@ class RichTextParser {
|
|||||||
tags: ImmutableListOfLists<String>,
|
tags: ImmutableListOfLists<String>,
|
||||||
): Segment {
|
): Segment {
|
||||||
// First #[n]
|
// First #[n]
|
||||||
|
|
||||||
val matcher = tagIndex.matcher(word)
|
|
||||||
try {
|
try {
|
||||||
if (matcher.find()) {
|
val matcher = tagIndex.find(word)
|
||||||
val index = matcher.group(1)?.toInt()
|
if (matcher != null) {
|
||||||
val suffix = matcher.group(2)
|
val index = matcher.groups[1]?.value?.toInt()
|
||||||
|
val suffix = matcher.groups[2]?.value
|
||||||
|
|
||||||
if (index != null && index >= 0 && index < tags.lists.size) {
|
if (index != null && index >= 0 && index < tags.lists.size) {
|
||||||
val tag = tags.lists[index]
|
val tag = tags.lists[index]
|
||||||
@@ -317,13 +314,12 @@ class RichTextParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Second #Amethyst
|
// Second #Amethyst
|
||||||
val hashtagMatcher = hashTagsPattern.matcher(word)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (hashtagMatcher.find()) {
|
val hashtagMatcher = hashTagsPattern.find(word)
|
||||||
val hashtag = hashtagMatcher.group(1)
|
if (hashtagMatcher != null) {
|
||||||
|
val hashtag = hashtagMatcher.groups[1]?.value
|
||||||
if (hashtag != null) {
|
if (hashtag != null) {
|
||||||
return HashTagSegment(word, hashtag, hashtagMatcher.group(2).ifEmpty { null })
|
return HashTagSegment(word, hashtag, hashtagMatcher.groups[2]?.value?.ifEmpty { null })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -335,22 +331,26 @@ class RichTextParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val longDatePattern: Pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$")
|
val longDatePattern: Regex = Regex("^\\d{4}-\\d{2}-\\d{2}$")
|
||||||
val shortDatePattern: Pattern = Pattern.compile("^\\d{2}-\\d{2}-\\d{2}$")
|
val shortDatePattern: Regex = Regex("^\\d{2}-\\d{2}-\\d{2}$")
|
||||||
val numberPattern: Pattern = Pattern.compile("^(-?[\\d.]+)([a-zA-Z%]*)$")
|
val numberPattern: Regex = Regex("^(-?[\\d.]+)([a-zA-Z%]*)$")
|
||||||
|
|
||||||
// Android9 seems to have an issue starting this regex.
|
// Android9 seems to have an issue starting this regex.
|
||||||
val noProtocolUrlValidator =
|
val noProtocolUrlValidator =
|
||||||
try {
|
try {
|
||||||
Pattern.compile(
|
Regex(
|
||||||
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+[^\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}])*\\/?)(.*)",
|
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+[^\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}])*\\/?)(.*)",
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Pattern.compile(
|
Regex(
|
||||||
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)",
|
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val additionalUrlSchema =
|
||||||
|
"""^([A-Za-z0-9-_]+(\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\?[^#]*)?(#.*)?"""
|
||||||
|
.toRegex(RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
val HTTPRegex =
|
val HTTPRegex =
|
||||||
"^((http|https)://)?([A-Za-z0-9-_]+(\\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\\?[^#]*)?(#.*)?"
|
"^((http|https)://)?([A-Za-z0-9-_]+(\\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\\?[^#]*)?(#.*)?"
|
||||||
.toRegex(RegexOption.IGNORE_CASE)
|
.toRegex(RegexOption.IGNORE_CASE)
|
||||||
@@ -361,9 +361,9 @@ class RichTextParser {
|
|||||||
val imageExtensions = imageExt + imageExt.map { it.uppercase() }
|
val imageExtensions = imageExt + imageExt.map { it.uppercase() }
|
||||||
val videoExtensions = videoExt + videoExt.map { it.uppercase() }
|
val videoExtensions = videoExt + videoExt.map { it.uppercase() }
|
||||||
|
|
||||||
val tagIndex = Pattern.compile("\\#\\[([0-9]+)\\](.*)")
|
val tagIndex = Regex("\\#\\[([0-9]+)\\](.*)")
|
||||||
val hashTagsPattern: Pattern =
|
val hashTagsPattern: Regex =
|
||||||
Pattern.compile("#([^\\s!@#\$%^&*()=+./,\\[{\\]};:'\"?><]+)(.*)", Pattern.CASE_INSENSITIVE)
|
Regex("#([^\\s!@#\$%^&*()=+./,\\[{\\]};:'\"?><]+)(.*)", RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
val acceptedNIP19schemes =
|
val acceptedNIP19schemes =
|
||||||
listOf("npub1", "naddr1", "note1", "nprofile1", "nevent1", "nembed") +
|
listOf("npub1", "naddr1", "note1", "nprofile1", "nevent1", "nembed") +
|
||||||
@@ -436,6 +436,6 @@ class RichTextParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isUrlWithoutScheme(url: String) = noProtocolUrlValidator.matcher(url).matches()
|
fun isUrlWithoutScheme(url: String) = noProtocolUrlValidator.matches(url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-36
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 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.amethyst.commons.base64Image
|
|
||||||
|
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
|
|
||||||
import java.util.regex.Pattern
|
|
||||||
|
|
||||||
object Base64Image {
|
|
||||||
val pattern: Pattern =
|
|
||||||
Pattern.compile(
|
|
||||||
"data:image/(${RichTextParser.imageExtensions.joinToString(separator = "|")});base64,([a-zA-Z0-9+/]+={0,2})",
|
|
||||||
)
|
|
||||||
|
|
||||||
fun isBase64(content: String): Boolean {
|
|
||||||
val matcher = pattern.matcher(content)
|
|
||||||
return matcher.find()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
@@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.commons.base64Image
|
|||||||
|
|
||||||
import com.vitorpamplona.amethyst.commons.blurhash.PlatformImage
|
import com.vitorpamplona.amethyst.commons.blurhash.PlatformImage
|
||||||
import com.vitorpamplona.amethyst.commons.blurhash.toPlatformImage
|
import com.vitorpamplona.amethyst.commons.blurhash.toPlatformImage
|
||||||
|
import com.vitorpamplona.amethyst.commons.richtext.Base64Image
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.util.Base64
|
import java.util.Base64
|
||||||
import javax.imageio.ImageIO
|
import javax.imageio.ImageIO
|
||||||
|
|||||||
Reference in New Issue
Block a user