1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt

121 lines
3.7 KiB
Kotlin
Raw Normal View History

package dev.inmo.tgbotapi.extensions.utils.formatting
2020-10-04 11:06:30 +00:00
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(
2020-11-06 12:52:59 +00:00
entities: TextSourcesList,
partLength: Int = textLength.last,
mode: ParseMode = MarkdownParseMode
): List<String> {
val texts = mutableListOf<String>()
val textBuilder = StringBuilder(partLength)
for (entity in entities) {
val string = when (mode) {
2020-11-16 07:32:20 +00:00
is MarkdownParseMode -> entity.markdown
is MarkdownV2ParseMode -> entity.markdownV2
is HTMLParseMode -> 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(
2020-11-06 12:52:59 +00:00
entities: TextSourcesList,
partLength: Int = textLength.last
): List<String> = createFormattedText(entities, partLength, MarkdownParseMode)
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownCaptions(): List<String> = createMarkdownText(
this,
captionLength.last
)
2021-01-09 12:25:11 +00:00
fun CaptionedInput.toMarkdownCaptions(): List<String> = textSources.toMarkdownCaptions()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownTexts(): List<String> = createMarkdownText(
this,
textLength.last
)
2021-01-09 12:25:11 +00:00
fun TextContent.toMarkdownTexts(): List<String> = textSources.toMarkdownTexts()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownExplanations(): List<String> = createMarkdownText(
this,
explanationLimit.last
)
2021-01-09 12:25:11 +00:00
fun ExplainedInput.toMarkdownExplanations(): List<String> = textSources.toMarkdownTexts()
fun createMarkdownV2Text(
2020-11-06 12:52:59 +00:00
entities: TextSourcesList,
partLength: Int = textLength.last
): List<String> = createFormattedText(entities, partLength, MarkdownV2ParseMode)
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownV2Captions(): List<String> = createMarkdownV2Text(
this,
captionLength.last
)
2021-01-09 12:25:11 +00:00
fun CaptionedInput.toMarkdownV2Captions(): List<String> = textSources.toMarkdownV2Captions()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownV2Texts(): List<String> = createMarkdownV2Text(
this,
textLength.last
)
2021-01-09 12:25:11 +00:00
fun TextContent.toMarkdownV2Texts(): List<String> = textSources.toMarkdownV2Texts()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toMarkdownV2Explanations(): List<String> = createMarkdownV2Text(
this,
explanationLimit.last
)
2021-01-09 12:25:11 +00:00
fun ExplainedInput.toMarkdownV2Explanations(): List<String> = textSources.toMarkdownV2Texts()
fun createHtmlText(
2020-11-06 12:52:59 +00:00
entities: TextSourcesList,
partLength: Int = textLength.last
): List<String> = createFormattedText(entities, partLength, HTMLParseMode)
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toHtmlCaptions(): List<String> = createHtmlText(
this,
captionLength.last
)
2021-01-09 12:25:11 +00:00
fun CaptionedInput.toHtmlCaptions(): List<String> = textSources.toHtmlCaptions()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toHtmlTexts(): List<String> = createHtmlText(
this,
textLength.last
)
2021-01-09 12:25:11 +00:00
fun TextContent.toHtmlTexts(): List<String> = textSources.toHtmlTexts()
2020-11-06 12:52:59 +00:00
fun TextSourcesList.toHtmlExplanations(): List<String> = createHtmlText(
this,
explanationLimit.last
)
2021-01-09 12:25:11 +00:00
fun ExplainedInput.toHtmlExplanations(): List<String> = textSources.toHtmlTexts()