mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +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
|
package dev.inmo.tgbotapi.CommonAbstracts
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.types.MessageEntity.toTextParts
|
||||||
import dev.inmo.tgbotapi.types.captionLength
|
import dev.inmo.tgbotapi.types.captionLength
|
||||||
import dev.inmo.tgbotapi.types.textLength
|
import dev.inmo.tgbotapi.types.textLength
|
||||||
|
|
||||||
@ -15,7 +16,10 @@ interface TextSource {
|
|||||||
|
|
||||||
|
|
||||||
interface MultilevelTextSource : TextSource {
|
interface MultilevelTextSource : TextSource {
|
||||||
|
@Deprecated("Will be removed in near major release")
|
||||||
val textParts: List<TextPart>
|
val textParts: List<TextPart>
|
||||||
|
get() = textParts(0)
|
||||||
|
val textSources: List<TextSource>
|
||||||
}
|
}
|
||||||
|
|
||||||
data class TextPart(
|
data class TextPart(
|
||||||
@ -25,6 +29,7 @@ data class TextPart(
|
|||||||
|
|
||||||
fun List<TextPart>.justTextSources() = map { it.source }
|
fun List<TextPart>.justTextSources() = map { it.source }
|
||||||
fun List<TextSource>.makeString() = joinToString("") { 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>> {
|
fun List<TextSource>.separateForMessage(limit: IntRange, numberOfParts: Int? = null): List<List<TextSource>> {
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
return emptyList()
|
return emptyList()
|
||||||
|
@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.MessageEntity
|
|||||||
import dev.inmo.tgbotapi.CommonAbstracts.*
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.*
|
import dev.inmo.tgbotapi.types.MessageEntity.textsources.*
|
||||||
import dev.inmo.tgbotapi.types.User
|
import dev.inmo.tgbotapi.types.User
|
||||||
|
import dev.inmo.tgbotapi.utils.fullListOfSubSource
|
||||||
import dev.inmo.tgbotapi.utils.shiftSourcesToTheLeft
|
import dev.inmo.tgbotapi.utils.shiftSourcesToTheLeft
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@ -16,26 +17,29 @@ internal data class RawMessageEntity(
|
|||||||
val language: String? = null
|
val language: String? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
internal fun RawMessageEntity.asTextParts(source: String, subParts: List<TextPart>): List<TextPart> {
|
internal fun RawMessageEntity.asTextParts(
|
||||||
val sourceSubstring = source.substring(offset, offset + length)
|
source: String,
|
||||||
|
subParts: List<TextPart>
|
||||||
|
): List<TextPart> {
|
||||||
|
val sourceSubstring: String = source.substring(offset, offset + length)
|
||||||
val range = offset until (offset + length)
|
val range = offset until (offset + length)
|
||||||
val shiftedSubParts = subParts.shiftSourcesToTheLeft(offset)
|
val shiftedSubSources = sourceSubstring.fullListOfSubSource(subParts.shiftSourcesToTheLeft(offset)).justTextSources()
|
||||||
return when (type) {
|
return when (type) {
|
||||||
"mention" -> MentionTextSource(sourceSubstring, shiftedSubParts)
|
"mention" -> MentionTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"hashtag" -> HashTagTextSource(sourceSubstring, shiftedSubParts)
|
"hashtag" -> HashTagTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"cashtag" -> CashTagTextSource(sourceSubstring, shiftedSubParts)
|
"cashtag" -> CashTagTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"bot_command" -> BotCommandTextSource(sourceSubstring, shiftedSubParts)
|
"bot_command" -> BotCommandTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"url" -> URLTextSource(sourceSubstring)
|
"url" -> URLTextSource(sourceSubstring)
|
||||||
"email" -> EMailTextSource(sourceSubstring, shiftedSubParts)
|
"email" -> EMailTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"phone_number" -> PhoneNumberTextSource(sourceSubstring, shiftedSubParts)
|
"phone_number" -> PhoneNumberTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"bold" -> BoldTextSource(sourceSubstring, shiftedSubParts)
|
"bold" -> BoldTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"italic" -> ItalicTextSource(sourceSubstring, shiftedSubParts)
|
"italic" -> ItalicTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"code" -> CodeTextSource(sourceSubstring)
|
"code" -> CodeTextSource(sourceSubstring)
|
||||||
"pre" -> PreTextSource(sourceSubstring, language)
|
"pre" -> PreTextSource(sourceSubstring, language)
|
||||||
"text_link" -> TextLinkTextSource(sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link"))
|
"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)
|
"text_mention" -> TextMentionTextSource(sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"), shiftedSubSources)
|
||||||
"underline" -> UnderlineTextSource(sourceSubstring, shiftedSubParts)
|
"underline" -> UnderlineTextSource(sourceSubstring, shiftedSubSources)
|
||||||
"strikethrough" -> StrikethroughTextSource(sourceSubstring, shiftedSubParts)
|
"strikethrough" -> StrikethroughTextSource(sourceSubstring, shiftedSubSources)
|
||||||
else -> RegularTextSource(sourceSubstring)
|
else -> RegularTextSource(sourceSubstring)
|
||||||
}.let {
|
}.let {
|
||||||
val part = TextPart(range, it)
|
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()
|
val mutableEntities = entities.toMutableList()
|
||||||
mutableEntities.sortBy { it.offset }
|
mutableEntities.sortBy { it.offset }
|
||||||
val resultList = mutableListOf<TextPart>()
|
val resultList = mutableListOf<TextPart>()
|
||||||
@ -73,8 +77,8 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
|
|||||||
|
|
||||||
resultList.addAll(
|
resultList.addAll(
|
||||||
currentFirst.asTextParts(
|
currentFirst.asTextParts(
|
||||||
from,
|
originalFullString,
|
||||||
createTextPart(from, subEntities)
|
createTextPart(originalFullString, subEntities)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -82,31 +86,40 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
|
|||||||
return resultList
|
return resultList
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun List<TextPart>.asRawMessageEntities(): List<RawMessageEntity> = mapNotNull {
|
internal fun TextPart.asRawMessageEntities(): List<RawMessageEntity> {
|
||||||
val source = it.source
|
val source = source
|
||||||
val length = it.range.last - it.range.first + 1
|
val length = range.last - range.first + 1
|
||||||
when (source) {
|
|
||||||
is MentionTextSource -> RawMessageEntity("mention", it.range.first, length)
|
return listOfNotNull(
|
||||||
is HashTagTextSource -> RawMessageEntity("hashtag", it.range.first, length)
|
when (source) {
|
||||||
is CashTagTextSource -> RawMessageEntity("cashtag", it.range.first, length)
|
is MentionTextSource -> RawMessageEntity("mention", range.first, length)
|
||||||
is BotCommandTextSource -> RawMessageEntity("bot_command", it.range.first, length)
|
is HashTagTextSource -> RawMessageEntity("hashtag", range.first, length)
|
||||||
is URLTextSource -> RawMessageEntity("url", it.range.first, length)
|
is CashTagTextSource -> RawMessageEntity("cashtag", range.first, length)
|
||||||
is EMailTextSource -> RawMessageEntity("email", it.range.first, length)
|
is BotCommandTextSource -> RawMessageEntity("bot_command", range.first, length)
|
||||||
is PhoneNumberTextSource -> RawMessageEntity("phone_number", it.range.first, length)
|
is URLTextSource -> RawMessageEntity("url", range.first, length)
|
||||||
is BoldTextSource -> RawMessageEntity("bold", it.range.first, length)
|
is EMailTextSource -> RawMessageEntity("email", range.first, length)
|
||||||
is ItalicTextSource -> RawMessageEntity("italic", it.range.first, length)
|
is PhoneNumberTextSource -> RawMessageEntity("phone_number", range.first, length)
|
||||||
is CodeTextSource -> RawMessageEntity("code", it.range.first, length)
|
is BoldTextSource -> RawMessageEntity("bold", range.first, length)
|
||||||
is PreTextSource -> RawMessageEntity("pre", it.range.first, length, language = source.language)
|
is ItalicTextSource -> RawMessageEntity("italic", range.first, length)
|
||||||
is TextLinkTextSource -> RawMessageEntity("text_link", it.range.first, length, source.url)
|
is CodeTextSource -> RawMessageEntity("code", range.first, length)
|
||||||
is TextMentionTextSource -> RawMessageEntity("text_mention", it.range.first, length, user = source.user)
|
is PreTextSource -> RawMessageEntity("pre", range.first, length, language = source.language)
|
||||||
is UnderlineTextSource -> RawMessageEntity("underline", it.range.first, length)
|
is TextLinkTextSource -> RawMessageEntity("text_link", range.first, length, source.url)
|
||||||
is StrikethroughTextSource -> RawMessageEntity("strikethrough", it.range.first, length)
|
is TextMentionTextSource -> RawMessageEntity("text_mention", range.first, length, user = source.user)
|
||||||
else -> null
|
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> {
|
internal fun List<TextPart>.asRawMessageEntities(): List<RawMessageEntity> = flatMap { it.asRawMessageEntities() }
|
||||||
var i = 0
|
|
||||||
|
internal fun List<TextSource>.toTextParts(preOffset: Int = 0): List<TextPart> {
|
||||||
|
var i = preOffset
|
||||||
return map {
|
return map {
|
||||||
TextPart(
|
TextPart(
|
||||||
i until (i + it.source.length),
|
i until (i + it.source.length),
|
||||||
@ -114,9 +127,13 @@ internal fun List<TextSource>.toRawMessageEntities(): List<RawMessageEntity> {
|
|||||||
).also {
|
).also {
|
||||||
i = it.range.last + 1
|
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>
|
internal typealias RawMessageEntities = List<RawMessageEntity>
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
import dev.inmo.tgbotapi.types.MessageEntity.toTextParts
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class BoldTextSource(
|
class BoldTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.boldMarkdown() }
|
override val asMarkdownSource: String by lazy { source.boldMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { boldMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { boldMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { boldHTML() }
|
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
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
private val commandRegex = Regex("[/!][^@\\s]*")
|
private val commandRegex = Regex("[/!][^@\\s]*")
|
||||||
|
|
||||||
class BotCommandTextSource(
|
class BotCommandTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
val command: String by lazy {
|
val command: String by lazy {
|
||||||
commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
|
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 asMarkdownSource: String by lazy { source.commandMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { commandMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { commandMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { commandHTML() }
|
override val asHtmlSource: String by lazy { commandHTML() }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class CashTagTextSource(
|
class CashTagTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.cashTagMarkdown() }
|
override val asMarkdownSource: String by lazy { source.cashTagMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { cashTagMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { cashTagMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { cashTagHTML() }
|
override val asHtmlSource: String by lazy { cashTagHTML() }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class EMailTextSource(
|
class EMailTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.emailMarkdown() }
|
override val asMarkdownSource: String by lazy { source.emailMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { emailMarkdownV2(source) }
|
override val asMarkdownV2Source: String by lazy { emailMarkdownV2(source) }
|
||||||
override val asHtmlSource: String by lazy { emailHTML(source) }
|
override val asHtmlSource: String by lazy { emailHTML(source) }
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
private val String.withoutSharp
|
private val String.withoutSharp
|
||||||
@ -13,13 +12,8 @@ private val String.withoutSharp
|
|||||||
|
|
||||||
class HashTagTextSource(
|
class HashTagTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy {
|
|
||||||
source.withoutSharp.fullListOfSubSource(
|
|
||||||
textParts.shiftSourcesToTheLeft(1)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
override val asMarkdownSource: String by lazy { source.hashTagMarkdown() }
|
override val asMarkdownSource: String by lazy { source.hashTagMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { hashTagMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { hashTagMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { hashTagHTML() }
|
override val asHtmlSource: String by lazy { hashTagHTML() }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class ItalicTextSource(
|
class ItalicTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.italicMarkdown() }
|
override val asMarkdownSource: String by lazy { source.italicMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { italicMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { italicMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { italicHTML() }
|
override val asHtmlSource: String by lazy { italicHTML() }
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
private val String.withoutCommercialAt
|
private val String.withoutCommercialAt
|
||||||
@ -13,13 +12,8 @@ private val String.withoutCommercialAt
|
|||||||
|
|
||||||
class MentionTextSource(
|
class MentionTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy {
|
|
||||||
source.withoutCommercialAt.fullListOfSubSource(
|
|
||||||
textParts.shiftSourcesToTheLeft(1)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
override val asMarkdownSource: String by lazy { source.mentionMarkdown() }
|
override val asMarkdownSource: String by lazy { source.mentionMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { mentionMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { mentionMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { mentionHTML() }
|
override val asHtmlSource: String by lazy { mentionHTML() }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class PhoneNumberTextSource(
|
class PhoneNumberTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.phoneMarkdown() }
|
override val asMarkdownSource: String by lazy { source.phoneMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { phoneMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { phoneMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { phoneHTML() }
|
override val asHtmlSource: String by lazy { phoneHTML() }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class StrikethroughTextSource(
|
class StrikethroughTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asHtmlSource: String by lazy { strikethroughHTML() }
|
override val asHtmlSource: String by lazy { strikethroughHTML() }
|
||||||
override val asMarkdownV2Source: String by lazy { strikethroughMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { strikethroughMarkdownV2() }
|
||||||
override val asMarkdownSource: String by lazy { source.strikethroughMarkdown() }
|
override val asMarkdownSource: String by lazy { source.strikethroughMarkdown() }
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.types.User
|
import dev.inmo.tgbotapi.types.User
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class TextMentionTextSource(
|
class TextMentionTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
val user: User,
|
val user: User,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.textMentionMarkdown(user.id) }
|
override val asMarkdownSource: String by lazy { source.textMentionMarkdown(user.id) }
|
||||||
override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(user.id) }
|
override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(user.id) }
|
||||||
override val asHtmlSource: String by lazy { textMentionHTML(user.id) }
|
override val asHtmlSource: String by lazy { textMentionHTML(user.id) }
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
|
||||||
class UnderlineTextSource(
|
class UnderlineTextSource(
|
||||||
override val source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
override val textSources: List<TextSource>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
|
||||||
override val asMarkdownSource: String by lazy { source.underlineMarkdown() }
|
override val asMarkdownSource: String by lazy { source.underlineMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { underlineMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { underlineMarkdownV2() }
|
||||||
override val asHtmlSource: String by lazy { underlineHTML() }
|
override val asHtmlSource: String by lazy { underlineHTML() }
|
||||||
|
@ -92,11 +92,11 @@ internal data class RawMessage(
|
|||||||
) {
|
) {
|
||||||
private val content: MessageContent? by lazy {
|
private val content: MessageContent? by lazy {
|
||||||
val adaptedCaptionEntities = caption ?.let {
|
val adaptedCaptionEntities = caption ?.let {
|
||||||
it.fullListOfSubSource(caption_entities ?.asTextParts(caption) ?: emptyList())
|
(caption_entities ?: emptyList()).asTextParts(caption)
|
||||||
} ?: emptyList()
|
} ?: emptyList()
|
||||||
|
|
||||||
when {
|
when {
|
||||||
text != null -> TextContent(text, text.fullListOfSubSource(entities ?.asTextParts(text) ?: emptyList()))
|
text != null -> TextContent(text, (entities ?: emptyList()).asTextParts(text))
|
||||||
audio != null -> AudioContent(
|
audio != null -> AudioContent(
|
||||||
audio,
|
audio,
|
||||||
caption,
|
caption,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.utils
|
package dev.inmo.tgbotapi.utils
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.MultilevelTextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.*
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextPart
|
|
||||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.RegularTextSource
|
import dev.inmo.tgbotapi.types.MessageEntity.textsources.RegularTextSource
|
||||||
import dev.inmo.tgbotapi.types.UserId
|
import dev.inmo.tgbotapi.types.UserId
|
||||||
import dev.inmo.tgbotapi.types.link
|
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("") {
|
private fun List<TextSource>.joinSubSourcesMarkdownV2() = joinToString("") {
|
||||||
it.source.asMarkdownV2Source
|
it.asMarkdownV2Source
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<TextPart>.joinSubSourcesHtml() = joinToString("") {
|
private fun List<TextSource>.joinSubSourcesHtml() = joinToString("") {
|
||||||
it.source.asHtmlSource
|
it.asHtmlSource
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun MultilevelTextSource.markdownV2Default(
|
internal fun MultilevelTextSource.markdownV2Default(
|
||||||
openControlSymbol: String,
|
openControlSymbol: String,
|
||||||
closeControlSymbol: String = openControlSymbol
|
closeControlSymbol: String = openControlSymbol
|
||||||
) = "$openControlSymbol${textParts.joinSubSourcesMarkdownV2()}$closeControlSymbol"
|
) = "$openControlSymbol${textSources.joinSubSourcesMarkdownV2()}$closeControlSymbol"
|
||||||
internal fun MultilevelTextSource.htmlDefault(
|
internal fun MultilevelTextSource.htmlDefault(
|
||||||
openControlSymbol: String,
|
openControlSymbol: String,
|
||||||
closeControlSymbol: String = openControlSymbol
|
closeControlSymbol: String = openControlSymbol
|
||||||
) = "<$openControlSymbol>${textParts.joinSubSourcesHtml()}</$closeControlSymbol>"
|
) = "<$openControlSymbol>${textSources.joinSubSourcesHtml()}</$closeControlSymbol>"
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.linkMarkdownV2(
|
internal fun MultilevelTextSource.linkMarkdownV2(
|
||||||
link: String
|
link: String
|
||||||
) = "[${textParts.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})"
|
) = "[${textSources.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})"
|
||||||
internal fun MultilevelTextSource.linkHTML(
|
internal fun MultilevelTextSource.linkHTML(
|
||||||
link: String
|
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")
|
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.boldHTML(): String = htmlDefault(htmlBoldControl)
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.cashTagMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2()
|
internal fun MultilevelTextSource.cashTagMarkdownV2(): String = textSources.joinSubSourcesMarkdownV2()
|
||||||
internal fun MultilevelTextSource.cashTagHTML(): String = textParts.joinSubSourcesHtml()
|
internal fun MultilevelTextSource.cashTagHTML(): String = textSources.joinSubSourcesHtml()
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.italicMarkdownV2(): String = markdownV2Default(markdownItalicControl)
|
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.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link)
|
||||||
internal fun MultilevelTextSource.textMentionHTML(userId: UserId): String = linkHTML(userId.link)
|
internal fun MultilevelTextSource.textMentionHTML(userId: UserId): String = linkHTML(userId.link)
|
||||||
|
|
||||||
internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textParts.joinSubSourcesMarkdownV2()}"
|
internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textSources.joinSubSourcesMarkdownV2()}"
|
||||||
internal fun MultilevelTextSource.mentionHTML(): String = "@${textParts.joinSubSourcesHtml()}"
|
internal fun MultilevelTextSource.mentionHTML(): String = "@${textSources.joinSubSourcesHtml()}"
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textParts.joinSubSourcesMarkdownV2()}"
|
internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textSources.joinSubSourcesMarkdownV2()}"
|
||||||
internal fun MultilevelTextSource.hashTagHTML(): String = "#${textParts.joinSubSourcesHtml()}"
|
internal fun MultilevelTextSource.hashTagHTML(): String = "#${textSources.joinSubSourcesHtml()}"
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.phoneMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2()
|
internal fun MultilevelTextSource.phoneMarkdownV2(): String = textSources.joinSubSourcesMarkdownV2()
|
||||||
internal fun MultilevelTextSource.phoneHTML(): String = textParts.joinSubSourcesHtml()
|
internal fun MultilevelTextSource.phoneHTML(): String = textSources.joinSubSourcesHtml()
|
||||||
|
|
||||||
|
|
||||||
internal fun MultilevelTextSource.commandMarkdownV2(): String = "/${textParts.joinSubSourcesMarkdownV2()}"
|
internal fun MultilevelTextSource.commandMarkdownV2(): String = "/${textSources.joinSubSourcesMarkdownV2()}"
|
||||||
internal fun MultilevelTextSource.commandHTML(): String = "/${textParts.joinSubSourcesHtml()}"
|
internal fun MultilevelTextSource.commandHTML(): String = "/${textSources.joinSubSourcesHtml()}"
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity
|
package dev.inmo.tgbotapi.types.MessageEntity
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.CommonAbstracts.justTextSources
|
||||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.*
|
import dev.inmo.tgbotapi.types.MessageEntity.textsources.*
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
import kotlin.test.*
|
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 (
|
val boldSource = textParts[1].source as BoldTextSource
|
||||||
textParts.first().source is BoldTextSource
|
assertTrue (boldSource.textSources.first() is ItalicTextSource)
|
||||||
)
|
assertTrue (boldSource.textSources[1] is RegularTextSource)
|
||||||
|
assertTrue (boldSource.textSources[2] is StrikethroughTextSource)
|
||||||
val boldSource = textParts.first().source as BoldTextSource
|
assertTrue ((boldSource.textSources[2] as StrikethroughTextSource).textSources.first() is UnderlineTextSource)
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
formattedV2Text,
|
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 (
|
val boldSource = textParts[1].source as BoldTextSource
|
||||||
textParts.first().source is BoldTextSource
|
assertTrue (boldSource.textSources.first() is ItalicTextSource)
|
||||||
)
|
assertTrue (boldSource.textSources[1] is RegularTextSource)
|
||||||
|
assertTrue (boldSource.textSources[2] is StrikethroughTextSource)
|
||||||
val boldSource = textParts.first().source as BoldTextSource
|
assertTrue ((boldSource.textSources[2] as StrikethroughTextSource).textSources.first() is UnderlineTextSource)
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
formattedHtmlText,
|
formattedHtmlText,
|
||||||
createHtmlText(fullTextParts.map { it.source }).first()
|
textParts.justTextSources().toHtmlTexts().first()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user