File: Tile.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (224 lines) | stat: -rw-r--r-- 9,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021-2023 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
*/

import Foundation
import Markdown

/// A semantic model for a view that links to a content type that you specify.
///
/// A tile is a kind of thematic content block that contains links to resources
/// such as sample code, videos, or forum topics.
public final class Tile: Semantic, DirectiveConvertible {
    public static let introducedVersion = "5.5"
    
    /// A tile type to identify the tile during building page layout.
    public enum Identifier: String, Codable {
        /// Identifies a tile that links to documentation.
        case documentation = "documentation"
        
        /// Identifies a tile that links to sample code.
        case sampleCode = "sampleCode"
        
        /// Identifies a tile that links to downloads.
        case downloads = "downloads"
        
        /// Identifies a tile that links to videos.
        case videos = "videos"
        
        /// Identifies a tile that links to forum topics.
        case forums = "forums"
    }
    
    /// The possible directive names for a tile, corresponding to different expanded content.
    public enum DirectiveNames: String, CaseIterable {
        case documentation = "Documentation"
        case sampleCode = "SampleCode"
        case downloads = "Downloads"
        case videos = "Videos"
        case forums = "Forums"
        
        var tileIdentifier: Identifier {
            switch self {
                case .documentation: return .documentation
                case .sampleCode: return .sampleCode
                case .downloads: return .downloads
                case .videos: return .videos
                case .forums: return .forums
            }
        }
        
        var title: Semantics.Title {
            switch self {
                case .documentation: return .documentation
                case .sampleCode: return .sampleCode
                case .downloads: return .downloads
                case .videos: return .videos
                case .forums: return .forums
            }
        }
    }
    
    /// A fake directive name, the actual tile directives are in ``DirectiveNames``.
    public static let directiveName = "Tile"
    
    public let originalMarkup: BlockDirective
    
    /// An identifier for the tile.
    public let identifier: Tile.Identifier
    
    /// The title of the tile.
    public let title: String
    
    /// The destination of the tile's primary link.
    public let destination: URL?
    
    /// The contents of the tile.
    public let content: MarkupContainer
    
    override var children: [Semantic] {
        return [content]
    }
    
    enum Semantics {
        enum Destination: DirectiveArgument {
            typealias ArgumentValue = URL
            static let argumentName = "destination"
        }
        
        enum Title: String, DirectiveArgument {
            static let argumentName = "title"
            case documentation = "Documentation"
            case sampleCode = "Sample Code"
            case downloads = "Xcode and SDKs"
            case videos = "Videos"
            case forums = "Forums"
        }
    }
    
    init(originalMarkup: BlockDirective, identifier: Tile.Identifier, title: String, destination: URL?, content: MarkupContainer) {
        self.originalMarkup = originalMarkup
        self.identifier = identifier
        self.title = title
        self.destination = destination
        self.content = content
    }
    
    fileprivate static func firstParagraph(of directive: BlockDirective, source: URL?, for bundle: DocumentationBundle, in context: DocumentationContext, problems: inout [Problem]) -> (Paragraph?, remainder: MarkupContainer) {
        var remainder: MarkupContainer
        let paragraph: Paragraph?
        if let firstChild = directive.child(at: 0) {
            if let firstParagraph = firstChild as? Paragraph {
                paragraph = firstParagraph
                remainder = MarkupContainer(directive.children.dropFirst(1))
            } else {
                paragraph = nil
                remainder = MarkupContainer(directive.children)
            }
        } else {
            let diagnostic = Diagnostic(source: source, severity: .warning, range: directive.range, identifier: "org.swift.docc.Resources.\(directive.name).HasContent", summary: "\(directive.name.singleQuoted) directive requires an initial paragraph summarizing the contents of the tile's destination")
            problems.append(Problem(diagnostic: diagnostic, possibleSolutions: []))
            paragraph = nil
            remainder = MarkupContainer(directive.children)
        }
        return (paragraph, remainder: remainder)
    }
    
