mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 00:03:48 +00:00
add tools for work with html captions and texts
This commit is contained in:
parent
609a474874
commit
4679dc118d
@ -54,6 +54,7 @@ media for present out)
|
||||
* Optimized preparing of media group in `UpdatesPoller`
|
||||
* Add `CommonLimiter`
|
||||
* Add `MessageEntity#asHtmlSource` and `String#toHtml`
|
||||
* Add tools for work with html captions and texts
|
||||
|
||||
## 0.11.0
|
||||
|
||||
|
@ -2,90 +2,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RegularTextMessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.textLength
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings"
|
||||
)
|
||||
fun createMarkdownText(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): String {
|
||||
return createMarkdownText(
|
||||
convertToFullMessageEntityList(text, messageEntities)
|
||||
).first()
|
||||
}
|
||||
|
||||
fun createMarkdownText(
|
||||
entities: List<MessageEntity>,
|
||||
partLength: Int = 4096
|
||||
): List<String> {
|
||||
val texts = mutableListOf<String>()
|
||||
val textBuilder = StringBuilder(partLength)
|
||||
for (entity in entities) {
|
||||
val string = entity.asMarkdownSource
|
||||
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
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings",
|
||||
ReplaceWith(
|
||||
"toMarkdownCaptions().firstOrNull()",
|
||||
"com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownCaptions"
|
||||
)
|
||||
)
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = toMarkdownCaptions().firstOrNull()
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaptions(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
captionLength.endInclusive + 1
|
||||
)
|
||||
|
||||
fun CaptionedMediaContent.fullEntitiesList(): List<MessageEntity> = caption ?.let {
|
||||
convertToFullMessageEntityList(it, captionEntities)
|
||||
} ?: emptyList()
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings",
|
||||
ReplaceWith(
|
||||
"toMarkdownTexts().first()",
|
||||
"com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownTexts"
|
||||
)
|
||||
)
|
||||
fun TextContent.toMarkdownText(): String = toMarkdownTexts().first()
|
||||
|
||||
fun TextContent.toMarkdownTexts(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
textLength.endInclusive + 1
|
||||
)
|
||||
|
||||
fun TextContent.fullEntitiesList(): List<MessageEntity> = convertToFullMessageEntityList(text, entities)
|
||||
|
||||
fun convertToFullMessageEntityList(
|
||||
@ -111,3 +35,42 @@ fun convertToFullMessageEntityList(
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun createFormattedText(
|
||||
entities: List<MessageEntity>,
|
||||
partLength: Int = 4096,
|
||||
mode: ParseMode = MarkdownParseMode
|
||||
): List<String> {
|
||||
val texts = mutableListOf<String>()
|
||||
val textBuilder = StringBuilder(partLength)
|
||||
for (entity in entities) {
|
||||
val string = when (mode) {
|
||||
is MarkdownParseMode -> entity.asMarkdownSource
|
||||
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
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.textLength
|
||||
|
||||
fun createHtmlText(
|
||||
entities: List<MessageEntity>,
|
||||
partLength: Int = 4096
|
||||
): List<String> = createFormattedText(entities, partLength, HTMLParseMode)
|
||||
|
||||
fun CaptionedMediaContent.toHtmlCaptions(): List<String> = createHtmlText(
|
||||
fullEntitiesList(),
|
||||
captionLength.endInclusive + 1
|
||||
)
|
||||
|
||||
fun TextContent.toHtmlTexts(): List<String> = createHtmlText(
|
||||
fullEntitiesList(),
|
||||
textLength.endInclusive + 1
|
||||
)
|
@ -0,0 +1,53 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownParseMode
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.textLength
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings"
|
||||
)
|
||||
fun createMarkdownText(
|
||||
text: String,
|
||||
messageEntities: List<MessageEntity>
|
||||
): String {
|
||||
return createMarkdownText(
|
||||
convertToFullMessageEntityList(text, messageEntities)
|
||||
).first()
|
||||
}
|
||||
|
||||
fun createMarkdownText(
|
||||
entities: List<MessageEntity>,
|
||||
partLength: Int = 4096
|
||||
): List<String> = createFormattedText(entities, partLength, MarkdownParseMode)
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings",
|
||||
ReplaceWith(
|
||||
"toMarkdownCaptions().firstOrNull()",
|
||||
"com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownCaptions"
|
||||
)
|
||||
)
|
||||
fun CaptionedMediaContent.toMarkdownCaption(): String? = toMarkdownCaptions().firstOrNull()
|
||||
|
||||
fun CaptionedMediaContent.toMarkdownCaptions(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
captionLength.endInclusive + 1
|
||||
)
|
||||
|
||||
@Deprecated(
|
||||
"Deprecated because old version have problem with long texts, but new one must return list of strings",
|
||||
ReplaceWith(
|
||||
"toMarkdownTexts().first()",
|
||||
"com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownTexts"
|
||||
)
|
||||
)
|
||||
fun TextContent.toMarkdownText(): String = toMarkdownTexts().first()
|
||||
|
||||
fun TextContent.toMarkdownTexts(): List<String> = createMarkdownText(
|
||||
fullEntitiesList(),
|
||||
textLength.endInclusive + 1
|
||||
)
|
Loading…
Reference in New Issue
Block a user