improves plak

This commit is contained in:
2026-10-17 17:33:13 +02:00
parent 7b891819b0
commit a92d6f8871
633 changed files with 17623 additions and 7556 deletions
+70 -7
View File
@@ -1,17 +1,25 @@
import Foundation
/// The info string of a fenced block that the page shows as one object: a code block, or a table for csv.
public enum FenceKind: String, Sendable, CaseIterable {
case php, json, xml, csv
}
/// 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)
/// A code block or table: the whole fenced block, whose body may hold newlines and blank lines.
case fenced(FenceKind, 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)
case let .fenced(kind, body): PageFile.fencedBlock(kind, body)
}
}
@@ -44,16 +52,28 @@ public struct PageFile: Sendable, Equatable {
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
index += 1
}
lines.append(Self.line(line))
index += 1
let (parsed, next) = Self.line(raw, at: index, timeZone: timeZone)
lines.append(parsed)
index = next
}
self.init(lines: lines, dates: dates)
}
/// The lines of pasted text: image lines and fenced blocks, but no dates.
public static func lines(pasted text: String) -> [Line] {
let raw = text.replacingOccurrences(of: "\r\n", with: "\n").components(separatedBy: "\n")
var lines: [Line] = []
var index = 0
while index < raw.count {
let (parsed, next) = Self.line(raw, at: index, timeZone: .current)
lines.append(parsed)
index = next
}
return lines
}
/// 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 "" }
@@ -101,7 +121,50 @@ public struct PageFile: Sendable, Equatable {
"![](\(path))"
}
private static func line(_ text: String) -> Line {
imagePath(fromLine: text).map(Line.image) ?? .text(text)
/// The fence line, the body and the closing fence; the fence is longer than any run of backticks that starts a body line.
public static func fencedBlock(_ kind: FenceKind, _ body: String) -> String {
let fence = fence(for: body)
return "\(fence)\(kind.rawValue)\n\(body)\n\(fence)"
}
/// Every line break as LF: a CR or CRLF in a body would hide a fence line inside it.
public static func unixNewlines(_ text: String) -> String {
text.replacingOccurrences(of: "\r\n", with: "\n").replacingOccurrences(of: "\r", with: "\n")
}
/// Split on "\n" itself: as a Character, "\r\n" is one grapheme and would not split.
static func fence(for body: String) -> String {
var longest = 0
for line in body.components(separatedBy: "\n") {
longest = max(longest, line.prefix(while: { $0 == "`" }).count)
}
return String(repeating: "`", count: max(3, longest + 1))
}
/// The line that starts at `raw[index]`, and the index of the line after it.
private static func line(_ raw: [String], at index: Int, timeZone: TimeZone) -> (Line, next: Int) {
if let fenced = fencedBlock(raw, at: index, timeZone: timeZone) { return fenced }
let text = raw[index]
return (imagePath(fromLine: text).map(Line.image) ?? .text(text), index + 1)
}
/// A fence of a known kind with a body of at least one line and a closing fence of the same length.
///
/// A fence that is not the one `fencedBlock` would write stays text, so the round trip stays exact. A body
/// with a date line after a blank line stays text too: a stray fence must not swallow the dated blocks below it.
private static func fencedBlock(_ raw: [String], at index: Int, timeZone: TimeZone) -> (Line, next: Int)? {
let open = raw[index]
let ticks = open.prefix(while: { $0 == "`" }).count
guard ticks >= 3, let kind = FenceKind(rawValue: String(open.dropFirst(ticks))) else { return nil }
let fence = String(repeating: "`", count: ticks)
var close = index + 1
while close < raw.count, raw[close] != fence { close += 1 }
guard close < raw.count, close > index + 1 else { return nil }
for line in (index + 2)..<close where isBlank(raw[line - 1]) && CreatedDate.date(fromLine: raw[line], timeZone: timeZone) != nil {
return nil
}
let body = raw[(index + 1)..<close].joined(separator: "\n")
guard Self.fence(for: body) == fence else { return nil }
return (.fenced(kind, body), close + 1)
}
}