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` * 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>` * `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 * 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 ## 0.11.0

View File

@ -1,10 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
data class BoldTextMessageEntity( data class BoldTextMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : MessageEntity {
override val markdownFormatSymbol: String = "*" override val asMarkdownSource: String = sourceString.boldMarkdown()
override val htmlFormatTagname: String = "b" override val asHtmlSource: String = sourceString.boldHTML()
} }

View File

@ -1,10 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
data class BotCommandMessageEntity( data class BotCommandMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : MessageEntity { ) : MessageEntity {
override val asMarkdownSource: String = sourceString.commandMarkdown()
override val asHtmlSource: String = sourceString.commandHTML()
val command: String by lazy { val command: String by lazy {
sourceString.substring(1)// skip first symbol like "/" or "!" sourceString.substring(1)// skip first symbol like "/" or "!"
} }

View File

@ -1,10 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
data class CodeTextMessageEntity( data class CodeTextMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : MessageEntity {
override val markdownFormatSymbol: String = "`" override val asMarkdownSource: String = sourceString.codeMarkdown()
override val htmlFormatTagname: String = "code" override val asHtmlSource: String = sourceString.codeHTML()
} }

View File

@ -1,10 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
class EMailMessageEntity( class EMailMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : LinkMessageEntity( ) : MessageEntity {
sourceString, override val asMarkdownSource: String = sourceString.emailMarkdown()
"mailto://$sourceString" override val asHtmlSource: String = sourceString.emailHTML()
) }

View File

@ -1,7 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
data class HashTagMessageEntity( data class HashTagMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String 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 package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
data class ItalicTextMessageEntity( data class ItalicTextMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : MessageEntity {
override val markdownFormatSymbol: String = "_" override val asMarkdownSource: String = sourceString.italicMarkdown()
override val htmlFormatTagname: String = "i" 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 package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class MentionMessageEntity( class MentionMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String 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 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 { interface MessageEntity {
val offset: Int val offset: Int
val length: Int val length: Int
val sourceString: String val sourceString: String
val asMarkdownSource: String val asMarkdownSource: String
get() = sourceString.toMarkdown()
val asHtmlSource: String val asHtmlSource: String
get() = sourceString.toHtml()
} }

View File

@ -1,7 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
data class PhoneNumberMessageEntity( data class PhoneNumberMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String 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 package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
data class PreTextMessageEntity( data class PreTextMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : MessageEntity {
override val markdownFormatSymbol: String = "```" override val asMarkdownSource: String = sourceString.preMarkdown()
override val htmlFormatTagname: String = "pre" override val asHtmlSource: String = sourceString.preHTML()
} }

View File

@ -1,7 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity 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( data class RegularTextMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String 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 package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
data class TextLinkMessageEntity( data class TextLinkMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String, override val sourceString: String,
val url: String val url: String
) : LinkMessageEntity( ) : MessageEntity {
sourceString, override val asMarkdownSource: String = sourceString.linkMarkdown(url)
url override val asHtmlSource: String = sourceString.linkHTML(url)
) }

View File

@ -1,13 +1,14 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.types.User import com.github.insanusmokrassar.TelegramBotAPI.types.User
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
class TextMentionMessageEntity( class TextMentionMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String, override val sourceString: String,
val user: User val user: User
) : LinkMessageEntity( ) : MessageEntity {
sourceString, override val asMarkdownSource: String = sourceString.mentionMarkdown(user.id)
"tg://user?id=${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 package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
data class URLMessageEntity( data class URLMessageEntity(
override val offset: Int, override val offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : LinkMessageEntity( ) : MessageEntity{
sourceString, val url: String = sourceString
sourceString
) { override val asMarkdownSource: String = sourceString.linkMarkdown(url)
val url: String = link override val asHtmlSource: String = sourceString.linkHTML(url)
} }

View File

@ -78,6 +78,9 @@ fun String.command(): String = if (startsWith("/")) {
"/$this" "/$this"
} }
fun String.commandMarkdown(): String = command()
fun String.commandHTML(): String = command()
infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { infix fun String.bold(parseMode: ParseMode): String = when (parseMode) {
is HTML -> boldHTML() is HTML -> boldHTML()