Skip to content
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
94 changes: 71 additions & 23 deletions Sources/SwiftDocC/Infrastructure/DocumentationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
tutorials: [SemanticResult<Tutorial>],
tutorialArticles: [SemanticResult<TutorialArticle>],
bundle: DocumentationBundle) {

let sourceLanguages = soleRootModuleReference?.sourceLanguages ?? [.swift]

// Technologies

for technologyResult in technologies {
Expand All @@ -621,15 +624,30 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
// Add to document map
documentLocationMap[url] = technologyResult.topicGraphNode.reference

let sourceLanguage = SourceLanguage.swift
let node = DocumentationNode(reference: technologyResult.topicGraphNode.reference, kind: .technology, sourceLanguage: sourceLanguage, name: .conceptual(title: technology.intro.title), markup: technology.originalMarkup, semantic: technology)
documentationCache[technologyResult.topicGraphNode.reference] = node
let technologyReference = technologyResult.topicGraphNode.reference.withSourceLanguages(sourceLanguages)

let technologyNode = DocumentationNode(
reference: technologyReference,
kind: .technology,
sourceLanguage: Self.defaultLanguage(in: sourceLanguages),
availableSourceLanguages: sourceLanguages,
name: .conceptual(title: technology.intro.title),
markup: technology.originalMarkup,
semantic: technology
)
documentationCache[technologyReference] = technologyNode

// Update the reference in the topic graph with the technology's available languages.
topicGraph.updateReference(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This looks great- much easier for me to follow.

technologyResult.topicGraphNode.reference,
newReference: technologyReference
)

let anonymousVolumeName = "$volume"

for volume in technology.volumes {
// Graph node: Volume
let volumeReference = node.reference.appendingPath(volume.name ?? anonymousVolumeName)
let volumeReference = technologyNode.reference.appendingPath(volume.name ?? anonymousVolumeName)
let volumeNode = TopicGraph.Node(reference: volumeReference, kind: .volume, source: .file(url: url), title: volume.name ?? anonymousVolumeName)
topicGraph.addNode(volumeNode)

Expand All @@ -640,7 +658,7 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
// Graph node: Module
let baseNodeReference: ResolvedTopicReference
if volume.name == nil {
baseNodeReference = node.reference
baseNodeReference = technologyNode.reference
} else {
baseNodeReference = volumeNode.reference
}
Expand Down Expand Up @@ -678,8 +696,24 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
// Add to document map
documentLocationMap[url] = tutorialResult.topicGraphNode.reference

let tutorialNode = DocumentationNode(reference: tutorialResult.topicGraphNode.reference, kind: .tutorial, sourceLanguage: tutorialResult.topicGraphNode.reference.sourceLanguage, name: .conceptual(title: tutorial.intro.title), markup: tutorial.originalMarkup, semantic: tutorial)
documentationCache[tutorialResult.topicGraphNode.reference] = tutorialNode
let tutorialReference = tutorialResult.topicGraphNode.reference.withSourceLanguages(sourceLanguages)

let tutorialNode = DocumentationNode(
reference: tutorialReference,
kind: .tutorial,
sourceLanguage: Self.defaultLanguage(in: sourceLanguages),
availableSourceLanguages: sourceLanguages,
name: .conceptual(title: tutorial.intro.title),
markup: tutorial.originalMarkup,
semantic: tutorial
)
documentationCache[tutorialReference] = tutorialNode

// Update the reference in the topic graph with the tutorial's available languages.
topicGraph.updateReference(
tutorialResult.topicGraphNode.reference,
newReference: tutorialReference
)
}
}

Expand All @@ -695,9 +729,25 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {

// Add to document map
documentLocationMap[url] = articleResult.topicGraphNode.reference

let articleReference = articleResult.topicGraphNode.reference.withSourceLanguages(sourceLanguages)

let articleNode = DocumentationNode(
reference: articleReference,
kind: .tutorialArticle,
sourceLanguage: Self.defaultLanguage(in: sourceLanguages),
availableSourceLanguages: sourceLanguages,
name: .conceptual(title: article.title ?? ""),
markup: article.originalMarkup,
semantic: article
)
documentationCache[articleReference] = articleNode

let articleNode = DocumentationNode(reference: articleResult.topicGraphNode.reference, kind: .tutorialArticle, sourceLanguage: articleResult.topicGraphNode.reference.sourceLanguage, name: .conceptual(title: article.title ?? ""), markup: article.originalMarkup, semantic: article)
documentationCache[articleResult.topicGraphNode.reference] = articleNode
// Update the reference in the topic graph with the article's available languages.
topicGraph.updateReference(
articleResult.topicGraphNode.reference,
newReference: articleReference
)
}
}

