2019-01-17 01:42:23 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
|
|
|
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.CaptionedMediaContent
|
2019-01-23 04:15:57 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown
|
2019-01-17 01:42:23 +00:00
|
|
|
|
|
|
|
fun createMarkdownText(
|
|
|
|
text: String,
|
|
|
|
messageEntities: List<MessageEntity>
|
|
|
|
): String {
|
|
|
|
val builder = StringBuilder()
|
|
|
|
var offset = 0
|
|
|
|
for (entity in messageEntities) {
|
|
|
|
builder.append(
|
2019-01-23 04:15:57 +00:00
|
|
|
text.substring(offset until entity.offset).toMarkdown()
|
2019-01-17 01:42:23 +00:00
|
|
|
)
|
|
|
|
builder.append(
|
|
|
|
entity.asMarkdownSource
|
|
|
|
)
|
|
|
|
offset += entity.length
|
|
|
|
}
|
|
|
|
builder.append(
|
2019-01-23 04:15:57 +00:00
|
|
|
text.substring(offset).toMarkdown()
|
2019-01-17 01:42:23 +00:00
|
|
|
)
|
|
|
|
return builder.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun CaptionedMediaContent.toMarkdownCaption(): String? = caption ?.let {
|
|
|
|
createMarkdownText(
|
|
|
|
it,
|
|
|
|
captionEntities
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun TextContent.toMarkdownText(): String = createMarkdownText(
|
|
|
|
text,
|
|
|
|
entities
|
|
|
|
)
|