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>`.
22137 var html: String {
23137 var tabs = [tabMarkup(.coverage, name: "Coverage", file: IndexPage.sitePath)]
24137 if hasTestsPage {
2535 tabs.append(tabMarkup(.tests, name: "Tests", file: TestsPage.sitePath))
2635 }
27137 if let historyPath = run.historyPath {
2820 let path = currentTab == .history ? "index.html" : rootPath + historyPath
2920 let current = currentTab == .history ? #" aria-current="page""# : ""
3020 tabs.append(#"<a href="\#(escapingHTML(path))"\#(current)>History</a>"#)
3120 }
32137 return """
33137 <div class="site-heading"><h1 class="site-title">\(escapingHTML(title))</h1>\(runMarkup)</div>
34137 <nav class="tabs" aria-label="Report">\(tabs.joined())</nav>
35137 """
36137 }
37
38137 private var runMarkup: String {
39137 let shortCommit = run.commit.map { String($0.prefix(PublishedLayout.shortCommitLength)) }
40274 let parts = [shortCommit, run.subject].compactMap { $0 }.filter { !$0.isEmpty }
41137 guard !parts.isEmpty else { return "" }
4250 return #"<span class="run">\#(escapingHTML(parts.joined(separator: " · ")))</span>"#
43137 }
44
45172 private func tabMarkup(_ tab: Tab, name: String, file: String) -> String {
46172 let current = tab == currentTab ? #" aria-current="page""# : ""
47172 return #"<a href="\#(rootPath)\#(file)"\#(current)>\#(name)</a>"#
48172 }
49}