feat: add support for foreword/preface pages (Closes #1)
Add ForewordConfig to BookConfig, Foreword model type, ForewordParser for text files (quote/paragraphs/signatures), ForewordPage in PageContent, pipeline integration to insert foreword after TOC, and PDF rendering with styled quote, horizontal rule separator, word-wrapped paragraphs, and right-aligned signatures. Also adds Gradle wrapper and adjusts build toolchain for JDK 25 compat. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit was merged in pull request #8.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package de.pfadfinder.songbook.parser
|
||||
|
||||
import de.pfadfinder.songbook.model.Foreword
|
||||
import java.io.File
|
||||
|
||||
object ForewordParser {
|
||||
|
||||
/**
|
||||
* Parses a foreword text file into a [Foreword] object.
|
||||
*
|
||||
* Format:
|
||||
* - Lines starting with `> ` are collected as the quote (multiple lines joined)
|
||||
* - `---` is a horizontal rule separator (marks end of quote section)
|
||||
* - Lines starting with `-- ` are signatures
|
||||
* - Other non-blank lines are body text; blank lines separate paragraphs
|
||||
*/
|
||||
fun parse(input: String): Foreword {
|
||||
val lines = input.lines()
|
||||
|
||||
val quoteLines = mutableListOf<String>()
|
||||
val signatures = mutableListOf<String>()
|
||||
val paragraphs = mutableListOf<String>()
|
||||
|
||||
var currentParagraph = StringBuilder()
|
||||
var inQuote = true // Start assuming we might be in the quote section
|
||||
var foundSeparator = false
|
||||
|
||||
for (rawLine in lines) {
|
||||
val line = rawLine.trimEnd()
|
||||
|
||||
// Quote lines (before separator)
|
||||
if (!foundSeparator && line.trimStart().startsWith("> ")) {
|
||||
quoteLines.add(line.trimStart().removePrefix("> "))
|
||||
continue
|
||||
}
|
||||
|
||||
// If we had quote lines but now see non-quote content before separator,
|
||||
// the quote section is done
|
||||
if (!foundSeparator && quoteLines.isNotEmpty() && line.trimStart().isNotEmpty() && !line.trimStart().startsWith("> ")) {
|
||||
if (line.trim() == "---") {
|
||||
foundSeparator = true
|
||||
inQuote = false
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Separator line
|
||||
if (line.trim() == "---") {
|
||||
foundSeparator = true
|
||||
inQuote = false
|
||||
continue
|
||||
}
|
||||
|
||||
// Signature lines
|
||||
if (line.trimStart().startsWith("-- ")) {
|
||||
signatures.add(line.trimStart().removePrefix("-- "))
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip quote processing after we established there are no quotes
|
||||
if (inQuote && quoteLines.isEmpty() && line.isBlank()) {
|
||||
continue
|
||||
}
|
||||
|
||||
inQuote = false
|
||||
|
||||
// Body paragraphs
|
||||
if (line.isBlank()) {
|
||||
if (currentParagraph.isNotEmpty()) {
|
||||
paragraphs.add(currentParagraph.toString().trim())
|
||||
currentParagraph = StringBuilder()
|
||||
}
|
||||
} else {
|
||||
if (currentParagraph.isNotEmpty()) {
|
||||
currentParagraph.append(" ")
|
||||
}
|
||||
currentParagraph.append(line.trim())
|
||||
}
|
||||
}
|
||||
|
||||
// Flush remaining paragraph
|
||||
if (currentParagraph.isNotEmpty()) {
|
||||
paragraphs.add(currentParagraph.toString().trim())
|
||||
}
|
||||
|
||||
val quote = if (quoteLines.isNotEmpty()) quoteLines.joinToString(" ") else null
|
||||
|
||||
return Foreword(
|
||||
quote = quote,
|
||||
paragraphs = paragraphs,
|
||||
signatures = signatures
|
||||
)
|
||||
}
|
||||
|
||||
fun parseFile(file: File): Foreword = parse(file.readText())
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.pfadfinder.songbook.parser
|
||||
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.nulls.shouldBeNull
|
||||
import io.kotest.matchers.nulls.shouldNotBeNull
|
||||
import io.kotest.matchers.shouldBe
|
||||
import kotlin.test.Test
|
||||
|
||||
@@ -167,6 +169,29 @@ class ConfigParserTest {
|
||||
config.layout.verseSpacing shouldBe 6f
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse config with foreword section`() {
|
||||
val yaml = """
|
||||
book:
|
||||
title: "Test"
|
||||
foreword:
|
||||
file: "./vorwort.txt"
|
||||
""".trimIndent()
|
||||
val config = ConfigParser.parse(yaml)
|
||||
config.foreword.shouldNotBeNull()
|
||||
config.foreword!!.file shouldBe "./vorwort.txt"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse config without foreword section has null foreword`() {
|
||||
val yaml = """
|
||||
book:
|
||||
title: "Test"
|
||||
""".trimIndent()
|
||||
val config = ConfigParser.parse(yaml)
|
||||
config.foreword.shouldBeNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse config ignores unknown properties`() {
|
||||
val yaml = """
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package de.pfadfinder.songbook.parser
|
||||
|
||||
import io.kotest.matchers.collections.shouldBeEmpty
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.nulls.shouldBeNull
|
||||
import io.kotest.matchers.nulls.shouldNotBeNull
|
||||
import io.kotest.matchers.shouldBe
|
||||
import kotlin.test.Test
|
||||
|
||||
class ForewordParserTest {
|
||||
|
||||
@Test
|
||||
fun `parse foreword with quote, paragraphs and signatures`() {
|
||||
val input = """
|
||||
> This is a quote line one
|
||||
> and quote line two
|
||||
---
|
||||
This is the first paragraph of the foreword body.
|
||||
It continues on the next line.
|
||||
|
||||
This is the second paragraph.
|
||||
|
||||
-- Max Mustermann
|
||||
-- Erika Mustermann
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.quote.shouldNotBeNull()
|
||||
foreword.quote shouldBe "This is a quote line one and quote line two"
|
||||
foreword.paragraphs shouldHaveSize 2
|
||||
foreword.paragraphs[0] shouldBe "This is the first paragraph of the foreword body. It continues on the next line."
|
||||
foreword.paragraphs[1] shouldBe "This is the second paragraph."
|
||||
foreword.signatures shouldHaveSize 2
|
||||
foreword.signatures[0] shouldBe "Max Mustermann"
|
||||
foreword.signatures[1] shouldBe "Erika Mustermann"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword without quote`() {
|
||||
val input = """
|
||||
This is just a paragraph.
|
||||
|
||||
And another one.
|
||||
|
||||
-- Author Name
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.quote.shouldBeNull()
|
||||
foreword.paragraphs shouldHaveSize 2
|
||||
foreword.paragraphs[0] shouldBe "This is just a paragraph."
|
||||
foreword.paragraphs[1] shouldBe "And another one."
|
||||
foreword.signatures shouldHaveSize 1
|
||||
foreword.signatures[0] shouldBe "Author Name"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword without signatures`() {
|
||||
val input = """
|
||||
> A beautiful quote
|
||||
---
|
||||
The foreword body text goes here.
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.quote shouldBe "A beautiful quote"
|
||||
foreword.paragraphs shouldHaveSize 1
|
||||
foreword.paragraphs[0] shouldBe "The foreword body text goes here."
|
||||
foreword.signatures.shouldBeEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse empty foreword`() {
|
||||
val foreword = ForewordParser.parse("")
|
||||
|
||||
foreword.quote.shouldBeNull()
|
||||
foreword.paragraphs.shouldBeEmpty()
|
||||
foreword.signatures.shouldBeEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword with only paragraphs`() {
|
||||
val input = """
|
||||
First paragraph.
|
||||
|
||||
Second paragraph.
|
||||
|
||||
Third paragraph.
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.quote.shouldBeNull()
|
||||
foreword.paragraphs shouldHaveSize 3
|
||||
foreword.paragraphs[0] shouldBe "First paragraph."
|
||||
foreword.paragraphs[1] shouldBe "Second paragraph."
|
||||
foreword.paragraphs[2] shouldBe "Third paragraph."
|
||||
foreword.signatures.shouldBeEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword with multi-line paragraph`() {
|
||||
val input = """
|
||||
This is a long paragraph that
|
||||
spans multiple lines and should
|
||||
be joined into a single paragraph.
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.paragraphs shouldHaveSize 1
|
||||
foreword.paragraphs[0] shouldBe "This is a long paragraph that spans multiple lines and should be joined into a single paragraph."
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword with only a quote`() {
|
||||
val input = """
|
||||
> Just a quote
|
||||
---
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.quote shouldBe "Just a quote"
|
||||
foreword.paragraphs.shouldBeEmpty()
|
||||
foreword.signatures.shouldBeEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse foreword with multiple signatures`() {
|
||||
val input = """
|
||||
Some text here.
|
||||
|
||||
-- Person One
|
||||
-- Person Two
|
||||
-- Person Three
|
||||
""".trimIndent()
|
||||
|
||||
val foreword = ForewordParser.parse(input)
|
||||
|
||||
foreword.paragraphs shouldHaveSize 1
|
||||
foreword.signatures shouldHaveSize 3
|
||||
foreword.signatures[0] shouldBe "Person One"
|
||||
foreword.signatures[1] shouldBe "Person Two"
|
||||
foreword.signatures[2] shouldBe "Person Three"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user