mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-25 18:17:13 +00:00
MessageEntities are using string formatting extensions
This commit is contained in:
parent
769d0360f3
commit
c4a3e2cf3d
@ -60,6 +60,7 @@ of `]` in links titles
|
||||
* Added `Markdown` and `HTML` type aliases which actually means `MarkdownParseMode` and `HTMLParseMode`
|
||||
* `ChatId` now have extension `link` which will automatically create link like `tg://user?id=<chatId>`
|
||||
* Created a few of methods for all supported formats of text like bold, italic, links and others
|
||||
* Rewritten `MessageEntities` to use new formatting options
|
||||
|
||||
## 0.11.0
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
|
||||
|
||||
data class BoldTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : TextMessageEntity() {
|
||||
override val markdownFormatSymbol: String = "*"
|
||||
override val htmlFormatTagname: String = "b"
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.boldMarkdown()
|
||||
override val asHtmlSource: String = sourceString.boldHTML()
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
data class BotCommandMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.commandMarkdown()
|
||||
override val asHtmlSource: String = sourceString.commandHTML()
|
||||
|
||||
val command: String by lazy {
|
||||
sourceString.substring(1)// skip first symbol like "/" or "!"
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
|
||||
|
||||
data class CodeTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : TextMessageEntity() {
|
||||
override val markdownFormatSymbol: String = "`"
|
||||
override val htmlFormatTagname: String = "code"
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.codeMarkdown()
|
||||
override val asHtmlSource: String = sourceString.codeHTML()
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
|
||||
|
||||
class EMailMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : LinkMessageEntity(
|
||||
sourceString,
|
||||
"mailto://$sourceString"
|
||||
)
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.emailMarkdown()
|
||||
override val asHtmlSource: String = sourceString.emailHTML()
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
|
||||
|
||||
data class HashTagMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.hashTagMarkdown()
|
||||
override val asHtmlSource: String = sourceString.hashTagHTML()
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
|
||||
|
||||
data class ItalicTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : TextMessageEntity() {
|
||||
override val markdownFormatSymbol: String = "_"
|
||||
override val htmlFormatTagname: String = "i"
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.italicMarkdown()
|
||||
override val asHtmlSource: String = sourceString.italicHTML()
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
abstract class LinkMessageEntity(
|
||||
val linkTitle: String,
|
||||
val link: String
|
||||
) : MessageEntity {
|
||||
|
||||
override val asMarkdownSource: String by lazy {
|
||||
"[$linkTitle]($link)"
|
||||
}
|
||||
override val asHtmlSource: String by lazy {
|
||||
"<a href=\"$link\">$linkTitle</a>"
|
||||
}
|
||||
}
|
@ -1,7 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
||||
|
||||
class MentionMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.mentionMarkdown()
|
||||
override val asHtmlSource: String = sourceString.mentionHTML()
|
||||
}
|
||||
|
@ -1,16 +1,10 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
||||
|
||||
interface MessageEntity {
|
||||
val offset: Int
|
||||
val length: Int
|
||||
val sourceString: String
|
||||
|
||||
val asMarkdownSource: String
|
||||
get() = sourceString.toMarkdown()
|
||||
|
||||
val asHtmlSource: String
|
||||
get() = sourceString.toHtml()
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
|
||||
|
||||
data class PhoneNumberMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.phoneMarkdown()
|
||||
override val asHtmlSource: String = sourceString.phoneHTML()
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
|
||||
|
||||
data class PreTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : TextMessageEntity() {
|
||||
override val markdownFormatSymbol: String = "```"
|
||||
override val htmlFormatTagname: String = "pre"
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.preMarkdown()
|
||||
override val asHtmlSource: String = sourceString.preHTML()
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
||||
|
||||
data class RegularTextMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : MessageEntity
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.toMarkdown()
|
||||
override val asHtmlSource: String = sourceString.toHtml()
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
||||
|
||||
data class TextLinkMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String,
|
||||
val url: String
|
||||
) : LinkMessageEntity(
|
||||
sourceString,
|
||||
url
|
||||
)
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.linkMarkdown(url)
|
||||
override val asHtmlSource: String = sourceString.linkHTML(url)
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.User
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
||||
|
||||
class TextMentionMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String,
|
||||
val user: User
|
||||
) : LinkMessageEntity(
|
||||
sourceString,
|
||||
"tg://user?id=${user.id})"
|
||||
)
|
||||
) : MessageEntity {
|
||||
override val asMarkdownSource: String = sourceString.mentionMarkdown(user.id)
|
||||
override val asHtmlSource: String = sourceString.mentionMarkdown(user.id)
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
abstract class TextMessageEntity : MessageEntity {
|
||||
protected abstract val markdownFormatSymbol: String
|
||||
protected abstract val htmlFormatTagname: String
|
||||
|
||||
override val asMarkdownSource: String by lazy {
|
||||
"$markdownFormatSymbol$sourceString$markdownFormatSymbol"
|
||||
}
|
||||
|
||||
override val asHtmlSource: String by lazy {
|
||||
"<$htmlFormatTagname>$sourceString</$htmlFormatTagname>"
|
||||
}
|
||||
}
|
@ -1,12 +1,15 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
||||
|
||||
data class URLMessageEntity(
|
||||
override val offset: Int,
|
||||
override val length: Int,
|
||||
override val sourceString: String
|
||||
) : LinkMessageEntity(
|
||||
sourceString,
|
||||
sourceString
|
||||
) {
|
||||
val url: String = link
|
||||
) : MessageEntity{
|
||||
val url: String = sourceString
|
||||
|
||||
override val asMarkdownSource: String = sourceString.linkMarkdown(url)
|
||||
override val asHtmlSource: String = sourceString.linkHTML(url)
|
||||
}
|
||||
|
@ -78,6 +78,9 @@ fun String.command(): String = if (startsWith("/")) {
|
||||
"/$this"
|
||||
}
|
||||
|
||||
fun String.commandMarkdown(): String = command()
|
||||
fun String.commandHTML(): String = command()
|
||||
|
||||
|
||||
infix fun String.bold(parseMode: ParseMode): String = when (parseMode) {
|
||||
is HTML -> boldHTML()
|
||||
|
Loading…
Reference in New Issue
Block a user