Expand Down Expand Up @@ -1722,13 +1772,7 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {

// If available source languages are provided and it contains Swift, use Swift as the default language of
// the article.
let defaultSourceLanguage = availableSourceLanguages.map { availableSourceLanguages in
if availableSourceLanguages.contains(.swift) {
return .swift
} else {
return availableSourceLanguages.first ?? .swift
}
} ?? SourceLanguage.swift
let defaultSourceLanguage = defaultLanguage(in: availableSourceLanguages)

let reference = ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
Expand Down Expand Up @@ -2417,13 +2461,7 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
///
/// Adds any non-resolving reference to the `allCandidateURLs` collection.
func attemptToResolve(_ reference: ResolvedTopicReference) -> TopicReferenceResolutionResult? {
if topicGraph.nodeWithReference(reference) != nil {
let resolved = ResolvedTopicReference(
bundleIdentifier: referenceBundleIdentifier,
path: reference.url.path,
fragment: reference.fragment,
sourceLanguages: reference.sourceLanguages
)
if let resolved = topicGraph.nodeWithReference(reference)?.reference {
// If resolving a symbol link, only match symbol nodes.
if isCurrentlyResolvingSymbolLink && !(documentationCache[resolved]?.semantic is Symbol) {
allCandidateURLs.append(reference.url)
Expand Down Expand Up @@ -2698,6 +2736,16 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
}
.joined()
}

private static func defaultLanguage(in sourceLanguages: Set<SourceLanguage>?) -> SourceLanguage {
sourceLanguages.map { sourceLanguages in
if sourceLanguages.contains(.swift) {
return .swift
} else {
return sourceLanguages.first ?? .swift
}
} ?? SourceLanguage.swift
}
}

// MARK: - DocumentationCurator
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftDocC/Infrastructure/Topic Graph/TopicGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ struct TopicGraph {
self.isResolvable = isResolvable
}

func withReference(_ reference: ResolvedTopicReference) -> Node {
Node(
reference: reference,
kind: kind,
source: source,
title: title
)
}

func hash(into hasher: inout Hasher) {
hasher.combine(reference)
}
Expand Down Expand Up @@ -166,6 +175,11 @@ struct TopicGraph {
}
}

/// Updates the node with the given reference with a new reference.
mutating func updateReference(_ reference: ResolvedTopicReference, newReference: ResolvedTopicReference) {
nodes[reference] = nodes[reference]?.withReference(newReference)
}

/// Adds a topic edge but it doesn't verify if the nodes exist for the given references.
/// > Warning: If the references don't match already existing nodes this operation might corrupt the topic graph.
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public class DocumentationContentRenderer {
}

// Collect the reference dependencies.
dependencies.topicReferences = contentCompiler.collectedTopicReferences
dependencies.topicReferences = Array(contentCompiler.collectedTopicReferences)
dependencies.linkReferences = Array(contentCompiler.linkReferences.values)

let isRequired = (node?.semantic as? Symbol)?.isRequired ?? false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct RenderContentCompiler: MarkupVisitor {
var identifier: ResolvedTopicReference
var imageReferences: [String: ImageReference] = [:]
/// Resolved topic references that were seen by the visitor. These should be used to populate the references dictionary.
var collectedTopicReferences: [ResolvedTopicReference] = []
var collectedTopicReferences = GroupedSequence<String, ResolvedTopicReference> { $0.absoluteString }
var linkReferences: [String: LinkReference] = [:]

init(context: DocumentationContext, bundle: DocumentationBundle, identifier: ResolvedTopicReference) {
Expand Down
Loading