Skip to content

Commit f724095

Browse files
committed
Add @SupportedLanguage directive for articles
Adds support for the `@SupportedLanguage` directive, which can be used in article files to indicate what languages an article is available in, rather than using the languages that the module being documented is available in. rdar://102091191
1 parent bbdebdd commit f724095

File tree

19 files changed

+2613
-2018
lines changed

19 files changed

+2613
-2018
lines changed

Sources/SwiftDocC/Infrastructure/DocumentationContext.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -1789,9 +1789,9 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
17891789
articles.map { article in
17901790
guard let (documentation, title) = DocumentationContext.documentationNodeAndTitle(
17911791
for: article,
1792-
1793-
// Articles are available in the same languages the only root module is available in. If there is more
1794-
// than one module, we cannot determine what languages it's available in and default to Swift.
1792+
// By default, articles are available in the languages the module that's being documented
1793+
// is available in. It's possible to override that behavior using the `@SupportedLanguage`
1794+
// directive though; see its documentation for more details.
17951795
availableSourceLanguages: soleRootModuleReference.map { sourceLanguages(for: $0) },
17961796
kind: .article,
17971797
in: bundle
@@ -1839,6 +1839,19 @@ public class DocumentationContext: DocumentationContextDataProviderDelegate {
18391839

18401840
let path = NodeURLGenerator.pathForSemantic(article.value, source: article.source, bundle: bundle)
18411841

1842+
// Use the languages specified by the `@SupportedLanguage` directives if present.
1843+
let availableSourceLanguages = article.value
1844+
.metadata
1845+
.flatMap { metadata in
1846+
let languages = Set(
1847+
metadata.supportedLanguages
1848+
.map(\.language)
1849+
)
1850+
1851+
return languages.isEmpty ? nil : languages
1852+
}
1853+
?? availableSourceLanguages
1854+
18421855
// If available source languages are provided and it contains Swift, use Swift as the default language of
18431856
// the article.
18441857
let defaultSourceLanguage = defaultLanguage(in: availableSourceLanguages)

Sources/SwiftDocC/Model/SourceLanguage.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -133,6 +133,7 @@ public struct SourceLanguage: Hashable, Codable {
133133
id: "occ",
134134
idAliases: [
135135
"objective-c",
136+
"objc",
136137
"c", // FIXME: DocC should display C as its own language (github.com/apple/swift-docc/issues/169).
137138
],
138139
linkDisambiguationID: "c"

Sources/SwiftDocC/Semantics/Metadata/Metadata.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -26,6 +26,7 @@ import Markdown
2626
/// - ``CallToAction``
2727
/// - ``Availability``
2828
/// - ``PageKind``
29+
/// - ``SupportedLanguage``
2930
public final class Metadata: Semantic, AutomaticDirectiveConvertible {
3031
public let originalMarkup: BlockDirective
3132

@@ -57,6 +58,9 @@ public final class Metadata: Semantic, AutomaticDirectiveConvertible {
5758
@ChildDirective
5859
var pageKind: PageKind? = nil
5960

61+
@ChildDirective(requirements: .zeroOrMore)
62+
var supportedLanguages: [SupportedLanguage]
63+
6064
static var keyPaths: [String : AnyKeyPath] = [
6165
"documentationOptions" : \Metadata._documentationOptions,
6266
"technologyRoot" : \Metadata._technologyRoot,
@@ -66,6 +70,7 @@ public final class Metadata: Semantic, AutomaticDirectiveConvertible {
6670
"callToAction" : \Metadata._callToAction,
6771
"availability" : \Metadata._availability,
6872
"pageKind" : \Metadata._pageKind,
73+
"supportedLanguages" : \Metadata._supportedLanguages,
6974
]
7075

7176
/// Creates a metadata object with a given markup, documentation extension, and technology root.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2023 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 Markdown
13+
14+
/// A directive that controls what programming languages an article is available in.
15+
///
16+
/// By default, an article is available in the languages the module that's being documented is available in. Use
17+
/// this directive to override this behavior in a ``Metadata`` directive:
18+
///
19+
/// ```
20+
/// @Metadata {
21+
/// @SupportedLanguage(swift)
22+
/// @SupportedLanguage(objc)
23+
/// }
24+
/// ```
25+
///
26+
/// This directive supports any language identifier, but only the following are currently supported
27+
/// by Swift-DocC Render:
28+
///
29+
/// | Identifier | Language |
30+
/// | --------------------------------- | ----------------------|
31+
/// | `swift` | Swift |
32+
/// | `objc`, `objective-c` | Objective-C |
33+
public final class SupportedLanguage: Semantic, AutomaticDirectiveConvertible {
34+
public let originalMarkup: BlockDirective
35+
36+
/// A source language that this symbol is available in.
37+
///
38+
/// For supported values, see ``SupportedLanguage``.
39+
@DirectiveArgumentWrapped(
40+
name: .unnamed,
41+
parseArgument: { _, argumentValue in
42+
SourceLanguage(knownLanguageIdentifier: argumentValue)
43+
?? SourceLanguage(id: argumentValue)
44+
}
45+
)
46+
public var language: SourceLanguage
47+
48+
static var keyPaths: [String : AnyKeyPath] = [
49+
"language": \SupportedLanguage._language,
50+
]
51+
52+
@available(*, deprecated,
53+
message: "Do not call directly. Required for 'AutomaticDirectiveConvertible'."
54+
)
55+
init(originalMarkup: BlockDirective) {
56+
self.originalMarkup = originalMarkup
57+
}
58+
}

Sources/SwiftDocC/Utility/MarkupExtensions/BlockDirectiveExtensions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -40,6 +40,7 @@ extension BlockDirective {
4040
Stack.directiveName,
4141
Step.directiveName,
4242
Steps.directiveName,
43+
SupportedLanguage.directiveName,
4344
Technology.directiveName,
4445
TechnologyRoot.directiveName,
4546
TechnologyRoot.directiveName,

0 commit comments

Comments
 (0)