Skip to content

Commit

Permalink
Merge pull request #1 from dmcarth/master
Browse files Browse the repository at this point in the history
Added options and removed warning
  • Loading branch information
tanner0101 authored Mar 29, 2017
2 parents 8c88aa6 + e2b3564 commit ffe2600
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ Very simple Swift wrapper of [cmark](https://github.com/jgm/cmark). Uses a [fork

```swift
let markdown = "# Hello"
let html = try markdownToHTML(markdown)
let html = try markdownToHTML(markdown, options: [])
print(html) //"<h1>Hello</h1>\n"
```

# Warning
Due to a bug in SwiftPM Xcode generation [SR-2526](https://bugs.swift.org/browse/SR-2526), if you're using this package in Xcode, you'll need to add one more header search path to the ccmark target: `Packages/cmark-0.26.1/Sources/ccmark/include`. After you do, everything should build fine in Xcode.

# Installation

## Swift Package Manager
Expand Down
43 changes: 29 additions & 14 deletions Sources/cmark.swift/cmark_swift.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import ccmark

public enum MarkdownError: Error {
case conversionFailed
case conversionFailed
}

public func markdownToHTML(_ str: String) throws -> String {
public struct MarkdownOptions: OptionSet {
public let rawValue: Int32

public init(rawValue: Int32) {
self.rawValue = rawValue
}

static public let sourcePosition = MarkdownOptions(rawValue: 1 << 1)
static public let hardBreaks = MarkdownOptions(rawValue: 1 << 2)
static public let safe = MarkdownOptions(rawValue: 1 << 3)
static public let noBreaks = MarkdownOptions(rawValue: 1 << 4)
static public let normalize = MarkdownOptions(rawValue: 1 << 8)
static public let validateUTF8 = MarkdownOptions(rawValue: 1 << 9)
static public let smartQuotes = MarkdownOptions(rawValue: 1 << 10)
}

public func markdownToHTML(_ str: String, options: MarkdownOptions = []) throws -> String {
var buffer: String?
try str.withCString {
//TODO: add options, right now passing 0
guard let buf = cmark_markdown_to_html($0, Int(strlen($0)), 0) else {
throw MarkdownError.conversionFailed
}
buffer = String(cString: buf)
free(buf)
}
guard let output = buffer else {
throw MarkdownError.conversionFailed
}
return output
try str.withCString {
guard let buf = cmark_markdown_to_html($0, Int(strlen($0)), options.rawValue) else {
throw MarkdownError.conversionFailed
}
buffer = String(cString: buf)
free(buf)
}
guard let output = buffer else {
throw MarkdownError.conversionFailed
}
return output
}

0 comments on commit ffe2600

Please sign in to comment.