-
Notifications
You must be signed in to change notification settings - Fork 166
Add a helper function for rendering availability information as HTML #1376
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
Merged
d-ronnqvist
merged 1 commit into
swiftlang:main
from
d-ronnqvist:output-html-availability
Dec 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2025 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 | ||
| */ | ||
|
|
||
| #if canImport(FoundationXML) | ||
| // TODO: Consider other HTML rendering options as a future improvement (rdar://165755530) | ||
| package import FoundationXML | ||
| #else | ||
| package import Foundation | ||
| #endif | ||
|
|
||
| package extension MarkdownRenderer { | ||
| /// Information about the versions that a piece of API is available for a given platform. | ||
| struct AvailabilityInfo { | ||
| /// The name of the platform that this information applies to. | ||
| package var name: String | ||
| /// The pre-formatted version string that describes the version that this API was introduced in for this platform. | ||
| package var introduced: String? | ||
| /// The pre-formatted version string that describes the version that this API was deprecated in for this platform. | ||
| package var deprecated: String? | ||
| /// A Boolean value indicating if the platform is currently in beta. | ||
| package var isBeta: Bool | ||
|
|
||
| package init(name: String, introduced: String? = nil, deprecated: String? = nil, isBeta: Bool) { | ||
| self.name = name | ||
| self.introduced = introduced | ||
| self.deprecated = deprecated | ||
| self.isBeta = isBeta | ||
| } | ||
| } | ||
|
|
||
| /// Creates an HTML element that describes the versions that a piece of API is available for the platforms described in the given availability information. | ||
| func availability(_ info: [AvailabilityInfo]) -> XMLNode { | ||
| let items: [XMLNode] = info.map { | ||
| var text = $0.name | ||
|
|
||
| let description: String | ||
| if let introduced = $0.introduced { | ||
| if let deprecated = $0.deprecated{ | ||
| text += " \(introduced)–\(deprecated)" | ||
| description = "Introduced in \($0.name) \(introduced) and deprecated in \($0.name) \(deprecated)" | ||
| } else { | ||
| text += " \(introduced)+" | ||
| description = "Available on \(introduced) and later" | ||
| } | ||
| } else { | ||
| description = "Available on \($0.name)" | ||
| } | ||
|
|
||
| var attributes = [ | ||
| "role": "text", | ||
| "aria-label": "\(text), \(description)", | ||
| "title": description | ||
| ] | ||
| if $0.isBeta { | ||
| attributes["class"] = "beta" | ||
| } else if $0.deprecated != nil { | ||
| attributes["class"] = "deprecated" | ||
| } | ||
|
|
||
| return .element(named: "li", children: [.text(text)], attributes: goal == .richness ? attributes : [:]) | ||
| } | ||
|
|
||
| return .element( | ||
| named: "ul", | ||
| children: items, | ||
| attributes: ["id": "availability"] | ||
| ) | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
Tests/DocCHTMLTests/MarkdownRenderer+PageElementsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2025 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 | ||
| */ | ||
|
|
||
| #if canImport(FoundationXML) | ||
| // TODO: Consider other HTML rendering options as a future improvement (rdar://165755530) | ||
| import FoundationXML | ||
| import FoundationEssentials | ||
| #else | ||
| import Foundation | ||
| #endif | ||
|
|
||
| import Testing | ||
| import DocCHTML | ||
| import Markdown | ||
|
|
||
| struct MarkdownRenderer_PageElementsTests { | ||
| @Test(arguments: RenderGoal.allCases) | ||
| func testRenderAvailability(goal: RenderGoal) { | ||
| let availability = makeRenderer(goal: goal).availability([ | ||
| .init(name: "First", introduced: "1.2", deprecated: "3.4", isBeta: false), | ||
| .init(name: "Second", introduced: "1.2.3", isBeta: false), | ||
| .init(name: "Third", introduced: "4.5", isBeta: true), | ||
| ]) | ||
| switch goal { | ||
| case .richness: | ||
| availability.assertMatches(prettyFormatted: true, expectedXMLString: """ | ||
| <ul id="availability"> | ||
| <li aria-label="First 1.2–3.4, Introduced in First 1.2 and deprecated in First 3.4" class="deprecated" role="text" title="Introduced in First 1.2 and deprecated in First 3.4">First 1.2–3.4</li> | ||
| <li aria-label="Second 1.2.3+, Available on 1.2.3 and later" role="text" title="Available on 1.2.3 and later">Second 1.2.3+</li> | ||
| <li aria-label="Third 4.5+, Available on 4.5 and later" class="beta" role="text" title="Available on 4.5 and later">Third 4.5+</li> | ||
| </ul> | ||
| """) | ||
| case .conciseness: | ||
| availability.assertMatches(prettyFormatted: true, expectedXMLString: """ | ||
| <ul id="availability"> | ||
| <li>First 1.2–3.4</li> | ||
| <li>Second 1.2.3+</li> | ||
| <li>Third 4.5+</li> | ||
| </ul> | ||
| """) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - | ||
|
|
||
| private func makeRenderer( | ||
| goal: RenderGoal, | ||
| elementsToReturn: [LinkedElement] = [], | ||
| pathsToReturn: [String: URL] = [:], | ||
| assetsToReturn: [String: LinkedAsset] = [:], | ||
| fallbackLinkTextsToReturn: [String: String] = [:] | ||
| ) -> MarkdownRenderer<some LinkProvider> { | ||
| let path = URL(string: "/documentation/ModuleName/Something/ThisPage/index.html")! | ||
|
|
||
| var elementsByURL = [ | ||
| path: LinkedElement( | ||
| path: path, | ||
| names: .single( .symbol("ThisPage") ), | ||
| subheadings: .single( .symbol([ | ||
| .init(text: "class ", kind: .decorator), | ||
| .init(text: "ThisPage", kind: .identifier), | ||
| ])), | ||
| abstract: nil | ||
| ) | ||
| ] | ||
| for element in elementsToReturn { | ||
| elementsByURL[element.path] = element | ||
| } | ||
|
|
||
| return MarkdownRenderer(path: path, goal: goal, linkProvider: MultiValueLinkProvider( | ||
| elementsToReturn: elementsByURL, | ||
| pathsToReturn: pathsToReturn, | ||
| assetsToReturn: assetsToReturn, | ||
| fallbackLinkTextsToReturn: fallbackLinkTextsToReturn | ||
| )) | ||
| } | ||
|
|
||
| private func parseMarkup(string: String) -> [any Markup] { | ||
| let document = Document(parsing: string, options: [.parseBlockDirectives, .parseSymbolLinks]) | ||
| return Array(document.children) | ||
| } | ||
| } | ||
|
|
||
| struct MultiValueLinkProvider: LinkProvider { | ||
| var elementsToReturn: [URL: LinkedElement] | ||
| func element(for path: URL) -> LinkedElement? { | ||
| elementsToReturn[path] | ||
| } | ||
|
|
||
| var pathsToReturn: [String: URL] | ||
| func pathForSymbolID(_ usr: String) -> URL? { | ||
| pathsToReturn[usr] | ||
| } | ||
|
|
||
| var assetsToReturn: [String: LinkedAsset] | ||
| func assetNamed(_ assetName: String) -> LinkedAsset? { | ||
| assetsToReturn[assetName] | ||
| } | ||
|
|
||
| var fallbackLinkTextsToReturn: [String: String] | ||
| func fallbackLinkText(linkString: String) -> String { | ||
| fallbackLinkTextsToReturn[linkString] ?? linkString | ||
| } | ||
| } | ||
|
|
||
| extension RenderGoal: CaseIterable { | ||
| package static var allCases: [RenderGoal] { | ||
| [.richness, .conciseness] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just to double check my understanding - the MultiValueLinkProvider here is a convenience instance really just for this test scenario, as the upstream usage of parsing availability uses another type that conforms to the
LinkProviderprotocol - Is that correct?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.
Yes, there's a "real" link provider here that reads from the documentation context.
That 4000+ LOC PR isn't really meant to be reviewed and merged on its own, so I'm breaking it down into smaller pieces that can be merged individually and incrementally. Unfortunately, that means that some early PRs may add test code that's barely used (yet).