1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-29 13:38:43 +00:00

Actualized work with pre type of text - now it is possible to use language for formatting of text

This commit is contained in:
InsanusMokrassar 2020-01-03 00:27:24 +06:00
parent dbef69ffac
commit 398adf06ff
3 changed files with 62 additions and 4 deletions

View File

@ -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

View File

@ -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()}</$controlSymbol>"
private fun String.markdownDefault(
openControlSymbol: String,
closeControlSymbol: String = openControlSymbol
) = "$openControlSymbol${toMarkdown()}$closeControlSymbol"
private fun String.htmlDefault(
openControlSymbol: String,
closeControlSymbol: String = openControlSymbol
) = "<$openControlSymbol>${toHtml()}</$closeControlSymbol>"
fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)"
fun String.linkHTML(link: String): String = "<a href=\"$link\">${toHtml()}</a>"
@ -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></$htmlPreControl"
} ?: htmlPreControl
)
fun String.emailMarkdown(): String = linkMarkdown("mailto://$${toMarkdown()}")
@ -127,6 +143,11 @@ infix fun String.pre(parseMode: ParseMode): String = when (parseMode) {
is Markdown -> 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()

View File

@ -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(
"<pre><code class=\"language-$helloWorldLanguage\">$originalHelloWorld</code></pre>",
originalHelloWorld.preHTML(
helloWorldLanguage
)
)
assertEquals(
"<pre>$originalHelloWorld</pre>",
originalHelloWorld.preHTML()
)
assertEquals(
"```$helloWorldLanguage\n$originalHelloWorld\n```",
originalHelloWorld.preMarkdown(
helloWorldLanguage
)
)
assertEquals(
"```\n$originalHelloWorld\n```",
originalHelloWorld.preMarkdown()
)
}
}