1
0
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:
InsanusMokrassar 2019-03-31 11:26:41 +08:00
parent 769d0360f3
commit c4a3e2cf3d
18 changed files with 88 additions and 67 deletions

View File

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

View File

@ -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()
}

View File

@ -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 "!"
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

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

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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()
}

View File

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

View File

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

View File

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

View File

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

View File

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