Generate index.html from messages list, encode default values in json

This commit is contained in:
Mikołaj Pich 2021-10-03 16:12:44 +02:00
parent a9e90347f0
commit 2cece7fd52
7 changed files with 54 additions and 13 deletions

3
.gitignore vendored
View file

@ -72,4 +72,5 @@ gradle-app.setting
classes
.DS_Store
.idea/libraries-with-intellij-classes.xml
dist/v*.json
dist/
!dist/favicon.ico

View file

@ -16,5 +16,6 @@ group = "io.github.wulkanowy"
allprojects {
repositories {
mavenCentral()
maven { url "https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven" }
}
}

BIN
dist/favicon.ico vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

11
dist/index.html vendored
View file

@ -1,11 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Wulkanowy Messages</title>
</head>
<body>
<a href="v1.json">Open v1.json</a>
</body>
</html>

View file

@ -10,6 +10,7 @@ mainClassName = 'MainKt'
dependencies {
implementation project(":pojos")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0"
implementation "org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.2"
testImplementation platform("org.junit:junit-bom:5.8.1")
testImplementation "org.junit.jupiter:junit-jupiter"

View file

@ -0,0 +1,43 @@
import io.github.wulkanowy.messages.pojo.Message
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import java.time.LocalDate
fun generateHtml(messages: List<Message>): String = buildString {
appendLine("<!DOCTYPE html>")
appendHTML().html {
lang = "pl"
head {
meta { charset = "UTF-8" }
title("Wulkanowy Messages")
}
body {
h1 { +"Wulkanowy Messages" }
ul {
messages.forEach {
li {
b {
+"${it.id}. "
if (!it.destinationUrl.isNullOrBlank()) {
a(it.destinationUrl) {
+it.title
}
} else +it.title
}
br
span {
+it.content
}
}
}
}
footer {
+"© ${LocalDate.now().year} by Wulkanowy | "
+"wygenerowano ${LocalDate.now()} | "
a("v1.json") {
+"raw json"
}
}
}
}
}

View file

@ -2,10 +2,16 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
private val json = Json {
encodeDefaults = true
}
fun main() {
checkIdUniqueness(messages)
val json = Json.encodeToString(messages)
val json = json.encodeToString(messages)
val html = generateHtml(messages)
File("../dist/v1.json").writeText(json)
File("../dist/index.html").writeText(html)
}