From dbef69ffac2e8957ae021356037a5fce0b4a0370 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jan 2020 00:04:49 +0600 Subject: [PATCH] fixed problem that usually string formatting did not trigger escaping of control characters --- CHANGELOG.md | 1 + .../textsources/RegularTextSource.kt | 8 ++--- .../TelegramBotAPI/utils/StringFormatting.kt | 30 +++++++++++-------- .../TelegramBotAPI/utils/extensions/String.kt | 3 -- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43da309d6c..32eb165f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * All parseMode-specific functions in `StringFormatting` now are not `infix` * Now will not be thrown exception when there is income unknown type of `MessageEntity`. Instead of this will be created `RegularTextMessageEntity` with the same text +* Fixed problem that usually string formatting did not trigger escaping of control characters * Removed constructor of `TextMentionMessageEntity`, which was deprecated previously * Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt index e35c6bd53b..b8e236ea9d 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -1,14 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.regularHtml +import com.github.insanusmokrassar.TelegramBotAPI.utils.regularMarkdown class RegularTextSource( override val rawSource: String ) : TextSource { override val asMarkdownSource: String - get() = rawSource.toMarkdown() + get() = rawSource.regularMarkdown() override val asHtmlSource: String - get() = rawSource.toHtml() + get() = rawSource.regularHtml() } 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 d9c5266817..b9caf45f3f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -17,11 +17,11 @@ const val htmlPreControl = "pre" const val htmlUnderlineControl = "u" const val htmlStrikethroughControl = "s" -private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol" -private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this" +private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol${toMarkdown()}$controlSymbol" +private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>${toHtml()}" -fun String.linkMarkdown(link: String): String = "[$this]($link)" -fun String.linkHTML(link: String): String = "$this" +fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)" +fun String.linkHTML(link: String): String = "${toHtml()}" fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) @@ -40,8 +40,8 @@ fun String.preMarkdown(): String = markdownDefault(markdownPreControl) fun String.preHTML(): String = htmlDefault(htmlPreControl) -fun String.emailMarkdown(): String = linkMarkdown("mailto://$this") -fun String.emailHTML(): String = linkHTML("mailto://$this") +fun String.emailMarkdown(): String = linkMarkdown("mailto://$${toMarkdown()}") +fun String.emailHTML(): String = linkHTML("mailto://$${toHtml()}") /** * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct @@ -58,14 +58,14 @@ fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl) private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { - this + adapt() } else { "@${adapt()}" } private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { - this + adapt() } else { "#${adapt()}" } @@ -87,14 +87,18 @@ fun String.phoneMarkdown(): String = toMarkdown() fun String.phoneHTML(): String = toHtml() -fun String.command(): String = if (startsWith("/")) { - this +fun String.command(adapt: String.() -> String): String = if (startsWith("/")) { + adapt() } else { - "/$this" + "/${adapt()}" } -fun String.commandMarkdown(): String = command() -fun String.commandHTML(): String = command() +fun String.commandMarkdown(): String = command(String::toMarkdown) +fun String.commandHTML(): String = command(String::toHtml) + + +fun String.regularMarkdown(): String = toMarkdown() +fun String.regularHtml(): String = toHtml() infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index 040c6aa846..7d5457953c 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -25,7 +25,4 @@ fun String.toHtml(): String = replace( ).replace( "&", "&" -).replace( - "\"", - """ )