File: DocumentationMarkup.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 (341 lines) | stat: -rw-r--r-- 13,951 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021-2024 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 structured documentation markup data model.
///
/// ## Discussion
/// `DocumentationMarkup` parses a given piece of structured markup and provides access to the documentation content.
///
/// ### Title
/// The parser parses the title from the first level-one heading. If the markup doesn't start with a level-one heading, it's considered to not have a title.
/// ```
/// # My Document
/// ```
/// ### Abstract
/// The parser parses the abstract from the first leading paragraph (skipping the comments) in the markup after the title. If the markup doesn't start with a paragraph after the title heading, it's considered to not have an abstract.
/// ```
/// # My Document
/// An abstract shortly describing My Document.
/// ```
/// ### Discussion
/// The parser parses the discussion from the end of the abstract section until a "Topics" or "See Also" section is found (or the end of the document).
/// ```
/// # My Document
/// An abstract shortly describing My Document.
/// ## Discussion
/// A discussion that may contain further level-3 sub-sections, text, images, etc.
/// ```
/// ### Topics
/// The parser parses the topics from the end of the discussion section until a "See Also" section is found or the end of the document.
/// Links are organized inside "Topics" into task groups beginning with a level-3 heading.
/// ```
/// ## Topics
/// ### Basics
///  - <doc:article>
///  - ``MyClass``
/// ```
/// ### See Also
/// The parser parses the see also links from the end of the topics section until the end of the document.
/// "See Also" contains a flat list of links.
/// ```
/// ## See Also
///  - [website](https://website.com)
/// ```
struct DocumentationMarkup {
    /// The original markup.
    private let markup: Markup

    /// The various sections that are expected in documentation markup.
    ///
    /// The cases in this enumeration are sorted in the order sections are expected to appear in the documentation markup.
    /// For example the Discussion section is always expected to appear before the See Also section. This also enables
    /// ``init(markup:parseUpToSection:)`` to partially parse a document up to a given section.
    enum ParserSection: Int, Comparable {
        static func < (lhs: DocumentationMarkup.ParserSection, rhs: DocumentationMarkup.ParserSection) -> Bool {
            return lhs.rawValue < rhs.rawValue
        }
        
        case title
        case abstract
        case discussion
        case topics
        case seeAlso
        case end
    }
    
    /// Directives which are removed from the markdown content after being parsed.
    static let directivesRemovedFromContent = [
        Comment.directiveName,
        Metadata.directiveName,
        Options.directiveName,
        Redirect.directiveName,
    ]
    
    private static let allowedSectionsForDeprecationSummary = [
        ParserSection.abstract,
        ParserSection.discussion,
    ]

    // MARK: - Parsed Data
    
    /// The documentation title, if found.
    private(set) var titleHeading: Heading?
    
    /// The documentation abstract, if found.
    private(set) var abstractSection: AbstractSection?

    /// The documentation Discussion section, if found.
    private(set) var discussionSection: DiscussionSection?
    
    /// The documentation tags, if found.
    private(set) var discussionTags: TaggedListItemExtractor?
    
    /// The documentation Topics section, if found.
    private(set) var topicsSection: TopicsSection?

    /// The documentation See Also, if found.
    private(set) var seeAlsoSection: SeeAlsoSection?
    
    /// The symbol deprecation information, if found.
    private(set) var deprecation: MarkupContainer?
    
    // MARK: - Initialize and parse the markup

