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

Commit 6b622be

Browse files
committed
Replace <base> with baseURL parameter for path helper
1 parent 4387e56 commit 6b622be

File tree

16 files changed

+103
-67
lines changed

16 files changed

+103
-67
lines changed

Sources/swift-doc/Extensions/SwiftDoc+Extensions.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ extension Symbol {
1515
node.height = 0.5
1616
node.fixedSize = .shape
1717

18-
if !(api is Unknown) {
19-
node.href = "/" + path(for: self)
20-
}
21-
2218
switch api {
2319
case let `class` as Class:
2420
node.class = "class"
@@ -40,7 +36,7 @@ extension Symbol {
4036
return node
4137
}
4238

43-
func graph(in module: Module) -> Graph {
39+
func graph(in module: Module, baseURL: String) -> Graph {
4440
var graph = Graph(directed: true)
4541

4642
let relationships = module.interface.relationships.filter {
@@ -49,6 +45,11 @@ extension Symbol {
4945
}
5046

5147
var symbolNode = self.node
48+
49+
if !(api is Unknown) {
50+
symbolNode.href = path(for: self, with: baseURL)
51+
}
52+
5253
symbolNode.strokeWidth = 3.0
5354
symbolNode.class = [symbolNode.class, "current"].compactMap { $0 }.joined(separator: " ")
5455

Sources/swift-doc/Subcommands/Generate.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extension SwiftDoc {
4343

4444
func run() throws {
4545
let module = try Module(name: options.moduleName, paths: options.inputs)
46+
let baseURL = options.baseURL
4647

4748
let outputDirectoryURL = URL(fileURLWithPath: options.output)
4849
try fileManager.createDirectory(at: outputDirectoryURL, withIntermediateDirectories: true, attributes: fileAttributes)
@@ -56,9 +57,9 @@ extension SwiftDoc {
5657
for symbol in module.interface.topLevelSymbols.filter({ $0.isPublic }) {
5758
switch symbol.api {
5859
case is Class, is Enumeration, is Structure, is Protocol:
59-
pages[path(for: symbol)] = TypePage(module: module, symbol: symbol)
60+
pages[path(for: symbol, with: baseURL)] = TypePage(module: module, symbol: symbol, baseURL: baseURL)
6061
case let `typealias` as Typealias:
61-
pages[path(for: `typealias`.name)] = TypealiasPage(module: module, symbol: symbol)
62+
pages[path(for: `typealias`.name, with: baseURL)] = TypealiasPage(module: module, symbol: symbol, baseURL: baseURL)
6263
case let function as Function where !function.isOperator:
6364
globals[function.name, default: []] += [symbol]
6465
case let variable as Variable:
@@ -69,7 +70,7 @@ extension SwiftDoc {
6970
}
7071

7172
for (name, symbols) in globals {
72-
pages[path(for: name)] = GlobalPage(module: module, name: name, symbols: symbols)
73+
pages[path(for: name, with: baseURL)] = GlobalPage(module: module, name: name, symbols: symbols, baseURL: baseURL)
7374
}
7475

7576
guard !pages.isEmpty else {
@@ -87,15 +88,15 @@ extension SwiftDoc {
8788
}
8889

8990
let url = outputDirectoryURL.appendingPathComponent(filename)
90-
try page.write(to: url, format: format, baseURL: options.baseURL)
91+
try page.write(to: url, format: format)
9192
} else {
9293
switch format {
9394
case .commonmark:
94-
pages["Home"] = HomePage(module: module)
95-
pages["_Sidebar"] = SidebarPage(module: module)
96-
pages["_Footer"] = FooterPage()
95+
pages["Home"] = HomePage(module: module, baseURL: baseURL)
96+
pages["_Sidebar"] = SidebarPage(module: module, baseURL: baseURL)
97+
pages["_Footer"] = FooterPage(baseURL: baseURL)
9798
case .html:
98-
pages["Home"] = HomePage(module: module)
99+
pages["Home"] = HomePage(module: module, baseURL: baseURL)
99100
}
100101

101102
try pages.map { $0 }.parallelForEach {
@@ -110,7 +111,7 @@ extension SwiftDoc {
110111
}
111112

112113
let url = outputDirectoryURL.appendingPathComponent(filename)
113-
try $0.value.write(to: url, format: format, baseURL: options.baseURL)
114+
try $0.value.write(to: url, format: format)
114115
}
115116
}
116117

Sources/swift-doc/Supporting Types/Components/Declaration.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import Xcode
99
struct Declaration: Component {
1010
var symbol: Symbol
1111
var module: Module
12+
let baseURL: String
1213

13-
init(of symbol: Symbol, in module: Module) {
14+
init(of symbol: Symbol, in module: Module, baseURL: String) {
1415
self.symbol = symbol
1516
self.module = module
17+
self.baseURL = baseURL
1618
}
1719

1820
// MARK: - Component
@@ -27,7 +29,7 @@ struct Declaration: Component {
2729

2830
var html: HypertextLiteral.HTML {
2931
var html = try! SwiftSyntaxHighlighter.highlight(source: symbol.declaration, using: Xcode.self)
30-
html = linkCodeElements(of: html, for: symbol, in: module)
32+
html = linkCodeElements(of: html, for: symbol, in: module, with: baseURL)
3133
return HTML(html)
3234
}
3335
}

Sources/swift-doc/Supporting Types/Components/Documentation.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import Xcode
1010
struct Documentation: Component {
1111
var symbol: Symbol
1212
var module: Module
13+
let baseURL: String
1314

14-
init(for symbol: Symbol, in module: Module) {
15+
init(for symbol: Symbol, in module: Module, baseURL: String) {
1516
self.symbol = symbol
1617
self.module = module
18+
self.baseURL = baseURL
1719
}
1820

1921
// MARK: - Component
@@ -37,7 +39,7 @@ struct Documentation: Component {
3739
Fragment { "\(documentation.summary!)" }
3840
}
3941

40-
Declaration(of: symbol, in: module)
42+
Declaration(of: symbol, in: module, baseURL: baseURL)
4143

4244
ForEach(in: documentation.discussionParts) { part in
4345
if part is SwiftMarkup.Documentation.Callout {
@@ -87,7 +89,7 @@ struct Documentation: Component {
8789

8890
var fragments: [HypertextLiteralConvertible] = []
8991

90-
fragments.append(Declaration(of: symbol, in: module))
92+
fragments.append(Declaration(of: symbol, in: module, baseURL: baseURL))
9193

9294
if let summary = documentation.summary {
9395
fragments.append(#"""
@@ -111,11 +113,11 @@ struct Documentation: Component {
111113
let source = codeBlock.literal
112114
{
113115
var html = try! SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)
114-
html = linkCodeElements(of: html, for: symbol, in: module)
116+
html = linkCodeElements(of: html, for: symbol, in: module, with: baseURL)
115117
return HTML(html)
116118
} else {
117119
var html = (try! CommonMark.Document(part)).render(format: .html, options: [.unsafe])
118-
html = linkCodeElements(of: html, for: symbol, in: module)
120+
html = linkCodeElements(of: html, for: symbol, in: module, with: baseURL)
119121
return HTML(html)
120122
}
121123
} else {

Sources/swift-doc/Supporting Types/Components/Members.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import HypertextLiteral
77
struct Members: Component {
88
var symbol: Symbol
99
var module: Module
10+
let baseURL: String
1011

1112
var members: [Symbol]
1213

@@ -17,9 +18,11 @@ struct Members: Component {
1718
var methods: [Symbol]
1819
var genericallyConstrainedMembers: [[GenericRequirement] : [Symbol]]
1920

20-
init(of symbol: Symbol, in module: Module) {
21+
init(of symbol: Symbol, in module: Module, baseURL: String) {
2122
self.symbol = symbol
2223
self.module = module
24+
self.baseURL = baseURL
25+
2326
self.members = module.interface.members(of: symbol).filter { $0.extension?.genericRequirements.isEmpty != false }
2427

2528
self.typealiases = members.filter { $0.api is Typealias }
@@ -55,7 +58,7 @@ struct Members: Component {
5558
Heading {
5659
Code { member.name }
5760
}
58-
Documentation(for: member, in: module)
61+
Documentation(for: member, in: module, baseURL: baseURL)
5962
}
6063
}
6164
}
@@ -72,7 +75,7 @@ struct Members: Component {
7275
Section {
7376
ForEach(in: members) { member in
7477
Heading { member.name }
75-
Documentation(for: member, in: module)
78+
Documentation(for: member, in: module, baseURL: baseURL)
7679
}
7780
}
7881
}
@@ -97,7 +100,7 @@ struct Members: Component {
97100
<h3>
98101
<code>\#(softbreak(member.name))</code>
99102
</h3>
100-
\#(Documentation(for: member, in: module).html)
103+
\#(Documentation(for: member, in: module, baseURL: baseURL).html)
101104
</div>
102105
"""#
103106
})
@@ -117,7 +120,7 @@ struct Members: Component {
117120
\#(members.map { member -> HypertextLiteral.HTML in
118121
#"""
119122
<h4>\#(softbreak(member.name))</h4>
120-
\#(Documentation(for: member, in: module).html)
123+
\#(Documentation(for: member, in: module, baseURL: baseURL).html)
121124
"""#
122125
})
123126
</section>

Sources/swift-doc/Supporting Types/Components/Relationships.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ extension StringBuilder {
2828
struct Relationships: Component {
2929
var module: Module
3030
var symbol: Symbol
31+
let baseURL: String
3132
var inheritedTypes: [Symbol]
3233

33-
init(of symbol: Symbol, in module: Module) {
34+
init(of symbol: Symbol, in module: Module, baseURL: String) {
3435
self.module = module
3536
self.symbol = symbol
3637
self.inheritedTypes = module.interface.typesInherited(by: symbol) + module.interface.typesConformed(by: symbol)
38+
self.baseURL = baseURL
3739
}
3840

3941
var graphHTML: HypertextLiteral.HTML? {
40-
var graph = symbol.graph(in: module)
42+
var graph = symbol.graph(in: module, baseURL: baseURL)
4143
guard !graph.edges.isEmpty else { return nil }
4244

4345
graph.aspectRatio = 0.125
@@ -80,7 +82,7 @@ struct Relationships: Component {
8082
if type.api is Unknown {
8183
return "`\(type.id)`"
8284
} else {
83-
return "[`\(type.id)`](\(path(for: type)))"
85+
return "[`\(type.id)`](\(path(for: type, with: baseURL)))"
8486
}
8587
}.joined(separator: ", "))
8688
"""#
@@ -118,7 +120,7 @@ struct Relationships: Component {
118120
"""#
119121
} else {
120122
return #"""
121-
<dt class="\#(descriptor)"><code><a href="\#(path(for: symbol))">\#(symbol.id)</a></code></dt>
123+
<dt class="\#(descriptor)"><code><a href="\#(path(for: symbol, with: baseURL))">\#(symbol.id)</a></code></dt>
122124
<dd>\#(commonmark: symbol.documentation?.summary ?? "")</dd>
123125
"""#
124126
}

Sources/swift-doc/Supporting Types/Components/Requirements.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import HypertextLiteral
77
struct Requirements: Component {
88
var symbol: Symbol
99
var module: Module
10+
let baseURL: String
1011

11-
init(of symbol: Symbol, in module: Module) {
12+
init(of symbol: Symbol, in module: Module, baseURL: String) {
1213
self.symbol = symbol
1314
self.module = module
15+
self.baseURL = baseURL
1416
}
1517

1618
var sections: [(title: String, requirements: [Symbol])] {
@@ -31,7 +33,7 @@ struct Requirements: Component {
3133
Heading { section.title }
3234
ForEach(in: section.requirements) { requirement in
3335
Heading { requirement.name }
34-
Documentation(for: requirement, in: module)
36+
Documentation(for: requirement, in: module, baseURL: baseURL)
3537
}
3638
}
3739
}
@@ -53,7 +55,7 @@ struct Requirements: Component {
5355
<h3>
5456
<code>\#(softbreak(member.name))</code>
5557
</h3>
56-
\#(Documentation(for: member, in: module).html)
58+
\#(Documentation(for: member, in: module, baseURL: baseURL).html)
5759
</div>
5860
"""#
5961
})

Sources/swift-doc/Supporting Types/Helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import SwiftDoc
33
import HTML
44

5-
public func linkCodeElements(of html: String, for symbol: Symbol, in module: Module) -> String {
5+
public func linkCodeElements(of html: String, for symbol: Symbol, in module: Module, with baseURL: String) -> String {
66
let document = try! Document(string: html.description)!
77
for element in document.search(xpath: "//code | //pre/code//span[contains(@class,'type')]") {
88
guard let name = element.content else { continue }
@@ -12,7 +12,7 @@ public func linkCodeElements(of html: String, for symbol: Symbol, in module: Mod
1212
let candidate = candidates.filter({ $0 != symbol }).first
1313
{
1414
let a = Element(name: "a")
15-
a["href"] = path(for: candidate)
15+
a["href"] = path(for: candidate, with: baseURL)
1616
element.wrap(inside: a)
1717
}
1818
}

Sources/swift-doc/Supporting Types/Layout.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HypertextLiteral
22
import Foundation
33

4-
func layout(_ page: Page, baseURL: String) -> HTML {
4+
func layout(_ page: Page) -> HTML {
55
let html = page.html
66

77
return #"""
@@ -11,12 +11,11 @@ func layout(_ page: Page, baseURL: String) -> HTML {
1111
<meta charset="UTF-8">
1212
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1313
<title>\#(page.module.name) - \#(page.title)</title>
14-
<base href="\#(baseURL)"/>
15-
<link rel="stylesheet" type="text/css" href="all.css" media="all" />
14+
<link rel="stylesheet" type="text/css" href="/all.css" media="all" />
1615
</head>
1716
<body>
1817
<header>
19-
<a href="\#(baseURL)">
18+
<a href="\#(page.baseURL)">
2019
<strong>
2120
\#(page.module.name)
2221
</strong>
@@ -45,7 +44,7 @@ func layout(_ page: Page, baseURL: String) -> HTML {
4544
</main>
4645
4746
<footer>
48-
\#(FooterPage().html)
47+
\#(FooterPage(baseURL: page.baseURL).html)
4948
</footer>
5049
</body>
5150
</html>

Sources/swift-doc/Supporting Types/Page.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import HypertextLiteral
88

99
protocol Page: HypertextLiteralConvertible {
1010
var module: Module { get }
11+
var baseURL: String { get }
1112
var title: String { get }
1213
var document: CommonMark.Document { get }
1314
var html: HypertextLiteral.HTML { get }
@@ -19,13 +20,13 @@ extension Page {
1920
}
2021

2122
extension Page {
22-
func write(to url: URL, format: SwiftDoc.Generate.Format, baseURL: String) throws {
23+
func write(to url: URL, format: SwiftDoc.Generate.Format) throws {
2324
let data: Data?
2425
switch format {
2526
case .commonmark:
2627
data = document.render(format: .commonmark).data(using: .utf8)
2728
case .html:
28-
data = layout(self, baseURL: baseURL).description.data(using: .utf8)
29+
data = layout(self).description.data(using: .utf8)
2930
}
3031

3132
guard let filedata = data else { return }
@@ -34,12 +35,19 @@ extension Page {
3435
}
3536
}
3637

37-
func path(for symbol: Symbol) -> String {
38-
return path(for: symbol.id.description)
38+
func path(for symbol: Symbol, with baseURL: String) -> String {
39+
return path(for: symbol.id.description, with: baseURL)
3940
}
4041

41-
func path(for identifier: CustomStringConvertible) -> String {
42-
return "\(identifier)".replacingOccurrences(of: ".", with: "_")
42+
func path(for identifier: CustomStringConvertible, with baseURL: String) -> String {
43+
var urlComponents = URLComponents(string: baseURL)
44+
urlComponents?.path += "\(identifier)".replacingOccurrences(of: ".", with: "_")
45+
guard let path = urlComponents?.string else {
46+
logger.critical("Unable to construct path for \(identifier) with baseURL \(baseURL)")
47+
fatalError()
48+
}
49+
50+
return path
4351
}
4452

4553
func writeFile(_ data: Data, to url: URL) throws {

0 commit comments

Comments
 (0)