Skip to content

Commit b9f1384

Browse files
authored
Merge branch 'main' into allow-separate-strong-emphasis-marker
2 parents 8f7ca55 + 6b9aade commit b9f1384

File tree

25 files changed

+1092
-55
lines changed

25 files changed

+1092
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Swift `Markdown` is a Swift package for parsing, building, editing, and analyzin
44

55
The parser is powered by GitHub-flavored Markdown's [cmark-gfm](https://github.com/github/cmark-gfm) implementation, so it follows the spec closely. As the needs of the community change, the effective dialect implemented by this library may change.
66

7-
The markup tree provided by this package is comprised of immutable/persistent, thread-safe, copy-on-write value types that only copy substructure that has changed. Other examples of the main strategy behind this library can be seen in Swift's [lib/Syntax](https://github.com/apple/swift/tree/master/lib/Syntax) and its Swift bindings, [SwiftSyntax](https://github.com/apple/swift-syntax).
7+
The markup tree provided by this package is comprised of immutable/persistent, thread-safe, copy-on-write value types that only copy substructure that has changed. Other examples of the main strategy behind this library can be seen in [SwiftSyntax](https://github.com/apple/swift-syntax).
88

99
## Getting Started Using Markup
1010

Snippets/Parsing/test.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Sample document
22

33
This is a *sample document*.
4+
5+
<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Base/Markup.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func makeMarkup(_ data: _MarkupData) -> Markup {
7171
return SymbolLink(data)
7272
case .inlineAttributes:
7373
return InlineAttributes(data)
74+
case .doxygenParam:
75+
return DoxygenParameter(data)
76+
case .doxygenReturns:
77+
return DoxygenReturns(data)
7478
}
7579
}
7680

@@ -261,7 +265,7 @@ extension Markup {
261265
public func child(through path: TypedChildIndexPath) -> Markup? {
262266
var element: Markup = self
263267
for pathElement in path {
264-
guard pathElement.index <= raw.markup.childCount else {
268+
guard pathElement.index <= element.childCount else {
265269
return nil
266270
}
267271

Sources/Markdown/Base/RawMarkup.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ enum RawMarkupData: Equatable {
5151
case tableBody
5252
case tableRow
5353
case tableCell(colspan: UInt, rowspan: UInt)
54+
55+
case doxygenParam(name: String)
56+
case doxygenReturns
5457
}
5558

5659
extension RawMarkupData {
@@ -330,6 +333,14 @@ final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> {
330333
static func tableCell(parsedRange: SourceRange?, colspan: UInt, rowspan: UInt, _ children: [RawMarkup]) -> RawMarkup {
331334
return .create(data: .tableCell(colspan: colspan, rowspan: rowspan), parsedRange: parsedRange, children: children)
332335
}
336+
337+
static func doxygenParam(name: String, parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
338+
return .create(data: .doxygenParam(name: name), parsedRange: parsedRange, children: children)
339+
}
340+
341+
static func doxygenReturns(parsedRange: SourceRange?, _ children: [RawMarkup]) -> RawMarkup {
342+
return .create(data: .doxygenReturns, parsedRange: parsedRange, children: children)
343+
}
333344
}
334345

335346
fileprivate extension Sequence where Element == RawMarkup {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2023 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+
13+
/// A parsed Doxygen `\param` command.
14+
///
15+
/// The Doxygen support in Swift-Markdown parses `\param` commands of the form
16+
/// `\param name description`, where `description` extends until the next blank line or the next
17+
/// parsed command. For example, the following input will return two `DoxygenParam` instances:
18+
///
19+
/// ```markdown
20+
/// \param coordinate The coordinate used to center the transformation.
21+
/// \param matrix The transformation matrix that describes the transformation.
22+
/// For more information about transformation matrices, refer to the Transformation
23+
/// documentation.
24+
/// ```
25+
public struct DoxygenParameter: BlockContainer {
26+
public var _data: _MarkupData
27+
28+
init(_ raw: RawMarkup) throws {
29+
guard case .doxygenParam = raw.data else {
30+
throw RawMarkup.Error.concreteConversionError(from: raw, to: DoxygenParameter.self)
31+
}
32+
let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0))
33+
self.init(_MarkupData(absoluteRaw))
34+
}
35+
36+
init(_ data: _MarkupData) {
37+
self._data = data
38+
}
39+
40+
public func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {
41+
return visitor.visitDoxygenParameter(self)
42+
}
43+
}
44+
45+
public extension DoxygenParameter {
46+
/// Create a new Doxygen parameter definition.
47+
///
48+
/// - Parameter name: The name of the parameter being described.
49+
/// - Parameter children: Block child elements.
50+
init<Children: Sequence>(name: String, children: Children) where Children.Element == BlockMarkup {
51+
try! self.init(.doxygenParam(name: name, parsedRange: nil, children.map({ $0.raw.markup })))
52+
}
53+
54+
/// Create a new Doxygen parameter definition.
55+
///
56+
/// - Parameter name: The name of the parameter being described.
57+
/// - Parameter children: Block child elements.
58+
init(name: String, children: BlockMarkup...) {
59+
self.init(name: name, children: children)
60+
}
61+
62+
/// The name of the parameter being described.
63+
var name: String {
64+
get {
65+
guard case let .doxygenParam(name) = _data.raw.markup.data else {
66+
fatalError("\(self) markup wrapped unexpected \(_data.raw)")
67+
}
68+
return name
69+
}
70+
set {
71+
_data = _data.replacingSelf(.doxygenParam(
72+
name: newValue,
73+
parsedRange: nil,
74+
_data.raw.markup.copyChildren()
75+
))
76+
}
77+
}
78+
}
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) 2023 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+
13+
/// A parsed Doxygen `\returns`, `\return`, or `\result` command.
14+
///
15+
/// The Doxygen support in Swift-Markdown parses `\returns` commands of the form
16+
/// `\returns description`, where `description` continues until the next blank line or parsed
17+
/// command. The commands `\return` and `\result` are also accepted, with the same format.
18+
///
19+
/// ```markdown
20+
/// \returns A freshly-created object.
21+
/// ```
22+
public struct DoxygenReturns: BlockContainer {
23+
public var _data: _MarkupData
24+
25+
init(_ raw: RawMarkup) throws {
26+
guard case .doxygenReturns = raw.data else {
27+
throw RawMarkup.Error.concreteConversionError(from: raw, to: DoxygenReturns.self)
28+
}
29+
let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0))
30+
self.init(_MarkupData(absoluteRaw))
31+
}
32+
33+
init(_ data: _MarkupData) {
34+
self._data = data
35+
}
36+
37+
public func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {
38+
return visitor.visitDoxygenReturns(self)
39+
}
40+
}
41+
42+
public extension DoxygenReturns {
43+
/// Create a new Doxygen parameter definition.
44+
///
45+
/// - Parameter name: The name of the parameter being described.
46+
/// - Parameter children: Block child elements.
47+
init<Children: Sequence>(children: Children) where Children.Element == BlockMarkup {
48+
try! self.init(.doxygenReturns(parsedRange: nil, children.map({ $0.raw.markup })))
49+
}
50+
51+
/// Create a new Doxygen parameter definition.
52+
///
53+
/// - Parameter name: The name of the parameter being described.
54+
/// - Parameter children: Block child elements.
55+
init(children: BlockMarkup...) {
56+
self.init(children: children)
57+
}
58+
}

Sources/Markdown/Inline Nodes/Inline Containers/Link.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -63,6 +63,16 @@ public extension Link {
6363
}
6464
}
6565

