mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
reorganize text sources and text parts
This commit is contained in:
parent
ee6f0f3d5d
commit
6665b6ef03
@ -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<TextPart>
|
||||
get() = textParts(0)
|
||||
val textSources: List<TextSource>
|
||||
}
|
||||
|
||||
data class TextPart(
|
||||
@ -25,6 +29,7 @@ data class TextPart(
|
||||
|
||||
fun List<TextPart>.justTextSources() = map { it.source }
|
||||
fun List<TextSource>.makeString() = joinToString("") { it.source }
|
||||
fun MultilevelTextSource.textParts(offset: Int): List<TextPart> = textSources.toTextParts(offset)
|
||||
fun List<TextSource>.separateForMessage(limit: IntRange, numberOfParts: Int? = null): List<List<TextSource>> {
|
||||
if (isEmpty()) {
|
||||
return emptyList()
|
||||
|
@ -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<TextPart>): List<TextPart> {
|
||||
val sourceSubstring = source.substring(offset, offset + length)
|
||||
internal fun RawMessageEntity.asTextParts(
|
||||
source: String,
|
||||
subParts: List<TextPart>
|
||||
): List<TextPart> {
|
||||
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<TextPar
|
||||
}
|
||||
}
|
||||
|
||||
internal fun createTextPart(from: String, entities: RawMessageEntities): List<TextPart> {
|
||||
internal fun createTextPart(originalFullString: String, entities: RawMessageEntities): List<TextPart> {
|
||||
val mutableEntities = entities.toMutableList()
|
||||
mutableEntities.sortBy { it.offset }
|
||||
val resultList = mutableListOf<TextPart>()
|
||||
@ -73,8 +77,8 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
|
||||
|
||||
resultList.addAll(
|
||||
currentFirst.asTextParts(
|
||||
from,
|
||||
createTextPart(from, subEntities)
|
||||
originalFullString,
|
||||
createTextPart(originalFullString, subEntities)
|
||||
)
|
||||
)
|
||||
}
|
||||
@ -82,31 +86,40 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
|
||||
return resultList
|
||||
}
|
||||
|
||||
internal fun List<TextPart>.asRawMessageEntities(): List<RawMessageEntity> = 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<RawMessageEntity> {
|
||||
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<TextSource>.toRawMessageEntities(): List<RawMessageEntity> {
|
||||
var i = 0
|
||||
internal fun List<TextPart>.asRawMessageEntities(): List<RawMessageEntity> = flatMap { it.asRawMessageEntities() }
|
||||
|
||||
internal fun List<TextSource>.toTextParts(preOffset: Int = 0): List<TextPart> {
|
||||
var i = preOffset
|
||||
return map {
|
||||
TextPart(
|
||||
i until (i + it.source.length),
|
||||
@ -114,9 +127,13 @@ internal fun List<TextSource>.toRawMessageEntities(): List<RawMessageEntity> {
|
||||
).also {
|
||||
i = it.range.last + 1
|
||||
}
|
||||
}.asRawMessageEntities()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun RawMessageEntities.asTextParts(sourceString: String): List<TextPart> = createTextPart(sourceString, this)
|
||||
internal fun List<TextSource>.toRawMessageEntities(): List<RawMessageEntity> = toTextParts().asRawMessageEntities()
|
||||
|
||||
internal fun RawMessageEntities.asTextParts(sourceString: String): List<TextPart> = sourceString.fullListOfSubSource(
|
||||
createTextPart(sourceString, this)
|
||||
)
|
||||
|
||||
internal typealias RawMessageEntities = List<RawMessageEntity>
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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<TextSource>) = BoldTextSource(parts.makeString(), parts)
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
val command: String by lazy {
|
||||
commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
|
||||
}
|
||||
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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) }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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) }
|
||||
|
@ -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<TextPart>
|
||||
override val textSources: List<TextSource>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> 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() }
|
||||
|
@ -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,
|
||||
|
@ -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<TextPart>.shiftSourcesToTheLeft(shiftCount: Int = 1): List<Tex
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<TextPart>.joinSubSourcesMarkdownV2() = joinToString("") {
|
||||
it.source.asMarkdownV2Source
|
||||
private fun List<TextSource>.joinSubSourcesMarkdownV2() = joinToString("") {
|
||||
it.asMarkdownV2Source
|
||||
}
|
||||
|
||||
private fun List<TextPart>.joinSubSourcesHtml() = joinToString("") {
|
||||
it.source.asHtmlSource
|
||||
private fun List<TextSource>.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()}</$closeControlSymbol>"
|
||||
) = "<$openControlSymbol>${textSources.joinSubSourcesHtml()}</$closeControlSymbol>"
|
||||
|
||||
|
||||
internal fun MultilevelTextSource.linkMarkdownV2(
|
||||
link: String
|
||||
) = "[${textParts.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})"
|
||||
) = "[${textSources.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})"
|
||||
internal fun MultilevelTextSource.linkHTML(
|
||||
link: String
|
||||
) = "<a href=\"${link.toHtml()}\">${textParts.joinSubSourcesHtml()}</a>"
|
||||
) = "<a href=\"${link.toHtml()}\">${textSources.joinSubSourcesHtml()}</a>"
|
||||
|
||||
|
||||
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()}"
|
||||
|
||||
|
@ -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()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user