Skip to content

Commit 67d76cc

Browse files
authored
Merge pull request swiftlang#131 from google/format-markdown
Add `CommonMark` module for manipulating Markdown ASTs.
2 parents 21bfa7d + 5b4ba07 commit 67d76cc

39 files changed

+3072
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "Sources/CCommonMark/cmark"]
2+
path = Sources/CCommonMark/cmark
3+
url = https://github.com/apple/swift-cmark

Package.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,23 @@ let package = Package(
4040
.target(
4141
name: "Configuration",
4242
dependencies: []),
43+
.target(name: "CommonMark", dependencies: ["CCommonMark"]),
44+
.target(
45+
name: "CCommonMark",
46+
exclude: [
47+
"cmark/api_test",
48+
// We must exclude main.c or SwiftPM will treat this target as an
49+
// executable target and we won't be able to import it from the
50+
// CommonMark Swift module.
51+
"cmark/src/main.c",
52+
]
53+
),
4354
.testTarget(
4455
name: "SwiftFormatTests",
4556
dependencies: ["Core", "Configuration", "Rules", "PrettyPrint", "SwiftSyntax"]),
4657
.testTarget(
4758
name: "PrettyPrinterTests",
4859
dependencies: ["Core", "Configuration", "Rules", "PrettyPrint", "SwiftSyntax"]),
60+
.testTarget(name: "CommonMarkTests", dependencies: ["CommonMark"]),
4961
]
5062
)

Sources/CCommonMark/cmark

Submodule cmark added at d875488

Sources/CCommonMark/include/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This directory contains hand-written versions of C headers for cmark that would
2+
normally be generated by CMake, and a module map that allows it to be imported
3+
from Swift. It is automatically added to clang's header search path by SwiftPM.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Formatter open source project.
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef CMARK_EXPORT_H
16+
#define CMARK_EXPORT_H
17+
18+
#define CMARK_EXPORT
19+
20+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Formatter open source project.
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef CMARK_VERSION_H
16+
#define CMARK_VERSION_H
17+
18+
#define CMARK_VERSION ((0 << 16) | (22 << 8) | 0)
19+
#define CMARK_VERSION_STRING "0.22.0"
20+
21+
#endif

Sources/CCommonMark/include/config.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Formatter open source project.
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#define HAVE_STDBOOL_H
16+
17+
#ifdef HAVE_STDBOOL_H
18+
#include <stdbool.h>
19+
#elif !defined(__cplusplus)
20+
typedef char bool;
21+
#endif
22+
23+
#define HAVE___BUILTIN_EXPECT
24+
25+
#define HAVE___ATTRIBUTE__
26+
27+
#ifdef HAVE___ATTRIBUTE__
28+
#define CMARK_ATTRIBUTE(list) __attribute__ (list)
29+
#else
30+
#define CMARK_ATTRIBUTE(list)
31+
#endif
32+
33+
#ifndef CMARK_INLINE
34+
#if defined(_MSC_VER) && !defined(__cplusplus)
35+
#define CMARK_INLINE __inline
36+
#else
37+
#define CMARK_INLINE inline
38+
#endif
39+
#endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CCommonMark {
2+
header "../cmark/src/cmark.h"
3+
export *
4+
}

Sources/CommonMark/BlockContent.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Formatter open source project.
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// A Markdown node that represents block content; that is, content that occupies the full width of
16+
/// the viewport when rendered.
17+
///
18+
/// Examples of block content include paragraphs, block quotes, and code blocks.
19+
///
20+
/// At this time, the `BlockContent` protocol does not add any members of its own over what is
21+
/// already required by `MarkdownNode`. Instead, it is used as a means of enforcing containment
22+
/// relationships between nodes in the AST.
23+
public protocol BlockContent: MarkdownNode {}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Formatter open source project.
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// A block element that represents a long quotation, typically rendered in a callout box.
16+
public struct BlockQuoteNode: BlockContent {
17+
18+
/// The children of the receiver.
19+
public let children: [BlockContent]
20+
21+
public let sourceRange: Range<SourceLocation>?
22+
23+
public var primitiveRepresentation: PrimitiveNode { return .blockQuote(self) }
24+
25+
/// Creates a new block quote node.
26+
///
27+
/// - Parameters:
28+
/// - children: Block content nodes that are children of the new node.
29+
/// - sourceRange: The source range from which the node was parsed, if known.
30+
public init(children: [BlockContent], sourceRange: Range<SourceLocation>? = nil) {
31+
self.children = children
32+
self.sourceRange = sourceRange
33+
}
34+
35+
/// Returns a new node equivalent to the receiver, but whose children have been replaced with the
36+
/// given list of nodes.
37+
///
38+
/// - Parameter children: The new list of children.
39+
/// - Returns: The new node.
40+
public func replacingChildren(_ children: [BlockContent]) -> BlockQuoteNode {
41+
return BlockQuoteNode(children: children, sourceRange: sourceRange)
42+
}
43+
44+
/// Returns a new node equivalent to the receiver, but whose source range has been replaced with
45+
/// the given value.
46+
///
47+
/// - Parameter sourceRange: The new source range.
48+
/// - Returns: The new node.
49+
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> BlockQuoteNode {
50+
return BlockQuoteNode(children: children, sourceRange: sourceRange)
51+
}
52+
}

0 commit comments

Comments
 (0)