import Foundation /// The page file as lines, with the created date of each block; reads the text and writes it back unchanged. public struct PageFile: Sendable, Equatable { public enum Line: Sendable, Equatable { case text(String) /// An image line; the path is relative to the page file folder, as `images/`. case image(String) /// The line as it stands in the page file. public var text: String { switch self { case let .text(text): text case let .image(path): PageFile.imageLine(path) } } public var isBlank: Bool { if case let .text(text) = self { return PageFile.isBlank(text) } return false } } public var lines: [Line] /// Created dates by the index of the first line of a block; a date on any other line is not written. public var dates: [Int: Date] public init(lines: [Line] = [], dates: [Int: Date] = [:]) { self.lines = lines self.dates = dates } /// Reads page file text; a date line directly above the first line of a block is its created date. public init(text: String, timeZone: TimeZone = .current) { var raw = text.replacingOccurrences(of: "\r\n", with: "\n").components(separatedBy: "\n") if raw.last == "" { raw.removeLast() } var lines: [Line] = [] var dates: [Int: Date] = [:] var index = 0 while index < raw.count { let line = raw[index] let startsBlock = !Self.isBlank(line) && (index == 0 || Self.isBlank(raw[index - 1])) // A date line with no line under it in its block stays text, so the round trip stays exact. if startsBlock, index + 1 < raw.count, !Self.isBlank(raw[index + 1]), let date = CreatedDate.date(fromLine: line, timeZone: timeZone) { dates[lines.count] = date lines.append(Self.line(raw[index + 1])) index += 2 continue } lines.append(Self.line(line)) index += 1 } self.init(lines: lines, dates: dates) } /// The page file text: every line, the date line above each dated block, and a trailing newline. public func text(timeZone: TimeZone = .current) -> String { guard !lines.isEmpty else { return "" } var out: [String] = [] out.reserveCapacity(lines.count + dates.count) for (index, line) in lines.enumerated() { if let date = dates[index], startsBlock(at: index) { out.append(CreatedDate.line(for: date, timeZone: timeZone)) } out.append(line.text) } return out.joined(separator: "\n") + "\n" } /// The line ranges of the blocks, top to bottom; a run of blank lines of any length separates two blocks. public var blocks: [Range] { var blocks: [Range] = [] var start: Int? for (index, line) in lines.enumerated() { if line.isBlank { if let first = start { blocks.append(first.. Bool { !lines[index].isBlank && (index == 0 || lines[index - 1].isBlank) } public static func isBlank(_ line: S) -> Bool { line.allSatisfy(\.isWhitespace) } /// The path an image line points to, as `images/`; nil when the line is not exactly an image line. public static func imagePath(fromLine line: String) -> String? { line.wholeMatch(of: #/!\[\]\((images/[^/()\s]+)\)/#).map { String($0.1) } } public static func imageLine(_ path: String) -> String { "![](\(path))" } private static func line(_ text: String) -> Line { imagePath(fromLine: text).map(Line.image) ?? .text(text) } }