1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
import Markdown
/// A directive that contains various metadata about a page.
///
/// This directive acts as a container for metadata and configuration without any arguments of its own.
///
/// ## Topics
///
/// ### Child Directives
///
/// - ``DocumentationExtension``
/// - ``TechnologyRoot``
/// - ``DisplayName``
/// - ``PageImage``
/// - ``PageColor``
/// - ``CallToAction``
/// - ``Availability``
/// - ``PageKind``
/// - ``SupportedLanguage``
/// - ``TitleHeading``
public final class Metadata: Semantic, AutomaticDirectiveConvertible {
public static let introducedVersion = "5.5"
public let originalMarkup: BlockDirective
/// Configuration that describes how this documentation extension file merges or overrides the in-source documentation.
@ChildDirective
var documentationOptions: DocumentationExtension? = nil
/// Configuration to make this page root-level documentation.
@ChildDirective
var technologyRoot: TechnologyRoot? = nil
/// Configuration to customize this page's symbol's display name.
@ChildDirective
var displayName: DisplayName? = nil
/// The optional, custom image used to represent this page.
@ChildDirective(requirements: .zeroOrMore)
var pageImages: [PageImage]
@ChildDirective(requirements: .zeroOrMore)
var customMetadata: [CustomMetadata]
@ChildDirective
var callToAction: CallToAction? = nil
@ChildDirective(requirements: .zeroOrMore)
var availability: [Availability]
@ChildDirective
var pageKind: PageKind? = nil
@ChildDirective(requirements: .zeroOrMore)
var supportedLanguages: [SupportedLanguage]
@ChildDirective
var _pageColor: PageColor? = nil
/// The optional, context-dependent color used to represent this page.
var pageColor: PageColor.Color? {
_pageColor?.color
}
@ChildDirective
var titleHeading: TitleHeading? = nil
@ChildDirective
var redirects: [Redirect]? = nil
static var keyPaths: [String : AnyKeyPath] = [
"documentationOptions" : \Metadata._documentationOptions,
"technologyRoot" : \Metadata._technologyRoot,
"displayName" : \Metadata._displayName,
"pageImages" : \Metadata._pageImages,
"customMetadata" : \Metadata._customMetadata,
"callToAction" : \Metadata._callToAction,
"availability" : \Metadata._availability,
"pageKind" : \Metadata._pageKind,
"supportedLanguages" : \Metadata._supportedLanguages,
"_pageColor" : \Metadata.__pageColor,
"titleHeading" : \Metadata._titleHeading,
"redirects" : \Metadata._redirects,
]
@available(*, deprecated, message: "Do not call directly. Required for 'AutomaticDirectiveConvertible'.")
init(originalMarkup: BlockDirective) {
self.originalMarkup = originalMarkup
}
func validate(source: URL?, for bundle: DocumentationBundle, in context: DocumentationContext, problems: inout [Problem]) -> Bool {
// Check that something is configured in the metadata block
if documentationOptions == nil && technologyRoot == nil && displayName == nil && pageImages.isEmpty && customMetadata.isEmpty && callToAction == nil && availability.isEmpty && pageKind == nil && pageColor == nil && titleHeading == nil && redirects == nil {
let diagnostic = Diagnostic(
source: source,
severity: .information,
range: originalMarkup.range,
identifier: "org.swift.docc.\(Metadata.directiveName).NoConfiguration",
summary: "\(Metadata.directiveName.singleQuoted) doesn't configure anything and has no effect"
)
let solutions = originalMarkup.range.map {
[Solution(summary: "Remove this \(Metadata.directiveName.singleQuoted) directive.", replacements: [Replacement(range: $0, replacement: "")])]
} ?? []
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: solutions))
}
// Check that there is only a single `@PageImage` directive for each supported purpose
var categorizedPageImages = [PageImage.Purpose : [PageImage]]()
for pageImage in pageImages {
categorizedPageImages[pageImage.purpose, default: []].append(pageImage)
}
for pageImages in categorizedPageImages.values {
guard pageImages.count > 1 else {
continue
}
for extraPageImage in pageImages {
let diagnostic = Diagnostic(
source: extraPageImage.originalMarkup.nameLocation?.source,
severity: .warning,
range: extraPageImage.originalMarkup.range,
identifier: "org.swift.docc.DuplicatePageImage",
summary: "Duplicate \(PageImage.directiveName.singleQuoted) directive with \(extraPageImage.purpose.rawValue.singleQuoted) purpose",
explanation: """
A documentation page can only contain a single \(PageImage.directiveName.singleQuoted) \
directive for each purpose.
"""
)
guard let range = extraPageImage.originalMarkup.range else {
problems.append(Problem(diagnostic: diagnostic))
continue
}
let solution = Solution(
summary: "Remove extraneous \(extraPageImage.purpose.rawValue.singleQuoted) \(PageImage.directiveName.singleQuoted) directive",
replacements: [
Replacement(range: range, replacement: "")
]
)
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: [solution]))
}
}
let categorizedAvailability = Dictionary(grouping: availability, by: \.platform)
for duplicateIntroduced in categorizedAvailability.values {
guard duplicateIntroduced.count > 1 else {
continue
}
for availability in duplicateIntroduced {
let diagnostic = Diagnostic(
source: availability.originalMarkup.nameLocation?.source,
severity: .warning,
range: availability.originalMarkup.range,
identifier: "org.swift.docc.\(Metadata.Availability.self).DuplicateIntroduced",
summary: "Duplicate \(Metadata.Availability.directiveName.singleQuoted) directive with 'introduced' argument",
explanation: """
A documentation page can only contain a single 'introduced' version for each platform.
"""
)
guard let range = availability.originalMarkup.range else {
problems.append(Problem(diagnostic: diagnostic))
continue
}
let solution = Solution(
summary: "Remove extraneous \(Metadata.Availability.directiveName.singleQuoted) directive",
replacements: [
Replacement(range: range, replacement: "")
]
)
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: [solution]))
}
}
return true
}
}
|