    /// Checks if the provided directive contains a list and if so returns the list and the remainder of the markup.
    /// This helper function abstracts checking for an optional list inside a "tile" directive.
    fileprivate static func list(in directive: BlockDirective, source: URL?, problems: inout [Problem]) -> (list: UnorderedList?, remainder: MarkupContainer) {
        var remainder: MarkupContainer
        let list: UnorderedList?
        
        if let element = directive.child(at: 1) as? UnorderedList {
            list = element
            remainder = MarkupContainer(directive.children.dropFirst(1))
        } else {
            list = nil
            remainder = MarkupContainer(directive.children)
        }
        
        return (list, remainder: remainder)
    }
    
    convenience init?(genericTile directive: BlockDirective, title: String, source: URL?, for bundle: DocumentationBundle, in context: DocumentationContext, problems: inout [Problem], shouldContainLinks: Bool = false) {
        let arguments = directive.arguments(problems: &problems)
        let destination = Semantic.Analyses.HasArgument<Tile, Semantics.Destination>(severityIfNotFound: nil).analyze(directive, arguments: arguments, problems: &problems)

        let _ = Tile.firstParagraph(of: directive, source: source, for: bundle, in: context, problems: &problems)
        let (list, remainder: _) = Tile.list(in: directive, source: source, problems: &problems)

        if shouldContainLinks, list == nil {
            let diagnostic = Diagnostic(source: source, severity: .warning, range: directive.range, identifier: "org.swift.docc.Resources.\(directive.name).HasLinks", summary: "\(directive.name.singleQuoted) directive should contain at least one list item")
            problems.append(Problem(diagnostic: diagnostic, possibleSolutions: []))
        }

        guard let tileIdentifier = DirectiveNames(rawValue: directive.name)?.tileIdentifier else {
            return nil
        }

        self.init(originalMarkup: directive, identifier: tileIdentifier, title: title, destination: destination, content: MarkupContainer(directive.children))
    }
    
    public convenience init?(from directive: BlockDirective, source: URL?, for bundle: DocumentationBundle, in context: DocumentationContext, problems: inout [Problem]) {
        switch directive.name {
        case Tile.DirectiveNames.documentation.rawValue:
            self.init(genericTile: directive,
                      title: Tile.Semantics.Title.documentation.rawValue,
                      source: source,
                      for: bundle,
                      in: context,
                      problems: &problems,
                      shouldContainLinks: true)
        case Tile.DirectiveNames.sampleCode.rawValue:
            self.init(genericTile: directive,
                      title: Tile.Semantics.Title.sampleCode.rawValue,
                      source: source,
                      for: bundle,
                      in: context,
                      problems: &problems,
                      shouldContainLinks: true)
        case Tile.DirectiveNames.downloads.rawValue:
            self.init(genericTile: directive,
                      title: Tile.Semantics.Title.downloads.rawValue,
                      source: source,
                      for: bundle,
                      in: context,
                      problems: &problems)
        case Tile.DirectiveNames.videos.rawValue:
            self.init(genericTile: directive,
                      title: Tile.Semantics.Title.videos.rawValue,
                      source: source,
                      for: bundle,
                      in: context,
                      problems: &problems)
        case Tile.DirectiveNames.forums.rawValue:
            self.init(genericTile: directive,
                      title: Tile.Semantics.Title.forums.rawValue,
                      source: source,
                      for: bundle,
                      in: context,
                      problems: &problems)
        default:
            let possibleTileDirectiveNames = Tile.DirectiveNames.allCases
                .map { $0.rawValue.singleQuoted }
                .list(finalConjunction: .or)
            let directiveReference = directive.name.isEmpty
                ? "anonymous child directive"
                : "child directive \(directive.name.singleQuoted)"
            let diagnostic = Diagnostic(source: source, severity: .warning, range: directive.range, identifier: "org.swift.docc.Resources.UnknownTile", summary: "Unknown \(directiveReference) of \(Resources.directiveName.singleQuoted); must be one of \(possibleTileDirectiveNames)")
            problems.append(Problem(diagnostic: diagnostic, possibleSolutions: []))
            return nil
        }
    }
    
    public override func accept<V>(_ visitor: inout V) -> V.Result where V : SemanticVisitor {
        return visitor.visitTile(self)
    }
}