Skip to content

Commit 2ba1ed5

Browse files
franklinschQuietMisdreavusbitjammeramigid-ronnqvist
committed
Initial commit for open source
Co-authored-by: Victoria Mitchell <victoria_m@apple.com> Co-authored-by: Ashley Garland <acgarland@apple.com> Co-authored-by: Alex Migicovsky <migi@apple.com> Co-authored-by: David Rönnqvist <ronnqvist@apple.com> Co-authored-by: Ethan Kusters <ekusters@apple.com> Co-authored-by: Marin Todorov <marin@underplot.com>
0 parents  commit 2ba1ed5

File tree

119 files changed

+13889
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+13889
-0
lines changed

.github/pull_request_template.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Bug/issue #, if applicable:
2+
3+
## Summary
4+
5+
_Provide a description of what your PR addresses, explaining the expected user experience.
6+
Also, provide an overview of your implementation._
7+
8+
## Dependencies
9+
10+
_Describe any dependencies this PR might have, such as an associated branch in another repository._
11+
12+
## Testing
13+
14+
_Describe how a reviewer can test the functionality of your PR. Provide test content to test with if
15+
applicable._
16+
17+
Steps:
18+
1. _Provide setup instructions._
19+
2. _Explain in detail how the functionality can be tested._
20+
21+
## Checklist
22+
23+
Make sure you check off the following items. If they cannot be completed, provide a reason.
24+
25+
- [ ] Added tests
26+
- [ ] Ran the `./bin/test` script and it succeeded
27+
- [ ] Updated documentation if necessary

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
.DS_Store
10+
/.build
11+
/Packages
12+
/*.xcodeproj

CODE_OF_CONDUCT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Code of Conduct
2+
3+
The code of conduct for this project can be found at https://swift.org/code-of-conduct.
4+
5+
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
By submitting a pull request, you represent that you have the right to license your
2+
contribution to Apple and the community, and agree by submitting the patch that
3+
your contributions are licensed under the [Swift license](https://swift.org/LICENSE.txt).
4+
5+
---
6+
7+
Before submitting the pull request, please make sure you have tested your changes
8+
and that they follow the Swift project [guidelines for contributing
9+
code](https://swift.org/contributing/#contributing-code).
10+
11+
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->

Documentation/BlockDirectives.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Block Directives
2+
3+
Block directives are a syntax extension that create attributed containers to hold other block elements, such as paragraphs and lists, or even other block directives. Here is what one looks like:
4+
5+
```markdown
6+
@Directive(x: 1, y: 2
7+
z: 3) {
8+
- A
9+
- B
10+
- C
11+
}
12+
```
13+
14+
This creates a syntax tree that looks like this:
15+
16+
```
17+
Document
18+
└─ BlockDirective name: "Directive"
19+
├─ Argument text segments:
20+
| "x: 1, y: 2"
21+
| " z: 3"
22+
└─ UnorderedList
23+
├─ ListItem
24+
│ └─ Paragraph
25+
│ └─ Text "A"
26+
├─ ListItem
27+
│ └─ Paragraph
28+
│ └─ Text "B"
29+
└─ ListItem
30+
└─ Paragraph
31+
└─ Text "C"
32+
```
33+
34+
There are three main pieces to a block directive: the name, the argument text, and its content.
35+
36+
## Names
37+
38+
Block directives are opened with an at-symbol `@` immediately followed by a non-empty name. Most characters are allowed except whitespace and punctuation used for other parts of block directive syntax unless they are escaped, such as parentheses `()`, curly brackets `{}`, commas `,`, and colons `:`.
39+
40+
```
41+
BlockDirectiveOpening -> @ BlockDirectiveName
42+
BlockDirectiveName -> [^(){}:, \t]
43+
```
44+
45+
## Argument Text
46+
47+
Block directives can have one or more *argument text segments* inside parentheses.
48+
49+
```
50+
ArgumentText -> ( ArgumentTextSegment ArgumentTextRest? )
51+
| ε
52+
ArgumentTextRest -> \n ArgumentText
53+
ArgumentTextSegment* -> [^)]
54+
55+
* Escaping allowed with a backslash \ character.
56+
```
57+
58+
If you don't need any argument text, you can simply omit the parentheses.
59+
60+
```
61+
@Directive {
62+
- A
63+
- B
64+
- C
65+
}
66+
```
67+
68+
You can parse argument text segments however you like. Swift Markdown also includes a default name-value argument parser that can cover lots of use cases. These are comma-separated pairs of name and value *literals*. For example:
69+
70+
```markdown
71+
@Directive(x: 1, y: "2")
72+
```
73+
74+
When using the name-value argument parser, this results in arguments `x` with value `1` and `y` with value `2`. Names and values are both strings; it's up to you to decide how to convert them into something more specific.
75+
76+
Here is the grammar of name-value argument syntax:
77+
78+
```
79+
Arguments -> Argument ArgumentsRest?
80+
ArgumentsRest -> , Arguments
81+
Argument -> Literal : Literal
82+
Literal -> QuotedLiteral
83+
| UnquotedLiteral
84+
QuotedLiteral -> " QuotedLiteralContent "
85+
QuotedLiteralContent* -> [^:{}(),"]
86+
UnquotedLiteral* -> [^ \t:{}(),"]
87+
88+
* Escaping allowed with a backslash \ character.
89+
```
90+
91+
> Note: Because of the way Markdown is usually parsed, name-value arguments cannot span multiple lines.
92+
93+
## Content
94+
95+
Wrap content with curly brackets `{}`.
96+
97+
```markdown
98+
@Outer {
99+
@Inner {
100+
- A
101+
- B
102+
- C
103+
}
104+
}
105+
```
106+
107+
If a block directive doesn't have any content, you can omit the curly brackets:
108+
109+
```
110+
@TOC
111+
112+
# Title
113+
114+
...
115+
```
116+
117+
## Nesting and Indentation
118+
119+
Since it's very common for block directives to nest, you can indent the lines that make up the name, arguments, and contents any amount.
120+
121+
```markdown
122+
@Outer {
123+
@Inner {
124+
- A
125+
- B
126+
}
127+
}
128+
```
129+
130+
For the contents, indentation is established by the first non-blank line, assuming that indentation for the rest of a directive's contents. Runs of lines that don't make up the definition of a block directive are handed off to the cmark parser. For `@Inner`'s contents above, the cmark parser will see:
131+
132+
```markdown
133+
- A
134+
- B
135+
```
136+
137+
Swift Markdown adjusts the source locations reported by cmark after parsing.
138+
139+
## Enabling Block Directive Syntax
140+
141+
Pass the `.parseBlockDirectives` option when parsing a document to enable block directive syntax:
142+
143+
```swift
144+
let document = Document(parsing: source, options: .parseBlockDirectives)
145+
```
146+
147+
## Collecting Diagnostics
148+
149+
When parsing block directive syntax, Swift Markdown supplies an optional diagnostic infrastructure for reporting parsing problems to a user. See ``Diagnostic``, ``DiagnosticEngine``, and ``DiagnosticConsumer``.
150+
151+
Here is a simple case if you just want to collect diagnostics:
152+
153+
```swift
154+
class DiagnosticCollector: DiagnosticConsumer {
155+
var diagnostics = [Diagnostic]()
156+
func receive(_ diagnostic: Diagnostic) {
157+
diagnostics.append(diagnostic)
158+
}
159+
}
160+
161+
let collector = DiagnosticCollector()
162+
let diagnostics = DiagnosticEngine()
163+
diagnostics.subscribe(collector)
164+
165+
let document = Document(parsing: source,
166+
options: .parseBlockDirectives,
167+
diagnostics: diagnostics)
168+
169+
for diagnostic in collector.diagnostics {
170+
print(diagnostic)
171+
}
172+
```
173+
174+
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->

0 commit comments

Comments
 (0)