-
Notifications
You must be signed in to change notification settings - Fork 166
Add a helper function for rendering symbol declarations as HTML #1384
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 3 commits into
swiftlang:main
from
d-ronnqvist:output-html-declaration
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,78 @@ | ||
| /* | ||
| 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 import DocCCommon | ||
| package import SymbolKit | ||
|
|
||
| package extension MarkdownRenderer { | ||
|
|
||
| typealias DeclarationFragment = SymbolGraph.Symbol.DeclarationFragments.Fragment | ||
|
|
||
| /// Creates a`<pre><code>` HTML element hierarchy that represents the symbol's language-specific declarations. | ||
| /// | ||
| /// When the renderer has a ``RenderGoal/richness`` goal, it creates a `<span>` element for each declaration fragment so that to enable syntax highlighting. | ||
| /// | ||
| /// When the renderer has a ``RenderGoal/conciseness`` goal, it joins the different fragments into string. | ||
| func declaration(_ fragmentsByLanguage: [SourceLanguage: [DeclarationFragment]]) -> XMLElement { | ||
| let fragmentsByLanguage = RenderHelpers.sortedLanguageSpecificValues(fragmentsByLanguage) | ||
|
|
||
| guard goal == .richness else { | ||
| // On the rendered page, language specific content _could_ be hidden through CSS but that wouldn't help the tool that reads the raw HTML. | ||
| // So that tools don't need to filter out language specific content themselves, include only the primary language's (plain text) declaration. | ||
| let plainTextDeclaration: [XMLNode] = fragmentsByLanguage.first.map { _, fragments in | ||
| // The main purpose of individual HTML elements per declaration fragment would be syntax highlighting on the rendered page. | ||
| // That structure likely won't be beneficial (and could even be detrimental) to the tool's ability to consume the declaration information. | ||
| [.element(named: "code", children: [.text(fragments.map(\.spelling).joined())])] | ||
| } ?? [] | ||
| return .element(named: "pre", children: plainTextDeclaration) | ||
| } | ||
|
|
||
| let declarations: [XMLElement] = if fragmentsByLanguage.count == 1 { | ||
| // If there's only a single language there's no need to mark anything as language specific. | ||
| [XMLNode.element(named: "code", children: _declarationTokens(for: fragmentsByLanguage.first!.value))] | ||
| } else { | ||
| fragmentsByLanguage.map { language, fragments in | ||
| XMLNode.element(named: "code", children: _declarationTokens(for: fragments), attributes: ["class": "\(language.id)-only"]) | ||
| } | ||
| } | ||
| return .element(named: "pre", children: declarations, attributes: ["id": "declaration"]) | ||
| } | ||
|
|
||
| private func _declarationTokens(for fragments: [DeclarationFragment]) -> [XMLNode] { | ||
| // TODO: Pretty print declarations for Swift and Objective-C by placing attributes and parameters on their own lines (rdar://165918402) | ||
| fragments.map { fragment in | ||
| let elementClass = "token-\(fragment.kind.rawValue)" | ||
|
|
||
| if fragment.kind == .typeIdentifier, | ||
| let symbolID = fragment.preciseIdentifier, | ||
| let reference = linkProvider.pathForSymbolID(symbolID) | ||
| { | ||
| // If the token refers to a symbol that the `linkProvider` is aware of, make that fragment a link to that symbol. | ||
| return .element(named: "a", children: [.text(fragment.spelling)], attributes: [ | ||
| "href": path(to: reference), | ||
| "class": elementClass | ||
| ]) | ||
| } else if fragment.kind == .text { | ||
| // ???: Does text also need a <span> element or can that be avoided? | ||
| return .text(fragment.spelling) | ||
| } else { | ||
| // The declaration element is expected to scroll, so individual fragments don't need to contain explicit word breaks. | ||
| return .element(named: "span", children: [.text(fragment.spelling)], attributes: ["class": elementClass]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Reading this pattern in three or four functions now makes me wonder if you could extract the general behavior into a single place, and use a protocol, closure or some other means of calling the individual functions like
_declarationTokensor_singleTaskGroupElementsbut maybe that would be difficult and not worth the effort?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.
So far I don't feel that a clear enough pattern has emerged to know what abstraction to add, if any.
In the places so far, the common structure is a simply
if count == 1 {...} else {...}and it would likely be hard to beat the readability and explicitness of that in an abstraction.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.
So far I'm feeling that it's probably the caller's side (rather than these implementations) that would benefit from a helper function to perform some common work.