diff --git a/CHANGELOG.md b/CHANGELOG.md index 137240d8d6..d693385ccf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ bot. ## 0.20.0 MPP Migration +* Added support of strikethrough and underline + * Added `UnderlineTextSource` and `UnderlineMessageEntity` + * Added `StrikethroughTextSource` and `StrikethroughMessageEntity` + * Added support in `RawMessageEntity` * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock` * `Currencied` now using as `currency` value with type `String` * For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index 489531935c..83fc584dd5 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.internal.ArrayListSerializer @@ -28,7 +29,9 @@ internal data class RawMessageEntity( "code" -> CodeTextMessageEntity(offset, length, sourceSubstring) "pre" -> PreTextMessageEntity(offset, length, sourceSubstring) "text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) - "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention")) + "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user as PrivateChat) + "underline" -> UnderlineMessageEntity(offset, length, sourceSubstring) + "strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring) else -> throw IllegalArgumentException("Unknown type of message entity") } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt new file mode 100644 index 0000000000..494980e8d0 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.StrikethroughTextSource + +class StrikethroughMessageEntity( + override val offset: Int, + override val length: Int, + override val sourceString: String +) : MessageEntity, TextSource by StrikethroughTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt new file mode 100644 index 0000000000..7f630187cb --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.UnderlineTextSource + +class UnderlineMessageEntity( + override val offset: Int, + override val length: Int, + override val sourceString: String +) : MessageEntity, TextSource by UnderlineTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt new file mode 100644 index 0000000000..2cd6305be3 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown + +class StrikethroughTextSource( + sourceString: String +) : TextSource { + override val asHtmlSource: String = sourceString.strikethroughHTML() + override val asMarkdownSource: String = sourceString.strikethroughMarkdown() +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt new file mode 100644 index 0000000000..f0d6ed4904 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown + +class UnderlineTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.underlineMarkdown() + override val asHtmlSource: String = sourceString.underlineHTML() +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index 1eb31850c4..7a1c6e7238 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -41,6 +41,19 @@ fun String.preHTML(): String = htmlDefault(htmlPreControl) fun String.emailMarkdown(): String = linkMarkdown("mailto://$this") fun String.emailHTML(): String = linkHTML("mailto://$this") +/** + * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("") +fun String.strikethroughHTML(): String = htmlDefault("s") + + +/** + * Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("") +fun String.underlineHTML(): String = htmlDefault("u") + private inline infix fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { this