Speeds up parsing of the event content.
This commit is contained in:
@@ -45,6 +45,7 @@ import kotlinx.collections.immutable.persistentListOf
|
|||||||
import kotlinx.collections.immutable.toImmutableList
|
import kotlinx.collections.immutable.toImmutableList
|
||||||
import kotlinx.collections.immutable.toImmutableMap
|
import kotlinx.collections.immutable.toImmutableMap
|
||||||
import kotlinx.collections.immutable.toImmutableSet
|
import kotlinx.collections.immutable.toImmutableSet
|
||||||
|
import kotlinx.collections.immutable.toPersistentList
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
@@ -99,12 +100,12 @@ val HTTPRegex =
|
|||||||
class RichTextParser() {
|
class RichTextParser() {
|
||||||
fun parseMediaUrl(
|
fun parseMediaUrl(
|
||||||
fullUrl: String,
|
fullUrl: String,
|
||||||
tags: ImmutableListOfLists<String>,
|
eventTags: ImmutableListOfLists<String>,
|
||||||
): ZoomableUrlContent? {
|
): ZoomableUrlContent? {
|
||||||
val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl)
|
val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl)
|
||||||
return if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) {
|
return if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) {
|
||||||
val frags = Nip54().parse(fullUrl)
|
val frags = Nip54().parse(fullUrl)
|
||||||
val tags = Nip92().parse(fullUrl, tags.lists)
|
val tags = Nip92().parse(fullUrl, eventTags.lists)
|
||||||
|
|
||||||
ZoomableUrlImage(
|
ZoomableUrlImage(
|
||||||
url = fullUrl,
|
url = fullUrl,
|
||||||
@@ -116,7 +117,7 @@ class RichTextParser() {
|
|||||||
)
|
)
|
||||||
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
|
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
|
||||||
val frags = Nip54().parse(fullUrl)
|
val frags = Nip54().parse(fullUrl)
|
||||||
val tags = Nip92().parse(fullUrl, tags.lists)
|
val tags = Nip92().parse(fullUrl, eventTags.lists)
|
||||||
ZoomableUrlVideo(
|
ZoomableUrlVideo(
|
||||||
url = fullUrl,
|
url = fullUrl,
|
||||||
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
|
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
|
||||||
@@ -179,34 +180,34 @@ class RichTextParser() {
|
|||||||
emojis: Map<String, String>,
|
emojis: Map<String, String>,
|
||||||
tags: ImmutableListOfLists<String>,
|
tags: ImmutableListOfLists<String>,
|
||||||
): ImmutableList<ParagraphState> {
|
): ImmutableList<ParagraphState> {
|
||||||
var paragraphSegments = persistentListOf<ParagraphState>()
|
val lines = content.split('\n')
|
||||||
|
val paragraphSegments = ArrayList<ParagraphState>(lines.size)
|
||||||
|
|
||||||
content.split('\n').forEach { paragraph ->
|
lines.forEach { paragraph ->
|
||||||
var segments = persistentListOf<Segment>()
|
|
||||||
var isDirty = false
|
var isDirty = false
|
||||||
|
|
||||||
val isRTL = isArabic(paragraph)
|
val isRTL = isArabic(paragraph)
|
||||||
|
|
||||||
val wordList = paragraph.trimEnd().split(' ')
|
val wordList = paragraph.trimEnd().split(' ')
|
||||||
|
val segments = ArrayList<Segment>(wordList.size)
|
||||||
wordList.forEach { word ->
|
wordList.forEach { word ->
|
||||||
val wordSegment = wordIdentifier(word, images, urls, emojis, tags)
|
val wordSegment = wordIdentifier(word, images, urls, emojis, tags)
|
||||||
if (wordSegment !is RegularTextSegment) {
|
if (wordSegment !is RegularTextSegment) {
|
||||||
isDirty = true
|
isDirty = true
|
||||||
}
|
}
|
||||||
segments = segments.add(wordSegment)
|
segments.add(wordSegment)
|
||||||
}
|
}
|
||||||
|
|
||||||
val newSegments =
|
val newSegments =
|
||||||
if (isDirty) {
|
if (isDirty) {
|
||||||
ParagraphState(segments, isRTL)
|
ParagraphState(segments.toPersistentList(), isRTL)
|
||||||
} else {
|
} else {
|
||||||
ParagraphState(persistentListOf<Segment>(RegularTextSegment(paragraph)), isRTL)
|
ParagraphState(persistentListOf<Segment>(RegularTextSegment(paragraph)), isRTL)
|
||||||
}
|
}
|
||||||
|
|
||||||
paragraphSegments = paragraphSegments.add(newSegments)
|
paragraphSegments.add(newSegments)
|
||||||
}
|
}
|
||||||
|
|
||||||
return paragraphSegments
|
return paragraphSegments.toImmutableList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isNumber(word: String): Boolean {
|
fun isNumber(word: String): Boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user