Skip to content

Commit 75bd319

Browse files
authored
Add support for formatting the new Doxygen types using MarkupFormatter (#163)
* Add the new commands to the docs * Remove duplicate code from DoxygenParameter/DoxygenReturns formatters * Add support for formatting DoxygenDiscussion/DoxygenNote * Add tests for DoxygenDiscussion/DoxygenNote printing * Add a test to verify printing both Doxygen prefixes works * Update copyright years
1 parent cfe8c84 commit 75bd319

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

Sources/Markdown/Markdown.docc/Markdown/DoxygenCommands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Doxygen commands are not parsed within code blocks or block directive content.
4444

4545
### Commands
4646

47+
- ``DoxygenDiscussion``
48+
- ``DoxygenNote``
4749
- ``DoxygenParam``
4850
- ``DoxygenReturns``
4951

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 20 additions & 6 deletions
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-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2024 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
@@ -1165,15 +1165,29 @@ public struct MarkupFormatter: MarkupWalker {
11651165
}
11661166
}
11671167

1168-
public mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) -> () {
1169-
print("\(formattingOptions.doxygenCommandPrefix.rawValue)param", for: doxygenParam)
1170-
print(" \(doxygenParam.name) ", for: doxygenParam)
1168+
private mutating func printDoxygenStart(_ name: String, for element: Markup) {
1169+
print(formattingOptions.doxygenCommandPrefix.rawValue + name + " ", for: element)
1170+
}
1171+
1172+
public mutating func visitDoxygenDiscussion(_ doxygenDiscussion: DoxygenDiscussion) {
1173+
printDoxygenStart("discussion", for: doxygenDiscussion)
1174+
descendInto(doxygenDiscussion)
1175+
}
1176+
1177+
public mutating func visitDoxygenNote(_ doxygenNote: DoxygenNote) {
1178+
printDoxygenStart("note", for: doxygenNote)
1179+
descendInto(doxygenNote)
1180+
}
1181+
1182+
public mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) {
1183+
printDoxygenStart("param", for: doxygenParam)
1184+
print("\(doxygenParam.name) ", for: doxygenParam)
11711185
descendInto(doxygenParam)
11721186
}
11731187

1174-
public mutating func visitDoxygenReturns(_ doxygenReturns: DoxygenReturns) -> () {
1188+
public mutating func visitDoxygenReturns(_ doxygenReturns: DoxygenReturns) {
11751189
// FIXME: store the actual command name used in the original markup
1176-
print("\(formattingOptions.doxygenCommandPrefix.rawValue)returns ", for: doxygenReturns)
1190+
printDoxygenStart("returns", for: doxygenReturns)
11771191
descendInto(doxygenReturns)
11781192
}
11791193
}

Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift

Lines changed: 51 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-2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2024 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
@@ -289,6 +289,56 @@ class MarkupFormatterSingleElementTests: XCTestCase {
289289
XCTAssertEqual(expected, printed)
290290
}
291291

292+
func testPrintDoxygenPrefix() {
293+
let expectedSlash = #"\discussion Discussion"#
294+
let printedSlash = DoxygenDiscussion(children: Paragraph(Text("Discussion")))
295+
.format(options: .init(doxygenCommandPrefix: .backslash))
296+
XCTAssertEqual(expectedSlash, printedSlash)
297+
298+
let expectedAt = "@discussion Discussion"
299+
let printedAt = DoxygenDiscussion(children: Paragraph(Text("Discussion")))
300+
.format(options: .init(doxygenCommandPrefix: .at))
301+
XCTAssertEqual(expectedAt, printedAt)
302+
}
303+
304+
func testPrintDoxygenDiscussion() {
305+
let expected = #"\discussion Another thing."#
306+
let printed = DoxygenDiscussion(children: Paragraph(Text("Another thing."))).format()
307+
XCTAssertEqual(expected, printed)
308+
}
309+
310+
func testPrintDoxygenDiscussionMultiline() {
311+
let expected = #"""
312+
\discussion Another thing.
313+
This is an extended discussion.
314+
"""#
315+
let printed = DoxygenDiscussion(children: Paragraph(
316+
Text("Another thing."),
317+
SoftBreak(),
318+
Text("This is an extended discussion.")
319+
)).format()
320+
XCTAssertEqual(expected, printed)
321+
}
322+
323+
func testPrintDoxygenNote() {
324+
let expected = #"\note Another thing."#
325+
let printed = DoxygenNote(children: Paragraph(Text("Another thing."))).format()
326+
XCTAssertEqual(expected, printed)
327+
}
328+
329+
func testPrintDoxygenNoteMultiline() {
330+
let expected = #"""
331+
\note Another thing.
332+
This is an extended discussion.
333+
"""#
334+
let printed = DoxygenNote(children: Paragraph(
335+
Text("Another thing."),
336+
SoftBreak(),
337+
Text("This is an extended discussion.")
338+
)).format()
339+
XCTAssertEqual(expected, printed)
340+
}
341+
292342
func testPrintDoxygenParameter() {
293343
let expected = #"\param thing The thing."#
294344
let printed = DoxygenParameter(name: "thing", children: Paragraph(Text("The thing."))).format()

0 commit comments

Comments
 (0)