We like to refactor. We do a little refactoring
authorLanius Trolling <lanius@laniustrolling.dev>
Sat, 6 Apr 2024 12:29:03 +0000 (08:29 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Sat, 6 Apr 2024 12:29:03 +0000 (08:29 -0400)
src/jvmMain/kotlin/info/mechyrdia/Factbooks.kt
src/jvmMain/kotlin/info/mechyrdia/data/nations.kt
src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt
src/jvmMain/kotlin/info/mechyrdia/lore/html_utils.kt
src/jvmMain/kotlin/info/mechyrdia/lore/parser_html.kt
src/jvmMain/kotlin/info/mechyrdia/lore/preparser.kt
src/jvmMain/kotlin/info/mechyrdia/lore/view_tpl.kt
src/jvmMain/kotlin/info/mechyrdia/lore/views_quote.kt

index 3b5055d51b1e738518be0bd50b5315ae0a43e621..af9fcc4d580aa834ed268b71499fd4f007431202 100644 (file)
@@ -34,9 +34,6 @@ import org.slf4j.event.Level
 import java.io.IOException
 import java.util.concurrent.atomic.AtomicLong
 
-lateinit var application: Application
-       private set
-
 fun main() {
        System.setProperty("logback.statusListenerClass", "ch.qos.logback.core.status.NopStatusListener")
        
@@ -48,8 +45,6 @@ fun main() {
 }
 
 fun Application.factbooks() {
-       application = this
-       
        install(AutoHeadResponse)
        install(IgnoreTrailingSlash)
        
index 15c12fed7e9a7920d0341b8df4a13427f5ec30b5..5f1e74487e4f0732d987bf734b07b366e3735fdc 100644 (file)
@@ -2,7 +2,6 @@ package info.mechyrdia.data
 
 import com.github.agadar.nationstates.shard.NationShard
 import info.mechyrdia.OwnerNationId
-import info.mechyrdia.application
 import info.mechyrdia.auth.NSAPI
 import info.mechyrdia.auth.UserSession
 import info.mechyrdia.auth.executeSuspend
@@ -11,6 +10,8 @@ import io.ktor.server.sessions.*
 import io.ktor.util.*
 import kotlinx.serialization.SerialName
 import kotlinx.serialization.Serializable
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
 import java.util.concurrent.ConcurrentHashMap
 
 @Serializable
@@ -23,6 +24,8 @@ data class NationData(
        val isBanned: Boolean = false
 ) : DataDocument<NationData> {
        companion object : TableHolder<NationData> {
+               private val logger: Logger = LoggerFactory.getLogger(NationData::class.java)
+               
                override val Table = DocumentTable<NationData>()
                
                override suspend fun initialize() {
@@ -30,7 +33,7 @@ data class NationData(
                }
                
                fun unknown(id: Id<NationData>): NationData {
-                       application.log.warn("Unable to find nation with Id $id - did it CTE?")
+                       logger.warn("Unable to find nation with Id $id - did it CTE?")
                        return NationData(id, "Unknown Nation", "https://www.nationstates.net/images/flags/exnation.png")
                }
                
index 6fc9f79c0bcd6c7c5f1927f8c06e2361a3e7a1f7..9ddcf3e0320c22e094ca766a6783ac74b0853e1d 100644 (file)
@@ -4,12 +4,12 @@ import com.jaredrummler.fontreader.truetype.FontFileReader
 import com.jaredrummler.fontreader.truetype.TTFFile
 import com.jaredrummler.fontreader.util.GlyphSequence
 import info.mechyrdia.Configuration
-import info.mechyrdia.application
 import info.mechyrdia.route.KeyedEnumSerializer
 import info.mechyrdia.yieldThread
-import io.ktor.server.application.*
 import io.ktor.util.*
 import kotlinx.serialization.Serializable
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
 import java.awt.Font
 import java.awt.Shape
 import java.awt.geom.AffineTransform
@@ -46,6 +46,8 @@ enum class TextAlignment {
 object TextAlignmentSerializer : KeyedEnumSerializer<TextAlignment>(TextAlignment.entries)
 
 object MechyrdiaSansFont {
+       private val logger: Logger = LoggerFactory.getLogger(MechyrdiaSansFont::class.java)
+       
        fun renderTextToSvg(text: String, bold: Boolean, italic: Boolean, align: TextAlignment): String {
                val (file, font) = getFont(bold, italic)
                return layoutText(text, file, font, align).toSvgDocument(80.0 / file.unitsPerEm, 12.0)
@@ -197,7 +199,7 @@ object MechyrdiaSansFont {
                        
                        return shape
                } catch (ex: Exception) {
-                       application.log.error("Error converting text $text to font shape", ex)
+                       logger.error("Error converting text $text to font shape", ex)
                        return GeneralPath()
                } finally {
                        g2d.dispose()
index 5d4e59fdfc0fe57c0ef01399c6bacbc889cdbfa5..c8ba93a6e707cd8a79f0c3039da53ffc1bf346bb 100644 (file)
@@ -3,18 +3,6 @@ package info.mechyrdia.lore
 import kotlinx.html.*
 import java.time.Instant
 
-fun interface SECTIONS {
-       fun section(body: SECTION.() -> Unit)
-}
-
-fun MAIN.sectioned(): SECTIONS = MainSections(this)
-
-private class MainSections(private val delegate: MAIN) : SECTIONS {
-       override fun section(body: SECTION.() -> Unit) {
-               delegate.section(block = body)
-       }
-}
-
 fun FlowOrPhrasingContent.dateTime(instant: Instant) {
        span(classes = "moment") {
                style = "display:none"
index ecbb623521b9fffee9c1f2dfaa7f26018253aa20..46973f706a594a40917bd8c993fb1d9ec831c250 100644 (file)
@@ -368,8 +368,6 @@ fun processCommentLink(param: String?): Map<String, String> = processExternalLin
 
 fun processCommentImage(url: String, domain: String) = "https://$domain/${url.sanitizeExtImgLink()}"
 
-fun processAnchor(param: String?): Map<String, String> = param?.let { mapOf("id" to it, "name" to it) }.orEmpty()
-
 enum class FactbookFormattingTag(val type: HtmlLexerTag) {
        B(HtmlTagLexerTag(attributes = mapOf("style" to "font-weight:bold"), tagMode = HtmlTagMode.INLINE, tagCreator = TagConsumer<*>::span.toTagCreator())),
        I(HtmlTagLexerTag(attributes = mapOf("style" to "font-style:italic"), tagMode = HtmlTagMode.INLINE, tagCreator = TagConsumer<*>::span.toTagCreator())),
index e5cf35ab57eedf59b0c72e2a9cee77b0905c34ac..a3ce0a28e1a9dc871e7e0c9b5f6a50c9085d72a1 100644 (file)
@@ -1,12 +1,14 @@
 package info.mechyrdia.lore
 
-import info.mechyrdia.application
-import io.ktor.server.application.*
 import io.pebbletemplates.pebble.PebbleEngine
 import io.pebbletemplates.pebble.error.PebbleException
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
 import java.io.StringWriter
 
 object PreParser {
+       private val logger: Logger = LoggerFactory.getLogger(PreParser::class.java)
+       
        private val pebble = PebbleEngine.Builder()
                .addEscapingStrategy("none", PebbleNoEscaping)
                .defaultEscapingStrategy("none")
@@ -24,7 +26,7 @@ object PreParser {
                        template.evaluate(writer, context)
                        return writer.toString()
                } catch (ex: PebbleException) {
-                       application.log.error("Runtime error pre-parsing factbook $name", ex)
+                       logger.error("Runtime error pre-parsing factbook $name", ex)
                        "[h1]Error[/h1]\n\nThere was a runtime error pre-parsing this factbook: ${ex.message}"
                }
        }
index 746744c4f608c7411947fc9da7b83c517e4df754..bfa20dd980dac58fe188ba97c3193d59074f1ef0 100644 (file)
@@ -32,6 +32,24 @@ val preloadImages = listOf(
        "icon.png",
 )
 
+context(ApplicationCall)
+private fun HEAD.initialHead(pageTitle: String, ogData: OpenGraphData?) {
+       meta(charset = "utf-8")
+       meta(name = "viewport", content = "width=device-width, initial-scale=1.0")
+       
+       meta(name = "theme-color", content = "#FFCC33")
+       
+       ogData?.let { data ->
+               renderOgData(pageTitle, data)
+       }
+       
+       link(rel = "icon", type = "image/svg+xml", href = "/static/images/icon.png")
+       
+       title {
+               +pageTitle
+       }
+}
+
 fun ApplicationCall.page(pageTitle: String, navBar: List<NavItem>? = null, sidebar: Sidebar? = null, ogData: OpenGraphData? = null, content: MAIN.() -> Unit): HTML.() -> Unit {
        return {
                pageTheme.attributeValue?.let { attributes["data-theme"] = it }
@@ -39,14 +57,7 @@ fun ApplicationCall.page(pageTitle: String, navBar: List<NavItem>? = null, sideb
                lang = "en"
                
                head {
-                       meta(charset = "utf-8")
-                       meta(name = "viewport", content = "width=device-width, initial-scale=1.0")
-                       
-                       meta(name = "theme-color", content = "#FFCC33")
-                       
-                       ogData?.let { data ->
-                               renderOgData(pageTitle, data)
-                       }
+                       initialHead(pageTitle, ogData)
                        
                        for (font in preloadFonts)
                                link(
@@ -66,8 +77,6 @@ fun ApplicationCall.page(pageTitle: String, navBar: List<NavItem>? = null, sideb
                                        attributes["as"] = "image"
                                }
                        
-                       link(rel = "icon", type = "image/svg+xml", href = "/static/images/icon.png")
-                       
                        link(rel = "stylesheet", type = "text/css", href = "/static/style.css")
                        
                        request.queryParameters["redirect"]?.let { redirect ->
@@ -80,10 +89,6 @@ fun ApplicationCall.page(pageTitle: String, navBar: List<NavItem>? = null, sideb
                        }
                        
                        script(src = "/static/init.js") {}
-                       
-                       title {
-                               +pageTitle
-                       }
                }
                body {
                        div { id = "bg" }
@@ -168,22 +173,9 @@ fun ApplicationCall.rawPage(pageTitle: String, ogData: OpenGraphData? = null, co
                lang = "en"
                
                head {
-                       meta(charset = "utf-8")
-                       meta(name = "viewport", content = "width=device-width, initial-scale=1.0")
-                       
-                       meta(name = "theme-color", content = "#FFCC33")
-                       
-                       ogData?.let { data ->
-                               renderOgData(pageTitle, data)
-                       }
-                       
-                       link(rel = "icon", type = "image/svg+xml", href = "/static/images/icon.png")
+                       initialHead(pageTitle, ogData)
                        
                        link(rel = "stylesheet", type = "text/css", href = "/static/raw.css")
-                       
-                       title {
-                               +pageTitle
-                       }
                }
                body {
                        content()
index 652650076f683a3e1218f90e3f1bcf9267cc67d5..4b540edfcda10839e0cffe4495a5a9758773be42 100644 (file)
@@ -94,21 +94,26 @@ fun Quote.toJson(): String {
        }.toString()
 }
 
+context(Quote)
+private fun FlowContent.quoteWithAttribution() {
+       h1 { +title }
+       blockQuote {
+               +quote
+       }
+       p {
+               style = "align:right"
+               unsafe { raw("&#x2015;") }
+               +Entities.nbsp
+               a(href = fullLink) { +author }
+       }
+}
+
 context(ApplicationCall)
 suspend fun Quote.toHtml(title: String): HTML.() -> Unit {
        return page(title, standardNavBar(), QuoteOriginSidebar(author, fullPortrait, fullLink)) {
                section {
                        a { id = "page-top" }
-                       h1 { +title }
-                       blockQuote {
-                               +quote
-                       }
-                       p {
-                               style = "align:right"
-                               unsafe { raw("&#x2015;") }
-                               +Entities.nbsp
-                               a(href = fullLink) { +author }
-                       }
+                       quoteWithAttribution()
                }
        }
 }
@@ -116,16 +121,7 @@ suspend fun Quote.toHtml(title: String): HTML.() -> Unit {
 context(ApplicationCall)
 fun Quote.toRawHtml(title: String): HTML.() -> Unit {
        return rawPage(title) {
-               h1 { +title }
-               blockQuote {
-                       +quote
-               }
-               p {
-                       style = "align:right"
-                       unsafe { raw("&#x2015;") }
-                       +Entities.nbsp
-                       a(href = fullLink) { +author }
-               }
+               quoteWithAttribution()
                p {
                        style = "align:center"
                        a(href = fullLink) {