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

Bugfix/relative links #127

Merged
merged 13 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#140 by @JaapWijnen.
- Fixed whitespace of code listings.
#144 by @mbrandonw.
- Fixed crash when attempting to generate paths with no base URL specified.
#127 by @mattpolzin, @kareman, and @mattt.

## [1.0.0-beta.3] - 2020-05-19

Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftDoc/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

public func route(for symbol: Symbol) -> String {
return route(for: symbol.id)
}

public func route(for name: CustomStringConvertible) -> String {
return name.description.replacingOccurrences(of: ".", with: "_")
}

public func path(for symbol: Symbol, with baseURL: String) -> String {
return path(for: route(for: symbol), with: baseURL)
}

public func path(for identifier: CustomStringConvertible, with baseURL: String) -> String {
let url = URL(string: baseURL)?.appendingPathComponent("\(identifier)") ?? URL(string: "\(identifier)")
guard let string = url?.absoluteString else {
fatalError("Unable to construct path for \(identifier) with baseURL \(baseURL)")
}

return string
}
1 change: 1 addition & 0 deletions Sources/swift-doc/Supporting Types/Layout.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftDoc
import HypertextLiteral
import Foundation

Expand Down
34 changes: 0 additions & 34 deletions Sources/swift-doc/Supporting Types/Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,7 @@ extension Page {
}
}

func route(for symbol: Symbol) -> String {
return route(for: symbol.id)
}

func route(for name: CustomStringConvertible) -> String {
return name.description.replacingOccurrences(of: ".", with: "_")
}

func path(for symbol: Symbol, with baseURL: String) -> String {
return path(for: route(for: symbol), with: baseURL)
}

func path(for identifier: CustomStringConvertible, with baseURL: String) -> String {
var urlComponents = URLComponents(string: baseURL)
urlComponents = urlComponents?.appendingPathComponent("\(identifier)")
guard let string = urlComponents?.string else {
logger.critical("Unable to construct path for \(identifier) with baseURL \(baseURL)")
fatalError()
}

return string
}

func writeFile(_ data: Data, to url: URL) throws {
let fileManager = FileManager.default
Expand All @@ -65,16 +44,3 @@ func writeFile(_ data: Data, to url: URL) throws {
try data.write(to: url)
try fileManager.setAttributes([.posixPermissions: 0o744], ofItemAtPath: url.path)
}

// MARK: -

fileprivate extension URLComponents {
func appendingPathComponent(_ component: String) -> URLComponents? {
var urlComponents = self
var pathComponents = urlComponents.path.split(separator: "/").map { "\($0)" }
pathComponents.append(component)
urlComponents.path = "/" + pathComponents.joined(separator: "/")

return urlComponents
}
}
47 changes: 47 additions & 0 deletions Tests/SwiftDocTests/PathTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest

import SwiftDoc

final class PathTests: XCTestCase {
func testEmptyBaseURL() {
XCTAssertEqual(path(for: "Class", with: ""), "Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: ""), "(lhs:rhs:)")
}

func testRootDirectoryBaseURL() {
XCTAssertEqual(path(for: "Class", with: "/"), "/Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: "/"), "/(lhs:rhs:)")
}

func testCurrentDirectoryBaseURL() {
XCTAssertEqual(path(for: "Class", with: "./"), "./Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: "./"), "./(lhs:rhs:)")
}

func testNestedSubdirectoryBaseURL() {
XCTAssertEqual(path(for: "Class", with: "/path/to/directory"), "/path/to/directory/Class")
XCTAssertEqual(path(for: "Class", with: "/path/to/directory/"), "/path/to/directory/Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: "/path/to/directory"), "/path/to/directory/(lhs:rhs:)")
XCTAssertEqual(path(for: "(lhs:rhs:)", with: "/path/to/directory/"), "/path/to/directory/(lhs:rhs:)")
}

func testDomainBaseURL() {
XCTAssertEqual(path(for: "Class", with: "https://example.com"), "https://example.com/Class")
XCTAssertEqual(path(for: "Class", with: "https://example.com/"), "https://example.com/Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: "https://example.com"), "https://example.com/(lhs:rhs:)")
XCTAssertEqual(path(for: "(lhs:rhs:)", with: "https://example.com/"), "https://example.com/(lhs:rhs:)")
}

func testDomainSubdirectoryBaseURL() {
XCTAssertEqual(path(for: "Class", with: "https://example.com/docs"), "https://example.com/docs/Class")
XCTAssertEqual(path(for: "Class", with: "https://example.com/docs/"), "https://example.com/docs/Class")

XCTAssertEqual(path(for: "(lhs:rhs:)", with: "https://example.com/docs"), "https://example.com/docs/(lhs:rhs:)")
XCTAssertEqual(path(for: "(lhs:rhs:)", with: "https://example.com/docs/"), "https://example.com/docs/(lhs:rhs:)")
}
}
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ inputs:
format:
description: "The output format (commonmark or html)"
default: "commonmark"
base-url:
description: "The base URL for all relative URLs generated in documents"
required: false
default: "./"
output:
description: "The path for generated output"
required: true
Expand All @@ -26,6 +30,8 @@ runs:
"${{ inputs.inputs }}",
--format,
"${{ inputs.format }}",
--base-url,
"${{ inputs.base-url }}",
--module-name,
"${{ inputs.module-name }}",
--output,
Expand Down