From 83d5715577a50e688e0be4b18a61610e132970d8 Mon Sep 17 00:00:00 2001 From: Dylan McArthur Date: Mon, 27 Mar 2017 11:06:49 -0700 Subject: [PATCH 1/2] Created MarkdownOptions, added default options to markdownToHTML(_:options:) --- Sources/cmark.swift/cmark_swift.swift | 43 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/Sources/cmark.swift/cmark_swift.swift b/Sources/cmark.swift/cmark_swift.swift index 9eb75f0..d6200cd 100644 --- a/Sources/cmark.swift/cmark_swift.swift +++ b/Sources/cmark.swift/cmark_swift.swift @@ -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 } From e2b3564d15dfe526c16465ec4215d8136410ce5e Mon Sep 17 00:00:00 2001 From: Dylan McArthur Date: Mon, 27 Mar 2017 11:08:41 -0700 Subject: [PATCH 2/2] Updated API, removed unnecessary warning --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 17df3df..6fd0ec2 100644 --- a/README.md +++ b/README.md @@ -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) //"

Hello

\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