adds first version of the app
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import Foundation
|
||||
|
||||
/// The created date of a block: its date line in the page file and its label in the margin.
|
||||
public enum CreatedDate {
|
||||
static let months = ["jan", "feb", "mrt", "apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]
|
||||
|
||||
/// The date line above the first line of a block, as `<!-- 2026-10-16 08:55 -->`.
|
||||
public static func line(for date: Date, timeZone: TimeZone = .current) -> String {
|
||||
let c = calendar(timeZone).dateComponents([.year, .month, .day, .hour, .minute], from: date)
|
||||
return "<!-- \(pad(c.year, 4))-\(pad(c.month))-\(pad(c.day)) \(pad(c.hour)):\(pad(c.minute)) -->"
|
||||
}
|
||||
|
||||
/// The date a page file line holds; nil when the line is not exactly a date line.
|
||||
///
|
||||
/// A line that does not come back the same when written again (`2026-02-30`, or a time
|
||||
/// skipped by a clock change) is not a date, so the page file round trip stays exact.
|
||||
public static func date(fromLine line: String, timeZone: TimeZone = .current) -> Date? {
|
||||
guard let match = line.wholeMatch(of: /<!-- (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) -->/) else { return nil }
|
||||
let components = DateComponents(
|
||||
year: Int(match.1), month: Int(match.2), day: Int(match.3), hour: Int(match.4), minute: Int(match.5)
|
||||
)
|
||||
guard let date = calendar(timeZone).date(from: components), self.line(for: date, timeZone: timeZone) == line else { return nil }
|
||||
return date
|
||||
}
|
||||
|
||||
/// The margin label: `16 okt 08:55`, with the year when it is not the year of `now`.
|
||||
public static func label(for date: Date, now: Date, timeZone: TimeZone = .current) -> String {
|
||||
let calendar = calendar(timeZone)
|
||||
let c = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
|
||||
let month = months[((c.month ?? 1) - 1) % 12]
|
||||
let year = c.year == calendar.component(.year, from: now) ? "" : " \(c.year ?? 0)"
|
||||
return "\(c.day ?? 0) \(month)\(year) \(pad(c.hour)):\(pad(c.minute))"
|
||||
}
|
||||
|
||||
private static func calendar(_ timeZone: TimeZone) -> Calendar {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
calendar.timeZone = timeZone
|
||||
return calendar
|
||||
}
|
||||
|
||||
private static func pad(_ value: Int?, _ width: Int = 2) -> String {
|
||||
let digits = String(value ?? 0)
|
||||
return String(repeating: "0", count: max(0, width - digits.count)) + digits
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
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/<name>`.
|
||||
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<Int>] {
|
||||
var blocks: [Range<Int>] = []
|
||||
var start: Int?
|
||||
for (index, line) in lines.enumerated() {
|
||||
if line.isBlank {
|
||||
if let first = start { blocks.append(first..<index) }
|
||||
start = nil
|
||||
} else if start == nil {
|
||||
start = index
|
||||
}
|
||||
}
|
||||
if let first = start { blocks.append(first..<lines.count) }
|
||||
return blocks
|
||||
}
|
||||
|
||||
public func startsBlock(at index: Int) -> Bool {
|
||||
!lines[index].isBlank && (index == 0 || lines[index - 1].isBlank)
|
||||
}
|
||||
|
||||
public static func isBlank<S: StringProtocol>(_ line: S) -> Bool {
|
||||
line.allSatisfy(\.isWhitespace)
|
||||
}
|
||||
|
||||
/// The path an image line points to, as `images/<name>`; 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 {
|
||||
")"
|
||||
}
|
||||
|
||||
private static func line(_ text: String) -> Line {
|
||||
imagePath(fromLine: text).map(Line.image) ?? .text(text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user