|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import SwiftDocC |
| 13 | + |
| 14 | +extension MergeAction { |
| 15 | + struct RootRenderReferences { |
| 16 | + var documentation, tutorials: [TopicRenderReference] |
| 17 | + |
| 18 | + fileprivate var all: [TopicRenderReference] { |
| 19 | + documentation + tutorials |
| 20 | + } |
| 21 | + var isEmpty: Bool { |
| 22 | + documentation.isEmpty && tutorials.isEmpty |
| 23 | + } |
| 24 | + fileprivate var containsBothKinds: Bool { |
| 25 | + !documentation.isEmpty && !tutorials.isEmpty |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + func readRootNodeRenderReferencesIn(dataDirectory: URL) throws -> RootRenderReferences { |
| 30 | + func inner(url: URL) throws -> [TopicRenderReference] { |
| 31 | + try fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []) |
| 32 | + .compactMap { |
| 33 | + guard $0.pathExtension == "json" else { |
| 34 | + return nil |
| 35 | + } |
| 36 | + |
| 37 | + let data = try fileManager.contents(of: $0) |
| 38 | + return try JSONDecoder().decode(RootNodeRenderReference.self, from: data) |
| 39 | + .renderReference |
| 40 | + } |
| 41 | + .sorted(by: { lhs, rhs in |
| 42 | + lhs.title < rhs.title |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + return .init( |
| 47 | + documentation: try inner(url: dataDirectory.appendingPathComponent("documentation", isDirectory: true)), |
| 48 | + tutorials: try inner(url: dataDirectory.appendingPathComponent("tutorials", isDirectory: true)) |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + func makeSynthesizedLandingPage( |
| 53 | + name: String, |
| 54 | + reference: ResolvedTopicReference, |
| 55 | + roleHeading: String, |
| 56 | + topicsStyle: TopicsVisualStyle.Style, |
| 57 | + rootRenderReferences: RootRenderReferences |
| 58 | + ) -> RenderNode { |
| 59 | + var renderNode = RenderNode(identifier: reference, kind: .article) |
| 60 | + |
| 61 | + renderNode.topicSectionsStyle = switch topicsStyle { |
| 62 | + case .list: .list |
| 63 | + case .compactGrid: .compactGrid |
| 64 | + case .detailedGrid: .detailedGrid |
| 65 | + case .hidden: .hidden |
| 66 | + } |
| 67 | + renderNode.metadata.title = name |
| 68 | + renderNode.metadata.roleHeading = roleHeading |
| 69 | + renderNode.metadata.role = "collection" |
| 70 | + renderNode.hierarchy = nil |
| 71 | + renderNode.sections = [] |
| 72 | + |
| 73 | + if rootRenderReferences.containsBothKinds { |
| 74 | + // If the combined archive contains both documentation and tutorial content, create separate topic sections for each. |
| 75 | + renderNode.topicSections = [ |
| 76 | + .init(title: "Modules", abstract: nil, discussion: nil, identifiers: rootRenderReferences.documentation.map(\.identifier.identifier)), |
| 77 | + .init(title: "Tutorials", abstract: nil, discussion: nil, identifiers: rootRenderReferences.tutorials.map(\.identifier.identifier)), |
| 78 | + ] |
| 79 | + } else { |
| 80 | + // Otherwise, create a single unnamed topic section |
| 81 | + renderNode.topicSections = [ |
| 82 | + .init(title: nil, abstract: nil, discussion: nil, identifiers: (rootRenderReferences.all).map(\.identifier.identifier)), |
| 83 | + ] |
| 84 | + } |
| 85 | + |
| 86 | + for renderReference in rootRenderReferences.documentation { |
| 87 | + renderNode.references[renderReference.identifier.identifier] = renderReference |
| 88 | + } |
| 89 | + for renderReference in rootRenderReferences.tutorials { |
| 90 | + renderNode.references[renderReference.identifier.identifier] = renderReference |
| 91 | + } |
| 92 | + |
| 93 | + return renderNode |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// A type that decodes the root node reference from a root node's encoded render node data. |
| 98 | +private struct RootNodeRenderReference: Decodable { |
| 99 | + /// The decoded root node render reference |
| 100 | + var renderReference: TopicRenderReference |
| 101 | + |
| 102 | + enum CodingKeys: CodingKey { |
| 103 | + // The only render node keys that should be needed |
| 104 | + case identifier, references |
| 105 | + // Extra render node keys in case we need to re-create the render reference from page content. |
| 106 | + case metadata, abstract, kind |
| 107 | + } |
| 108 | + |
| 109 | + struct StringCodingKey: CodingKey { |
| 110 | + var stringValue: String |
| 111 | + init?(stringValue: String) { |
| 112 | + self.stringValue = stringValue |
| 113 | + } |
| 114 | + var intValue: Int? = nil |
| 115 | + init?(intValue: Int) { |
| 116 | + fatalError("`SparseRenderNode.StringCodingKey` only support string values") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + init(from decoder: any Decoder) throws { |
| 121 | + // Instead of decoding the full render node, we only decode the information that's needed. |
| 122 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 123 | + |
| 124 | + let identifier = try container.decode(ResolvedTopicReference.self, forKey: .identifier) |
| 125 | + let rawIdentifier = identifier.url.absoluteString |
| 126 | + |
| 127 | + // Every node should include a reference to the root page. |
| 128 | + // For reference documentation, this is because the root appears as a link in the breadcrumbs on every page. |
| 129 | + // For tutorials, this is because the tutorial table of content appears as a link in the top navigator. |
| 130 | + // |
| 131 | + // If the root page has a reference to itself, then that the fastest and easiest way to access the correct topic render reference. |
| 132 | + if container.contains(.references) { |
| 133 | + let referencesContainer = try container.nestedContainer(keyedBy: StringCodingKey.self, forKey: .references) |
| 134 | + if let selfReference = try referencesContainer.decodeIfPresent(TopicRenderReference.self, forKey: .init(stringValue: rawIdentifier)!) { |
| 135 | + renderReference = selfReference |
| 136 | + return |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // If for some unexpected reason this wasn't true, for example because of an unknown page kind, |
| 141 | + // we can create a new topic reference by decoding a little bit more information from the render node. |
| 142 | + let metadata = try container.decode(RenderMetadata.self, forKey: .metadata) |
| 143 | + |
| 144 | + renderReference = TopicRenderReference( |
| 145 | + identifier: RenderReferenceIdentifier(rawIdentifier), |
| 146 | + title: metadata.title ?? identifier.lastPathComponent, |
| 147 | + abstract: try container.decodeIfPresent([RenderInlineContent].self, forKey: .abstract) ?? [], |
| 148 | + url: identifier.path.lowercased(), |
| 149 | + kind: try container.decode(RenderNode.Kind.self, forKey: .kind), |
| 150 | + images: metadata.images |
| 151 | + ) |
| 152 | + } |
| 153 | +} |
0 commit comments