From 54453fc853ea2a5141140d595deb73ee29453f1e Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 7 Jul 2024 14:31:14 +0100 Subject: [PATCH] chore: Code cleanup --- backend/src/main/kotlin/Module.kt | 5 +- .../main/kotlin/meilisearch/MeiliClient.kt | 8 +- .../main/kotlin/meilisearch/MeiliPlugin.kt | 16 ---- .../kotlin/website/{Html.kt => HtmlExt.kt} | 0 .../src/main/kotlin/website/pages/NotFound.kt | 4 +- .../kotlin/website/pages/docs/DocsPage.kt | 80 ++++++++++--------- .../website/pages/docs/resource/Docs.kt | 6 -- docs/src/main/kotlin/Application.kt | 6 -- .../main/kotlin/markdown/FenceBlockParser.kt | 2 +- .../main/kotlin/markdown/MarkdownRenderer.kt | 8 +- .../markdown/highlight/CodeHighlight.kt | 36 --------- .../highlight/language/LanguageDefinition.kt | 2 +- .../highlight/language/highlighter/Comment.kt | 2 +- .../highlight/language/highlighter/String.kt | 2 +- 14 files changed, 56 insertions(+), 121 deletions(-) rename backend/src/main/kotlin/website/{Html.kt => HtmlExt.kt} (100%) delete mode 100644 backend/src/main/kotlin/website/pages/docs/resource/Docs.kt delete mode 100644 docs/src/main/kotlin/markdown/highlight/CodeHighlight.kt diff --git a/backend/src/main/kotlin/Module.kt b/backend/src/main/kotlin/Module.kt index f62fac2..4c84b8e 100644 --- a/backend/src/main/kotlin/Module.kt +++ b/backend/src/main/kotlin/Module.kt @@ -3,7 +3,6 @@ package dev.triumphteam.backend import dev.triumphteam.backend.api.apiRoutes import dev.triumphteam.backend.api.auth.TriumphPrincipal import dev.triumphteam.backend.meilisearch.Meili -import dev.triumphteam.backend.website.pages.respondNotFound import dev.triumphteam.backend.website.websiteRoutes import dev.triumphteam.website.JsonSerializer import io.ktor.http.CacheControl @@ -17,7 +16,6 @@ import io.ktor.server.application.Application import io.ktor.server.application.install import io.ktor.server.auth.Authentication import io.ktor.server.auth.bearer -import io.ktor.server.html.respondHtml import io.ktor.server.http.content.staticFiles import io.ktor.server.http.content.staticResources import io.ktor.server.plugins.cachingheaders.CachingHeaders @@ -30,7 +28,6 @@ import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders import io.ktor.server.plugins.statuspages.StatusPages import io.ktor.server.resources.Resources import io.ktor.server.response.respondRedirect -import io.ktor.server.response.respondText import io.ktor.server.routing.routing /** Module of the application. */ @@ -44,7 +41,7 @@ public fun Application.module() { } install(StatusPages) { - status(HttpStatusCode.NotFound) { call, status -> + status(HttpStatusCode.NotFound) { call, _ -> call.respondRedirect("/404") } } diff --git a/backend/src/main/kotlin/meilisearch/MeiliClient.kt b/backend/src/main/kotlin/meilisearch/MeiliClient.kt index 0454684..c5e2785 100644 --- a/backend/src/main/kotlin/meilisearch/MeiliClient.kt +++ b/backend/src/main/kotlin/meilisearch/MeiliClient.kt @@ -69,7 +69,7 @@ public class MeiliClient( ) { /** Create the index. Success even if it already exists. */ - public suspend fun create(): HttpResponse = client.post(Indexes()) { + private suspend fun create(): HttpResponse = client.post(Indexes()) { contentType(ContentType.Application.Json) setBody(Create(uid, primaryKey)) }.also { @@ -124,14 +124,14 @@ public class MeiliClient( public suspend fun transferTo(index: Index): HttpResponse { val createResponse = index.create() - // If there was an error creating the new index we return the response + // If there was an error creating the new index, we return the response if (!createResponse.status.isSuccess()) return createResponse val swapResponse = client.post(Indexes.Swap()) { setBody(listOf(Swap(listOf(uid, this@Index.uid)))) } - // If there was an error swapping the new indexes we return the response + // If there was an error swapping the new indexes, we return the response if (!swapResponse.status.isSuccess()) return swapResponse // Then we delete the current index @@ -166,7 +166,7 @@ public class MeiliClient( public class Swap(public val parent: Indexes = Indexes()) } - /** Serializable class for the create end point. */ + /** Serializable class for the "create" end point. */ @Serializable public data class Create( val uid: String, diff --git a/backend/src/main/kotlin/meilisearch/MeiliPlugin.kt b/backend/src/main/kotlin/meilisearch/MeiliPlugin.kt index 8cd6d1e..8d3842d 100644 --- a/backend/src/main/kotlin/meilisearch/MeiliPlugin.kt +++ b/backend/src/main/kotlin/meilisearch/MeiliPlugin.kt @@ -2,12 +2,8 @@ package dev.triumphteam.backend.meilisearch import io.ktor.http.URLProtocol import io.ktor.server.application.Application -import io.ktor.server.application.ApplicationCall import io.ktor.server.application.BaseApplicationPlugin -import io.ktor.server.application.application -import io.ktor.server.application.plugin import io.ktor.util.AttributeKey -import io.ktor.util.pipeline.PipelineContext public class Meili(config: Configuration) { @@ -31,15 +27,3 @@ public class Meili(config: Configuration) { } } } - -public suspend inline fun PipelineContext<*, ApplicationCall>.search( - index: String, - query: String, - limit: Int = 20, - filter: String? = null, -): List = with(this.application.plugin(Meili).client) { - return index(index).search(query, limit, filter) -} - -public suspend inline fun PipelineContext<*, ApplicationCall>.index(index: String): MeiliClient.Index = - application.plugin(Meili).client.index(index) diff --git a/backend/src/main/kotlin/website/Html.kt b/backend/src/main/kotlin/website/HtmlExt.kt similarity index 100% rename from backend/src/main/kotlin/website/Html.kt rename to backend/src/main/kotlin/website/HtmlExt.kt diff --git a/backend/src/main/kotlin/website/pages/NotFound.kt b/backend/src/main/kotlin/website/pages/NotFound.kt index c9c9bc2..398de70 100644 --- a/backend/src/main/kotlin/website/pages/NotFound.kt +++ b/backend/src/main/kotlin/website/pages/NotFound.kt @@ -1,10 +1,8 @@ package dev.triumphteam.backend.website.pages -import kotlinx.css.h1 import kotlinx.html.HTML import kotlinx.html.body import kotlinx.html.classes -import kotlinx.html.div import kotlinx.html.h1 import kotlinx.html.meta import kotlinx.html.title @@ -18,7 +16,7 @@ public fun HTML.respondNotFound(developmentMode: Boolean) { meta { name = "og:title" - content = "TriumphTeam" + content = "TriumphTeam | Not Found" } meta { diff --git a/backend/src/main/kotlin/website/pages/docs/DocsPage.kt b/backend/src/main/kotlin/website/pages/docs/DocsPage.kt index ca6b14e..720cea6 100644 --- a/backend/src/main/kotlin/website/pages/docs/DocsPage.kt +++ b/backend/src/main/kotlin/website/pages/docs/DocsPage.kt @@ -169,7 +169,7 @@ private fun HTML.renderFullPage( src = "https://unpkg.com/htmx.org@2.0.0" } - val title = "TrimphTeam | ${project.name} - ${currentPage.title}" + val title = "TriumphTeam | ${project.name} - ${currentPage.title}" // TODO: Replace with final URL, sucks that it can't be relative val image = "https://new.triumphteam.dev/assets/${project.id}/${version.reference}/${currentPage.id}/banner.png" @@ -228,43 +228,47 @@ private fun HTML.renderFullPage( title { +title } style { - +CssBuilder().apply { - rule(".project-color-bg") { - backgroundColor = Color(project.color) - } - - rule(".project-color") { - color = Color(project.color) - } - - rule(".project-color-hover") { - transitionDuration = 0.3.s - } - - rule(".project-color-border") { - transitionDuration = 0.3.s - } - - rule(".project-color-hover:hover") { - color = Color(project.color) - } - - rule(".project-color-border:hover") { - borderColor = Color(project.color) - } - - rule(".docs-content a") { - color = Color(project.color) - transitionDuration = 0.3.s - } - rule(".docs-content a:hover") { - color = Color("${project.color}C8") - } - - rule(".summary-active *") { - color = Color(project.color) - } - }.toString() + unsafe { + raw( + CssBuilder().apply { + rule(".project-color-bg") { + backgroundColor = Color(project.color) + } + + rule(".project-color") { + color = Color(project.color) + } + + rule(".project-color-hover") { + transitionDuration = 0.3.s + } + + rule(".project-color-border") { + transitionDuration = 0.3.s + } + + rule(".project-color-hover:hover") { + color = Color(project.color) + } + + rule(".project-color-border:hover") { + borderColor = Color(project.color) + } + + rule(".docs-content a") { + color = Color(project.color) + transitionDuration = 0.3.s + } + rule(".docs-content a:hover") { + color = Color("${project.color}C8") + } + + rule(".summary-active *") { + color = Color(project.color) + } + }.toString() + ) + } } } diff --git a/backend/src/main/kotlin/website/pages/docs/resource/Docs.kt b/backend/src/main/kotlin/website/pages/docs/resource/Docs.kt deleted file mode 100644 index f4dff2e..0000000 --- a/backend/src/main/kotlin/website/pages/docs/resource/Docs.kt +++ /dev/null @@ -1,6 +0,0 @@ -package dev.triumphteam.backend.website.pages.docs.resource - -import io.ktor.resources.Resource - -@Resource("/docs") -public data object Docs diff --git a/docs/src/main/kotlin/Application.kt b/docs/src/main/kotlin/Application.kt index dcd9c2a..57e955f 100644 --- a/docs/src/main/kotlin/Application.kt +++ b/docs/src/main/kotlin/Application.kt @@ -33,7 +33,6 @@ import io.ktor.http.Headers import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode import io.ktor.serialization.kotlinx.json.json -import io.ktor.util.InternalAPI import net.lingala.zip4j.ZipFile import org.apache.commons.cli.DefaultParser import org.apache.commons.cli.Option @@ -67,7 +66,6 @@ private val mdParser = Parser.builder() private val logger: Logger = LoggerFactory.getLogger("docs") -@OptIn(InternalAPI::class) public suspend fun main(args: Array) { val options = DefaultParser().parse( @@ -274,8 +272,4 @@ private data class Projects(val projects: List) { fun toRepository(): Repository { return Repository(projects.map(ProjectWithIcon::project)) } - - fun icons(): List { - return projects.map(ProjectWithIcon::icon) - } } diff --git a/docs/src/main/kotlin/markdown/FenceBlockParser.kt b/docs/src/main/kotlin/markdown/FenceBlockParser.kt index 94cb3e5..2ef4c64 100644 --- a/docs/src/main/kotlin/markdown/FenceBlockParser.kt +++ b/docs/src/main/kotlin/markdown/FenceBlockParser.kt @@ -21,7 +21,7 @@ public abstract class FenceBlockParser(private val skipCharacter: Char) : Abstra line[nextNonSpace] == skipCharacter && isClosing(line, nextNonSpace) ) { - // closing fence - we're at end of line, so we can finalize now + // closing "fence" - we're at the end of line, so we can finalize now return BlockContinue.finished() } diff --git a/docs/src/main/kotlin/markdown/MarkdownRenderer.kt b/docs/src/main/kotlin/markdown/MarkdownRenderer.kt index 1c5ebe0..a032986 100644 --- a/docs/src/main/kotlin/markdown/MarkdownRenderer.kt +++ b/docs/src/main/kotlin/markdown/MarkdownRenderer.kt @@ -204,7 +204,7 @@ public class MarkdownRenderer(private val context: HtmlNodeRendererContext) : Ab } override fun visit(orderedList: OrderedList) { - val start = orderedList.startNumber + val start = orderedList.markerStartNumber val attrs: MutableMap = LinkedHashMap() if (start != 1) { attrs["start"] = start.toString() @@ -309,9 +309,9 @@ public class MarkdownRenderer(private val context: HtmlNodeRendererContext) : Ab private fun isInTightList(paragraph: Paragraph): Boolean { val parent: Node? = paragraph.parent if (parent != null) { - val gramps = parent.parent - if (gramps is ListBlock) { - return gramps.isTight + val grandParent = parent.parent + if (grandParent is ListBlock) { + return grandParent.isTight } } return false diff --git a/docs/src/main/kotlin/markdown/highlight/CodeHighlight.kt b/docs/src/main/kotlin/markdown/highlight/CodeHighlight.kt deleted file mode 100644 index b226db7..0000000 --- a/docs/src/main/kotlin/markdown/highlight/CodeHighlight.kt +++ /dev/null @@ -1,36 +0,0 @@ -package dev.triumphteam.website.docs.markdown.highlight - -/* -import dev.snipme.highlights.internal.locator.AnnotationLocator -import dev.snipme.highlights.internal.locator.CommentLocator -import dev.snipme.highlights.internal.locator.KeywordLocator -import dev.snipme.highlights.internal.locator.MarkLocator -import dev.snipme.highlights.internal.locator.MultilineCommentLocator -import dev.snipme.highlights.internal.locator.NumericLiteralLocator -import dev.snipme.highlights.internal.locator.PunctuationLocator -import dev.snipme.highlights.internal.locator.StringLocator -import dev.snipme.highlights.internal.toRangeSet -import dev.snipme.highlights.model.CodeStructure - -private fun analyzeCodeWithKeywords(code: String, keywords: Set): CodeStructure { - val comments = CommentLocator.locate(code) - val multiLineComments = MultilineCommentLocator.locate(code) - val commentRanges = (comments + multiLineComments).toRangeSet() - - val strings = StringLocator.locate(code, commentRanges) - val plainTextRanges = (comments + multiLineComments + strings).toRangeSet() - - // TODO Apply ignored ranges to other locators - return CodeStructure( - marks = MarkLocator.locate(code), - punctuations = PunctuationLocator.locate(code), - keywords = KeywordLocator.locate(code, keywords, plainTextRanges), - strings = strings, - literals = NumericLiteralLocator.locate(code), - comments = comments, - multilineComments = multiLineComments, - annotations = AnnotationLocator.locate(code), - incremental = false, - ) -} -*/ diff --git a/docs/src/main/kotlin/markdown/highlight/language/LanguageDefinition.kt b/docs/src/main/kotlin/markdown/highlight/language/LanguageDefinition.kt index fc05a00..60e45e1 100644 --- a/docs/src/main/kotlin/markdown/highlight/language/LanguageDefinition.kt +++ b/docs/src/main/kotlin/markdown/highlight/language/LanguageDefinition.kt @@ -29,7 +29,7 @@ public abstract class LanguageDefinition( } } - public fun captureHighlights(code: String): Map> { + private fun captureHighlights(code: String): Map> { val highlights = components.flatMap { it.highlight(code) }.toMutableList() globalValidator.forEach { validator -> diff --git a/docs/src/main/kotlin/markdown/highlight/language/highlighter/Comment.kt b/docs/src/main/kotlin/markdown/highlight/language/highlighter/Comment.kt index ced6683..0989444 100644 --- a/docs/src/main/kotlin/markdown/highlight/language/highlighter/Comment.kt +++ b/docs/src/main/kotlin/markdown/highlight/language/highlighter/Comment.kt @@ -72,7 +72,7 @@ public class MultilineCommentHighlighter : LanguageHighlightComponent { if (char == ASTERISK) { asteriskIndex = index - // Previous was a slash, no it's asterisk, we found an "opener" + // Previous was a slash, no it's asterisk, we found an "opener". if (slashIndex + 1 == index) { openerIndex = slashIndex } diff --git a/docs/src/main/kotlin/markdown/highlight/language/highlighter/String.kt b/docs/src/main/kotlin/markdown/highlight/language/highlighter/String.kt index 2ada23e..562bdaf 100644 --- a/docs/src/main/kotlin/markdown/highlight/language/highlighter/String.kt +++ b/docs/src/main/kotlin/markdown/highlight/language/highlighter/String.kt @@ -55,7 +55,7 @@ public class StringHighlighter( return@forEachIndexed } - // If we find end of line reset since it's not allowed + // If we find the end of line reset since it's not allowed if (char.isNewLine()) { openerIndex = INVALID_INDEX }