refactor(posts): one frontmatter read/write codec
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
#180: one PostFrontmatter module now owns the lenient read (parse/split/decoded) and every write posture — fresh stamp, merge-import, update-keys — behind one known-key table (type, pass_state, origin; one insertion order). The three near-twin walkers are deleted byte-identically: PostStore.stamped/splitFrontmatter/ mergedFrontmatter, LearningDecay.stampingFrontmatter, Post.frontmatter(of:) — 21 call sites swept across seven production files and eight test files. The Unaided First Draft eligibility is named once and consumed by both gates: isUnaidedFirstDraftPair (writer ∧ post — the scan's read-back gate) and isCaptureEligible (pair ∧ First Draft — the capture gate at pass invocation). The lenient ?? default stamp decode is extracted with it (the third copy's deferred extraction). One deliberate divergence from the absorbed writer, doc-commented and pinned: mergingImport now preserves hand-edit lines above the opening --- on import where the old split-first shape silently dropped them — lossless through every posture is the ticket's contract. Seven codec pins (PostFrontmatterTests) cover the posture round-trip (foreign keys verbatim through import → save → advance → micropub), the lenient unknown-type decode, and the predicates' truth table. Existing pieces are untouched on disk: every posture moved verbatim and the full suite pins the byte shapes (664/664 green; the one under-load flake in the #179 repoint pin is pre-existing on main, proven by a stashed-tree control run). Closes #180
This commit is contained in:
1 parent
5bf09552f6
commit
218e2db397
22 files changed
+479
-221
No files matched your search
@@ -160,7 +160,7 @@ nonisolated enum LearningDecay {
|
||||
throw FixCardStore.CardError(message: "learning not found: \(file)")
|
||||
}
|
||||
|
||||
let frontmatter = Post.frontmatter(of: text)
|
||||
let frontmatter = PostFrontmatter.parse(text)
|
||||
let clock = passCounts(home: home)[typeDir] ?? 0
|
||||
if let remaining = passesHidden(frontmatter, currentPass: clock) {
|
||||
throw FixCardStore.CardError(message: "already dismissed — resurfaces in \(remaining) \(remaining == 1 ? "pass" : "passes")")
|
||||
@@ -168,8 +168,10 @@ nonisolated enum LearningDecay {
|
||||
// Clamped before +1 — an absurd hand-edited dismiss_count must not
|
||||
// trap the stamp.
|
||||
let count = min(dismissCount(in: frontmatter), valueCeiling)
|
||||
let stamped = stampingFrontmatter(
|
||||
of: text,
|
||||
// #180: the codec's update-keys posture (the mechanical walk this
|
||||
// file used to own); the Learning document shape is unchanged.
|
||||
let stamped = PostFrontmatter.updating(
|
||||
text: text,
|
||||
with: [
|
||||
("dismissed", FixCardStore.formatted(now, "yyyy-MM-dd")),
|
||||
("dismiss_count", "\(count + 1)"),
|
||||
@@ -178,48 +180,4 @@ nonisolated enum LearningDecay {
|
||||
)
|
||||
try stamped.write(to: url, atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
/// The surgical frontmatter rewrite (mergedFrontmatter's line-walk,
|
||||
/// generalized to ordered pairs): set-overwrite the keys in place,
|
||||
/// insert absent ones before the closing `---`, preserve every other
|
||||
/// line verbatim (leading hand-edit lines above the opening `---`
|
||||
/// included), keep the block's trailing newline. No frontmatter
|
||||
/// block at all → prepend a fresh one. Internal since #31: the
|
||||
/// first-draft stats capture is the third caller.
|
||||
// ponytail: PostStore.mergedFrontmatter's line-walk duplicated and
|
||||
// generalized — extraction would grow PostStore at its type-body
|
||||
// ceiling; #31's capture is the third caller and it STILL isn't
|
||||
// extracted (LearningDecay + PostStore stay the two owners; the
|
||||
// type-body ceilings named above are unchanged).
|
||||
static func stampingFrontmatter(of text: String, with pairs: [(key: String, value: String)]) -> String {
|
||||
let (block, body) = PostStore.splitFrontmatter(text)
|
||||
guard !block.isEmpty else {
|
||||
return "---\n" + pairs.map { "\($0.key): \($0.value)" }.joined(separator: "\n") + "\n---\n" + body
|
||||
}
|
||||
// Lines ABOVE the opening `---` (a hand-edit blank line —
|
||||
// Post.frontmatter tolerates one) fall in neither block nor
|
||||
// body; keep them verbatim so the rewrite loses nothing (#27 review).
|
||||
let prefix = text.range(of: block).map { String(text[..<$0.lowerBound]) } ?? ""
|
||||
var lines = block.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
|
||||
// Drop the empty tail the block's final newline produced — re-added below.
|
||||
if let last = lines.last, last.isEmpty {
|
||||
lines.removeLast()
|
||||
}
|
||||
let oursByKey = Dictionary(pairs.map { ($0.key, $0.value) }, uniquingKeysWith: { first, _ in first })
|
||||
var seen = Set<String>()
|
||||
for (index, line) in lines.enumerated() {
|
||||
let trimmed = line.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty, trimmed != "---" else { continue }
|
||||
let key = String(line.split(separator: ":", maxSplits: 1).first ?? "").trimmingCharacters(in: .whitespaces)
|
||||
if let value = oursByKey[key] {
|
||||
lines[index] = "\(key): \(value)"
|
||||
seen.insert(key)
|
||||
}
|
||||
}
|
||||
let insertions = pairs.filter { !seen.contains($0.key) }.map { "\($0.key): \($0.value)" }
|
||||
if let close = lines.lastIndex(where: { $0.trimmingCharacters(in: .whitespacesAndNewlines) == "---" }), close > 0 {
|
||||
lines.insert(contentsOf: insertions, at: close)
|
||||
}
|
||||
return prefix + lines.joined(separator: "\n") + "\n" + body
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user