66+
var isAutolink: Bool {
67+
guard let destination = destination,
68+
childCount == 1,
69+
let text = child(at: 0) as? Text,
70+
destination == text.string else {
71+
return false
72+
}
73+
return true
74+
}
75+
6676
// MARK: Visitation
6777

6878
func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result {

Sources/Markdown/Markdown.docc/Markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Swift `Markdown` is a Swift package for parsing, building, editing, and analyzin
66

77
The parser is powered by GitHub-flavored Markdown's [cmark-gfm](https://github.com/github/cmark-gfm) implementation, so it follows the spec closely. As the needs of the community change, the effective dialect implemented by this library may change.
88

9-
The markup tree provided by this package is comprised of immutable/persistent, thread-safe, copy-on-write value types that only copy substructure that has changed. Other examples of the main strategy behind this library can be seen in Swift's [lib/Syntax](https://github.com/apple/swift/tree/master/lib/Syntax) and its Swift bindings, [SwiftSyntax](https://github.com/apple/swift-syntax).
9+
The markup tree provided by this package is comprised of immutable/persistent, thread-safe, copy-on-write value types that only copy substructure that has changed. Other examples of the main strategy behind this library can be seen in [SwiftSyntax](https://github.com/apple/swift-syntax).
1010

1111
## Topics
1212

Sources/Markdown/Markdown.docc/Markdown/BlockMarkup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626

2727
## See Also
2828
- <doc:BlockDirectives>
29+
- <doc:DoxygenCommands>
2930

3031
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Doxygen Commands
2+
3+
Include a limited set of Doxygen commands in parsed Markdown.
4+
5+
Swift Markdown includes an option to parse a limited set of Doxygen commands, to facilitate
6+
transitioning from a different Markdown parser. To include these commands in the output, include
7+
the options ``ParseOptions/parseBlockDirectives`` and ``ParseOptions/parseMinimalDoxygen`` when
8+
parsing a ``Document``. In the resulting document, parsed Doxygen commands appear as regular
9+
``Markup`` types in the hierarchy.
10+
11+
## Parsing Strategy
12+
13+
Doxygen commands are written by using either a backslash (`\`) or an at-sign (`@`) character,
14+
followed by the name of the command. Any parameters are then parsed as whitespace-separated words,
15+
then a "description" argument is taken from the remainder of the line, as well as all lines
16+
immediately after the command, until the parser sees a blank line, another Doxygen command, or a
17+
block directive. The description is then parsed for regular Markdown formatting, which is then
18+
stored as the children of the command type. For example, with Doxygen parsing turned on, the
19+
following document will parse three separate commands and one block directive:
20+
21+
```markdown
22+
\param thing The thing.
23+
This is the thing that is modified.
24+
\param otherThing The other thing.
25+
26+
\returns A thing that has been modified.
27+
@Comment {
28+
This is not part of the `\returns` command.
29+
}
30+
```
31+
32+
Trailing lines in a command's description are allowed to be indented relative to the command. For
33+
example, the description below is parsed as a paragraph, not a code block:
34+
35+
```markdown
36+
\param thing
37+
The thing.
38+
This is the thing that is modified.
39+
```
40+
41+
Doxygen commands are not parsed within code blocks or block directive content.
42+
43+
## Topics
44+
45+
### Commands
46+
47+
- ``DoxygenParam``
48+
- ``DoxygenReturns``
49+
50+
<!-- Copyright (c) 2023 Apple Inc and the Swift Project authors. All Rights Reserved. -->

0 commit comments

Comments
 (0)