Skip to content

Commit e506935

Browse files
Publish Swift Markdown's documentation to GitHub pages (#32)
* Adopt the Swift-DocC Plugin for documentation generation * Add a script for publishing docs to GitHub pages * Add missing license headers * Move README docs to articles in the DocC catalog * Remove out-of-date documentation about `DiagnosticEngine` Swift markdown no longer includes a DiagnosticEngine so these links were failing to resolve.
1 parent 5f10cfb commit e506935

13 files changed

+252
-156
lines changed

[email protected]

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
5454
.package(url: "https://github.com/apple/swift-cmark.git", .branch("gfm")),
5555
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "1.0.1")),
5656
]
57+
58+
// SwiftPM command plugins are only supported by Swift version 5.6 and later.
59+
#if swift(>=5.6)
60+
package.dependencies += [
61+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
62+
]
63+
#endif
5764
} else {
5865
// Building in the Swift.org CI system, so rely on local versions of dependencies.
5966
package.dependencies += [

README.md

Lines changed: 3 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Add the dependency to any targets you've declared in your manifest:
2020
.target(name: "MyTarget", dependencies: ["Markdown"]),
2121
```
2222

23-
## Parsing
24-
2523
To parse a document, use `Document(parsing:)`, supplying a `String` or `URL`:
2624

2725
```swift
@@ -38,132 +36,8 @@ print(document.debugDescription())
3836
// └─ Text "."
3937
```
4038

41-
Parsing text is just one way to build a tree of `Markup` elements. You can also build them yourself declaratively.
42-
43-
## Building Markup Trees
44-
45-
You can build trees using initializers for the various element types provided.
46-
47-
```swift
48-
import Markdown
49-
50-
let document = Document(
51-
Paragraph(
52-
Text("This is a "),
53-
Emphasis(
54-
Text("paragraph."))))
55-
```
56-
57-
This would be equivalent to parsing `"This is a *paragraph.*"` but allows you to programmatically insert content from other data sources into individual elements.
58-
59-
## Modifying Markup Trees with Persistence
60-
61-
Swift Markdown uses a [persistent](https://en.wikipedia.org/wiki/Persistent_data_structure) tree for its backing storage, providing effectively immutable, copy-on-write value types that only copy the substructure necessary to create a unique root without affecting the previous version of the tree.
62-
63-
### Modifying Elements Directly
64-
65-
If you just need to make a quick change, you can modify an element anywhere in a tree, and Swift Markdown will create copies of substructure that cannot be shared.
66-
67-
```swift
68-
import Markdown
69-
70-
let source = "This is *emphasized.*"
71-
let document = Document(parsing: source)
72-
print(document.debugDescription())
73-
// Document
74-
// └─ Paragraph
75-
// ├─ Text "This is "
76-
// └─ Emphasis
77-
// └─ Text "emphasized."
78-
79-
var text = document.child(through:
80-
0, // Paragraph
81-
1, // Emphasis
82-
0) as! Text // Text
83-
84-
text.string = "really emphasized!"
85-
print(text.root.debugDescription())
86-
// Document
87-
// └─ Paragraph
88-
// ├─ Text "This is "
89-
// └─ Emphasis
90-
// └─ Text "really emphasized!"
91-
92-
// The original document is unchanged:
93-
94-
print(document.debugDescription())
95-
// Document
96-
// └─ Paragraph
97-
// ├─ Text "This is "
98-
// └─ Emphasis
99-
// └─ Text "emphasized."
100-
```
101-
102-
If you find yourself needing to systematically change many parts of a tree, or even provide a complete transformation into something else, maybe the familiar [Visitor Pattern](https://en.wikipedia.org/wiki/Visitor_pattern) is what you want.
103-
104-
## Visitors, Walkers, and Rewriters
105-
106-
There is a core `MarkupVisitor` protocol that provides the basis for transforming, walking, or rewriting a markup tree.
107-
108-
```swift
109-
public protocol MarkupVisitor {
110-
associatedtype Result
111-
}
112-
```
113-
114-
Using its `Result` type, you can transform a markup tree into anything: another markup tree, or perhaps a tree of XML or HTML elements. There are two included refinements of `MarkupVisitor` for common uses.
115-
116-
The first refinement, `MarkupWalker`, has an associated `Result` type of `Void`, so it's meant for summarizing or detecting aspects of a markup tree. If you wanted to append to a string as elements are visited, this might be a good tool for that.
117-
118-
```swift
119-
import Markdown
120-
121-
/// Counts `Link`s in a `Document`.
122-
struct LinkCounter: MarkupWalker {
123-
var count = 0
124-
mutating func visitLink(_ link: Link) {
125-
if link.destination == "https://swift.org" {
126-
count += 1
127-
}
128-
descendInto(link)
129-
}
130-
}
131-
132-
let source = "There are [two](https://swift.org) links to <https://swift.org> here."
133-
let document = Document(parsing: source)
134-
print(document.debugDescription())
135-
var linkCounter = LinkCounter()
136-
linkCounter.visit(document)
137-
print(linkCounter.count)
138-
// 2
139-
```
140-
141-
The second refinement, `MarkupRewriter`, has an associated `Result` type of `Markup?`, so it's meant to change or even remove elements from a markup tree. You can return `nil` to delete an element, or return another element to substitute in its place.
142-
143-
```swift
144-
import Markdown
145-
146-
/// Delete all **strong** elements in a markup tree.
147-
struct StrongDeleter: MarkupRewriter {
148-
mutating func visitStrong(_ strong: Strong) -> Markup? {
149-
return nil
150-
}
151-
}
152-
153-
let source = "Now you see me, **now you don't**"
154-
let document = Document(parsing: source)
155-
var strongDeleter = StrongDeleter()
156-
let newDocument = strongDeleter.visit(document)
157-
158-
print(newDocument!.debugDescription())
159-
// Document
160-
// └─ Paragraph
161-
// └─ Text "Now you see me, "
162-
```
163-
164-
## Block Directives
165-
166-
Swift Markdown includes a syntax extension for attributed block elements. See [Block Directive](Documentation/BlockDirectives.md) documentation for more information.
39+
Please see Swift `Markdown`'s [documentation site](https://apple.github.io/swift-markdown/documentation/markdown/)
40+
for more detailed information about the library.
16741

16842
## Getting Involved
16943

@@ -188,4 +62,4 @@ Swift Markdown can be improved to better meet your needs.
18862

18963
Please see the [contributing guide](https://swift.org/contributing/#contributing-code) for more information.
19064

191-
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->
65+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Base/DirectiveArgument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public struct DirectiveArgumentText: Equatable {
156156
/// - required: whether the character is required
157157
/// - allowEscape: whether to allow the character to be escaped
158158
/// - diagnoseIfNotFound: if `true` and the character was both required and not found, emit a diagnostic
159-
/// - diagnosticEngine: the diagnostic engine to use if diagnosing
159+
/// - parseErrors: an array to update with any errors encountered while parsing
160160
/// - Returns: `true` if the character was found.
161161
func parseCharacter(_ character: Character,
162162
from line: inout TrimmedLine,
@@ -193,7 +193,7 @@ public struct DirectiveArgumentText: Equatable {
193193
/// - A comma is expected between name-value pairs.
194194
/// - The first argument can be unnamed. An unnamed argument will have an empty ``DirectiveArgument/name`` with no ``DirectiveArgument/nameRange``.
195195
///
196-
/// - Parameter diagnosticEngine: the diagnostic engine to use for emitting diagnostics.
196+
/// - Parameter parseErrors: an array to update with any errors encountered while parsing
197197
/// - Returns: an array of successfully parsed ``DirectiveArgument`` values.
198198
public func parseNameValueArguments(parseErrors: inout [ParseError]) -> [DirectiveArgument] {
199199
var arguments = [DirectiveArgument]()

Sources/Markdown/Markdown.docc/Markdown.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ The parser is powered by GitHub-flavored Markdown's [cmark-gfm](https://github.c
88

99
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).
1010

11-
## Topics
11+
## Topics
12+
13+
### Getting Started
14+
15+
- <doc:Parsing-Building-and-Modifying-Markup-Trees>
16+
- <doc:Visitors-Walkers-and-Rewriters>
1217

1318
### Essentials
1419

Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,7 @@ let document = Document(parsing: source, options: .parseBlockDirectives)
152152

153153
## Collecting Diagnostics
154154

155-
When parsing block directive syntax, Swift Markdown supplies an optional diagnostic infrastructure for reporting parsing problems to a user. See ``Diagnostic``, ``DiagnosticEngine``, and ``DiagnosticConsumer``.
156-
157-
Here is a simple case if you just want to collect diagnostics:
158-
159-
```swift
160-
class DiagnosticCollector: DiagnosticConsumer {
161-
var diagnostics = [Diagnostic]()
162-
func receive(_ diagnostic: Diagnostic) {
163-
diagnostics.append(diagnostic)
164-
}
165-
}
166-
167-
let collector = DiagnosticCollector()
168-
let diagnostics = DiagnosticEngine()
169-
diagnostics.subscribe(collector)
170-
171-
let document = Document(parsing: source,
172-
options: .parseBlockDirectives,
173-
diagnostics: diagnostics)
174-
175-
for diagnostic in collector.diagnostics {
176-
print(diagnostic)
177-
}
178-
```
155+
When parsing block directive syntax, you can optionally provide an array of errors
156+
to collect parsing problems and report them to a user. See ``DirectiveArgumentText/ParseError``.
179157

180158
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626

2727
## See Also
2828
- <doc:BlockDirectives>
29+
30+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Markdown.docc/Markdown/FormatterAndOptions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
### Options
1010

1111
- ``MarkupDumpOptions``
12+
13+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Markdown.docc/Markdown/Infrastructure.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111
- ``SourceLocation``
1212
- ``SourceRange``
13+
14+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Markdown.docc/Markdown/InlineMarkup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
- ``SoftBreak``
1818
- ``SymbolLink``
1919
- ``Text``
20+
21+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Sources/Markdown/Markdown.docc/Markdown/VisitMarkup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
### Rewriter
1616

1717
- ``MarkupRewriter``
18+
19+
<!-- Copyright (c) 2021-2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->

0 commit comments

Comments
 (0)