diff --git a/CHANGELOG.md b/CHANGELOG.md index d996a84e50..8fb7d16720 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt index 615784cfd0..0024ae2bdd 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt @@ -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 ): 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, + partLength: Int = 4096 +): List { + val texts = mutableListOf() + 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 = createMarkdownText( + fullEntitiesList(), + captionLength.endInclusive + 1 +) + fun CaptionedMediaContent.fullEntitiesList(): List = 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 = createMarkdownText( + fullEntitiesList(), + textLength.endInclusive + 1 ) fun TextContent.fullEntitiesList(): List = convertToFullMessageEntityList(text, entities)