-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('.') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/com/potomushto/statik/template/HandlebarsTemplateEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |