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

Commit 12d27b0

Browse files
committed
perform some path sanitization over the generated names
The current scheme for the file generation uses the function label, which uses ':' as a parameter separator. However, this character is not valid on all file systems. This results in partial content generation on Windows. Add a sanitization step which replaces "invalid" characters with `_` instead. This does not impact the rendering, only the filenames and the generated URL references between the pages. With this, we can generate and link function documentation on Windows.
1 parent 75108dd commit 12d27b0

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Sources/SwiftDoc/Helpers.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ public func path(for symbol: Symbol, with baseURL: String) -> String {
1313
}
1414

1515
public func path(for identifier: CustomStringConvertible, with baseURL: String) -> String {
16-
let url = URL(string: baseURL)?.appendingPathComponent("\(identifier)") ?? URL(string: "\(identifier)")
16+
let tail: String = path(for: "\(identifier)")
17+
let url = URL(string: baseURL)?.appendingPathComponent(tail) ?? URL(string: tail)
1718
guard let string = url?.absoluteString else {
1819
fatalError("Unable to construct path for \(identifier) with baseURL \(baseURL)")
1920
}
2021

2122
return string
2223
}
24+
25+
public func path(for identifier: String) -> String {
26+
let kReservedCharacters: CharacterSet = [
27+
// Windows Reserved Characters
28+
"<", ">", ":", "\"", "/", "\\", "|", "?", "*",
29+
]
30+
return identifier.components(separatedBy: kReservedCharacters).joined(separator: "_")
31+
}

Sources/swift-doc/Subcommands/Generate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ extension SwiftDoc {
121121
let filename: String
122122
switch format {
123123
case .commonmark:
124-
filename = "\($0.key).md"
124+
filename = "\(path(for: $0.key)).md"
125125
case .html where $0.key == "Home":
126126
filename = "index.html"
127127
case .html:
128-
filename = "\($0.key)/index.html"
128+
filename = "\(path(for: $0.key))/index.html"
129129
}
130130

131131
let url = outputDirectoryURL.appendingPathComponent(filename)

0 commit comments

Comments
 (0)