task 7: restructure PlainTextParser to single-chapter model, update tests
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(UIKit)
|
||||
import UIKit
|
||||
#elseif canImport(AppKit)
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
public struct PlainTextParser {
|
||||
public static func parse(text: String, title: String, author: String? = nil) -> Book {
|
||||
let paragraphs = text.components(separatedBy: "\n\n")
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
|
||||
let chapters = paragraphs.enumerated().map { index, text in
|
||||
Chapter(index: index, title: "Section \(index + 1)", text: text)
|
||||
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty else {
|
||||
return Book(title: title, author: author, chapters: [])
|
||||
}
|
||||
|
||||
return Book(title: title, author: author, chapters: chapters)
|
||||
let attributedText = AttributedStringBuilder.buildPlain(text: trimmed)
|
||||
let chapter = Chapter(index: 0, title: title, text: trimmed, attributedText: attributedText)
|
||||
return Book(title: title, author: author, chapters: [chapter])
|
||||
}
|
||||
|
||||
public static func parse(url: URL) throws -> Book {
|
||||
|
||||
@@ -17,7 +17,7 @@ struct BookParserFacadeTests {
|
||||
defer { try? FileManager.default.removeItem(at: tmpFile) }
|
||||
|
||||
let book = try BookParser.parse(url: tmpFile)
|
||||
#expect(book.chapters.count == 2)
|
||||
#expect(book.chapters.count == 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,14 +4,6 @@ import Foundation
|
||||
|
||||
@Suite("PlainTextParser")
|
||||
struct PlainTextParserTests {
|
||||
@Test func parsesMultipleChapters() throws {
|
||||
let text = "First chapter content.\n\nSecond chapter content.\n\nThird chapter."
|
||||
let book = PlainTextParser.parse(text: text, title: "Test Book")
|
||||
#expect(book.chapters.count == 3)
|
||||
#expect(book.chapters[0].text == "First chapter content.")
|
||||
#expect(book.chapters[1].text == "Second chapter content.")
|
||||
}
|
||||
|
||||
@Test func parsesSingleParagraphAsOneChapter() {
|
||||
let text = "Just a single paragraph with no double newlines."
|
||||
let book = PlainTextParser.parse(text: text, title: "Test")
|
||||
@@ -19,6 +11,13 @@ struct PlainTextParserTests {
|
||||
#expect(book.chapters[0].text == text)
|
||||
}
|
||||
|
||||
@Test func multiParagraphTextBecomesOneChapter() {
|
||||
let text = "First paragraph.\n\nSecond paragraph.\n\nThird paragraph."
|
||||
let book = PlainTextParser.parse(text: text, title: "Test Book")
|
||||
#expect(book.chapters.count == 1)
|
||||
#expect(book.chapters[0].text == text)
|
||||
}
|
||||
|
||||
@Test func setsTitle() {
|
||||
let book = PlainTextParser.parse(text: "Hello", title: "My Book")
|
||||
#expect(book.title == "My Book")
|
||||
@@ -30,7 +29,13 @@ struct PlainTextParserTests {
|
||||
defer { try? FileManager.default.removeItem(at: tmpFile) }
|
||||
|
||||
let book = try PlainTextParser.parse(url: tmpFile)
|
||||
#expect(book.chapters.count == 2)
|
||||
#expect(book.chapters.count == 1)
|
||||
#expect(book.title == "test")
|
||||
}
|
||||
|
||||
@Test func attributedTextStringMatchesPlainText() {
|
||||
let text = "Hello world.\n\nSecond paragraph."
|
||||
let book = PlainTextParser.parse(text: text, title: "Test")
|
||||
#expect(book.chapters[0].attributedText.string == book.chapters[0].text)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user