Skip to content

Commit

Permalink
Add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmt committed Jan 2, 2025
1 parent 1bef936 commit 74fd296
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
44 changes: 44 additions & 0 deletions src/com/potomushto/statik/generators/FileWalker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.potomushto.statik.generators

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.extension
import kotlin.streams.asSequence

class FileWalker(private val rootPath: String) {

/**
* Walks through the blog files directory and returns a sequence of markdown files
*/
fun walkBlogFiles(): Sequence<Path> {
val postsPath = Paths.get(rootPath, "posts")
return Files.walk(postsPath)
.asSequence()
.filter { Files.isRegularFile(it) }
.filter { it.extension in setOf("md", "html") }
}

/**
* Walks through the static files directory and returns a sequence of files
*/
fun walkStaticFiles(assetsPath: String): Sequence<Path> {
val staticPath = Paths.get(assetsPath)
return Files.walk(staticPath)
.asSequence()
.filter { Files.isRegularFile(it) }
}

/**
* Generates a URL path for a blog post based on its file path
*/
fun generatePath(file: Path): String {
val postsPath = Paths.get(rootPath, "content", "posts")
val relativePath = postsPath.relativize(file)

// Remove the file extension and convert to URL path
return relativePath.toString()
.replace('\\', '/')
.substringBeforeLast('.')
}
}
15 changes: 8 additions & 7 deletions src/com/potomushto/statik/generators/SiteGenerator.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.potomushto.statik.generators

import HandlebarsTemplateEngine
import com.potomushto.statik.models.BlogPost
import kotlin.io.path.readText
import com.potomushto.statik.config.BlogConfig
import com.potomushto.statik.processors.MarkdownProcessor
import com.potomushto.statik.template.HandlebarsTemplateEngine
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
Expand All @@ -16,7 +16,7 @@ import kotlin.io.path.nameWithoutExtension
class SiteGenerator(private val rootPath: String,
private val config: BlogConfig) {
private val markdownProcessor = MarkdownProcessor()
private val templateEngine = HandlebarsTemplateEngine(rootPath)
private val templateEngine = HandlebarsTemplateEngine()
// private val rssGenerator = RssGenerator()
private val fileWalker = FileWalker(rootPath)

Expand All @@ -32,7 +32,6 @@ class SiteGenerator(private val rootPath: String,
private fun loadBlogPosts(): List<BlogPost> {
return fileWalker.walkBlogFiles()
.map { file ->

val postContent = file.readText()
val parsedPost = markdownProcessor.process(postContent)
val title = parsedPost.metadata["title"] ?: file.nameWithoutExtension
Expand Down Expand Up @@ -65,10 +64,11 @@ class SiteGenerator(private val rootPath: String,
}

private fun generateBlogPosts(posts: List<BlogPost>) {
val template = templateEngine.compile("post")
val templateFile = Paths.get(rootPath, config.theme.templates, "post.${templateEngine.extension}")
val template = templateEngine.compile(templateFile.readText())

posts.forEach { post ->
val html = template.apply(mapOf(
val html = template(mapOf(
"post" to post,
"baseUrl" to config.baseUrl
))
Expand All @@ -80,9 +80,10 @@ class SiteGenerator(private val rootPath: String,
}

private fun generateHomePage(posts: List<BlogPost>) {
val template = templateEngine.compile("home")
val templateFile = Paths.get(rootPath, config.theme.templates, "home.${templateEngine.extension}")
val template = templateEngine.compile(templateFile.readText())

val html = template.apply(mapOf(
val html = template(mapOf(
"posts" to posts,
"siteName" to config.siteName,
"baseUrl" to config.baseUrl
Expand Down
45 changes: 45 additions & 0 deletions src/com/potomushto/statik/template/HandlebarsTemplateEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import com.github.jknack.handlebars.Handlebars
import com.github.jknack.handlebars.Helper
import java.time.LocalDateTime

class HandlebarsTemplateEngine : TemplateEngine {
override val extension = "hbs"

private val handlebars: Handlebars = Handlebars()

init {
registerHelper("formatDate", object: Helper<LocalDateTime> {
override fun apply(context: LocalDateTime?, options: com.github.jknack.handlebars.Options?): Any? {
return context?.let {
val formatter = options?.hash?.get("format") as? String ?: "MMMM dd, yyyy"
val dateTimeFormatter = java.time.format.DateTimeFormatter.ofPattern(formatter)
context.format(dateTimeFormatter)
}
}
})
registerHelper("excerpt", object: Helper<String> {
override fun apply(context: String?, options: com.github.jknack.handlebars.Options?): Any? {
val words = options?.hash?.get("words") as? Int ?: 30
return context?.split(" ")?.take(words)?.joinToString(" ")?.plus("...")
}
})
}

override fun compile(template: String): (Map<String, Any?>) -> String {
val compiledTemplate = handlebars.compileInline(template)
return { data -> compiledTemplate.apply(data) }
}

override fun render(template: String, data: Map<String, Any?>): String {
val compiledTemplate = compile(template)
return compiledTemplate(data)
}

fun registerHelper(name: String, helper: Helper<*>) {
handlebars.registerHelper(name, helper)
}

override fun registerPartial(name: String, partial: String) {
TODO()
}
}
9 changes: 9 additions & 0 deletions src/com/potomushto/statik/template/TemplateEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import com.github.jknack.handlebars.Helper

interface TemplateEngine {
val extension: String

fun compile(template: String): (Map<String, Any?>) -> String
fun render(template: String, data: Map<String, Any?>): String
fun registerPartial(name: String, partial: String)
}

0 comments on commit 74fd296

Please sign in to comment.