| 1 | | import Foundation |
| 2 | | |
| 3 | | /// One published run: the folder it sits in (the commit's short hash) and its numbers. |
| 4 | | public struct HistoryRun: Sendable, Equatable { |
| 5 | | public let folder: String |
| 6 | | public let totals: RunTotals |
| 7 | | |
| 8 | 24 | public init(folder: String, totals: RunTotals) { |
| 9 | 24 | self.folder = folder |
| 10 | 24 | self.totals = totals |
| 11 | 24 | } |
| 12 | | } |
| 13 | | |
| 14 | | public 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. |
| 20 | | public 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. |
| 29 | 10 | public init(title: String, runs: [HistoryRun]) throws { |
| 30 | 10 | guard !runs.isEmpty else { throw HistoryPageError.noRuns } |
| 31 | 9 | self.title = title |
| 32 | 16 | self.newestFirst = runs.sorted { |
| 33 | 16 | ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder) |
| 34 | 16 | } |
| 35 | 9 | } |
| 36 | | |
| 37 | | private let title: String |
| 38 | | private let newestFirst: [HistoryRun] |
| 39 | | |
| 40 | 9 | public var html: String { |
| 41 | 9 | """ |
| 42 | 9 | <!doctype html> |
| 43 | 9 | <html lang="en"> |
| 44 | 9 | <head> |
| 45 | 9 | <meta charset="utf-8"> |
| 46 | 9 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 47 | 9 | <meta http-equiv="refresh" content="0; url=\(Self.reportLink(newestFirst[0]))"> |
| 48 | 9 | <title>History · \(escapingHTML(title))</title> |
| 49 | 9 | <link rel="stylesheet" href="\(Self.stylesheetPath)"> |
| 50 | 9 | </head> |
| 51 | 9 | <body class="history-page"> |
| 52 | 9 | <header class="page-header"> |
| 53 | 9 | <h1 class="site-title">\(escapingHTML(title))</h1> |
| 54 | 9 | <p class="totals">\(groupingDigits(newestFirst.count)) \ |
| 55 | 9 | \(newestFirst.count == 1 ? "commit" : "commits"), newest first</p> |
| 56 | 9 | </header> |
| 57 | 9 | <main> |
| 58 | 9 | <table class="history"> |
| 59 | 9 | <thead><tr><th>Commit</th><th>Date</th><th>Lines</th><th>Subject</th></tr></thead> |
| 60 | 9 | <tbody> |
| 61 | 19 | \(newestFirst.map(Self.rowMarkup).joined(separator: "\n")) |
| 62 | 9 | </tbody> |
| 63 | 9 | </table> |
| 64 | 9 | </main> |
| 65 | 9 | </body> |
| 66 | 9 | </html> |
| 67 | 9 | |
| 68 | 9 | """ |
| 69 | 9 | } |
| 70 | | |
| 71 | | /// Relative to the page, with the folder's name encoded so any name stays one link. |
| 72 | 28 | private static func reportLink(_ run: HistoryRun) -> String { |
| 73 | 28 | let folder = run.folder.addingPercentEncoding(withAllowedCharacters: folderCharacters) |
| 74 | 28 | return "\(folder ?? run.folder)/\(IndexPage.sitePath)" |
| 75 | 28 | } |
| 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 | | |
| 81 | 19 | private static func rowMarkup(_ run: HistoryRun) -> String { |
| 82 | 19 | let subject = escapingHTML(run.totals.subject ?? "") |
| 83 | 19 | return #"<tr><td class="commit"><a href="\#(escapingHTML(reportLink(run)))">"# |
| 84 | 19 | + "\(escapingHTML(run.folder))</a></td>" |
| 85 | 19 | + #"<td class="date">\#(dateMarkup(run.totals.date))</td>"# |
| 86 | 19 | + #"<td class="lines">\#(linesMarkup(run.totals))</td>"# |
| 87 | 19 | + #"<td class="subject">\#(subject)</td></tr>"# |
| 88 | 19 | } |
| 89 | | |
| 90 | | /// The line coverage, or the reason a run has none. |
| 91 | 19 | private static func linesMarkup(_ totals: RunTotals) -> String { |
| 92 | 19 | if let reason = totals.unavailable { |
| 93 | 2 | return #"<span class="unavailable">\#(escapingHTML(reason))</span>"# |
| 94 | 17 | } |
| 95 | 17 | let lines = LineSummary(count: totals.lines.total, covered: totals.lines.covered) |
| 96 | 17 | return #"<span class="percent \#(lines.tier.rawValue)">\#(lines.percentText)</span>"# |
| 97 | 19 | } |
| 98 | | |
| 99 | | /// The machine-readable date with a short text beside it, `2026-12-01 08:30 UTC`. |
| 100 | 19 | private static func dateMarkup(_ date: Date) -> String { |
| 101 | 19 | let iso = date.formatted(.iso8601) |
| 102 | 19 | let shown = iso.prefix(shownDateLength).replacingOccurrences(of: "T", with: " ") |
| 103 | 19 | return #"<time datetime="\#(iso)">\#(shown) UTC</time>"# |
| 104 | 19 | } |
| 105 | | |
| 106 | | /// `2026-12-01T08:30`: up to the minute. |
| 107 | | private static let shownDateLength = 16 |
| 108 | | } |