1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

MessageEntity#asHtmlSource

This commit is contained in:
InsanusMokrassar 2019-03-27 09:35:37 +08:00
parent 1bdcf3ae60
commit 609a474874
14 changed files with 67 additions and 33 deletions

View File

@ -49,10 +49,11 @@ media for present out)
* `PhotoContent` now choose biggest photo size from its collection as `media`
* Fix in order of media group messages which was received by webhooks
### 0.12.4 UpdatesPoller optimisations
### 0.12.4
* Optimized preparing of media group in `UpdatesPoller`
* Add `CommonLimiter`
* Add `MessageEntity#asHtmlSource` and `String#toHtml`
## 0.11.0

View File

@ -5,5 +5,6 @@ data class BoldTextMessageEntity(
override val length: Int,
override val sourceString: String
) : TextMessageEntity() {
override val formatSymbol: String = "*"
override val markdownFormatSymbol: String = "*"
override val htmlFormatTagname: String = "b"
}

View File

@ -5,5 +5,6 @@ data class CodeTextMessageEntity(
override val length: Int,
override val sourceString: String
) : TextMessageEntity() {
override val formatSymbol: String = "`"
override val markdownFormatSymbol: String = "`"
override val htmlFormatTagname: String = "code"
}

View File

@ -4,8 +4,7 @@ class EMailMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity {
override val asMarkdownSource: String by lazy {
"[$sourceString](mailto://$sourceString)"
}
}
) : LinkMessageEntity(
sourceString,
"mailto://$sourceString"
)

View File

@ -5,5 +5,6 @@ data class ItalicTextMessageEntity(
override val length: Int,
override val sourceString: String
) : TextMessageEntity() {
override val formatSymbol: String = "_"
override val markdownFormatSymbol: String = "_"
override val htmlFormatTagname: String = "i"
}

View File

@ -0,0 +1,14 @@
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

@ -4,7 +4,4 @@ class MentionMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity {
override val asMarkdownSource: String
get() = sourceString
}
) : MessageEntity

View File

@ -1,5 +1,6 @@
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 {
@ -9,4 +10,7 @@ interface MessageEntity {
val asMarkdownSource: String
get() = sourceString.toMarkdown()
val asHtmlSource: String
get() = sourceString.toHtml()
}

View File

@ -5,5 +5,6 @@ data class PreTextMessageEntity(
override val length: Int,
override val sourceString: String
) : TextMessageEntity() {
override val formatSymbol: String = "```"
override val markdownFormatSymbol: String = "```"
override val htmlFormatTagname: String = "pre"
}

View File

@ -5,8 +5,7 @@ data class TextLinkMessageEntity(
override val length: Int,
override val sourceString: String,
val url: String
) : MessageEntity {
override val asMarkdownSource: String by lazy {
"[$sourceString]($url)"
}
}
) : LinkMessageEntity(
sourceString,
url
)

View File

@ -7,8 +7,7 @@ class TextMentionMessageEntity(
override val length: Int,
override val sourceString: String,
val user: User
) : MessageEntity {
override val asMarkdownSource: String by lazy {
"[$sourceString](tg://user?id=${user.id})"
}
}
) : LinkMessageEntity(
sourceString,
"tg://user?id=${user.id})"
)

View File

@ -1,9 +1,14 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
abstract class TextMessageEntity : MessageEntity {
protected abstract val formatSymbol: String
protected abstract val markdownFormatSymbol: String
protected abstract val htmlFormatTagname: String
override val asMarkdownSource: String by lazy {
"$formatSymbol$sourceString$formatSymbol"
"$markdownFormatSymbol$sourceString$markdownFormatSymbol"
}
override val asHtmlSource: String by lazy {
"<$htmlFormatTagname>$sourceString</$htmlFormatTagname>"
}
}

View File

@ -4,11 +4,9 @@ data class URLMessageEntity(
override val offset: Int,
override val length: Int,
override val sourceString: String
) : MessageEntity {
val url: String
get() = sourceString
override val asMarkdownSource: String by lazy {
"[$sourceString]($url)"
}
) : LinkMessageEntity(
sourceString,
sourceString
) {
val url: String = link
}

View File

@ -9,3 +9,17 @@ fun String.toMarkdown(): String {
"\\_"
)
}
fun String.toHtml(): String = replace(
"<",
"&lt;"
).replace(
">",
"&gt;"
).replace(
"&",
"&amp;"
).replace(
"\"",
"&quot;"
)