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

CommonMark output improvements #55

Merged
merged 6 commits into from
Apr 6, 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
14 changes: 12 additions & 2 deletions Sources/SwiftDoc/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ public struct SourceFile: Hashable, Codable {
}

override func visit(_ node: EnumCaseDeclSyntax) -> SyntaxVisitorContinueKind {
for `case` in Enumeration.Case.cases(from: node) {
let cases = node.elements.compactMap { element in
Enumeration.Case(element.withRawValue(nil))
}

for `case` in cases {
push(symbol(node, api: `case`))
}

return .skipChildren
}

Expand Down Expand Up @@ -179,9 +184,14 @@ public struct SourceFile: Hashable, Codable {
}

override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
for variable in Variable.variables(from: node) {
let variables = node.bindings.compactMap { binding in
Variable(binding.withInitializer(nil))
}

for variable in variables {
push(symbol(node, api: variable))
}

return .skipChildren
}

Expand Down
47 changes: 31 additions & 16 deletions Sources/swift-doc/Subcommands/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension SwiftDoc {
var inputs: [String]

@Option(name: [.long, .customShort("n")],
help: "The name of the module")
help: "The name of the module")
var moduleName: String

@Option(name: .shortAndLong,
Expand Down Expand Up @@ -47,15 +47,6 @@ extension SwiftDoc {

var pages: [String: Page] = [:]

switch format {
case .commonmark:
pages["Home"] = HomePage(module: module)
pages["_Sidebar"] = SidebarPage(module: module)
pages["_Footer"] = FooterPage()
case .html:
pages["Home"] = HomePage(module: module)
}

var globals: [String: [Symbol]] = [:]
for symbol in module.interface.topLevelSymbols.filter({ $0.isPublic }) {
switch symbol.api {
Expand All @@ -76,19 +67,43 @@ extension SwiftDoc {
pages[path(for: name)] = GlobalPage(module: module, name: name, symbols: symbols)
}

try pages.map { $0 }.parallelForEach {
guard !pages.isEmpty else { return }

if pages.count == 1, let page = pages.first?.value {
let filename: String
switch format {
case .commonmark:
filename = "\($0.key).md"
case .html where $0.key == "Home":
filename = "index.html"
filename = "Home.md"
case .html:
filename = "\($0.key)/index.html"
filename = "index.html"
}

let url = outputDirectoryURL.appendingPathComponent(filename)
try $0.value.write(to: url, format: format)
try page.write(to: url, format: format)
} else {
switch format {
case .commonmark:
pages["Home"] = HomePage(module: module)
pages["_Sidebar"] = SidebarPage(module: module)
pages["_Footer"] = FooterPage()
case .html:
pages["Home"] = HomePage(module: module)
}

try pages.map { $0 }.parallelForEach {
let filename: String
switch format {
case .commonmark:
filename = "\($0.key).md"
case .html where $0.key == "Home":
filename = "index.html"
case .html:
filename = "\($0.key)/index.html"
}

let url = outputDirectoryURL.appendingPathComponent(filename)
try $0.value.write(to: url, format: format)
}
}
} catch {
logger.error("\(error)")
Expand Down
11 changes: 8 additions & 3 deletions Sources/swift-doc/Supporting Types/Components/Members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ struct Members: Component {
ForEach(in: sections) { section -> BlockConvertible in
Section {
Heading { section.title }
ForEach(in: section.members) { member in
Heading { member.name }
Documentation(for: member, in: module)

Section {
ForEach(in: section.members) { member in
Heading {
Code { member.name }
}
Documentation(for: member, in: module)
}
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions Sources/swift-doc/Supporting Types/Pages/GlobalPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ struct GlobalPage: Page {

var document: CommonMark.Document {
return Document {
Heading { name }

Section {
ForEach(in: symbols) { symbol in
Heading { symbol.id.description }
Documentation(for: symbol, in: module)
}
ForEach(in: symbols) { symbol in
Heading { symbol.id.description }
Documentation(for: symbol, in: module)
}
}
}
Expand Down