mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-12-22 22:26:01 +00:00
add spoiler support
This commit is contained in:
@@ -49,6 +49,7 @@ internal fun RawMessageEntity.asTextSource(
|
||||
)
|
||||
"underline" -> UnderlineTextSource(sourceSubstring, subPartsWithRegulars)
|
||||
"strikethrough" -> StrikethroughTextSource(sourceSubstring, subPartsWithRegulars)
|
||||
"spoiler" -> SpoilerTextSource(sourceSubstring, subPartsWithRegulars)
|
||||
else -> RegularTextSource(sourceSubstring)
|
||||
}
|
||||
}
|
||||
@@ -158,6 +159,7 @@ internal fun TextSource.toRawMessageEntities(offset: Int = 0): List<RawMessageEn
|
||||
is TextMentionTextSource -> RawMessageEntity("text_mention", offset, length, user = user)
|
||||
is UnderlineTextSource -> RawMessageEntity("underline", offset, length)
|
||||
is StrikethroughTextSource -> RawMessageEntity("strikethrough", offset, length)
|
||||
is SpoilerTextSource -> RawMessageEntity("spoiler", offset, length)
|
||||
else -> null
|
||||
}
|
||||
) + if (this is MultilevelTextSource) {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||
|
||||
import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import dev.inmo.tgbotapi.utils.extensions.makeString
|
||||
import dev.inmo.tgbotapi.utils.internal.*
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* @see italic
|
||||
*/
|
||||
@Serializable
|
||||
data class SpoilerTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor (
|
||||
override val source: String,
|
||||
override val subsources: TextSourcesList
|
||||
) : MultilevelTextSource {
|
||||
override val markdown: String by lazy { source.spoilerMarkdown() }
|
||||
override val markdownV2: String by lazy { spoilerMarkdownV2() }
|
||||
override val html: String by lazy { spoilerHTML() }
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun spoiler(parts: TextSourcesList) = SpoilerTextSource(parts.makeString(), parts)
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun spoiler(vararg parts: TextSource) = spoiler(parts.toList())
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun spoiler(text: String) = spoiler(regular(text))
|
||||
|
||||
@@ -20,6 +20,7 @@ private val baseSerializers: Map<String, KSerializer<out TextSource>> = mapOf(
|
||||
"text_mention" to TextMentionTextSource.serializer(),
|
||||
"hashtag" to HashTagTextSource.serializer(),
|
||||
"cashtag" to CashTagTextSource.serializer(),
|
||||
"spoiler" to SpoilerTextSource.serializer(),
|
||||
)
|
||||
|
||||
object TextSourceSerializer : TypedSerializer<TextSource>(TextSource::class, baseSerializers) {
|
||||
|
||||
@@ -49,6 +49,10 @@ internal fun MultilevelTextSource.italicMarkdownV2(): String = markdownV2Default
|
||||
internal fun MultilevelTextSource.italicHTML(): String = htmlDefault(htmlItalicControl)
|
||||
|
||||
|
||||
internal fun MultilevelTextSource.spoilerMarkdownV2(): String = markdownV2Default(markdownSpoilerControl)
|
||||
internal fun MultilevelTextSource.spoilerHTML(): String = htmlDefault(htmlSpoilerControl, htmlSpoilerClosingControl)
|
||||
|
||||
|
||||
internal fun MultilevelTextSource.strikethroughMarkdownV2(): String = markdownV2Default(markdownV2StrikethroughControl)
|
||||
internal fun MultilevelTextSource.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.utils.extensions.*
|
||||
|
||||
const val markdownBoldControl = "*"
|
||||
const val markdownItalicControl = "_"
|
||||
const val markdownSpoilerControl = "||"
|
||||
const val markdownCodeControl = "`"
|
||||
const val markdownPreControl = "```"
|
||||
|
||||
@@ -18,6 +19,8 @@ const val markdownV2ItalicEndControl = "$markdownItalicControl$markdownV2ItalicU
|
||||
|
||||
const val htmlBoldControl = "b"
|
||||
const val htmlItalicControl = "i"
|
||||
const val htmlSpoilerControl = "span class=\"tg-spoiler\""
|
||||
const val htmlSpoilerClosingControl = "span"
|
||||
const val htmlCodeControl = "code"
|
||||
const val htmlPreControl = "pre"
|
||||
const val htmlUnderlineControl = "u"
|
||||
@@ -46,12 +49,13 @@ internal fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl
|
||||
|
||||
internal fun String.italicMarkdown(): String = markdownDefault(markdownItalicControl)
|
||||
|
||||
internal fun String.spoilerMarkdown(): String = markdownDefault(markdownSpoilerControl)
|
||||
|
||||
/**
|
||||
* Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct
|
||||
*/
|
||||
|
||||
internal fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("")
|
||||
internal fun String.strikethroughMarkdownV2(): String = markdownV2Default(markdownV2StrikethroughControl)
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user