Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocTemplatesTests/TemplateSupport.swift3// Description: Helpers of the template tests: walking every paragraph of a document, collecting its texts and identifiers, parsing localized numbers.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel9@testable import DocTemplates1011let languages = ["en", "fr"]12let templateIDs = TemplateLibrary.all.map(\.id)13let nbspChar: Character = "\u{00A0}"1415/// 14 March 2025 at noon, local time.16let sampleDate: Date = {17 var comps = DateComponents()18 comps.year = 2025; comps.month = 3; comps.day = 14; comps.hour = 1219 return Calendar(identifier: .gregorian).date(from: comps)!20}()2122func sampleContext(_ language: String, paper: PaperSize = .letter) -> TemplateContext {23 TemplateContext(author: "Camille Tremblay-Okafor", organization: "Atelier Boréal Zeta inc.", date: sampleDate, language: language, paper: paper)24}2526func make(_ id: String, _ language: String, paper: PaperSize = .letter) -> DocumentModel {27 TemplateLibrary.template(id: id)!.make(sampleContext(language, paper: paper))28}2930extension DocumentModel {31 /// Page setups of every section, in order.32 var sections: [PageSetup] {33 [pageSetup] + body.compactMap { if case .sectionBreak(let s) = $0 { return s.setup } else { return nil } }34 }3536 var headerFooterParagraphs: [Paragraph] {37 sections.flatMap { s in s.header.default + s.header.first + s.header.even + s.footer.default + s.footer.first + s.footer.even }38 }3940 var bodyParagraphs: [Paragraph] { body.compactMap(\.paragraph) }41 var tables: [Table] { body.compactMap(\.table) }42 var tableParagraphs: [Paragraph] { tables.flatMap { $0.rows.flatMap { $0.cells.flatMap(\.paragraphs) } } }43 /// Note paragraphs in a stable order (the dictionary has none).44 var noteParagraphs: [Paragraph] { notes.values.flatMap(\.paragraphs).sorted { $0.plainText < $1.plainText } }4546 /// Every paragraph of the document: body, table cells, notes, headers and footers.47 var allParagraphs: [Paragraph] { bodyParagraphs + tableParagraphs + noteParagraphs + headerFooterParagraphs }4849 var allInlines: [Inline] { allParagraphs.flatMap { $0.runs.compactMap(\.inline) } }5051 var allFields: [Field] { allInlines.compactMap { if case .field(let f) = $0 { return f } else { return nil } } }52 var headerFooterFields: [Field] {53 headerFooterParagraphs.flatMap { $0.runs.compactMap(\.inline) }.compactMap { if case .field(let f) = $0 { return f } else { return nil } }54 }55 var shapes: [ShapeObject] { allInlines.compactMap { if case .shape(let s) = $0 { return s } else { return nil } } }56 var crossRefs: [CrossRef] { allInlines.compactMap { if case .crossRef(let c) = $0 { return c } else { return nil } } }57 var citations: [Citation] { allInlines.compactMap { if case .citation(let c) = $0 { return c } else { return nil } } }58 var noteRefs: [NoteRef] { allInlines.compactMap { if case .note(let n) = $0 { return n } else { return nil } } }59 var tocBlocks: [TOCBlock] { body.compactMap { if case .toc(let t) = $0 { return t } else { return nil } } }60 var bibliographyBlocks: [BibliographyBlock] { body.compactMap { if case .bibliography(let b) = $0 { return b } else { return nil } } }61 var formulaCells: [TableCell] { tables.flatMap { $0.rows.flatMap(\.cells) }.filter { $0.formula != nil } }6263 func outlineLevel(_ p: Paragraph) -> Int { p.props.outlineLevel ?? styles.styleProps(p.style).para.outlineLevel ?? 0 }64 var headings: [Paragraph] { bodyParagraphs.filter { outlineLevel($0) >= 1 } }6566 /// Every piece of text a reader can see, including generated titles, alternative texts, placeholder references and custom style names.67 var visibleStrings: [String] {68 let builtIn = Set(StyleSheet.standard().styles.map(\.id))69 // Inline objects stay visible as U+FFFC so the text around them keeps its spacing.70 var out = allParagraphs.map(\.plainText)71 out += tocBlocks.compactMap(\.title) + bibliographyBlocks.compactMap(\.title)72 out += shapes.flatMap { [$0.text, $0.altText] }73 out += bibliography.flatMap { e in e.fields.values.sorted() + e.authors.map(\.family) }74 out += [meta.title, meta.subject] + meta.keywords75 out += styles.styles.filter { !builtIn.contains($0.id) }.map(\.name)76 return out.filter { !$0.isEmpty }77 }7879 var fullText: String { visibleStrings.joined(separator: "\n") }8081 /// Every identifier that must be unique in the document.82 var allIDs: [String] {83 var ids = body.map(\.id)84 for t in tables { for r in t.rows { ids.append(r.id); for c in r.cells { ids.append(c.id); ids += c.paragraphs.map(\.id) } } }85 ids += notes.keys86 ids += noteParagraphs.map(\.id)87 ids += headerFooterParagraphs.map(\.id)88 return ids89 }90}9192/// Lower-cased words of a text.93func words(_ s: String) -> Set<String> {94 Set(s.lowercased().components(separatedBy: CharacterSet.letters.inverted).filter { !$0.isEmpty })95}9697/// Parses "1,234.50", "$1,234.50", "1 234,50 $", "10,3 %" … Returns nil for text that is not a number.98func parseNumber(_ s: String, french: Bool) -> Double? {99 var t = s.replacingOccurrences(of: "\u{00A0}", with: "").replacingOccurrences(of: " ", with: "")100 t = t.replacingOccurrences(of: "$", with: "").replacingOccurrences(of: "%", with: "")101 t = french ? t.replacingOccurrences(of: ",", with: ".") : t.replacingOccurrences(of: ",", with: "")102 return Double(t)103}104