From 398adf06ff0f312829f7f73ae3c251d8181a6fca Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jan 2020 00:27:24 +0600 Subject: [PATCH] Actualized work with pre type of text - now it is possible to use language for formatting of text --- CHANGELOG.md | 1 + .../TelegramBotAPI/utils/StringFormatting.kt | 29 ++++++++++++--- .../utils/StringFormattingTests.kt | 36 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 32eb165f59..ffd979eea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 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 +* Actualized work with `pre` type of text - now it is possible to use `language` for formatting of text * 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/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index b9caf45f3f..175fcf0569 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -17,8 +17,14 @@ const val htmlPreControl = "pre" const val htmlUnderlineControl = "u" const val htmlStrikethroughControl = "s" -private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol${toMarkdown()}$controlSymbol" -private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>${toHtml()}" +private fun String.markdownDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "$openControlSymbol${toMarkdown()}$closeControlSymbol" +private fun String.htmlDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "<$openControlSymbol>${toHtml()}" fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)" fun String.linkHTML(link: String): String = "${toHtml()}" @@ -36,8 +42,18 @@ fun String.codeMarkdown(): String = markdownDefault(markdownCodeControl) fun String.codeHTML(): String = htmlDefault(htmlCodeControl) -fun String.preMarkdown(): String = markdownDefault(markdownPreControl) -fun String.preHTML(): String = htmlDefault(htmlPreControl) +fun String.preMarkdown(language: String? = null): String = markdownDefault( + "$markdownPreControl${language ?.let { "$it\n" } ?: "\n"}", + "\n$markdownPreControl" +) +fun String.preHTML(language: String? = null): String = htmlDefault( + language ?.let { _ -> + "$htmlPreControl><$htmlCodeControl class=\"language-$language\"" + } ?: htmlPreControl, + language ?.let { _ -> + "$htmlCodeControl> preMarkdown() } +fun String.pre(parseMode: ParseMode, language: String? = null): String = when (parseMode) { + is HTML -> preHTML(language) + is Markdown -> preMarkdown(language) +} + infix fun String.email(parseMode: ParseMode): String = when (parseMode) { is HTML -> emailHTML() is Markdown -> emailMarkdown() diff --git a/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt new file mode 100644 index 0000000000..48a7db2ccf --- /dev/null +++ b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt @@ -0,0 +1,36 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlin.test.Test +import kotlin.test.assertEquals + +class StringFormattingTests { + @Test + fun testThatPreEscapingWorksCorrectly() { + val originalHelloWorld = """ + fun main() { + println("Hello world") + } + """.replace(" ", "") + val helloWorldLanguage = "kotlin" + assertEquals( + "
$originalHelloWorld
", + originalHelloWorld.preHTML( + helloWorldLanguage + ) + ) + assertEquals( + "
$originalHelloWorld
", + originalHelloWorld.preHTML() + ) + assertEquals( + "```$helloWorldLanguage\n$originalHelloWorld\n```", + originalHelloWorld.preMarkdown( + helloWorldLanguage + ) + ) + assertEquals( + "```\n$originalHelloWorld\n```", + originalHelloWorld.preMarkdown() + ) + } +}