import Foundation /// The lorem text that `#lorem` puts in the page: a bookmark, an opening sentence of 16 words, two paragraphs and a contact sample. public struct LoremText: Equatable, Sendable { public static let trigger = "#lorem" /// The first line of the lorem text, so it shows in the bookmark index. public static let bookmark = "=lorem" public static let openingWords = 16 public static let paragraphSentences = 3...5 public static let resourceBundleName = "Plak_PlakCore.bundle" public var opening: String public var paragraphs: [[String]] public var contacts: ContactSample public init(opening: String, paragraphs: [[String]], contacts: ContactSample) { self.opening = opening self.paragraphs = paragraphs self.contacts = contacts } /// The bookmark, the sentence, the two paragraphs, the phones, the addresses and the e-mails, with a blank line between each. public var text: String { ([Self.bookmark, opening] + paragraphs.map { $0.joined(separator: " ") } + contacts.blocks).joined(separator: "\n\n") } public static func wordCount(_ sentence: String) -> Int { sentence.split(whereSeparator: \.isWhitespace).count } /// Picks random sentences, no sentence twice; nil when `sentences` has no line of 16 words or too few lines. public static func pick( from sentences: [String], names: ContactNames = .bundled, using generator: inout G ) -> LoremText? { let openings = sentences.indices.filter { wordCount(sentences[$0]) == openingWords } guard let opening = openings.randomElement(using: &generator) else { return nil } let counts = (0..<2).map { _ in Int.random(in: paragraphSentences, using: &generator) } let rest = sentences.indices.filter { $0 != opening }.shuffled(using: &generator) guard rest.count >= counts.reduce(0, +) else { return nil } let second = rest.dropFirst(counts[0]).prefix(counts[1]) let paragraphs = [rest.prefix(counts[0]), second].map { $0.map { sentences[$0] } } guard let contacts = ContactSample.pick(from: names, using: &generator) else { return nil } return LoremText(opening: sentences[opening], paragraphs: paragraphs, contacts: contacts) } public static func pick(from sentences: [String], names: ContactNames = .bundled) -> LoremText? { var generator = SystemRandomNumberGenerator() return pick(from: sentences, names: names, using: &generator) } /// The sentences of lorem.txt, one per line; empty when the file is missing. public static let sentences: [String] = { guard let url = fileURL(), let text = try? String(contentsOf: url, encoding: .utf8) else { return [] } return text.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty } }() /// A resource of PlakCore, found without `Bundle.module`: its generated accessor looks next to the .app /// (not in Contents/Resources) and falls back to an absolute build path. public static func fileURL(named name: String = "lorem.txt") -> URL? { let candidates = [ Bundle.main.resourceURL, Bundle.main.bundleURL, Bundle.main.executableURL?.deletingLastPathComponent(), Bundle(for: BundleToken.self).bundleURL.deletingLastPathComponent(), ] for base in candidates.compactMap({ $0 }) { let bundle = base.appendingPathComponent(resourceBundleName) for path in [name, "Contents/Resources/\(name)"] { let url = bundle.appendingPathComponent(path) if FileManager.default.fileExists(atPath: url.path) { return url } } } return nil } private final class BundleToken {} }