package dev.inmo.tgbotapi.extensions.utils.formatting import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ParseMode.* import dev.inmo.tgbotapi.types.message.content.TextContent fun createFormattedText( entities: TextSourcesList, partLength: Int = textLength.last, mode: ParseMode = defaultParseMode ): List { val texts = mutableListOf() val textBuilder = StringBuilder(partLength) for (entity in entities) { val string = when (mode) { is Markdown -> entity.markdown is MarkdownV2 -> entity.markdownV2 is HTML -> entity.html } 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: TextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, Markdown) fun TextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, captionLength.last ) fun CaptionedInput.toMarkdownCaptions(): List = textSources.toMarkdownCaptions() fun TextSourcesList.toMarkdownTexts(): List = createMarkdownText( this, textLength.last ) fun TextContent.toMarkdownTexts(): List = textSources.toMarkdownTexts() fun TextSourcesList.toMarkdownExplanations(): List = createMarkdownText( this, explanationLimit.last ) fun ExplainedInput.toMarkdownExplanations(): List = textSources.toMarkdownTexts() fun createMarkdownV2Text( entities: TextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownV2) fun TextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, captionLength.last ) fun CaptionedInput.toMarkdownV2Captions(): List = textSources.toMarkdownV2Captions() fun TextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( this, textLength.last ) fun TextContent.toMarkdownV2Texts(): List = textSources.toMarkdownV2Texts() fun TextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( this, explanationLimit.last ) fun ExplainedInput.toMarkdownV2Explanations(): List = textSources.toMarkdownV2Texts() fun createHtmlText( entities: TextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, HTML) fun TextSourcesList.toHtmlCaptions(): List = createHtmlText( this, captionLength.last ) fun CaptionedInput.toHtmlCaptions(): List = textSources.toHtmlCaptions() fun TextSourcesList.toHtmlTexts(): List = createHtmlText( this, textLength.last ) fun TextContent.toHtmlTexts(): List = textSources.toHtmlTexts() fun TextSourcesList.toHtmlExplanations(): List = createHtmlText( this, explanationLimit.last ) fun ExplainedInput.toHtmlExplanations(): List = textSources.toHtmlTexts()