Skip to content

Commit

Permalink
Merge pull request swiftlang#131 from google/format-markdown
Browse files Browse the repository at this point in the history
Add `CommonMark` module for manipulating Markdown ASTs.
  • Loading branch information
allevato committed Nov 3, 2018
2 parents 21bfa7d + 5b4ba07 commit 67d76cc
Show file tree
Hide file tree
Showing 39 changed files with 3,072 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Sources/CCommonMark/cmark"]
path = Sources/CCommonMark/cmark
url = https://github.com/apple/swift-cmark
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ let package = Package(
.target(
name: "Configuration",
dependencies: []),
.target(name: "CommonMark", dependencies: ["CCommonMark"]),
.target(
name: "CCommonMark",
exclude: [
"cmark/api_test",
// We must exclude main.c or SwiftPM will treat this target as an
// executable target and we won't be able to import it from the
// CommonMark Swift module.
"cmark/src/main.c",
]
),
.testTarget(
name: "SwiftFormatTests",
dependencies: ["Core", "Configuration", "Rules", "PrettyPrint", "SwiftSyntax"]),
.testTarget(
name: "PrettyPrinterTests",
dependencies: ["Core", "Configuration", "Rules", "PrettyPrint", "SwiftSyntax"]),
.testTarget(name: "CommonMarkTests", dependencies: ["CommonMark"]),
]
)
1 change: 1 addition & 0 deletions Sources/CCommonMark/cmark
Submodule cmark added at d87548
3 changes: 3 additions & 0 deletions Sources/CCommonMark/include/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory contains hand-written versions of C headers for cmark that would
normally be generated by CMake, and a module map that allows it to be imported
from Swift. It is automatically added to clang's header search path by SwiftPM.
20 changes: 20 additions & 0 deletions Sources/CCommonMark/include/cmark_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Formatter open source project.
//
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#ifndef CMARK_EXPORT_H
#define CMARK_EXPORT_H

#define CMARK_EXPORT

#endif
21 changes: 21 additions & 0 deletions Sources/CCommonMark/include/cmark_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Formatter open source project.
//
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#ifndef CMARK_VERSION_H
#define CMARK_VERSION_H

#define CMARK_VERSION ((0 << 16) | (22 << 8) | 0)
#define CMARK_VERSION_STRING "0.22.0"

#endif
39 changes: 39 additions & 0 deletions Sources/CCommonMark/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Formatter open source project.
//
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#define HAVE_STDBOOL_H

#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#elif !defined(__cplusplus)
typedef char bool;
#endif

#define HAVE___BUILTIN_EXPECT

#define HAVE___ATTRIBUTE__

#ifdef HAVE___ATTRIBUTE__
#define CMARK_ATTRIBUTE(list) __attribute__ (list)
#else
#define CMARK_ATTRIBUTE(list)
#endif

#ifndef CMARK_INLINE
#if defined(_MSC_VER) && !defined(__cplusplus)
#define CMARK_INLINE __inline
#else
#define CMARK_INLINE inline
#endif
#endif
4 changes: 4 additions & 0 deletions Sources/CCommonMark/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CCommonMark {
header "../cmark/src/cmark.h"
export *
}
23 changes: 23 additions & 0 deletions Sources/CommonMark/BlockContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Formatter open source project.
//
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// A Markdown node that represents block content; that is, content that occupies the full width of
/// the viewport when rendered.
///
/// Examples of block content include paragraphs, block quotes, and code blocks.
///
/// At this time, the `BlockContent` protocol does not add any members of its own over what is
/// already required by `MarkdownNode`. Instead, it is used as a means of enforcing containment
/// relationships between nodes in the AST.
public protocol BlockContent: MarkdownNode {}
52 changes: 52 additions & 0 deletions Sources/CommonMark/BlockQuoteNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Formatter open source project.
//
// Copyright (c) 2018 Apple Inc. and the Swift Formatter project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Formatter project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// A block element that represents a long quotation, typically rendered in a callout box.
public struct BlockQuoteNode: BlockContent {

/// The children of the receiver.
public let children: [BlockContent]

public let sourceRange: Range<SourceLocation>?

public var primitiveRepresentation: PrimitiveNode { return .blockQuote(self) }

/// Creates a new block quote node.
///
/// - Parameters:
/// - children: Block content nodes that are children of the new node.
/// - sourceRange: The source range from which the node was parsed, if known.
public init(children: [BlockContent], sourceRange: Range<SourceLocation>? = nil) {
self.children = children
self.sourceRange = sourceRange
}

/// Returns a new node equivalent to the receiver, but whose children have been replaced with the
/// given list of nodes.
///
/// - Parameter children: The new list of children.
/// - Returns: The new node.
public func replacingChildren(_ children: [BlockContent]) -> BlockQuoteNode {
return BlockQuoteNode(children: children, sourceRange: sourceRange)
}

/// Returns a new node equivalent to the receiver, but whose source range has been replaced with
/// the given value.
///
/// - Parameter sourceRange: The new source range.
/// - Returns: The new node.
public func replacingSourceRange(_ sourceRange: Range<SourceLocation>?) -> BlockQuoteNode {
return BlockQuoteNode(children: children, sourceRange: sourceRange)
}
}
Loading

0 comments on commit 67d76cc

Please sign in to comment.