-
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| 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 { | ||
| // If the goal is conciseness, display only the primary language's plain text declaration | ||
| let plainTextDeclaration: [XMLNode] = fragmentsByLanguage.first.map { _, fragments in | ||
| [.element(named: "code", children: [.text(fragments.map(\.spelling).joined())])] | ||
| } ?? [] | ||
| return .element(named: "pre", children: plainTextDeclaration) | ||
| } | ||
|
|
||
| let declarations: [XMLElement] = if fragmentsByLanguage.count == 1 { | ||
|
Contributor
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. 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 |
||
| // 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]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
See my comment in #1382 - can you explain why the concise version only displays info about the primary language? Maybe paste 2 examples for richness and conciseness above in the doc comments and then a quick sentence about why the concise version is sooo concise :)