mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
new "createMarkdownText"
This commit is contained in:
parent
81029db165
commit
4c0472a139
@ -17,6 +17,8 @@
|
||||
* Add `RegularTextMessageEntity` which is useful for representing regular text message entity
|
||||
* Add `convertToFullMessageEntityList` which create list of entities with `RegularTextMessageEntity` on places where
|
||||
must be regular text
|
||||
* Change signature of `createMarkdownText`: now it will return list of strings
|
||||
* Deprecate old signatures of `createMarkdownText`, `toMarkdownCaption`, `toMarkdownText`
|
||||
|
||||
### 0.8.5
|
||||
|
||||
|
@ -2,31 +2,81 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RegularTextMessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.textLength
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings"
|
||||
)
|
||||
fun createMarkdownText(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): String {
|
||||
return convertToFullMessageEntityList(text, messageEntities).joinToString("", "", "") { it.asMarkdownSource }
|
||||
return createMarkdownText(
|
||||
convertToFullMessageEntityList(text, messageEntities)
|
||||
).first()
|
||||
}
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
|
||||
createMarkdownText(
|
||||
it,
|
||||
captionEntities
|
||||
)
|
||||
fun createMarkdownText(
|
||||
entities: List<MessageEntity>,
|
||||
partLength: Int = 4096
|
||||
): List<String> {
|
||||
val texts = mutableListOf<String>()
|
||||
val textBuilder = StringBuilder(partLength)
|
||||
for (entity in entities) {
|
||||
val string = entity.asMarkdownSource
|
||||
if (textBuilder.length + string.length > partLength) {
|
||||
if (textBuilder.isNotEmpty()) {
|
||||
texts.add(textBuilder.toString())
|
||||
textBuilder.clear()
|
||||
}
|
||||
val chunked = string.chunked(partLength)
|
||||
val last = chunked.last()
|
||||
textBuilder.append(last)
|
||||
val listToAdd = if (chunked.size > 1) {
|
||||
chunked.subList(0, chunked.size - 1)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
listToAdd.forEach {
|
||||
texts.add(it)
|
||||
}
|
||||
} else {
|
||||
textBuilder.append(string)
|
||||
}
|
||||
}
|
||||
if (textBuilder.isNotEmpty()) {
|
||||
texts.add(textBuilder.toString())
|
||||
textBuilder.clear()
|
||||
}
|
||||
return texts
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings"
|
||||
)
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = toMarkdownCaptions().firstOrNull()
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaptions(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
captionLength.endInclusive + 1
|
||||
)
|
||||
|
||||
fun CaptionedMediaContent.fullEntitiesList(): List<MessageEntity> = caption ?.let {
|
||||
convertToFullMessageEntityList(it, captionEntities)
|
||||
} ?: emptyList()
|
||||
|
||||
fun TextContent.toMarkdownText(): String = createMarkdownText(
|
||||
text,
|
||||
entities
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings"
|
||||
)
|
||||
fun TextContent.toMarkdownText(): String = toMarkdownTexts().first()
|
||||
|
||||
fun TextContent.toMarkdownTexts(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
textLength.endInclusive + 1
|
||||
)
|
||||
|
||||
fun TextContent.fullEntitiesList(): List<MessageEntity> = convertToFullMessageEntityList(text, entities)
|
||||
|
Loading…
Reference in New Issue
Block a user