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

Add baseUrl option #65

Merged
merged 6 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `--base-url` option.
#65 by @kean.

## [1.0.0-beta.2] - 2020-04-08

### Changed
Expand Down
9 changes: 7 additions & 2 deletions Sources/swift-doc/Subcommands/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ extension SwiftDoc {
default: .commonmark,
help: "The output format")
var format: Format

@Option(name: .customLong("base-url"),
default: "/",
help: "The base URL used for all relative URLs in generated documents.")
var baseURL: String
}

static var configuration = CommandConfiguration(abstract: "Generates Swift documentation")
Expand Down Expand Up @@ -79,7 +84,7 @@ extension SwiftDoc {
}

let url = outputDirectoryURL.appendingPathComponent(filename)
try page.write(to: url, format: format)
try page.write(to: url, format: format, baseURL: options.baseURL)
} else {
switch format {
case .commonmark:
Expand All @@ -102,7 +107,7 @@ extension SwiftDoc {
}

let url = outputDirectoryURL.appendingPathComponent(filename)
try $0.value.write(to: url, format: format)
try $0.value.write(to: url, format: format, baseURL: options.baseURL)
}
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct Relationships: Component {
"""#
} else {
return #"""
<dt class="\#(descriptor)"><code><a href="/\#(path(for: symbol))">\#(symbol.id)</a></code></dt>
<dt class="\#(descriptor)"><code><a href="\#(path(for: symbol))">\#(symbol.id)</a></code></dt>
<dd>\#(commonmark: symbol.documentation?.summary ?? "")</dd>
"""#
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-doc/Supporting Types/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public func linkCodeElements(of html: String, for symbol: Symbol, in module: Mod
let candidate = candidates.filter({ $0 != symbol }).first
{
let a = Element(name: "a")
a["href"] = "/" + path(for: candidate)
a["href"] = path(for: candidate)
element.wrap(inside: a)
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/swift-doc/Supporting Types/Layout.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HypertextLiteral
import Foundation

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

return #"""
Expand All @@ -11,13 +11,14 @@ func layout(_ page: Page) -> HTML {
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>\#(page.module.name) - \#(page.title)</title>
<base href="\#(baseURL)"/>
<style type="text/css">
\#(unsafeUnescaped: css)
</style>
</head>
<body>
<header>
<a href="/">
<a href="\#(baseURL)">
<strong>
\#(page.module.name)
</strong>
Expand Down
4 changes: 2 additions & 2 deletions Sources/swift-doc/Supporting Types/Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ extension Page {
}

extension Page {
func write(to url: URL, format: SwiftDoc.Generate.Format) throws {
func write(to url: URL, format: SwiftDoc.Generate.Format, baseURL: String) throws {
let data: Data?
switch format {
case .commonmark:
data = document.render(format: .commonmark).data(using: .utf8)
case .html:
data = layout(self).description.data(using: .utf8)
data = layout(self, baseURL: baseURL).description.data(using: .utf8)
}

let fileManager = FileManager.default
Expand Down