diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1acf9f43..2bc405cef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ media for present out) * Optimized preparing of media group in `UpdatesPoller` * Add `CommonLimiter` * Add `MessageEntity#asHtmlSource` and `String#toHtml` +* Add tools for work with html captions and texts ## 0.11.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt similarity index 61% rename from src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt rename to src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt index 336496dcd2..7763f3ad6d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionSourcer.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt @@ -2,90 +2,14 @@ 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.ParseMode.* 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 - -@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 createMarkdownText( - convertToFullMessageEntityList(text, messageEntities) - ).first() -} - -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", - ReplaceWith( - "toMarkdownCaptions().firstOrNull()", - "com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownCaptions" - ) -) -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() -@Deprecated( - "Deprecated because old version have problem with long texts, but new one must return list of strings", - ReplaceWith( - "toMarkdownTexts().first()", - "com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownTexts" - ) -) -fun TextContent.toMarkdownText(): String = toMarkdownTexts().first() - -fun TextContent.toMarkdownTexts(): List = createMarkdownText( - fullEntitiesList(), - textLength.endInclusive + 1 -) - fun TextContent.fullEntitiesList(): List = convertToFullMessageEntityList(text, entities) fun convertToFullMessageEntityList( @@ -111,3 +35,42 @@ fun convertToFullMessageEntityList( } return result } + +fun createFormattedText( + entities: List, + partLength: Int = 4096, + mode: ParseMode = MarkdownParseMode +): List { + val texts = mutableListOf() + val textBuilder = StringBuilder(partLength) + for (entity in entities) { + val string = when (mode) { + is MarkdownParseMode -> entity.asMarkdownSource + is HTMLParseMode -> entity.asHtmlSource + } + 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 +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt new file mode 100644 index 0000000000..b75bb4b178 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt @@ -0,0 +1,23 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode +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 + +fun createHtmlText( + entities: List, + partLength: Int = 4096 +): List = createFormattedText(entities, partLength, HTMLParseMode) + +fun CaptionedMediaContent.toHtmlCaptions(): List = createHtmlText( + fullEntitiesList(), + captionLength.endInclusive + 1 +) + +fun TextContent.toHtmlTexts(): List = createHtmlText( + fullEntitiesList(), + textLength.endInclusive + 1 +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt new file mode 100644 index 0000000000..21d1b490c7 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt @@ -0,0 +1,53 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownParseMode +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 + +@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 createMarkdownText( + convertToFullMessageEntityList(text, messageEntities) + ).first() +} + +fun createMarkdownText( + entities: List, + partLength: Int = 4096 +): List = createFormattedText(entities, partLength, MarkdownParseMode) + +@Deprecated( + "Deprecated because old version have problem with long texts, but new one must return list of strings", + ReplaceWith( + "toMarkdownCaptions().firstOrNull()", + "com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownCaptions" + ) +) +fun CaptionedMediaContent.toMarkdownCaption(): String? = toMarkdownCaptions().firstOrNull() + +fun CaptionedMediaContent.toMarkdownCaptions(): List = createMarkdownText( + fullEntitiesList(), + captionLength.endInclusive + 1 +) + +@Deprecated( + "Deprecated because old version have problem with long texts, but new one must return list of strings", + ReplaceWith( + "toMarkdownTexts().first()", + "com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownTexts" + ) +) +fun TextContent.toMarkdownText(): String = toMarkdownTexts().first() + +fun TextContent.toMarkdownTexts(): List = createMarkdownText( + fullEntitiesList(), + textLength.endInclusive + 1 +)