Skip to content

Commit 1202a1d

Browse files
committed
Add support for @Small directive
Adds support for the @Small directive as described here: https://forums.swift.org/t/supporting-more-dynamic-content-in-swift-docc-reference-documentation/59527#small-18 A new @Small directive based on HTML’s small tag. This directive can be used to specify small print like legal, license, or copyright text that should be rendered in a smaller font size. @Small accepts no parameters and accepts arbitrary DocC markup body content. Example: You can create a sloth using the ``init(name:color:power:)`` initializer, or create randomly generated sloth using a ``SlothGenerator``: let slothGenerator = MySlothGenerator(seed: randomSeed()) let habitat = Habitat(isHumid: false, isWarm: true) // ... @Small { Licensed under Apache License v2.0 with Runtime Library Exception } Resolves rdar://97744845
1 parent 1c4b479 commit 1202a1d

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

Sources/SwiftDocC/Indexing/RenderBlockContent+TextIndexing.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ extension RenderBlockContent: TextIndexing {
6262
return row.columns.map { column in
6363
return column.content.rawIndexableTextContent(references: references)
6464
}.joined(separator: " ")
65+
case .small(let small):
66+
return small.inlineContent.rawIndexableTextContent(references: references)
6567
default:
6668
fatalError("unknown RenderBlockContent case in rawIndexableTextContent")
6769
}

Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public enum RenderBlockContent: Equatable {
6262
case table(Table)
6363

6464
case row(Row)
65+
66+
case small(Small)
6567

6668
// Warning: If you add a new case to this enum, make sure to handle it in the Codable
6769
// conformance at the bottom of this file, and in the `rawIndexableTextContent` method in
@@ -427,6 +429,10 @@ public enum RenderBlockContent: Equatable {
427429
public let content: [RenderBlockContent]
428430
}
429431
}
432+
433+
public struct Small: Codable, Equatable {
434+
public let inlineContent: [RenderInlineContent]
435+
}
430436
}
431437

432438
// Codable conformance
@@ -489,11 +495,15 @@ extension RenderBlockContent: Codable {
489495
columns: container.decode([Row.Column].self, forKey: .columns)
490496
)
491497
)
498+
case .small:
499+
self = try .small(
500+
Small(inlineContent: container.decode([RenderInlineContent].self, forKey: .inlineContent))
501+
)
492502
}
493503
}
494504

495505
private enum BlockType: String, Codable {
496-
case paragraph, aside, codeListing, heading, orderedList, unorderedList, step, endpointExample, dictionaryExample, table, termList, row
506+
case paragraph, aside, codeListing, heading, orderedList, unorderedList, step, endpointExample, dictionaryExample, table, termList, row, small
497507
}
498508

499509
private var type: BlockType {
@@ -510,6 +520,7 @@ extension RenderBlockContent: Codable {
510520
case .table: return .table
511521
case .termList: return .termList
512522
case .row: return .row
523+
case .small: return .small
513524
default: fatalError("unknown RenderBlockContent case in type property")
514525
}
515526
}
@@ -559,6 +570,8 @@ extension RenderBlockContent: Codable {
559570
case .row(let row):
560571
try container.encode(row.numberOfColumns, forKey: .numberOfColumns)
561572
try container.encode(row.columns, forKey: .columns)
573+
case .small(let small):
574+
try container.encode(small.inlineContent, forKey: .inlineContent)
562575
default:
563576
fatalError("unknown RenderBlockContent case in encode method")
564577
}

Sources/SwiftDocC/Semantics/DirectiveInfrastructure/DirectiveIndex.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct DirectiveIndex {
1717
Snippet.self,
1818
DeprecationSummary.self,
1919
Row.self,
20+
Small.self,
2021
]
2122

2223
private static let topLevelTutorialDirectives: [AutomaticDirectiveConvertible.Type] = [
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) 2022 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+
public final class Small: Semantic, AutomaticDirectiveConvertible, MarkupContaining {
15+
public let originalMarkup: BlockDirective
16+
17+
@ChildMarkup(numberOfParagraphs: .oneOrMore)
18+
public private(set) var content: MarkupContainer
19+
20+
static var keyPaths: [String : AnyKeyPath] = [
21+
"content" : \Small._content,
22+
]
23+
24+
override var children: [Semantic] {
25+
return [content]
26+
}
27+
28+
var childMarkup: [Markup] {
29+
return content.elements
30+
}
31+
32+
@available(*, deprecated,
33+
message: "Do not call directly. Required for 'AutomaticDirectiveConvertible'."
34+
)
35+
init(originalMarkup: BlockDirective) {
36+
self.originalMarkup = originalMarkup
37+
}
38+
}
39+
40+
extension Small: RenderableDirectiveConvertible {
41+
func render(with contentCompiler: inout RenderContentCompiler) -> [RenderContent] {
42+
// Render the content normally
43+
let renderBlockContent = content.elements.flatMap { markupElement in
44+
return contentCompiler.visit(markupElement) as! [RenderBlockContent]
45+
}
46+
47+
// Transform every paragraph in the render block content to a small paragraph
48+
let transformedRenderBlockContent = renderBlockContent.map { block -> RenderBlockContent in
49+
guard case let .paragraph(paragraph) = block else {
50+
return block
51+
}
52+
53+
return .small(RenderBlockContent.Small(inlineContent: paragraph.inlineContent))
54+
}
55+
56+
return transformedRenderBlockContent
57+
}
58+
}

Sources/SwiftDocC/SwiftDocC.docc/Resources/RenderNode.spec.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@
434434
{
435435
"$ref": "#/components/schemas/Aside"
436436
},
437+
{
438+
"$ref": "#/components/schemas/Small"
439+
},
437440
{
438441
"$ref": "#/components/schemas/Heading"
439442
},
@@ -619,6 +622,25 @@
619622
}
620623
}
621624
},
625+
"Small": {
626+
"type": "object",
627+
"required": [
628+
"type",
629+
"inlineContent",
630+
],
631+
"properties": {
632+
"type": {
633+
"type": "string",
634+
"enum": ["small"]
635+
},
636+
"inlineContent": {
637+
"type": "array",
638+
"items": {
639+
"$ref": "#/components/schemas/RenderInlineContent"
640+
}
641+
}
642+
}
643+
},
622644
"Heading": {
623645
"type": "object",
624646
"required": [

0 commit comments

Comments
 (0)