Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ktor = "1.6.8"

kotlinx-serialization = "1.3.0"
jackson = "2.13.0"
kaml = "0.43.0"

tgbotapi = "0.35.9"

Expand Down Expand Up @@ -45,6 +46,7 @@ ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serializati
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" }
kaml = { group = "com.charleskorn.kaml", name = "kaml", version.ref = "kaml" }

tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" }
tgbotapi-extensions-api = { group = "dev.inmo", name = "tgbotapi.extensions.api", version.ref = "tgbotapi" }
Expand Down
11 changes: 11 additions & 0 deletions herald/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
application
}

application {
mainClass.set("by.jprof.telegram.bot.herald.AppKt")
}

dependencies {
implementation(libs.kotlinx.serialization.core)
implementation(libs.kaml)

testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.params)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(libs.log4j.core)
}
11 changes: 10 additions & 1 deletion herald/src/main/kotlin/by/jprof/telegram/bot/herald/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package by.jprof.telegram.bot.herald

import by.jprof.telegram.bot.herald.impl.post
import by.jprof.telegram.bot.herald.impl.postFile

suspend fun main(args: Array<String>) {
println("Hello, world!")
val postFile = postFile() ?: run { println("No post for today"); return }

println("Today's post: $postFile")

val post = post(postFile) ?: run { println("Cannot parse the file"); return }

println("Parsed the post: $post")
}
23 changes: 23 additions & 0 deletions herald/src/main/kotlin/by/jprof/telegram/bot/herald/impl/post.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package by.jprof.telegram.bot.herald.impl

import by.jprof.telegram.bot.herald.model.Post
import com.charleskorn.kaml.Yaml
import kotlinx.serialization.decodeFromString
import java.nio.file.Path
import kotlin.io.path.readText
import kotlin.text.RegexOption.DOT_MATCHES_ALL
import kotlin.text.RegexOption.MULTILINE

fun post(path: Path): Post? {
val text = path.readText()
val regex = Regex("-{3,}\\R(?<frontmatter>.*)\\R-{3,}\\s+(?<content>.*)", setOf(MULTILINE, DOT_MATCHES_ALL))

return regex.find(text)?.let {
val groups = it.groups as MatchNamedGroupCollection

val frontmatter = groups["frontmatter"]?.value ?: return null
val content = groups["content"]?.value ?: return null

Post(Yaml.default.decodeFromString(frontmatter), content)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package by.jprof.telegram.bot.herald.impl

import java.nio.file.Path
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import kotlin.io.path.Path
import kotlin.io.path.exists

fun postFile(): Path? {
val cwd = Path("")
val today = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd")

return cwd.resolve(today.format(formatter) + ".md").takeIf { it.exists() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package by.jprof.telegram.bot.herald.model

import kotlinx.serialization.Serializable

@Serializable
data class Frontmatter(
val chats: List<Long>,
val image: String? = null,
val disableWebPagePreview: Boolean = false,
val votes: List<String>? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package by.jprof.telegram.bot.herald.model

data class Post(
val frontmatter: Frontmatter,
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package by.jprof.telegram.bot.herald.impl

import by.jprof.telegram.bot.herald.model.Frontmatter
import by.jprof.telegram.bot.herald.model.Post
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Named
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.nio.file.Path
import java.util.stream.Stream

internal class PostTest {
@ParameterizedTest(name = "{0}")
@MethodSource
fun post(path: Path, post: Post) {
assertEquals(
post,
post(path)
)
}

companion object {
@JvmStatic
fun post() = Stream.of(
Arguments.of(
Named.of("001", "001".testResourcePath),
Post(
Frontmatter(chats = listOf(-1001146107319, -1001585354456)),
"This is content.\n"
)
),
Arguments.of(
Named.of("002", "002".testResourcePath),
Post(
Frontmatter(chats = listOf(-1001146107319, -1001585354456), image = "002.png"),
"This is a\nmultiline content!\n"
)
),
)

private val String.testResourcePath: Path
get() = Path.of(this@Companion::class.java.classLoader.getResource("posts/$this.md").toURI())
}
}
7 changes: 7 additions & 0 deletions herald/src/test/resources/posts/001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
chats:
- -1001146107319 # JPROF.BY
- -1001585354456 # JPROF.PL
---

This is content.
9 changes: 9 additions & 0 deletions herald/src/test/resources/posts/002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
chats:
- -1001146107319 # JPROF.BY
- -1001585354456 # JPROF.PL
image: 002.png
---

This is a
multiline content!