1import Foundation
2
3/// One published run: the folder it sits in (the commit's short hash) and its numbers.
4public struct HistoryRun: Sendable, Equatable {
5 public let folder: String
6 public let totals: RunTotals
7
824 public init(folder: String, totals: RunTotals) {
924 self.folder = folder
1024 self.totals = totals
1124 }
12}
13
14public enum HistoryPageError: Error, Equatable {
15 case noRuns
16}
17
18/// The page above the published runs (DESIGN.md D7): it redirects to the newest run's
19/// report and lists every run, newest first, so earlier ones stay reachable.
20public struct HistoryPage: Sendable {
21 /// The stylesheet's name; it sits beside the page.
22 public static let stylesheetPath = SiteStylesheet.fileName
23
24 /// - Parameters:
25 /// - title: the project's name, shown as given.
26 /// - runs: in any order; each run's report is `<folder>/index.html` beside the page.
27 /// - Throws: `HistoryPageError.noRuns` when `runs` is empty, since there is nothing
28 /// to redirect to.
2910 public init(title: String, runs: [HistoryRun]) throws {
3010 guard !runs.isEmpty else { throw HistoryPageError.noRuns }
319 self.title = title
3216 self.newestFirst = runs.sorted {
3316 ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder)
3416 }
359 }
36
37 private let title: String
38 private let newestFirst: [HistoryRun]
39
409 public var html: String {
419 """
429 <!doctype html>
439 <html lang="en">
449 <head>
459 <meta charset="utf-8">
469 <meta name="viewport" content="width=device-width, initial-scale=1">
479 <meta http-equiv="refresh" content="0; url=\(Self.reportLink(newestFirst[0]))">
489 <title>History · \(escapingHTML(title))</title>
499 <link rel="stylesheet" href="\(Self.stylesheetPath)">
509 </head>
519 <body class="history-page">
529 <header class="page-header">
539 <h1 class="site-title">\(escapingHTML(title))</h1>
549 <p class="totals">\(groupingDigits(newestFirst.count)) \
559 \(newestFirst.count == 1 ? "commit" : "commits"), newest first</p>
569 </header>
579 <main>
589 <table class="history">
599 <thead><tr><th>Commit</th><th>Date</th><th>Lines</th><th>Subject</th></tr></thead>
609 <tbody>
6119 \(newestFirst.map(Self.rowMarkup).joined(separator: "\n"))
629 </tbody>
639 </table>
649 </main>
659 </body>
669 </html>
679
689 """
699 }
70
71 /// Relative to the page, with the folder's name encoded so any name stays one link.
7228 private static func reportLink(_ run: HistoryRun) -> String {
7328 let folder = run.folder.addingPercentEncoding(withAllowedCharacters: folderCharacters)
7428 return "\(folder ?? run.folder)/\(IndexPage.sitePath)"
7528 }
76
77 /// Unreserved characters only: `#`, `?`, `%` and spaces in a name must not end the path.
78 private static let folderCharacters = CharacterSet(
79 charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~")
80
8119 private static func rowMarkup(_ run: HistoryRun) -> String {
8219 let subject = escapingHTML(run.totals.subject ?? "")
8319 return #"<tr><td class="commit"><a href="\#(escapingHTML(reportLink(run)))">"#
8419 + "\(escapingHTML(run.folder))</a></td>"
8519 + #"<td class="date">\#(dateMarkup(run.totals.date))</td>"#
8619 + #"<td class="lines">\#(linesMarkup(run.totals))</td>"#
8719 + #"<td class="subject">\#(subject)</td></tr>"#
8819 }
89
90 /// The line coverage, or the reason a run has none.
9119 private static func linesMarkup(_ totals: RunTotals) -> String {
9219 if let reason = totals.unavailable {
932 return #"<span class="unavailable">\#(escapingHTML(reason))</span>"#
9417 }
9517 let lines = LineSummary(count: totals.lines.total, covered: totals.lines.covered)
9617 return #"<span class="percent \#(lines.tier.rawValue)">\#(lines.percentText)</span>"#
9719 }
98
99 /// The machine-readable date with a short text beside it, `2026-12-01 08:30 UTC`.
10019 private static func dateMarkup(_ date: Date) -> String {
10119 let iso = date.formatted(.iso8601)
10219 let shown = iso.prefix(shownDateLength).replacingOccurrences(of: "T", with: " ")
10319 return #"<time datetime="\#(iso)">\#(shown) UTC</time>"#
10419 }
105
106 /// `2026-12-01T08:30`: up to the minute.
107 private static let shownDateLength = 16
108}