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>`.
22231 var html: String {
23231 var tabs = [tabMarkup(.coverage, name: "Coverage", file: IndexPage.sitePath)]
24231 if hasTestsPage {
2556 tabs.append(tabMarkup(.tests, name: "Tests", file: TestsPage.sitePath))
2656 }
27231 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 }
32231 return """
33231 <div class="site-heading"><h1 class="site-title">\(escapingHTML(title))</h1>\(runMarkup)</div>
34231 <nav class="tabs" aria-label="Report">\(tabs.joined())</nav>
35231 """
36231 }
37
38231 private var runMarkup: String {
39231 let shortCommit = run.commit.map { String($0.prefix(PublishedLayout.shortCommitLength)) }
40461 let parts = [shortCommit, run.subject].compactMap { $0 }.filter { !$0.isEmpty }
41231 guard !parts.isEmpty else { return "" }
4275 return #"<span class="run">\#(escapingHTML(parts.joined(separator: " · ")))</span>"#
43231 }
44
45287 private func tabMarkup(_ tab: Tab, name: String, file: String) -> String {
46287 let current = tab == currentTab ? #" aria-current="page""# : ""
47287 return #"<a href="\#(rootPath)\#(file)"\#(current)>\#(name)</a>"#
48287 }
49}