// Keys a page section reads so that a page can ask the owner something and // say a thing by hand, each drawn once for any table that names it (Photos was // the first: an album's "Cotton put these together — keep the album?" with // Keep and Not this while it is open, "You said not this one." in red with // Bring it back once dismissed, its days and pictures as one handwritten line, // and a photo's review with its Keep and Delete): // // * `when` — the section is drawn only while the record holds a value; // * `note` with its `ink` — one sentence by hand at the section's head; // * `hand` — one handwritten line of some columns, or a sentence naming // them ("Suggested by {review_by} on {review_at}"); // * `answers` — the slip's answers (`SlipAnswers`), under the section; // * `actions` — typed verbs that are not one choice: clear some columns, or // delete the record after its question. // // The reading lives here, pure, so the words are settled by tests; what // draws a record only draws them. import CottonKit import CottonValues import Foundation /// The values a record must hold for a section to be drawn: one of `values` /// in `column`, or — `present` — anything at all (true) or nothing (false). public typealias SectionWhen = DefinitionSectionWhen extension DefinitionSectionWhen { public init(column: String, values: [String]? = nil, present: Bool? = nil) { self.init(_generated: (), column: column) self.values = values self.present = present } } /// The ink a hand line is drawn in. Red is a word the owner should not miss; /// never a tint that carries meaning on its own. public typealias SectionInk = DefinitionInk /// One handwritten line: some columns' words joined by the page's separator, /// those holding nothing left out — an entry may be words naming a column /// ("Away from {home_label}"), left out with it; or one sentence whose /// `{column}` tokens are those columns' words, drawn only when every token /// has words. public typealias PageHand = DefinitionPageHand extension DefinitionPageHand { public var keys: [String] { switch self { case .columns(let columns): return columns.flatMap { $0.contains("{") ? PageHand.tokens(in: $0) : [$0] } case .sentence(let sentence): return PageHand.tokens(in: sentence) } } static func tokens(in sentence: String) -> [String] { var out: [String] = [] var rest = Substring(sentence) while let open = rest.firstIndex(of: "{"), let close = rest[open...].firstIndex(of: "}") { let key = String(rest[rest.index(after: open).. Bool { holds(section.when, for: record) } /// Whether a `when` holds for the record as read; nil always holds. public func holds(_ when: SectionWhen?, for record: TableRecord) -> Bool { guard let when else { return true } let value = record[when.column] if let present = when.present, present == RecordValue.isBlank(value) { return false } if let values = when.values { guard let text = value.text else { return false } return values.contains(text) } return true } /// Words with their `{column}` tokens filled, or nil when one says nothing. private func filled(_ words: String, of record: TableRecord, links: [String: String], now: Date) -> String? { var line = words for key in PageHand.tokens(in: words) { guard let value = handWords(key, of: record, links: links, now: now) else { return nil } line = line.replacingOccurrences(of: "{\(key)}", with: value) } return line } /// A column's words in a hand line: a date as a hand writes a day ("12 /// September 2026", or its range with `until`), anything else as a page /// writes it. private func handWords(_ key: String, of record: TableRecord, links: [String: String], now: Date) -> String? { guard let column = column(key) else { return nil } if column.type == .date, column.until == nil, record[key].text != nil { return record.dayParts(key)?.dayLong } return words(key, of: record, links: links, now: now) } } // MARK: - Answers as drawn /// One answer as drawn: the value it writes, its words, and whether it is /// the agreeing one (`primary`, drawn filled). public struct AnswerVerb: Hashable, Sendable { public var value: String public var words: String public var primary: Bool } extension PageSection { /// The list the section draws. A page's `main` list is the page's own /// list, not a second one under it: it pages on as a list does, so a /// `limit` never caps it (a statement's every line, under it). public var drawnList: SectionList? { guard role == .main, var list else { return list } list.limit = nil return list } } extension SlipAnswers { /// Whether the record still waits on these answers: its column holds /// nothing, or one of `when` — the rule `isAnswered` reads, for answers a /// page section carries apart from the slip's. public func isOpen(_ record: TableRecord) -> Bool { let value = record[column] if RecordValue.isBlank(value) { return true } guard let text = value.text else { return false } return when.contains(text) } /// The verbs drawn for the record in `answerVerbs`' order — the agreeing /// one first, then the column's choices — none once it is answered. public func verbs(for record: TableRecord, in definition: TableDefinition) -> [AnswerVerb] { guard isOpen(record) else { return [] } var probe = definition probe.slip.answers = self return probe.answerVerbs(record).map { AnswerVerb(value: $0.value, words: $0.words, primary: $0.value == primary) } } } extension TableDefinition { /// The slip's answers for the record as drawn, the agreeing one marked; /// none when it asks nothing or is answered. public func drawnAnswers(_ record: TableRecord) -> [AnswerVerb] { slip.answers?.verbs(for: record, in: self) ?? [] } }