mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
refactor and preparing to multilevel message entities
This commit is contained in:
parent
e7495468a2
commit
482d924070
15
CHANGELOG.md
15
CHANGELOG.md
@ -2,6 +2,17 @@
|
|||||||
|
|
||||||
## 0.21.0 TelegramBotAPI 4.5
|
## 0.21.0 TelegramBotAPI 4.5
|
||||||
|
|
||||||
|
* Added support of strikethrough and underline
|
||||||
|
* Added `UnderlineTextSource` and `UnderlineMessageEntity`
|
||||||
|
* Added `StrikethroughTextSource` and `StrikethroughMessageEntity`
|
||||||
|
* Added support in `RawMessageEntity`
|
||||||
|
* Now `TextSource` have its `rawSource` const
|
||||||
|
* `sourceString` in `MessageEntity` now is deprecated
|
||||||
|
* All `MessageEntity` classes now have no income parameter `sourceString`
|
||||||
|
* In most part of `TextSource` classes `asMarkdownSource` and `asHtmlSource` now are getters instead if fields
|
||||||
|
* All parseMode-specific functions in `StringFormatting` now are not `infix`
|
||||||
|
* Removed constructor of `TextMentionMessageEntity`, which was deprecated previously
|
||||||
|
* Removed constructor of `TextMentionTextSource`, which was deprecated previously
|
||||||
* All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API
|
* All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API
|
||||||
* Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId`
|
* Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId`
|
||||||
* Now any administrator object instance have `customTitle` nullable field
|
* Now any administrator object instance have `customTitle` nullable field
|
||||||
@ -11,10 +22,6 @@ bot.
|
|||||||
|
|
||||||
## 0.20.0 MPP Migration
|
## 0.20.0 MPP Migration
|
||||||
|
|
||||||
* Added support of strikethrough and underline
|
|
||||||
* Added `UnderlineTextSource` and `UnderlineMessageEntity`
|
|
||||||
* Added `StrikethroughTextSource` and `StrikethroughMessageEntity`
|
|
||||||
* Added support in `RawMessageEntity`
|
|
||||||
* Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock`
|
* Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock`
|
||||||
* `Currencied` now using as `currency` value with type `String`
|
* `Currencied` now using as `currency` value with type `String`
|
||||||
* For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way
|
* For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts
|
package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts
|
||||||
|
|
||||||
interface TextSource {
|
interface TextSource {
|
||||||
|
val rawSource: String
|
||||||
val asMarkdownSource: String
|
val asMarkdownSource: String
|
||||||
val asHtmlSource: String
|
val asHtmlSource: String
|
||||||
}
|
}
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by BoldTextSource(sourceString)
|
) : MessageEntity, TextSource by BoldTextSource(rawSource)
|
||||||
|
@ -8,8 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown
|
|||||||
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 rawSource: String,
|
||||||
private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(sourceString)
|
private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(rawSource)
|
||||||
) : MessageEntity, TextSource by botCommandTextSource {
|
) : MessageEntity, TextSource by botCommandTextSource {
|
||||||
val command: String
|
val command: String
|
||||||
get() = botCommandTextSource.command
|
get() = botCommandTextSource.command
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by CodeTextSource(sourceString)
|
) : MessageEntity, TextSource by CodeTextSource(rawSource)
|
||||||
|
@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
|
|||||||
data class EMailMessageEntity(
|
data class EMailMessageEntity(
|
||||||
override val offset: Int,
|
override val offset: Int,
|
||||||
override val length: Int,
|
override val length: Int,
|
||||||
override val sourceString: String
|
override val rawSource: String
|
||||||
) : MessageEntity, TextSource by EMailTextSource(sourceString)
|
) : MessageEntity, TextSource by EMailTextSource(rawSource)
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by HashTagTextSource(sourceString)
|
) : MessageEntity, TextSource by HashTagTextSource(rawSource)
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by ItalicTextSource(sourceString)
|
) : MessageEntity, TextSource by ItalicTextSource(rawSource)
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by MentionTextSource(sourceString)
|
) : MessageEntity, TextSource by MentionTextSource(rawSource)
|
||||||
|
@ -5,5 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
interface MessageEntity : TextSource {
|
interface MessageEntity : TextSource {
|
||||||
val offset: Int
|
val offset: Int
|
||||||
val length: Int
|
val length: Int
|
||||||
|
@Deprecated("Due to opportunity to get the same string from rawSource const", ReplaceWith("rawSource"))
|
||||||
val sourceString: String
|
val sourceString: String
|
||||||
|
get() = rawSource
|
||||||
}
|
}
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by PhoneNumberTextSource(sourceString)
|
) : MessageEntity, TextSource by PhoneNumberTextSource(rawSource)
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by PreTextSource(sourceString)
|
) : MessageEntity, TextSource by PreTextSource(rawSource)
|
||||||
|
@ -29,7 +29,7 @@ internal data class RawMessageEntity(
|
|||||||
"code" -> CodeTextMessageEntity(offset, length, sourceSubstring)
|
"code" -> CodeTextMessageEntity(offset, length, sourceSubstring)
|
||||||
"pre" -> PreTextMessageEntity(offset, length, sourceSubstring)
|
"pre" -> PreTextMessageEntity(offset, length, sourceSubstring)
|
||||||
"text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link"))
|
"text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link"))
|
||||||
"text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user as PrivateChat)
|
"text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"))
|
||||||
"underline" -> UnderlineMessageEntity(offset, length, sourceSubstring)
|
"underline" -> UnderlineMessageEntity(offset, length, sourceSubstring)
|
||||||
"strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring)
|
"strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring)
|
||||||
else -> throw IllegalArgumentException("Unknown type of message entity")
|
else -> throw IllegalArgumentException("Unknown type of message entity")
|
||||||
|
@ -8,5 +8,5 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by RegularTextSource(sourceString)
|
) : MessageEntity, TextSource by RegularTextSource(rawSource)
|
||||||
|
@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource
|
|||||||
class StrikethroughMessageEntity(
|
class StrikethroughMessageEntity(
|
||||||
override val offset: Int,
|
override val offset: Int,
|
||||||
override val length: Int,
|
override val length: Int,
|
||||||
override val sourceString: String
|
override val rawSource: String
|
||||||
) : MessageEntity, TextSource by StrikethroughTextSource(sourceString)
|
) : MessageEntity, TextSource by StrikethroughTextSource(rawSource)
|
||||||
|
@ -8,6 +8,6 @@ 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 rawSource: String,
|
||||||
val url: String
|
val url: String
|
||||||
) : MessageEntity, TextSource by TextLinkTextSource(sourceString, url)
|
) : MessageEntity, TextSource by TextLinkTextSource(rawSource, url)
|
||||||
|
@ -10,14 +10,6 @@ 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 rawSource: String,
|
||||||
val privateChat: PrivateChat
|
val privateChat: PrivateChat
|
||||||
) : MessageEntity, TextSource by TextMentionTextSource(sourceString, privateChat) {
|
) : MessageEntity, TextSource by TextMentionTextSource(rawSource, privateChat)
|
||||||
@Deprecated("Deprecated due to the fact that there is more common constructor")
|
|
||||||
constructor(
|
|
||||||
offset: Int,
|
|
||||||
length: Int,
|
|
||||||
sourceString: String,
|
|
||||||
user: User
|
|
||||||
) : this(offset, length, sourceString, user as PrivateChat)
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,8 @@ 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 rawSource: String
|
||||||
) : MessageEntity, TextSource by URLTextSource(sourceString) {
|
) : MessageEntity, TextSource by URLTextSource(rawSource) {
|
||||||
val url: String = sourceString
|
val url: String
|
||||||
|
get() = rawSource
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource
|
|||||||
class UnderlineMessageEntity(
|
class UnderlineMessageEntity(
|
||||||
override val offset: Int,
|
override val offset: Int,
|
||||||
override val length: Int,
|
override val length: Int,
|
||||||
override val sourceString: String
|
override val rawSource: String
|
||||||
) : MessageEntity, TextSource by UnderlineTextSource(sourceString)
|
) : MessageEntity, TextSource by UnderlineTextSource(rawSource)
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown
|
||||||
|
|
||||||
class BoldTextSource(
|
class BoldTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.boldMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.boldHTML()
|
get() = rawSource.boldMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.boldHTML()
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,14 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown
|
|||||||
private val commandRegex = Regex("[/!][^@\\s]*")
|
private val commandRegex = Regex("[/!][^@\\s]*")
|
||||||
|
|
||||||
class BotCommandTextSource(
|
class BotCommandTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.commandMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.commandHTML()
|
get() = rawSource.commandMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.commandHTML()
|
||||||
|
|
||||||
val command: String by lazy {
|
val command: String by lazy {
|
||||||
commandRegex.find(sourceString) ?.value ?.substring(1) ?: sourceString.substring(1)// skip first symbol like "/" or "!"
|
commandRegex.find(rawSource) ?.value ?.substring(1) ?: rawSource.substring(1)// skip first symbol like "/" or "!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown
|
||||||
|
|
||||||
class CodeTextSource(
|
class CodeTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.codeMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.codeHTML()
|
get() = rawSource.codeMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.codeHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown
|
||||||
|
|
||||||
class EMailTextSource(
|
class EMailTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.emailMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.emailHTML()
|
get() = rawSource.emailMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.emailHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown
|
||||||
|
|
||||||
class HashTagTextSource(
|
class HashTagTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.hashTagMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.hashTagHTML()
|
get() = rawSource.hashTagMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.hashTagHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown
|
||||||
|
|
||||||
class ItalicTextSource(
|
class ItalicTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.italicMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.italicHTML()
|
get() = rawSource.italicMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.italicHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
||||||
|
|
||||||
class MentionTextSource(
|
class MentionTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.mentionMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.mentionHTML()
|
get() = rawSource.mentionMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.mentionHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown
|
||||||
|
|
||||||
class PhoneNumberTextSource(
|
class PhoneNumberTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.phoneMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.phoneHTML()
|
get() = rawSource.phoneMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.phoneHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown
|
||||||
|
|
||||||
class PreTextSource(
|
class PreTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.preMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.preHTML()
|
get() = rawSource.preMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.preHTML()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
||||||
|
|
||||||
class RegularTextSource(
|
class RegularTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.toMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.toHtml()
|
get() = rawSource.toMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.toHtml()
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown
|
||||||
|
|
||||||
class StrikethroughTextSource(
|
class StrikethroughTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asHtmlSource: String = sourceString.strikethroughHTML()
|
override val asHtmlSource: String
|
||||||
override val asMarkdownSource: String = sourceString.strikethroughMarkdown()
|
get() = rawSource.strikethroughHTML()
|
||||||
|
override val asMarkdownSource: String
|
||||||
|
get() = rawSource.strikethroughMarkdown()
|
||||||
}
|
}
|
@ -5,9 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
||||||
|
|
||||||
class TextLinkTextSource(
|
class TextLinkTextSource(
|
||||||
sourceString: String,
|
override val rawSource: String,
|
||||||
url: String
|
url: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.linkMarkdown(url)
|
override val asMarkdownSource: String = rawSource.linkMarkdown(url)
|
||||||
override val asHtmlSource: String = sourceString.linkHTML(url)
|
override val asHtmlSource: String = rawSource.linkHTML(url)
|
||||||
}
|
}
|
||||||
|
@ -7,15 +7,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown
|
||||||
|
|
||||||
class TextMentionTextSource(
|
class TextMentionTextSource(
|
||||||
sourceString: String,
|
override val rawSource: String,
|
||||||
privateChat: PrivateChat
|
privateChat: PrivateChat
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
@Deprecated("Deprecated due to the fact that there is more common constructor")
|
override val asMarkdownSource: String = rawSource.mentionMarkdown(privateChat.id)
|
||||||
constructor(
|
override val asHtmlSource: String = rawSource.mentionHTML(privateChat.id)
|
||||||
sourceString: String,
|
|
||||||
user: User
|
|
||||||
) : this(sourceString, user as PrivateChat)
|
|
||||||
|
|
||||||
override val asMarkdownSource: String = sourceString.mentionMarkdown(privateChat.id)
|
|
||||||
override val asHtmlSource: String = sourceString.mentionHTML(privateChat.id)
|
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown
|
||||||
|
|
||||||
class URLTextSource(
|
class URLTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource{
|
) : TextSource{
|
||||||
override val asMarkdownSource: String = sourceString.linkMarkdown(sourceString)
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.linkHTML(sourceString)
|
get() = rawSource.linkMarkdown(rawSource)
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.linkHTML(rawSource)
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown
|
||||||
|
|
||||||
class UnderlineTextSource(
|
class UnderlineTextSource(
|
||||||
sourceString: String
|
override val rawSource: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String = sourceString.underlineMarkdown()
|
override val asMarkdownSource: String
|
||||||
override val asHtmlSource: String = sourceString.underlineHTML()
|
get() = rawSource.underlineMarkdown()
|
||||||
|
override val asHtmlSource: String
|
||||||
|
get() = rawSource.underlineHTML()
|
||||||
}
|
}
|
@ -14,12 +14,14 @@ const val htmlBoldControl = "b"
|
|||||||
const val htmlItalicControl = "i"
|
const val htmlItalicControl = "i"
|
||||||
const val htmlCodeControl = "code"
|
const val htmlCodeControl = "code"
|
||||||
const val htmlPreControl = "pre"
|
const val htmlPreControl = "pre"
|
||||||
|
const val htmlUnderlineControl = "u"
|
||||||
|
const val htmlStrikethroughControl = "s"
|
||||||
|
|
||||||
private infix fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol"
|
private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol"
|
||||||
private infix fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this</$controlSymbol>"
|
private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this</$controlSymbol>"
|
||||||
|
|
||||||
infix fun String.linkMarkdown(link: String): String = "[$this]($link)"
|
fun String.linkMarkdown(link: String): String = "[$this]($link)"
|
||||||
infix fun String.linkHTML(link: String): String = "<a href=\"$link\">$this</a>"
|
fun String.linkHTML(link: String): String = "<a href=\"$link\">$this</a>"
|
||||||
|
|
||||||
|
|
||||||
fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl)
|
fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl)
|
||||||
@ -45,32 +47,32 @@ fun String.emailHTML(): String = linkHTML("mailto://$this")
|
|||||||
* Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct
|
* Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct
|
||||||
*/
|
*/
|
||||||
fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("")
|
fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("")
|
||||||
fun String.strikethroughHTML(): String = htmlDefault("s")
|
fun String.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct
|
* Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct
|
||||||
*/
|
*/
|
||||||
fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("")
|
fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("")
|
||||||
fun String.underlineHTML(): String = htmlDefault("u")
|
fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl)
|
||||||
|
|
||||||
|
|
||||||
private inline infix fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) {
|
private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) {
|
||||||
this
|
this
|
||||||
} else {
|
} else {
|
||||||
"@${adapt()}"
|
"@${adapt()}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private inline infix fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) {
|
private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) {
|
||||||
this
|
this
|
||||||
} else {
|
} else {
|
||||||
"#${adapt()}"
|
"#${adapt()}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
infix fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link)
|
fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link)
|
||||||
infix fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link)
|
fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link)
|
||||||
|
|
||||||
|
|
||||||
fun String.mentionMarkdown(): String = mention(String::toMarkdown)
|
fun String.mentionMarkdown(): String = mention(String::toMarkdown)
|
||||||
|
@ -7,6 +7,12 @@ fun String.toMarkdown(): String {
|
|||||||
).replace(
|
).replace(
|
||||||
"_",
|
"_",
|
||||||
"\\_"
|
"\\_"
|
||||||
|
).replace(
|
||||||
|
"`",
|
||||||
|
"\\`"
|
||||||
|
).replace(
|
||||||
|
"[",
|
||||||
|
"\\["
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user