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

Commit 7074084

Browse files
committed
Fix route resolution with base url and output option
1 parent 37d6724 commit 7074084

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

Sources/swift-doc/Subcommands/Generate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ extension SwiftDoc {
5757
for symbol in module.interface.topLevelSymbols.filter({ $0.isPublic }) {
5858
switch symbol.api {
5959
case is Class, is Enumeration, is Structure, is Protocol:
60-
pages[path(for: symbol, with: baseURL)] = TypePage(module: module, symbol: symbol, baseURL: baseURL)
60+
pages[route(for: symbol)] = TypePage(module: module, symbol: symbol, baseURL: baseURL)
6161
case let `typealias` as Typealias:
62-
pages[path(for: `typealias`.name, with: baseURL)] = TypealiasPage(module: module, symbol: symbol, baseURL: baseURL)
62+
pages[route(for: `typealias`.name)] = TypealiasPage(module: module, symbol: symbol, baseURL: baseURL)
6363
case let function as Function where !function.isOperator:
6464
globals[function.name, default: []] += [symbol]
6565
case let variable as Variable:
@@ -70,7 +70,7 @@ extension SwiftDoc {
7070
}
7171

7272
for (name, symbols) in globals {
73-
pages[path(for: name, with: baseURL)] = GlobalPage(module: module, name: name, symbols: symbols, baseURL: baseURL)
73+
pages[route(for: name)] = GlobalPage(module: module, name: name, symbols: symbols, baseURL: baseURL)
7474
}
7575

7676
guard !pages.isEmpty else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func layout(_ page: Page) -> 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-
<link rel="stylesheet" type="text/css" href="/all.css" media="all" />
14+
<link rel="stylesheet" type="text/css" href="\#(path(for: "all.css", with: page.baseURL))" media="all" />
1515
</head>
1616
<body>
1717
<header>

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,27 @@ extension Page {
3535
}
3636
}
3737

38+
func route(for symbol: Symbol) -> String {
39+
return route(for: symbol.id)
40+
}
41+
42+
func route(for name: CustomStringConvertible) -> String {
43+
return name.description.replacingOccurrences(of: ".", with: "_")
44+
}
45+
3846
func path(for symbol: Symbol, with baseURL: String) -> String {
39-
return path(for: symbol.id.description, with: baseURL)
47+
return path(for: route(for: symbol), with: baseURL)
4048
}
4149

4250
func path(for identifier: CustomStringConvertible, with baseURL: String) -> String {
4351
var urlComponents = URLComponents(string: baseURL)
44-
urlComponents?.path += "\(identifier)".replacingOccurrences(of: ".", with: "_")
45-
guard let path = urlComponents?.string else {
52+
urlComponents = urlComponents?.appendingPathComponent("\(identifier)")
53+
guard let string = urlComponents?.string else {
4654
logger.critical("Unable to construct path for \(identifier) with baseURL \(baseURL)")
4755
fatalError()
4856
}
4957

50-
return path
58+
return string
5159
}
5260

5361
func writeFile(_ data: Data, to url: URL) throws {
@@ -57,3 +65,16 @@ func writeFile(_ data: Data, to url: URL) throws {
5765
try data.write(to: url)
5866
try fileManager.setAttributes([.posixPermissions: 0o744], ofItemAtPath: url.path)
5967
}
68+
69+
// MARK: -
70+
71+
fileprivate extension URLComponents {
72+
func appendingPathComponent(_ component: String) -> URLComponents? {
73+
var urlComponents = self
74+
var pathComponents = urlComponents.path.split(separator: "/").map { "\($0)" }
75+
pathComponents.append(component)
76+
urlComponents.path = "/" + pathComponents.joined(separator: "/")
77+
78+
return urlComponents
79+
}
80+
}

0 commit comments

Comments
 (0)