1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 20:18:45 +00:00

fixed problem that usually string formatting did not trigger escaping of control characters

This commit is contained in:
InsanusMokrassar 2020-01-03 00:04:49 +06:00
parent f68edebd24
commit dbef69ffac
4 changed files with 22 additions and 20 deletions

View File

@ -13,6 +13,7 @@
* All parseMode-specific functions in `StringFormatting` now are not `infix` * 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 * 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 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 `TextMentionMessageEntity`, which was deprecated previously
* Removed constructor of `TextMentionTextSource`, 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 * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API

View File

@ -1,14 +1,14 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml import com.github.insanusmokrassar.TelegramBotAPI.utils.regularHtml
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown import com.github.insanusmokrassar.TelegramBotAPI.utils.regularMarkdown
class RegularTextSource( class RegularTextSource(
override val rawSource: String override val rawSource: String
) : TextSource { ) : TextSource {
override val asMarkdownSource: String override val asMarkdownSource: String
get() = rawSource.toMarkdown() get() = rawSource.regularMarkdown()
override val asHtmlSource: String override val asHtmlSource: String
get() = rawSource.toHtml() get() = rawSource.regularHtml()
} }

View File

@ -17,11 +17,11 @@ const val htmlPreControl = "pre"
const val htmlUnderlineControl = "u" const val htmlUnderlineControl = "u"
const val htmlStrikethroughControl = "s" const val htmlStrikethroughControl = "s"
private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol" private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol${toMarkdown()}$controlSymbol"
private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this</$controlSymbol>" private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>${toHtml()}</$controlSymbol>"
fun String.linkMarkdown(link: String): String = "[$this]($link)" fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)"
fun String.linkHTML(link: String): String = "<a href=\"$link\">$this</a>" fun String.linkHTML(link: String): String = "<a href=\"$link\">${toHtml()}</a>"
fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl)
@ -40,8 +40,8 @@ fun String.preMarkdown(): String = markdownDefault(markdownPreControl)
fun String.preHTML(): String = htmlDefault(htmlPreControl) fun String.preHTML(): String = htmlDefault(htmlPreControl)
fun String.emailMarkdown(): String = linkMarkdown("mailto://$this") fun String.emailMarkdown(): String = linkMarkdown("mailto://$${toMarkdown()}")
fun String.emailHTML(): String = linkHTML("mailto://$this") 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 * 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("@")) { private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) {
this adapt()
} else { } else {
"@${adapt()}" "@${adapt()}"
} }
private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) {
this adapt()
} else { } else {
"#${adapt()}" "#${adapt()}"
} }
@ -87,14 +87,18 @@ fun String.phoneMarkdown(): String = toMarkdown()
fun String.phoneHTML(): String = toHtml() fun String.phoneHTML(): String = toHtml()
fun String.command(): String = if (startsWith("/")) { fun String.command(adapt: String.() -> String): String = if (startsWith("/")) {
this adapt()
} else { } else {
"/$this" "/${adapt()}"
} }
fun String.commandMarkdown(): String = command() fun String.commandMarkdown(): String = command(String::toMarkdown)
fun String.commandHTML(): String = command() 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) { infix fun String.bold(parseMode: ParseMode): String = when (parseMode) {

View File

@ -25,7 +25,4 @@ fun String.toHtml(): String = replace(
).replace( ).replace(
"&", "&",
"&amp;" "&amp;"
).replace(
"\"",
"&quot;"
) )