feat(microblog): set and create categories
A piece's `category:` frontmatter line now travels to Micro.blog: a first send carries the list beside title and content, and a re-send replaces the post's categories with exactly the piece's list — only when the piece has the line (opt-in by key; `[]` clears; no line leaves the blog's). Naming a category Micro.blog doesn't know creates it there, so there is no separate create step. The publish sheet gains a Categories field: a comma list with the system completion menu over the blog's own categories. The system replaces the whole field text with a completion, so the completion is the merge — a pick adds, a half-typed last word is replaced. The edit lands on the piece at the send (a failed write refuses with nothing on the wire); Publish with a pending edit applies it first and flips the post that send touched. The one-line codec lives in PostFrontmatter (line-bounded; backslashes and quotes escaped; a Set de-duplicates). Import keeps an imported `category:` line — the writer's classification, unlike the micropub bookkeeping keys. Reviewed twice (two-axis during the build, finish-audit after): the stale pending baseline after a failed step, the backslash round-trip, and the publish targeting the caller's possibly-stale URL were found and fixed before this commit. 39 of the 40 new or changed pins were watched fail on their own guard. Rail 791/791; SwiftFormat and SwiftLint clean. Docs: AGENTS.md bullets and learnings, the GLOSSARY term, CHANGELOG [Unreleased], PRD S6.1, the devlog, and the swift wiki (text completion is a whole-text contract). Closes #207
This commit is contained in:
1 parent
6aa31a419a
commit
6db4cf8cf1
18 files changed
+1629
-67
No files matched your search
@@ -12,6 +12,10 @@
|
||||
// on disk never notice the migration. Learning/Fix Card DOCUMENTS stay out
|
||||
// of scope (#180); their dismiss stamp rides the generic update posture
|
||||
// without redesigning its shape.
|
||||
// #207: the one-line `category:` list — read (`categories(in:)`) and
|
||||
// render (`categoryValue(_:)`) over the unbracketed pair the publish
|
||||
// sheet's field also speaks (`categoryList`/`categoryListValue`): ONE
|
||||
// parser serves the piece, the sheet, and the wire.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@@ -123,6 +127,126 @@ nonisolated enum PostFrontmatter {
|
||||
return stamp.origin == .writer && stamp.type == .post && stamp.passState == .firstDraft
|
||||
}
|
||||
|
||||
// MARK: - Category list (#207)
|
||||
|
||||
/// #207: the quote-aware comma-list parse the `category:` line and
|
||||
/// the publish sheet's category FIELD share — ONE parser (the
|
||||
/// field holds exactly the bracket-inner text). Splits on the
|
||||
/// commas OUTSIDE double quotes, trims each item, strips ONE pair
|
||||
/// of surrounding double quotes and unescapes `\\` and `\"`;
|
||||
/// empties drop and the first occurrence of a name wins. Creation
|
||||
/// is implicit on Micro.blog: naming a category that doesn't exist
|
||||
/// creates it when the post is sent.
|
||||
static func categoryList(_ inner: String) -> [String] {
|
||||
var names: [String] = []
|
||||
var seen: Set<String> = []
|
||||
for item in categorySegments(inner) {
|
||||
let trimmed = item.trimmingCharacters(in: .whitespaces)
|
||||
var name = trimmed
|
||||
// A quoted item carries its whitespace and its escapes
|
||||
// (`\\` and `\"`) verbatim; a bare item is taken as written.
|
||||
if trimmed.hasPrefix("\""), trimmed.hasSuffix("\""), trimmed.count >= 2 {
|
||||
name = categoryUnescaped(String(trimmed.dropFirst().dropLast()))
|
||||
}
|
||||
guard !name.isEmpty, seen.insert(name).inserted else { continue }
|
||||
names.append(name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
/// `categoryListValue`'s escape, undone: `\\` is a backslash, `\"` a
|
||||
/// quote. Any other escaped character keeps its backslash (a
|
||||
/// hand-typed `\n` stays the two characters it is), and a trailing
|
||||
/// lone backslash stays.
|
||||
private static func categoryUnescaped(_ text: String) -> String {
|
||||
var out = ""
|
||||
var escaped = false
|
||||
for char in text {
|
||||
if escaped {
|
||||
if char != "\\", char != "\"" {
|
||||
out.append("\\")
|
||||
}
|
||||
out.append(char)
|
||||
escaped = false
|
||||
} else if char == "\\" {
|
||||
escaped = true
|
||||
} else {
|
||||
out.append(char)
|
||||
}
|
||||
}
|
||||
if escaped {
|
||||
out.append("\\")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/// The unbracketed render `categoryList` reads back — a name
|
||||
/// holding `,` `[` `]` `"` or `\` rides double-quoted with its
|
||||
/// backslashes and quotes escaped. Newlines and control characters
|
||||
/// strip from every name first (the frontmatter sink is
|
||||
/// line-bounded) and names left empty drop.
|
||||
static func categoryListValue(_ names: [String]) -> String {
|
||||
names.compactMap { name -> String? in
|
||||
let cleaned = String(name.trimmingCharacters(in: .whitespaces).unicodeScalars.filter {
|
||||
!CharacterSet.newlines.contains($0) && !CharacterSet.controlCharacters.contains($0)
|
||||
})
|
||||
guard !cleaned.isEmpty else { return nil }
|
||||
guard cleaned.contains(where: { ",[]\"\\".contains($0) }) else { return cleaned }
|
||||
let slashed = cleaned.replacingOccurrences(of: "\\", with: "\\\\")
|
||||
return "\"" + slashed.replacingOccurrences(of: "\"", with: "\\\"") + "\""
|
||||
}.joined(separator: ", ")
|
||||
}
|
||||
|
||||
/// The list items, split on the commas outside double quotes (an
|
||||
/// escaped quote inside a quoted item never toggles the quote
|
||||
/// state — `["say \"hi\", Notes]` is two items).
|
||||
/// The ONE segment scanner behind `categoryList` and the publish
|
||||
/// sheet's `partial` — the raw (untrimmed, still-quoted) items.
|
||||
static func categorySegments(_ text: String) -> [String] {
|
||||
var items: [String] = []
|
||||
var current = ""
|
||||
var quoted = false
|
||||
var escaped = false
|
||||
for char in text {
|
||||
if escaped {
|
||||
current.append(char)
|
||||
escaped = false
|
||||
} else if char == "\\" {
|
||||
current.append(char)
|
||||
escaped = true
|
||||
} else if char == "\"" {
|
||||
current.append(char)
|
||||
quoted.toggle()
|
||||
} else if char == ",", !quoted {
|
||||
items.append(current)
|
||||
current = ""
|
||||
} else {
|
||||
current.append(char)
|
||||
}
|
||||
}
|
||||
items.append(current)
|
||||
return items
|
||||
}
|
||||
|
||||
/// #207: the piece's categories from its `category:` line — ONE
|
||||
/// line by construction (the read is line-bounded, #33), so a
|
||||
/// value can never carry a newline. Shapes: `[Tech, Notes]` (the
|
||||
/// bracketed list, read through `categoryList`), a bare scalar
|
||||
/// (`Essays` — YAML's plain string) = ONE category, and empty /
|
||||
/// absent = none.
|
||||
static func categories(in matter: [String: String]) -> [String] {
|
||||
guard let raw = matter["category"]?.trimmingCharacters(in: .whitespaces), !raw.isEmpty else { return [] }
|
||||
let listShaped = raw.hasPrefix("[") && raw.hasSuffix("]") && raw.count >= 2
|
||||
return listShaped ? categoryList(String(raw.dropFirst().dropLast())) : [raw]
|
||||
}
|
||||
|
||||
/// #207: the one-line `category:` value `categories(in:)` reads
|
||||
/// back — the bracketed `categoryListValue`. `[]` is the empty
|
||||
/// list, the value a re-send's clear carries.
|
||||
static func categoryValue(_ names: [String]) -> String {
|
||||
"[" + categoryListValue(names) + "]"
|
||||
}
|
||||
|
||||
// MARK: - Write postures
|
||||
|
||||
/// The known-key table: Museion's three owned keys in stamp order —
|
||||
|
||||
Reference in new issue
Block a user