| 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). |
| 3 | | struct 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 | | |
| 19 | 6 | var run = ReportHeader() |
| 20 | | |
| 21 | | /// The heading and the tabs, for the page to put at the top of its `<header>`. |
| 22 | 231 | var html: String { |
| 23 | 231 | var tabs = [tabMarkup(.coverage, name: "Coverage", file: IndexPage.sitePath)] |
| 24 | 231 | if hasTestsPage { |
| 25 | 56 | tabs.append(tabMarkup(.tests, name: "Tests", file: TestsPage.sitePath)) |
| 26 | 56 | } |
| 27 | 231 | if let historyPath = run.historyPath { |
| 28 | 36 | let path = currentTab == .history ? "index.html" : rootPath + historyPath |
| 29 | 36 | let current = currentTab == .history ? #" aria-current="page""# : "" |
| 30 | 36 | tabs.append(#"<a href="\#(escapingHTML(path))"\#(current)>History</a>"#) |
| 31 | 36 | } |
| 32 | 231 | return """ |
| 33 | 231 | <div class="site-heading"><h1 class="site-title">\(escapingHTML(title))</h1>\(runMarkup)</div> |
| 34 | 231 | <nav class="tabs" aria-label="Report">\(tabs.joined())</nav> |
| 35 | 231 | """ |
| 36 | 231 | } |
| 37 | | |
| 38 | 231 | private var runMarkup: String { |
| 39 | 231 | let shortCommit = run.commit.map { String($0.prefix(PublishedLayout.shortCommitLength)) } |
| 40 | 461 | let parts = [shortCommit, run.subject].compactMap { $0 }.filter { !$0.isEmpty } |
| 41 | 231 | guard !parts.isEmpty else { return "" } |
| 42 | 75 | return #"<span class="run">\#(escapingHTML(parts.joined(separator: " · ")))</span>"# |
| 43 | 231 | } |
| 44 | | |
| 45 | 287 | private func tabMarkup(_ tab: Tab, name: String, file: String) -> String { |
| 46 | 287 | let current = tab == currentTab ? #" aria-current="page""# : "" |
| 47 | 287 | return #"<a href="\#(rootPath)\#(file)"\#(current)>\#(name)</a>"# |
| 48 | 287 | } |
| 49 | | } |