    /// Initialize a documentation model with the given markup.
    /// - Parameters:
    ///   - markup: The source markup.
    ///   - parseUpToSection: Documentation past this section will be ignored.
    init(markup: Markup, parseUpToSection: ParserSection = .end) {
        self.markup = markup
        
        // The current documentation section being parsed.
        var currentSection = ParserSection.title
        
        // Tracking the start indexes of various sections.
        var discussionIndex: Int?
        var topicsIndex: Int?
        var topicsFirstTaskGroupIndex: Int?
        var seeAlsoIndex: Int?
        
        // Index all headings as a lookup during parsing the content
        markup.children.enumerated().forEach({ pair in
            // If we've parsed the last section we're interested in, skip through the rest
            guard currentSection <= parseUpToSection || currentSection == .end else { return }
            
            let (index, child) = pair
            let isLastChild = index == (markup.childCount - 1)
            
            // Already parsed all expected content, return.
            guard currentSection != .end else { return }
            
            // Parse an H1 title, if found.
            if currentSection == .title {
                currentSection = .abstract
                
                // Index the title child node.
                if let heading = child as? Heading, heading.level == 1 {
                    titleHeading = heading
                    return
                }
            }
            
            // The deprecation summary directive is allowed to have an effect in multiple sections of the content.
            if let directive = child as? BlockDirective,
               directive.name == DeprecationSummary.directiveName,
               Self.allowedSectionsForDeprecationSummary.contains(currentSection) {
                deprecation = MarkupContainer(directive.children)
                return
            }
            
            // Parse an abstract, if found
            if currentSection == .abstract {
                if abstractSection == nil, let firstParagraph = child as? Paragraph {
                    abstractSection = AbstractSection(paragraph: firstParagraph)
                    return
                } else if let directive = child as? BlockDirective {
                    if Self.directivesRemovedFromContent.contains(directive.name) {
                        // These directives don't affect content so they shouldn't break us out of
                        // the automatic abstract section.
                        return
                    } else {
                        currentSection = .discussion
                    }
                } else if let _ = child as? HTMLBlock {
                    // Skip HTMLBlock comment.
                    return
                } else {
                    // Only directives and a single paragraph allowed in an abstract,
                    // advance to a discussion section.
                    currentSection = .discussion
                }
            }
            
            // Parse content into a discussion section and assorted tags
            let parseDiscussion: ([Markup])-> (discussion: DiscussionSection, tags: TaggedListItemExtractor) = { children in
                // Extract tags
                var extractor = TaggedListItemExtractor()
                let content: [Markup]
                
                if let remainder = extractor.visit(markup.withUncheckedChildren(children)) {
                    content = Array(remainder.children)
                } else {
                    content = []
                }
                
                return (discussion: DiscussionSection(content: content), tags: extractor)
            }
            
            // Parse a discussion, if found
            if currentSection == .discussion {
                // Scanning for the first discussion content child
                if discussionIndex == nil {
                    // Level 2 heading found at start of discussion
                    if let heading = child as? Heading, heading.level == 2 {
                        switch heading.plainText {
                        case TopicsSection.title:
                            currentSection = .topics
                            return
                        case SeeAlsoSection.title:
                            currentSection = .seeAlso
                            return
                        default: break
                        }
                    }
                    
                    // Discussion content starts at this index
                    discussionIndex = index
                }
                
                guard let discussionIndex else { return }
                
                // Level 2 heading found inside discussion
                if let heading = child as? Heading, heading.level == 2 {
                    switch heading.plainText {
                    case TopicsSection.title:
                        let (discussion, tags) = parseDiscussion(markup.children(at: discussionIndex ..< index))
                        discussionSection = discussion
                        discussionTags = tags
                        currentSection = .topics
                        return
                        
                    case SeeAlsoSection.title:
                        let (discussion, tags) = parseDiscussion(markup.children(at: discussionIndex ..< index))
                        discussionSection = discussion
                        discussionTags = tags
                        currentSection = .seeAlso
                        return
                    default: break
                    }
                }
                
                // If at end of content, parse discussion
                if isLastChild {
                    let (discussion, tags) = parseDiscussion(markup.children(at: discussionIndex ... index))
                    discussionSection = discussion
                    discussionTags = tags
                }
            }
            
            if currentSection == .topics {
                if let heading = child as? Heading {
                    // Level 2 heading found inside Topics
                    if heading.level == 2 {
                        switch heading.plainText {
                        case SeeAlsoSection.title:
                            if let topicsIndex, topicsFirstTaskGroupIndex != nil {
                                topicsSection = TopicsSection(content: markup.children(at: topicsIndex ..< index))
                            }
                            currentSection = .seeAlso
                            return
                        default: break
                        }
                    }
                    if heading.level == 3 {
                        topicsFirstTaskGroupIndex = index
                    }
                }
                // The first topic group in a topic section is allowed to be "anonymous", or without
                // an H3 heading. We account for this by treating both UnorderedLists and Paragraphs as
                // valid children indicating the start of a task group.
                else if child is UnorderedList {
                    topicsFirstTaskGroupIndex = index
                } else if child is Paragraph {
                    topicsFirstTaskGroupIndex = index
                }
                
                if topicsIndex == nil { topicsIndex = index }
                
                // If at end of content, parse topics
                if isLastChild && topicsFirstTaskGroupIndex != nil {
                    topicsSection = TopicsSection(content: markup.children(at: topicsIndex! ... index))
                }
            }

            if currentSection == .seeAlso {
                // Level 2 heading found inside See Also
                if child is Heading {
                    if let seeAlsoIndex {
                        seeAlsoSection = SeeAlsoSection(content: markup.children(at: seeAlsoIndex ..< index))
                    }
                    currentSection = .end
                    return
                }
                
                if seeAlsoIndex == nil { seeAlsoIndex = index }
                
                // If at end of content, parse topics
                if isLastChild {
                    seeAlsoSection = SeeAlsoSection(content: markup.children(at: seeAlsoIndex! ... index))
                }
            }
        })
    }
}

// MARK: - Convenience Markup extensions

extension Markup {
    /// Returns a sub-sequence of the children sequence.
    /// - Parameter range: A closed range.
    /// - Returns: A children sub-sequence.
    func children(at range: ClosedRange<Int>) -> [Markup] {
        var iterator = children.makeIterator()
        var counter = 0
        var result = [Markup]()
        
        while let next = iterator.next() {
            defer { counter += 1 }
            guard counter <= range.upperBound else { break }
            guard counter >= range.lowerBound else { continue }
            result.append(next)
        }
        return result
    }

    /// Returns a sub-sequence of the children sequence.
    /// - Parameter range: A half-closed range.
    /// - Returns: A children sub-sequence.
    func children(at range: Range<Int>) -> [Markup] {
        var iterator = children.makeIterator()
        var counter = 0
        var result = [Markup]()
        
        while let next = iterator.next() {
            defer { counter += 1 }
            guard counter < range.upperBound else { break }
            guard counter >= range.lowerBound else { continue }
            result.append(next)
        }
        return result
    }
}