diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/TextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/TextSource.kt index fbf2541d6e..9113ee5c18 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/TextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/TextSource.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.CommonAbstracts +import dev.inmo.tgbotapi.types.MessageEntity.toTextParts import dev.inmo.tgbotapi.types.captionLength import dev.inmo.tgbotapi.types.textLength @@ -15,7 +16,10 @@ interface TextSource { interface MultilevelTextSource : TextSource { + @Deprecated("Will be removed in near major release") val textParts: List + get() = textParts(0) + val textSources: List } data class TextPart( @@ -25,6 +29,7 @@ data class TextPart( fun List.justTextSources() = map { it.source } fun List.makeString() = joinToString("") { it.source } +fun MultilevelTextSource.textParts(offset: Int): List = textSources.toTextParts(offset) fun List.separateForMessage(limit: IntRange, numberOfParts: Int? = null): List> { if (isEmpty()) { return emptyList() diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/RawMessageEntity.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/RawMessageEntity.kt index a91ee59882..ce48f51baa 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/RawMessageEntity.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/RawMessageEntity.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.MessageEntity import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.types.MessageEntity.textsources.* import dev.inmo.tgbotapi.types.User +import dev.inmo.tgbotapi.utils.fullListOfSubSource import dev.inmo.tgbotapi.utils.shiftSourcesToTheLeft import kotlinx.serialization.Serializable @@ -16,26 +17,29 @@ internal data class RawMessageEntity( val language: String? = null ) -internal fun RawMessageEntity.asTextParts(source: String, subParts: List): List { - val sourceSubstring = source.substring(offset, offset + length) +internal fun RawMessageEntity.asTextParts( + source: String, + subParts: List +): List { + val sourceSubstring: String = source.substring(offset, offset + length) val range = offset until (offset + length) - val shiftedSubParts = subParts.shiftSourcesToTheLeft(offset) + val shiftedSubSources = sourceSubstring.fullListOfSubSource(subParts.shiftSourcesToTheLeft(offset)).justTextSources() return when (type) { - "mention" -> MentionTextSource(sourceSubstring, shiftedSubParts) - "hashtag" -> HashTagTextSource(sourceSubstring, shiftedSubParts) - "cashtag" -> CashTagTextSource(sourceSubstring, shiftedSubParts) - "bot_command" -> BotCommandTextSource(sourceSubstring, shiftedSubParts) + "mention" -> MentionTextSource(sourceSubstring, shiftedSubSources) + "hashtag" -> HashTagTextSource(sourceSubstring, shiftedSubSources) + "cashtag" -> CashTagTextSource(sourceSubstring, shiftedSubSources) + "bot_command" -> BotCommandTextSource(sourceSubstring, shiftedSubSources) "url" -> URLTextSource(sourceSubstring) - "email" -> EMailTextSource(sourceSubstring, shiftedSubParts) - "phone_number" -> PhoneNumberTextSource(sourceSubstring, shiftedSubParts) - "bold" -> BoldTextSource(sourceSubstring, shiftedSubParts) - "italic" -> ItalicTextSource(sourceSubstring, shiftedSubParts) + "email" -> EMailTextSource(sourceSubstring, shiftedSubSources) + "phone_number" -> PhoneNumberTextSource(sourceSubstring, shiftedSubSources) + "bold" -> BoldTextSource(sourceSubstring, shiftedSubSources) + "italic" -> ItalicTextSource(sourceSubstring, shiftedSubSources) "code" -> CodeTextSource(sourceSubstring) "pre" -> PreTextSource(sourceSubstring, language) "text_link" -> TextLinkTextSource(sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) - "text_mention" -> TextMentionTextSource(sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"), shiftedSubParts) - "underline" -> UnderlineTextSource(sourceSubstring, shiftedSubParts) - "strikethrough" -> StrikethroughTextSource(sourceSubstring, shiftedSubParts) + "text_mention" -> TextMentionTextSource(sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"), shiftedSubSources) + "underline" -> UnderlineTextSource(sourceSubstring, shiftedSubSources) + "strikethrough" -> StrikethroughTextSource(sourceSubstring, shiftedSubSources) else -> RegularTextSource(sourceSubstring) }.let { val part = TextPart(range, it) @@ -47,7 +51,7 @@ internal fun RawMessageEntity.asTextParts(source: String, subParts: List { +internal fun createTextPart(originalFullString: String, entities: RawMessageEntities): List { val mutableEntities = entities.toMutableList() mutableEntities.sortBy { it.offset } val resultList = mutableListOf() @@ -73,8 +77,8 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List.asRawMessageEntities(): List = mapNotNull { - val source = it.source - val length = it.range.last - it.range.first + 1 - when (source) { - is MentionTextSource -> RawMessageEntity("mention", it.range.first, length) - is HashTagTextSource -> RawMessageEntity("hashtag", it.range.first, length) - is CashTagTextSource -> RawMessageEntity("cashtag", it.range.first, length) - is BotCommandTextSource -> RawMessageEntity("bot_command", it.range.first, length) - is URLTextSource -> RawMessageEntity("url", it.range.first, length) - is EMailTextSource -> RawMessageEntity("email", it.range.first, length) - is PhoneNumberTextSource -> RawMessageEntity("phone_number", it.range.first, length) - is BoldTextSource -> RawMessageEntity("bold", it.range.first, length) - is ItalicTextSource -> RawMessageEntity("italic", it.range.first, length) - is CodeTextSource -> RawMessageEntity("code", it.range.first, length) - is PreTextSource -> RawMessageEntity("pre", it.range.first, length, language = source.language) - is TextLinkTextSource -> RawMessageEntity("text_link", it.range.first, length, source.url) - is TextMentionTextSource -> RawMessageEntity("text_mention", it.range.first, length, user = source.user) - is UnderlineTextSource -> RawMessageEntity("underline", it.range.first, length) - is StrikethroughTextSource -> RawMessageEntity("strikethrough", it.range.first, length) - else -> null +internal fun TextPart.asRawMessageEntities(): List { + val source = source + val length = range.last - range.first + 1 + + return listOfNotNull( + when (source) { + is MentionTextSource -> RawMessageEntity("mention", range.first, length) + is HashTagTextSource -> RawMessageEntity("hashtag", range.first, length) + is CashTagTextSource -> RawMessageEntity("cashtag", range.first, length) + is BotCommandTextSource -> RawMessageEntity("bot_command", range.first, length) + is URLTextSource -> RawMessageEntity("url", range.first, length) + is EMailTextSource -> RawMessageEntity("email", range.first, length) + is PhoneNumberTextSource -> RawMessageEntity("phone_number", range.first, length) + is BoldTextSource -> RawMessageEntity("bold", range.first, length) + is ItalicTextSource -> RawMessageEntity("italic", range.first, length) + is CodeTextSource -> RawMessageEntity("code", range.first, length) + is PreTextSource -> RawMessageEntity("pre", range.first, length, language = source.language) + is TextLinkTextSource -> RawMessageEntity("text_link", range.first, length, source.url) + is TextMentionTextSource -> RawMessageEntity("text_mention", range.first, length, user = source.user) + is UnderlineTextSource -> RawMessageEntity("underline", range.first, length) + is StrikethroughTextSource -> RawMessageEntity("strikethrough", range.first, length) + else -> null + } + ) + if (source is MultilevelTextSource) { + source.textParts(range.first).asRawMessageEntities() + } else { + emptyList() } } -internal fun List.toRawMessageEntities(): List { - var i = 0 +internal fun List.asRawMessageEntities(): List = flatMap { it.asRawMessageEntities() } + +internal fun List.toTextParts(preOffset: Int = 0): List { + var i = preOffset return map { TextPart( i until (i + it.source.length), @@ -114,9 +127,13 @@ internal fun List.toRawMessageEntities(): List { ).also { i = it.range.last + 1 } - }.asRawMessageEntities() + } } -internal fun RawMessageEntities.asTextParts(sourceString: String): List = createTextPart(sourceString, this) +internal fun List.toRawMessageEntities(): List = toTextParts().asRawMessageEntities() + +internal fun RawMessageEntities.asTextParts(sourceString: String): List = sourceString.fullListOfSubSource( + createTextPart(sourceString, this) +) internal typealias RawMessageEntities = List diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BoldTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BoldTextSource.kt index b5c2823022..882f2df3ea 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BoldTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BoldTextSource.kt @@ -1,15 +1,19 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* +import dev.inmo.tgbotapi.types.MessageEntity.toTextParts import dev.inmo.tgbotapi.utils.* class BoldTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.boldMarkdown() } override val asMarkdownV2Source: String by lazy { boldMarkdownV2() } override val asHtmlSource: String by lazy { boldHTML() } } + +@Suppress("NOTHING_TO_INLINE") +inline fun bold(text: String) = BoldTextSource(text, emptyList()) +@Suppress("NOTHING_TO_INLINE") +inline fun bold(parts: List) = BoldTextSource(parts.makeString(), parts) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BotCommandTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BotCommandTextSource.kt index eb95d69313..44a8be5e94 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BotCommandTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/BotCommandTextSource.kt @@ -1,24 +1,18 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* private val commandRegex = Regex("[/!][^@\\s]*") class BotCommandTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { val command: String by lazy { commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" } - override val textParts: List by lazy { - command.fullListOfSubSource( - textParts.shiftSourcesToTheLeft(1) - ) - } override val asMarkdownSource: String by lazy { source.commandMarkdown() } override val asMarkdownV2Source: String by lazy { commandMarkdownV2() } override val asHtmlSource: String by lazy { commandHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/CashTagTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/CashTagTextSource.kt index 13e490b753..b421977893 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/CashTagTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/CashTagTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class CashTagTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.cashTagMarkdown() } override val asMarkdownV2Source: String by lazy { cashTagMarkdownV2() } override val asHtmlSource: String by lazy { cashTagHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/EMailTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/EMailTextSource.kt index f5c7450238..b4355986bf 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/EMailTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/EMailTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class EMailTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.emailMarkdown() } override val asMarkdownV2Source: String by lazy { emailMarkdownV2(source) } override val asHtmlSource: String by lazy { emailHTML(source) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/HashTagTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/HashTagTextSource.kt index 2cd398ad47..4ff9ddd98a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/HashTagTextSource.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* private val String.withoutSharp @@ -13,13 +12,8 @@ private val String.withoutSharp class HashTagTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { - source.withoutSharp.fullListOfSubSource( - textParts.shiftSourcesToTheLeft(1) - ) - } override val asMarkdownSource: String by lazy { source.hashTagMarkdown() } override val asMarkdownV2Source: String by lazy { hashTagMarkdownV2() } override val asHtmlSource: String by lazy { hashTagHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/ItalicTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/ItalicTextSource.kt index 0c8ed6fa61..d142936213 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/ItalicTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/ItalicTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class ItalicTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.italicMarkdown() } override val asMarkdownV2Source: String by lazy { italicMarkdownV2() } override val asHtmlSource: String by lazy { italicHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/MentionTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/MentionTextSource.kt index bd3ef1efa4..f3251eea49 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/MentionTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/MentionTextSource.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* private val String.withoutCommercialAt @@ -13,13 +12,8 @@ private val String.withoutCommercialAt class MentionTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { - source.withoutCommercialAt.fullListOfSubSource( - textParts.shiftSourcesToTheLeft(1) - ) - } override val asMarkdownSource: String by lazy { source.mentionMarkdown() } override val asMarkdownV2Source: String by lazy { mentionMarkdownV2() } override val asHtmlSource: String by lazy { mentionHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/PhoneNumberTextSource.kt index 6aabef80fa..c3c697ee73 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class PhoneNumberTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.phoneMarkdown() } override val asMarkdownV2Source: String by lazy { phoneMarkdownV2() } override val asHtmlSource: String by lazy { phoneHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/StrikethroughTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/StrikethroughTextSource.kt index 63ec29c07f..0d34b23f2a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/StrikethroughTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class StrikethroughTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asHtmlSource: String by lazy { strikethroughHTML() } override val asMarkdownV2Source: String by lazy { strikethroughMarkdownV2() } override val asMarkdownSource: String by lazy { source.strikethroughMarkdown() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt index 5109236b1b..f71cb532dc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -1,16 +1,14 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.utils.* class TextMentionTextSource( override val source: String, val user: User, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.textMentionMarkdown(user.id) } override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(user.id) } override val asHtmlSource: String by lazy { textMentionHTML(user.id) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/UnderlineTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/UnderlineTextSource.kt index 00a195bc0a..35799730da 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/UnderlineTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -1,14 +1,12 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.utils.* class UnderlineTextSource( override val source: String, - textParts: List + override val textSources: List ) : MultilevelTextSource { - override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.underlineMarkdown() } override val asMarkdownV2Source: String by lazy { underlineMarkdownV2() } override val asHtmlSource: String by lazy { underlineHTML() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt index 55504c97e8..644e441646 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt @@ -92,11 +92,11 @@ internal data class RawMessage( ) { private val content: MessageContent? by lazy { val adaptedCaptionEntities = caption ?.let { - it.fullListOfSubSource(caption_entities ?.asTextParts(caption) ?: emptyList()) + (caption_entities ?: emptyList()).asTextParts(caption) } ?: emptyList() when { - text != null -> TextContent(text, text.fullListOfSubSource(entities ?.asTextParts(text) ?: emptyList())) + text != null -> TextContent(text, (entities ?: emptyList()).asTextParts(text)) audio != null -> AudioContent( audio, caption, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/MultilevelTextSourceFormatting.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/MultilevelTextSourceFormatting.kt index 23869d4946..0a8a14ae9b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/MultilevelTextSourceFormatting.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/MultilevelTextSourceFormatting.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.utils -import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.types.MessageEntity.textsources.RegularTextSource import dev.inmo.tgbotapi.types.UserId import dev.inmo.tgbotapi.types.link @@ -73,30 +72,30 @@ internal fun List.shiftSourcesToTheLeft(shiftCount: Int = 1): List.joinSubSourcesMarkdownV2() = joinToString("") { - it.source.asMarkdownV2Source +private fun List.joinSubSourcesMarkdownV2() = joinToString("") { + it.asMarkdownV2Source } -private fun List.joinSubSourcesHtml() = joinToString("") { - it.source.asHtmlSource +private fun List.joinSubSourcesHtml() = joinToString("") { + it.asHtmlSource } internal fun MultilevelTextSource.markdownV2Default( openControlSymbol: String, closeControlSymbol: String = openControlSymbol -) = "$openControlSymbol${textParts.joinSubSourcesMarkdownV2()}$closeControlSymbol" +) = "$openControlSymbol${textSources.joinSubSourcesMarkdownV2()}$closeControlSymbol" internal fun MultilevelTextSource.htmlDefault( openControlSymbol: String, closeControlSymbol: String = openControlSymbol -) = "<$openControlSymbol>${textParts.joinSubSourcesHtml()}" +) = "<$openControlSymbol>${textSources.joinSubSourcesHtml()}" internal fun MultilevelTextSource.linkMarkdownV2( link: String -) = "[${textParts.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})" +) = "[${textSources.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})" internal fun MultilevelTextSource.linkHTML( link: String -) = "${textParts.joinSubSourcesHtml()}" +) = "${textSources.joinSubSourcesHtml()}" internal fun MultilevelTextSource.emailMarkdownV2(address: String): String = linkMarkdownV2("mailto://$address") @@ -107,8 +106,8 @@ internal fun MultilevelTextSource.boldMarkdownV2(): String = markdownV2Default(m internal fun MultilevelTextSource.boldHTML(): String = htmlDefault(htmlBoldControl) -internal fun MultilevelTextSource.cashTagMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2() -internal fun MultilevelTextSource.cashTagHTML(): String = textParts.joinSubSourcesHtml() +internal fun MultilevelTextSource.cashTagMarkdownV2(): String = textSources.joinSubSourcesMarkdownV2() +internal fun MultilevelTextSource.cashTagHTML(): String = textSources.joinSubSourcesHtml() internal fun MultilevelTextSource.italicMarkdownV2(): String = markdownV2Default(markdownItalicControl) @@ -126,18 +125,18 @@ internal fun MultilevelTextSource.underlineHTML(): String = htmlDefault(htmlUnde internal fun MultilevelTextSource.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link) internal fun MultilevelTextSource.textMentionHTML(userId: UserId): String = linkHTML(userId.link) -internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textParts.joinSubSourcesMarkdownV2()}" -internal fun MultilevelTextSource.mentionHTML(): String = "@${textParts.joinSubSourcesHtml()}" +internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textSources.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.mentionHTML(): String = "@${textSources.joinSubSourcesHtml()}" -internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textParts.joinSubSourcesMarkdownV2()}" -internal fun MultilevelTextSource.hashTagHTML(): String = "#${textParts.joinSubSourcesHtml()}" +internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textSources.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.hashTagHTML(): String = "#${textSources.joinSubSourcesHtml()}" -internal fun MultilevelTextSource.phoneMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2() -internal fun MultilevelTextSource.phoneHTML(): String = textParts.joinSubSourcesHtml() +internal fun MultilevelTextSource.phoneMarkdownV2(): String = textSources.joinSubSourcesMarkdownV2() +internal fun MultilevelTextSource.phoneHTML(): String = textSources.joinSubSourcesHtml() -internal fun MultilevelTextSource.commandMarkdownV2(): String = "/${textParts.joinSubSourcesMarkdownV2()}" -internal fun MultilevelTextSource.commandHTML(): String = "/${textParts.joinSubSourcesHtml()}" +internal fun MultilevelTextSource.commandMarkdownV2(): String = "/${textSources.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.commandHTML(): String = "/${textSources.joinSubSourcesHtml()}" diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt index f39a5cfef0..afb30a7196 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types.MessageEntity +import dev.inmo.tgbotapi.CommonAbstracts.justTextSources import dev.inmo.tgbotapi.types.MessageEntity.textsources.* import dev.inmo.tgbotapi.utils.* import kotlin.test.* @@ -32,43 +33,21 @@ class TextPartsCreatingTests { ) ) - val textParts = createTextPart(text, entities) + val textParts = entities.asTextParts(text) + assertTrue (textParts.first().source is RegularTextSource) + assertTrue (textParts[1].source is BoldTextSource) + assertTrue (textParts[2].source is RegularTextSource) - assertTrue ( - textParts.first().source is BoldTextSource - ) - - val boldSource = textParts.first().source as BoldTextSource - assertTrue ( - boldSource.textParts.first().source is ItalicTextSource - ) - assertTrue ( - boldSource.textParts[1].source is RegularTextSource - ) - assertTrue ( - boldSource.textParts[2].source is StrikethroughTextSource - ) - assertTrue ( - (boldSource.textParts[2].source as StrikethroughTextSource).textParts.first().source is UnderlineTextSource - ) - - - val fullTextParts = text.fullListOfSubSource(textParts) - - assertTrue( - fullTextParts.first().source is RegularTextSource - ) - assertTrue( - fullTextParts[1].source is BoldTextSource - ) - assertTrue( - fullTextParts[2].source is RegularTextSource - ) + val boldSource = textParts[1].source as BoldTextSource + assertTrue (boldSource.textSources.first() is ItalicTextSource) + assertTrue (boldSource.textSources[1] is RegularTextSource) + assertTrue (boldSource.textSources[2] is StrikethroughTextSource) + assertTrue ((boldSource.textSources[2] as StrikethroughTextSource).textSources.first() is UnderlineTextSource) assertEquals( formattedV2Text, - createMarkdownV2Text(fullTextParts.map { it.source }).first() + textParts.justTextSources().toMarkdownV2Texts().first() ) } @@ -99,43 +78,21 @@ class TextPartsCreatingTests { ) ) - val textParts = createTextPart(text, entities) + val textParts = entities.asTextParts(text) + assertTrue (textParts.first().source is RegularTextSource) + assertTrue (textParts[1].source is BoldTextSource) + assertTrue (textParts[2].source is RegularTextSource) - assertTrue ( - textParts.first().source is BoldTextSource - ) - - val boldSource = textParts.first().source as BoldTextSource - assertTrue ( - boldSource.textParts.first().source is ItalicTextSource - ) - assertTrue ( - boldSource.textParts[1].source is RegularTextSource - ) - assertTrue ( - boldSource.textParts[2].source is StrikethroughTextSource - ) - assertTrue ( - (boldSource.textParts[2].source as StrikethroughTextSource).textParts.first().source is UnderlineTextSource - ) - - - val fullTextParts = text.fullListOfSubSource(textParts) - - assertTrue( - fullTextParts.first().source is RegularTextSource - ) - assertTrue( - fullTextParts[1].source is BoldTextSource - ) - assertTrue( - fullTextParts[2].source is RegularTextSource - ) + val boldSource = textParts[1].source as BoldTextSource + assertTrue (boldSource.textSources.first() is ItalicTextSource) + assertTrue (boldSource.textSources[1] is RegularTextSource) + assertTrue (boldSource.textSources[2] is StrikethroughTextSource) + assertTrue ((boldSource.textSources[2] as StrikethroughTextSource).textSources.first() is UnderlineTextSource) assertEquals( formattedHtmlText, - createHtmlText(fullTextParts.map { it.source }).first() + textParts.justTextSources().toHtmlTexts().first() ) } }