All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.insanusmokrassar.TelegramBotAPI.utils.CaptionAndTextSourcesToText.kt Maven / Gradle / Ivy

package com.github.insanusmokrassar.TelegramBotAPI.utils

import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.*
import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList
import com.github.insanusmokrassar.TelegramBotAPI.types.textLength

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 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: List,
    partLength: Int = 4096
): List = createFormattedText(entities, partLength, MarkdownParseMode)

fun CaptionedInput.toMarkdownCaptions(): List = createMarkdownText(
    fullEntitiesList(),
    captionLength.last + 1
)

fun TextContent.toMarkdownTexts(): List = createMarkdownText(
    fullEntitiesList(),
    textLength.last + 1
)


fun createMarkdownV2Text(
    entities: List,
    partLength: Int = 4096
): List = createFormattedText(entities, partLength, MarkdownV2ParseMode)

fun CaptionedInput.toMarkdownV2Captions(): List = createMarkdownV2Text(
    fullEntitiesList(),
    captionLength.last + 1
)

fun TextContent.toMarkdownV2Texts(): List = createMarkdownV2Text(
    fullEntitiesList(),
    textLength.last + 1
)


fun createHtmlText(
    entities: List,
    partLength: Int = 4096
): List = createFormattedText(entities, partLength, HTMLParseMode)

fun CaptionedInput.toHtmlCaptions(): List = createHtmlText(
    fullEntitiesList(),
    captionLength.last + 1
)

fun TextContent.toHtmlTexts(): List = createHtmlText(
    fullEntitiesList(),
    textLength.last + 1
)






© 2015 - 2024 Weber Informatics LLC | Privacy Policy