1/// The top of every page: the project's name and the tabs that move between the
2/// coverage report, test results and available history (docs/DESIGN.md, D4).
3struct SiteHeader {
4 enum Tab {
5 case coverage
6 case tests
7 case history
8 }
9
10 /// The project's name, shown as given.
11 let title: String
12 let currentTab: Tab
13 /// The Tests tab is left out when the site has no tests page to link.
14 let hasTestsPage: Bool
15 /// The path from the page's folder back to the site's root: empty beside the index,
16 /// `../` one folder down.
17 let rootPath: String
18
196 var run = ReportHeader()
20
21 /// The heading and the tabs, for the page to put at the top of its `<header>`.
22216 var html: String {
23216 var tabs = [tabMarkup(.coverage, name: "Coverage", file: IndexPage.sitePath)]
24216 if hasTestsPage {
2556 tabs.append(tabMarkup(.tests, name: "Tests", file: TestsPage.sitePath))
2656 }
27216 if let historyPath = run.historyPath {
2836 let path = currentTab == .history ? "index.html" : rootPath + historyPath
2936 let current = currentTab == .history ? #" aria-current="page""# : ""
3036 tabs.append(#"<a href="\#(escapingHTML(path))"\#(current)>History</a>"#)
3136 }
32216 return """
33216 <div class="site-heading"><h1 class="site-title">\(escapingHTML(title))</h1>\(runMarkup)</div>
34216 <nav class="tabs" aria-label="Report">\(tabs.joined())</nav>
35216 """
36216 }
37
38216 private var runMarkup: String {
39216 let shortCommit = run.commit.map { String($0.prefix(PublishedLayout.shortCommitLength)) }
40432 let parts = [shortCommit, run.subject].compactMap { $0 }.filter { !$0.isEmpty }
41216 guard !parts.isEmpty else { return "" }
4260 return #"<span class="run">\#(escapingHTML(parts.joined(separator: " · ")))</span>"#
43216 }
44
45272 private func tabMarkup(_ tab: Tab, name: String, file: String) -> String {
46272 let current = tab == currentTab ? #" aria-current="page""# : ""
47272 return #"<a href="\#(rootPath)\#(file)"\#(current)>\#(name)</a>"#
48272 }
49}