-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update LinkDestinationSummary to support multi-language content #54
Changes from 10 commits
0eafe44
93dbb4d
76dd568
b729677
145343f
e6196d9
2cc7169
347dd84
c68d31a
afaa56e
461d404
3994142
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -71,13 +71,15 @@ import SymbolKit | |||||
/// - In a paragraph of text, a link to this element will use the ``title`` as the link text and style the tile in code font if the ``kind`` is a type of symbol. | ||||||
/// - In a task group, the the ``title`` and ``abstract-swift.property`` is displayed together to give more context about this element and the element may be marked as deprecated | ||||||
/// based on the values of its ``platforms`` and other metadata about the current versions of the platforms. | ||||||
/// | ||||||
/// The summary may include content that vary based on the source language. The content that is different in another source language is specified in a ``Variant``. Any property on the variant that is `nil` has the same value as the summarized element's value. | ||||||
public struct LinkDestinationSummary: Codable, Equatable { | ||||||
/// The traits of the summarized element. | ||||||
let traits: [RenderNode.Variant.Trait] | ||||||
|
||||||
/// The kind of the summarized element. | ||||||
public let kind: DocumentationNode.Kind | ||||||
|
||||||
/// The language of the summarized element. | ||||||
public let language: SourceLanguage | ||||||
|
||||||
/// The relative path to this element. | ||||||
public let path: String | ||||||
|
||||||
|
@@ -142,6 +144,51 @@ public struct LinkDestinationSummary: Codable, Equatable { | |||||
/// | ||||||
/// A web server can use this list of URLs to redirect to the current URL. | ||||||
public let redirects: [URL]? | ||||||
|
||||||
/// A variant of content for a summarized element. | ||||||
/// | ||||||
/// - Note: All properties except for ``traits`` are optional. If a property is `nil` it means that the value is the same as the summarized element's value. | ||||||
public struct Variant: Codable, Equatable { | ||||||
/// The traits of the variant. | ||||||
public let traits: [RenderNode.Variant.Trait] | ||||||
|
||||||
/// A wrapper for variant values that are either set (the variant has a custom value) not set (the variant have the same value as the summarized element) | ||||||
/// | ||||||
/// This alias is used to make the property declarations more explicit while at the same time offering the convenient syntax of optionals. | ||||||
public typealias VariantValue = Optional | ||||||
|
||||||
/// The kind of the variant or `nil` if the kind is the same as the summarized element. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use .none here maybe? To differentiate between the wrapper and the inner value a little more.
Suggested change
|
||||||
public let kind: VariantValue<DocumentationNode.Kind> | ||||||
|
||||||
/// The relative path of the variant or `nil` if the relative is the same as the summarized element. | ||||||
public let path: VariantValue<String> | ||||||
|
||||||
/// The title of the variant or `nil` if the title is the same as the summarized element. | ||||||
public let title: VariantValue<String?> | ||||||
|
||||||
/// The abstract of the variant or `nil` if the abstract is the same as the summarized element. | ||||||
/// | ||||||
/// - Note: If the summarized element has an abstract but the variant doesn't, this property will be `Optional.some(nil)`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Note's render kind of strangely if they're not embedded in other content. It might be better to just make this a regular discussion.
Suggested change
|
||||||
public let abstract: VariantValue<Abstract?> | ||||||
|
||||||
/// The taskGroups of the variant or `nil` if the taskGroups is the same as the summarized element. | ||||||
/// | ||||||
/// - Note: If the summarized element has task groups but the variant doesn't, this property will be `Optional.some(nil)`. | ||||||
public let taskGroups: VariantValue<[TaskGroup]?> | ||||||
|
||||||
/// The precise symbol identifier of the variant or `nil` if the precise symbol identifier is the same as the summarized element. | ||||||
/// | ||||||
/// - Note: If the summarized element has a precise symbol identifier but the variant doesn't, this property will be `Optional.some(nil)`. | ||||||
public let usr: VariantValue<String?> | ||||||
|
||||||
/// The declaration of the variant or `nil` if the declaration is the same as the summarized element. | ||||||
/// | ||||||
/// - Note: If the summarized element has a declaration but the variant doesn't, this property will be `Optional.some(nil)`. | ||||||
public let declarationFragments: VariantValue<DeclarationFragments?> | ||||||
} | ||||||
|
||||||
/// The variants of content (kind, title, abstract, path, urs, declaration, and task groups) for this summarized element. | ||||||
public let variants: [Variant] | ||||||
} | ||||||
|
||||||
// MARK: - Accessing the externally linkable elements | ||||||
|
@@ -205,25 +252,22 @@ extension LinkDestinationSummary { | |||||
/// - taskGroups: The task groups that lists the children of this page. | ||||||
/// - compiler: The content compiler that's used to render the node's abstract. | ||||||
init(documentationNode: DocumentationNode, path: String, taskGroups: [TaskGroup], platforms: [PlatformAvailability]?, compiler: inout RenderContentCompiler) { | ||||||
let declaration = (documentationNode.semantic as? Symbol)?.subHeading.map { declaration in | ||||||
return declaration.map { fragment in | ||||||
DeclarationRenderSection.Token(fragment: fragment, identifier: nil) | ||||||
} | ||||||
} | ||||||
|
||||||
let symbol = documentationNode.semantic as? Symbol | ||||||
|
||||||
self.init( | ||||||
traits: [.interfaceLanguage(documentationNode.sourceLanguage.id)], | ||||||
kind: documentationNode.kind, | ||||||
language: documentationNode.sourceLanguage, | ||||||
path: path, | ||||||
referenceURL: documentationNode.reference.url, | ||||||
title: ReferenceResolver.title(forNode: documentationNode), | ||||||
abstract: (documentationNode.semantic as? Abstracted)?.renderedAbstract(using: &compiler), | ||||||
availableLanguages: documentationNode.availableSourceLanguages, | ||||||
platforms: platforms, | ||||||
taskGroups: taskGroups, | ||||||
usr: (documentationNode.semantic as? Symbol)?.externalID, | ||||||
declarationFragments: declaration, | ||||||
redirects: (documentationNode.semantic as? Redirected)?.redirects?.map { $0.oldPath } | ||||||
usr: symbol?.externalID, | ||||||
declarationFragments: symbol?.subHeading?.map { .init(fragment: $0, identifier: nil) }, | ||||||
redirects: (documentationNode.semantic as? Redirected)?.redirects?.map { $0.oldPath }, | ||||||
variants: [] | ||||||
) | ||||||
} | ||||||
} | ||||||
|
@@ -240,18 +284,18 @@ extension LinkDestinationSummary { | |||||
init(landmark: Landmark, basePath: String, page: DocumentationNode, platforms: [PlatformAvailability]?, compiler: inout RenderContentCompiler) { | ||||||
let anchor = urlReadableFragment(landmark.title) | ||||||
|
||||||
let abstract: Abstract | ||||||
let abstract: Abstract? | ||||||
if let abstracted = landmark as? Abstracted { | ||||||
abstract = abstracted.renderedAbstract(using: &compiler) ?? [] | ||||||
} else if let paragraph = landmark.markup.children.lazy.compactMap({ $0 as? Paragraph }).first, case RenderBlockContent.paragraph(let inlineContent)? = compiler.visitParagraph(paragraph).first { | ||||||
abstract = inlineContent | ||||||
} else { | ||||||
abstract = [] | ||||||
abstract = nil | ||||||
} | ||||||
|
||||||
self.init( | ||||||
traits: [.interfaceLanguage(page.sourceLanguage.id)], | ||||||
kind: .onPageLandmark, | ||||||
language: page.sourceLanguage, | ||||||
path: basePath + "#\(anchor)", // use an in-page anchor for the landmark's path | ||||||
referenceURL: page.reference.withFragment(anchor).url, | ||||||
title: landmark.title, | ||||||
|
@@ -261,7 +305,8 @@ extension LinkDestinationSummary { | |||||
taskGroups: [], // Landmarks have no children | ||||||
usr: nil, // Only symbols have a USR | ||||||
declarationFragments: nil, // Only symbols have declarations | ||||||
redirects: (landmark as? Redirected)?.redirects?.map { $0.oldPath } | ||||||
redirects: (landmark as? Redirected)?.redirects?.map { $0.oldPath }, | ||||||
variants: [] | ||||||
) | ||||||
} | ||||||
} | ||||||
|
@@ -271,24 +316,31 @@ extension LinkDestinationSummary { | |||||
// Add Codable methods—which include an initializer—in an extension so that it doesn't override the member-wise initializer. | ||||||
extension LinkDestinationSummary { | ||||||
enum CodingKeys: String, CodingKey { | ||||||
case kind, path, referenceURL, title, abstract, language, taskGroups, usr, availableLanguages, platforms, redirects | ||||||
case traits, kind, path, referenceURL, title, abstract, taskGroups, usr, availableLanguages, platforms, redirects, variants | ||||||
case declarationFragments = "fragments" | ||||||
} | ||||||
|
||||||
enum LegacyCodingKeys: String, CodingKey { | ||||||
case language | ||||||
} | ||||||
|
||||||
public func encode(to encoder: Encoder) throws { | ||||||
var container = encoder.container(keyedBy: CodingKeys.self) | ||||||
try container.encode(traits, forKey: .traits) | ||||||
try container.encode(referenceURL, forKey: .referenceURL) | ||||||
try container.encode(kind.id, forKey: .kind) | ||||||
try container.encode(path, forKey: .path) | ||||||
try container.encode(referenceURL, forKey: .referenceURL) | ||||||
try container.encode(title, forKey: .title) | ||||||
try container.encodeIfPresent(abstract, forKey: .abstract) | ||||||
try container.encode(language.id, forKey: .language) | ||||||
try container.encode(availableLanguages.map { $0.id }, forKey: .availableLanguages) | ||||||
try container.encodeIfPresent(platforms, forKey: .platforms) | ||||||
try container.encodeIfPresent(taskGroups, forKey: .taskGroups) | ||||||
try container.encodeIfPresent(usr, forKey: .usr) | ||||||
try container.encodeIfPresent(declarationFragments, forKey: .declarationFragments) | ||||||
try container.encodeIfPresent(redirects, forKey: .redirects) | ||||||
if !variants.isEmpty { | ||||||
try container.encode(variants, forKey: .variants) | ||||||
} | ||||||
} | ||||||
|
||||||
public init(from decoder: Decoder) throws { | ||||||
|
@@ -302,11 +354,6 @@ extension LinkDestinationSummary { | |||||
referenceURL = try container.decode(URL.self, forKey: .referenceURL) | ||||||
title = try container.decode(String.self, forKey: .title) | ||||||
abstract = try container.decodeIfPresent(Abstract.self, forKey: .abstract) | ||||||
let languageID = try container.decode(String.self, forKey: .language) | ||||||
guard let foundLanguage = SourceLanguage.knownLanguages.first(where: { $0.id == languageID }) else { | ||||||
throw DecodingError.dataCorruptedError(forKey: .language, in: container, debugDescription: "Unknown SourceLanguage identifier: '\(languageID)'.") | ||||||
} | ||||||
language = foundLanguage | ||||||
|
||||||
let availableLanguageIDs = try container.decode([String].self, forKey: .availableLanguages) | ||||||
availableLanguages = try Set(availableLanguageIDs.map { languageID in | ||||||
|
@@ -320,5 +367,75 @@ extension LinkDestinationSummary { | |||||
usr = try container.decodeIfPresent(String.self, forKey: .usr) | ||||||
declarationFragments = try container.decodeIfPresent(DeclarationFragments.self, forKey: .declarationFragments) | ||||||
redirects = try container.decodeIfPresent([URL].self, forKey: .redirects) | ||||||
|
||||||
variants = try container.decodeIfPresent([Variant].self, forKey: .variants) ?? [] | ||||||
do { | ||||||
let traits = try container.decode([RenderNode.Variant.Trait].self, forKey: .traits) | ||||||
for case .interfaceLanguage(let languageID) in traits { | ||||||
guard SourceLanguage.knownLanguages.contains(where: { $0.id == languageID }) else { | ||||||
throw DecodingError.dataCorruptedError(forKey: .traits, in: container, debugDescription: "Unknown SourceLanguage identifier: '\(languageID)'.") | ||||||
} | ||||||
} | ||||||
self.traits = traits | ||||||
} catch DecodingError.keyNotFound(_, let originalErrorContext) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we do a version check instead ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would require that we encode the version information in the JSON, which we would probably do once for the entire file instead of once for per element. |
||||||
// Attempts to decode the legacy format and raise the original keyNotFound if that doesn't work. | ||||||
do { | ||||||
let legacyContainer = try decoder.container(keyedBy: LegacyCodingKeys.self) | ||||||
|
||||||
let languageID = try legacyContainer.decode(String.self, forKey: .language) | ||||||
guard SourceLanguage.knownLanguages.contains(where: { $0.id == languageID }) else { | ||||||
throw DecodingError.dataCorruptedError(forKey: .language, in: legacyContainer, debugDescription: "Unknown SourceLanguage identifier: '\(languageID)'.") | ||||||
} | ||||||
traits = [.interfaceLanguage(languageID)] | ||||||
} catch { | ||||||
throw DecodingError.keyNotFound(CodingKeys.traits, originalErrorContext) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
extension LinkDestinationSummary.Variant { | ||||||
enum CodingKeys: String, CodingKey { | ||||||
case traits, kind, path, title, abstract, usr, declarationFragments = "fragments", taskGroups | ||||||
} | ||||||
|
||||||
public func encode(to encoder: Encoder) throws { | ||||||
var container = encoder.container(keyedBy: CodingKeys.self) | ||||||
try container.encode(traits, forKey: .traits) | ||||||
try container.encodeIfPresent(kind?.id, forKey: .kind) | ||||||
try container.encodeIfPresent(path, forKey: .path) | ||||||
try container.encodeIfPresent(title, forKey: .title) | ||||||
try container.encodeIfPresent(abstract, forKey: .abstract) | ||||||
try container.encodeIfPresent(usr, forKey: .usr) | ||||||
try container.encodeIfPresent(declarationFragments, forKey: .declarationFragments) | ||||||
try container.encodeIfPresent(taskGroups, forKey: .taskGroups) | ||||||
} | ||||||
|
||||||
public init(from decoder: Decoder) throws { | ||||||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||||||
|
||||||
let traits = try container.decode([RenderNode.Variant.Trait].self, forKey: .traits) | ||||||
for case .interfaceLanguage(let languageID) in traits { | ||||||
guard SourceLanguage.knownLanguages.contains(where: { $0.id == languageID }) else { | ||||||
throw DecodingError.dataCorruptedError(forKey: .traits, in: container, debugDescription: "Unknown SourceLanguage identifier: '\(languageID)'.") | ||||||
} | ||||||
} | ||||||
self.traits = traits | ||||||
|
||||||
let kindID = try container.decodeIfPresent(String.self, forKey: .kind) | ||||||
if let kindID = kindID { | ||||||
guard let foundKind = DocumentationNode.Kind.allKnownValues.first(where: { $0.id == kindID }) else { | ||||||
throw DecodingError.dataCorruptedError(forKey: .kind, in: container, debugDescription: "Unknown DocumentationNode.Kind identifier: '\(kindID)'.") | ||||||
} | ||||||
kind = foundKind | ||||||
} else { | ||||||
kind = nil | ||||||
} | ||||||
path = try container.decodeIfPresent(String.self, forKey: .path) | ||||||
title = try container.decodeIfPresent(String?.self, forKey: .title) | ||||||
abstract = try container.decodeIfPresent(LinkDestinationSummary.Abstract?.self, forKey: .abstract) | ||||||
usr = try container.decodeIfPresent(String?.self, forKey: .title) | ||||||
declarationFragments = try container.decodeIfPresent(LinkDestinationSummary.DeclarationFragments?.self, forKey: .declarationFragments) | ||||||
taskGroups = try container.decodeIfPresent([LinkDestinationSummary.TaskGroup]?.self, forKey: .taskGroups) | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.