diff --git a/CHANGELOG.md b/CHANGELOG.md index 62241d6218..3721a395d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,11 +59,13 @@ * New variant of `row` was added * `makeLinkToMessage` extensions has been deprecated (replaced into `TelegramBotAPI-extensions-utils`) * All `String` formatting public extensions and functions was deprecated and replaced into `TelegramBotAPI-extensions-utils` + * All extensions like `CaptionedInput#toHtmlCaptions` was deprecated and replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added * `makeLinkToAddStickerSet` function and its variations were added * All `String` formatting extensions and functions from `TelegramBotAPI` was added + * All extensions like `CaptionedInput#toHtmlCaptions` from `TelegramBotAPI` was added ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt new file mode 100644 index 0000000000..ed5d0ad118 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt @@ -0,0 +1,121 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList + +fun createFormattedText( + entities: FullTextSourcesList, + partLength: Int = textLength.last, + mode: ParseMode = MarkdownParseMode +): List { + val texts = mutableListOf() + val textBuilder = StringBuilder(partLength) + for (entity in entities) { + val string = when (mode) { + is MarkdownParseMode -> entity.asMarkdownSource + is MarkdownV2ParseMode -> entity.asMarkdownV2Source + 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 +} + + +fun createMarkdownText( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, MarkdownParseMode) + +fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( + this, + captionLength.last +) +fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions() + +fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText( + this, + textLength.last +) +fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() + +fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText( + this, + explanationLimit.last +) +fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts() + + +fun createMarkdownV2Text( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) + +fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( + this, + captionLength.last +) +fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions() + +fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( + this, + textLength.last +) +fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() + +fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( + this, + explanationLimit.last +) +fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts() + + +fun createHtmlText( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, HTMLParseMode) + +fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText( + this, + captionLength.last +) +fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions() + +fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( + this, + textLength.last +) +fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() + +fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText( + this, + explanationLimit.last +) +fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts() + + diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt index e30280f453..cc6003d826 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt @@ -6,6 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createFormattedText( entities: FullTextSourcesList, partLength: Int = textLength.last, @@ -47,75 +48,96 @@ fun createFormattedText( } +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createMarkdownText( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createMarkdownV2Text( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createHtmlText( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, HTMLParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts()