commonMain.com.github.insanusmokrassar.TelegramBotAPI.utils.CaptionAndTextSourcesToText.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TelegramBotAPI-jvm Show documentation
Show all versions of TelegramBotAPI-jvm Show documentation
Library for Object-Oriented and type-safe work with Telegram Bot API
package com.github.insanusmokrassar.TelegramBotAPI.utils
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 = 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 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 = 4096
): List = createFormattedText(entities, partLength, MarkdownParseMode)
fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText(
this,
captionLength.last + 1
)
fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions()
fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText(
this,
textLength.last + 1
)
fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts()
fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText(
this,
explanationLimit.last + 1
)
fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts()
fun createMarkdownV2Text(
entities: FullTextSourcesList,
partLength: Int = 4096
): List = createFormattedText(entities, partLength, MarkdownV2ParseMode)
fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text(
this,
captionLength.last + 1
)
fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions()
fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text(
this,
textLength.last + 1
)
fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts()
fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text(
this,
explanationLimit.last + 1
)
fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts()
fun createHtmlText(
entities: FullTextSourcesList,
partLength: Int = 4096
): List = createFormattedText(entities, partLength, HTMLParseMode)
fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText(
this,
captionLength.last + 1
)
fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions()
fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText(
this,
textLength.last + 1
)
fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts()
fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText(
this,
explanationLimit.last + 1
)
fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts()