1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 11:38:45 +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` * `PhotoContent` now choose biggest photo size from its collection as `media`
* Fix in order of media group messages which was received by webhooks * 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` * Optimized preparing of media group in `UpdatesPoller`
* Add `CommonLimiter` * Add `CommonLimiter`
* Add `MessageEntity#asHtmlSource` and `String#toHtml`
## 0.11.0 ## 0.11.0

View File

@ -5,5 +5,6 @@ data class BoldTextMessageEntity(
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : 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 length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : 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 offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : MessageEntity { ) : LinkMessageEntity(
override val asMarkdownSource: String by lazy { sourceString,
"[$sourceString](mailto://$sourceString)" "mailto://$sourceString"
} )
}

View File

@ -5,5 +5,6 @@ data class ItalicTextMessageEntity(
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : 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 offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : MessageEntity { ) : MessageEntity
override val asMarkdownSource: String
get() = sourceString
}

View File

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

View File

@ -5,5 +5,6 @@ data class PreTextMessageEntity(
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : TextMessageEntity() { ) : 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 length: Int,
override val sourceString: String, override val sourceString: String,
val url: String val url: String
) : MessageEntity { ) : LinkMessageEntity(
override val asMarkdownSource: String by lazy { sourceString,
"[$sourceString]($url)" url
} )
}

View File

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

View File

@ -1,9 +1,14 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity
abstract class TextMessageEntity : 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 { 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 offset: Int,
override val length: Int, override val length: Int,
override val sourceString: String override val sourceString: String
) : MessageEntity { ) : LinkMessageEntity(
val url: String sourceString,
get() = sourceString sourceString
) {
override val asMarkdownSource: String by lazy { val url: String = link
"[$sourceString]($url)"
}
} }

View File

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