Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 44784c0

Browse files
committed
Merge pull request #81 from kean/69-display-globals
1 parent 7705c4f commit 44784c0

File tree

2 files changed

+82
-29
lines changed

2 files changed

+82
-29
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Added asset pipeline for CSS assets.
1515
#49 by @kaishin.
1616

17+
### Changed
18+
19+
- Changed Home page to display globals for HTML format.
20+
#81 by @kean.
21+
1722
### Fixed
1823

1924
- Fixed relationship handling for members of nested types.

Sources/swift-doc/Supporting Types/Pages/HomePage.swift

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ struct HomePage: Page {
1010
var enumerations: [Symbol] = []
1111
var structures: [Symbol] = []
1212
var protocols: [Symbol] = []
13-
var operatorNames: Set<String> = []
14-
var globalTypealiasNames: Set<String> = []
15-
var globalFunctionNames: Set<String> = []
16-
var globalVariableNames: Set<String> = []
13+
var operators: [Symbol] = []
14+
var globalTypealias: [Symbol] = []
15+
var globalFunctions: [Symbol] = []
16+
var globalVariables: [Symbol] = []
1717

1818
init(module: Module) {
1919
self.module = module
@@ -28,16 +28,16 @@ struct HomePage: Page {
2828
structures.append(symbol)
2929
case is Protocol:
3030
protocols.append(symbol)
31-
case let `typealias` as Typealias:
32-
globalTypealiasNames.insert(`typealias`.name)
33-
case let `operator` as Operator:
34-
operatorNames.insert(`operator`.name)
31+
case is Typealias:
32+
globalTypealias.append(symbol)
33+
case is Operator:
34+
operators.append(symbol)
3535
case let function as Function where function.isOperator:
36-
operatorNames.insert(function.name)
37-
case let function as Function:
38-
globalFunctionNames.insert(function.name)
39-
case let variable as Variable:
40-
globalVariableNames.insert(variable.name)
36+
operators.append(symbol)
37+
case is Function:
38+
globalFunctions.append(symbol)
39+
case is Variable:
40+
globalVariables.append(symbol)
4141
default:
4242
continue
4343
}
@@ -54,6 +54,11 @@ struct HomePage: Page {
5454
let types = classes + enumerations + structures
5555
let typeNames = Set(types.map { $0.id.description })
5656
let protocolNames = Set(protocols.map { $0.id.description })
57+
let operatorNames = Set(operators.map { $0.id.description })
58+
59+
let globalTypealiasNames = Set(globalTypealias.map { $0.id.description })
60+
let globalFunctionNames = Set(globalFunctions.map { $0.id.description })
61+
let globalVariableNames = Set(globalVariables.map { $0.id.description })
5762

5863
return Document {
5964
ForEach(in: [
@@ -107,24 +112,67 @@ struct HomePage: Page {
107112
return #"""
108113
<section id=\#(heading.lowercased())>
109114
<h2>\#(heading)</h2>
110-
<dl>
111-
\#(symbols.sorted().map { symbol -> HypertextLiteral.HTML in
112-
let descriptor = String(describing: type(of: symbol.api)).lowercased()
113-
return #"""
114-
<dt class="\#(descriptor)">
115-
<a href=\#(path(for: symbol)) title="\#(descriptor) - \#(symbol.id.description)">
116-
\#(softbreak(symbol.id.description))
117-
</a>
118-
</dt>
119-
<dd>
120-
\#(commonmark: symbol.documentation?.summary ?? "")
121-
</dd>
122-
"""# as HypertextLiteral.HTML
123-
})
124-
</dl>
115+
\#(listHTML(symbols: symbols))
125116
</section>
126-
"""# as HypertextLiteral.HTML
117+
"""#
127118
})
119+
\#(globalsHTML)
120+
"""#
121+
}
122+
123+
private var globalsHTML: HypertextLiteral.HTML {
124+
guard !globalTypealias.isEmpty ||
125+
!globalFunctions.isEmpty ||
126+
!globalVariables.isEmpty else {
127+
return ""
128+
}
129+
130+
let heading = "Globals"
131+
return #"""
132+
<section id=\#(heading.lowercased())>
133+
<h2>\#(heading)</h2>
134+
\#(globalsListHTML)
135+
</section>
136+
"""#
137+
}
138+
139+
private var globalsListHTML: HypertextLiteral.HTML {
140+
let globals = [
141+
("Typealiases", globalTypealias),
142+
("Functions", globalFunctions),
143+
("Variables", globalVariables),
144+
]
145+
return #"""
146+
\#(globals.compactMap { (heading, symbols) -> HypertextLiteral.HTML? in
147+
guard !symbols.isEmpty else { return nil }
148+
149+
return #"""
150+
<section id=\#(heading.lowercased())>
151+
<h3>\#(heading)</h3>
152+
\#(listHTML(symbols: symbols))
153+
</section>
154+
"""#
155+
})
156+
"""#
157+
}
158+
159+
private func listHTML(symbols: [Symbol]) -> HypertextLiteral.HTML {
160+
#"""
161+
<dl>
162+
\#(symbols.sorted().map { symbol -> HypertextLiteral.HTML in
163+
let descriptor = String(describing: type(of: symbol.api)).lowercased()
164+
return #"""
165+
<dt class="\#(descriptor)">
166+
<a href=\#(path(for: symbol)) title="\#(descriptor) - \#(symbol.id.description)">
167+
\#(softbreak(symbol.id.description))
168+
</a>
169+
</dt>
170+
<dd>
171+
\#(commonmark: symbol.documentation?.summary ?? "")
172+
</dd>
173+
"""#
174+
})
175+
</dl>
128176
"""#
129177
}
130178
}

0 commit comments

Comments
 (0)