From 3e877bb67340f41fd5e85741c11341cfab9699ea Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Mon, 7 Jan 2019 10:59:24 +0100 Subject: [PATCH 01/13] Fix possible memory leaks. --- CommonMark.xcodeproj/project.pbxproj | 16 ++++++++-------- Sources/CommonMark/Node.swift | 14 ++++++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CommonMark.xcodeproj/project.pbxproj b/CommonMark.xcodeproj/project.pbxproj index 5dfd002..c6d1db5 100644 --- a/CommonMark.xcodeproj/project.pbxproj +++ b/CommonMark.xcodeproj/project.pbxproj @@ -108,7 +108,7 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/Ccmark.git--1076946679408688698" + "$(SRCROOT)/.build/checkouts/Ccmark.git-2580460302770786484" ); INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( @@ -117,16 +117,16 @@ ); OTHER_CFLAGS = ( "$(inherited)", - "-I/usr/local/Cellar/commonmark/0.25.2/include" + "-I/usr/local/include" ); OTHER_LDFLAGS = ( "$(inherited)", - "-L/usr/local/Cellar/commonmark/0.25.2/lib", + "-L/usr/local/lib", "-lcmark" ); OTHER_SWIFT_FLAGS = ( "$(inherited)", - "-I/usr/local/Cellar/commonmark/0.25.2/include" + "-I/usr/local/include" ); PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; @@ -150,7 +150,7 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/Ccmark.git--1076946679408688698" + "$(SRCROOT)/.build/checkouts/Ccmark.git-2580460302770786484" ); INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( @@ -159,16 +159,16 @@ ); OTHER_CFLAGS = ( "$(inherited)", - "-I/usr/local/Cellar/commonmark/0.25.2/include" + "-I/usr/local/include" ); OTHER_LDFLAGS = ( "$(inherited)", - "-L/usr/local/Cellar/commonmark/0.25.2/lib", + "-L/usr/local/lib", "-lcmark" ); OTHER_SWIFT_FLAGS = ( "$(inherited)", - "-I/usr/local/Cellar/commonmark/0.25.2/include" + "-I/usr/local/include" ); PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 71abe48..3059fe1 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -33,6 +33,12 @@ extension String { guard let cString = unsafeCString else { return nil } self.init(cString: cString) } + + init(freeingCString str: UnsafeMutablePointer?) { + guard let str = str else { self = ""; return } + self.init(cString: str) + str.deallocate() + } } /// A node in a Markdown document. @@ -144,22 +150,22 @@ public class Node: CustomStringConvertible { /// Renders the HTML representation public var html: String { - return String(cString: cmark_render_html(node, 0)) + return String(freeingCString: cmark_render_html(node, 0)) } /// Renders the XML representation public var xml: String { - return String(cString: cmark_render_xml(node, 0)) + return String(freeingCString: cmark_render_xml(node, 0)) } /// Renders the CommonMark representation public var commonMark: String { - return String(cString: cmark_render_commonmark(node, CMARK_OPT_DEFAULT, 80)) + return String(freeingCString: cmark_render_commonmark(node, CMARK_OPT_DEFAULT, 80)) } /// Renders the LaTeX representation public var latex: String { - return String(cString: cmark_render_latex(node, CMARK_OPT_DEFAULT, 80)) + return String(freeingCString: cmark_render_latex(node, CMARK_OPT_DEFAULT, 80)) } public var description: String { From adff63754fe93e729e55fb293e8eca8afb1eb655 Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Mon, 7 Jan 2019 14:13:24 +0100 Subject: [PATCH 02/13] Another attempt --- Sources/CommonMark/Node.swift | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 3059fe1..324ee44 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -9,6 +9,8 @@ import Foundation import Ccmark + + func markdownToHtml(string: String) -> String { let outString = cmark_markdown_to_html(string, string.utf8.count, 0)! defer { free(outString) } @@ -31,13 +33,15 @@ struct Markdown { extension String { init?(unsafeCString: UnsafePointer!) { guard let cString = unsafeCString else { return nil } - self.init(cString: cString) +// self.init(cString: cString) + self.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: cString), length: strlen(cString), encoding: .utf8, freeWhenDone: true) } - init(freeingCString str: UnsafeMutablePointer?) { - guard let str = str else { self = ""; return } - self.init(cString: str) - str.deallocate() + init?(freeingCString str: UnsafeMutablePointer?) { + guard let s = str else { return nil } + let p = UnsafeMutableRawPointer(s) + self.init(bytesNoCopy: p, length: strlen(s), encoding: .utf8, freeWhenDone: true) +// str.deallocate() } } @@ -150,22 +154,22 @@ public class Node: CustomStringConvertible { /// Renders the HTML representation public var html: String { - return String(freeingCString: cmark_render_html(node, 0)) + return String(freeingCString: cmark_render_html(node, 0)) ?? "" } /// Renders the XML representation public var xml: String { - return String(freeingCString: cmark_render_xml(node, 0)) + return String(freeingCString: cmark_render_xml(node, 0)) ?? "" } /// Renders the CommonMark representation public var commonMark: String { - return String(freeingCString: cmark_render_commonmark(node, CMARK_OPT_DEFAULT, 80)) + return String(freeingCString: cmark_render_commonmark(node, CMARK_OPT_DEFAULT, 80)) ?? "" } /// Renders the LaTeX representation public var latex: String { - return String(freeingCString: cmark_render_latex(node, CMARK_OPT_DEFAULT, 80)) + return String(freeingCString: cmark_render_latex(node, CMARK_OPT_DEFAULT, 80)) ?? "" } public var description: String { From 7bed03219ec27ed7824f52f9d9abb805b3dc7356 Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Mon, 7 Jan 2019 15:59:26 +0100 Subject: [PATCH 03/13] Fixes for memory leaks --- Sources/CommonMark/Node.swift | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 324ee44..f8a6720 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -31,17 +31,23 @@ struct Markdown { } extension String { + // We're going through Data instead of using init(cstring:) because that leaks memory on Linux. + init?(unsafeCString: UnsafePointer!) { guard let cString = unsafeCString else { return nil } -// self.init(cString: cString) - self.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: cString), length: strlen(cString), encoding: .utf8, freeWhenDone: true) + let data = cString.withMemoryRebound(to: UInt8.self, capacity: strlen(cString), { p in + return Data(UnsafeBufferPointer(start: p, count: strlen(cString))) + }) + self.init(data: data, encoding: .utf8) } init?(freeingCString str: UnsafeMutablePointer?) { - guard let s = str else { return nil } - let p = UnsafeMutableRawPointer(s) - self.init(bytesNoCopy: p, length: strlen(s), encoding: .utf8, freeWhenDone: true) -// str.deallocate() + guard let cString = str else { return nil } + let data = cString.withMemoryRebound(to: UInt8.self, capacity: strlen(cString), { p in + return Data(UnsafeBufferPointer(start: p, count: strlen(cString))) + }) + str?.deallocate() + self.init(data: data, encoding: .utf8) } } @@ -88,7 +94,7 @@ public class Node: CustomStringConvertible { } var typeString: String { - return String(cString: cmark_node_get_type_string(node)!) + return String(unsafeCString: cmark_node_get_type_string(node)) ?? "" } var literal: String? { From 229414cdf572555932834eddd63755e3a11cf521 Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Fri, 8 Mar 2019 11:38:05 +0100 Subject: [PATCH 04/13] Use Swift 5 package format --- CommonMark.xcodeproj/project.pbxproj | 282 +++++++++++++++++---------- Package.resolved | 16 -- Package.swift | 19 +- Sources/Ccmark/module.modulemap | 5 + Sources/CommonMark/Node.swift | 32 ++- 5 files changed, 214 insertions(+), 140 deletions(-) delete mode 100644 Package.resolved create mode 100644 Sources/Ccmark/module.modulemap diff --git a/CommonMark.xcodeproj/project.pbxproj b/CommonMark.xcodeproj/project.pbxproj index c6d1db5..58e2506 100644 --- a/CommonMark.xcodeproj/project.pbxproj +++ b/CommonMark.xcodeproj/project.pbxproj @@ -3,12 +3,22 @@ archiveVersion = "1"; objectVersion = "46"; objects = { + "CommonMark::CcMark::ProductTarget" = { + isa = "PBXAggregateTarget"; + buildConfigurationList = "OBJ_20"; + buildPhases = ( + ); + dependencies = ( + ); + name = "CcMark"; + productName = "CcMark"; + }; "CommonMark::CommonMark" = { isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_17"; + buildConfigurationList = "OBJ_24"; buildPhases = ( - "OBJ_20", - "OBJ_24" + "OBJ_27", + "OBJ_31" ); dependencies = ( ); @@ -24,9 +34,9 @@ }; "CommonMark::SwiftPMPackageDescription" = { isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_26"; + buildConfigurationList = "OBJ_33"; buildPhases = ( - "OBJ_29" + "OBJ_36" ); dependencies = ( ); @@ -37,6 +47,7 @@ "OBJ_1" = { isa = "PBXProject"; attributes = { + LastSwiftMigration = "9999"; LastUpgradeCheck = "9999"; }; buildConfigurationList = "OBJ_2"; @@ -47,24 +58,41 @@ "en" ); mainGroup = "OBJ_5"; - productRefGroup = "OBJ_14"; + productRefGroup = "OBJ_15"; projectDirPath = "."; targets = ( + "CommonMark::CcMark::ProductTarget", "CommonMark::CommonMark", "CommonMark::SwiftPMPackageDescription" ); }; "OBJ_10" = { + isa = "PBXGroup"; + children = ( + "OBJ_11", + "OBJ_12", + "OBJ_13" + ); + name = "CommonMark"; + path = "Sources/CommonMark"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_11" = { + isa = "PBXFileReference"; + path = "ASTOperations.swift"; + sourceTree = ""; + }; + "OBJ_12" = { isa = "PBXFileReference"; path = "Node.swift"; sourceTree = ""; }; - "OBJ_11" = { + "OBJ_13" = { isa = "PBXFileReference"; path = "SwiftAST.swift"; sourceTree = ""; }; - "OBJ_12" = { + "OBJ_14" = { isa = "PBXGroup"; children = ( ); @@ -72,15 +100,7 @@ path = ""; sourceTree = "SOURCE_ROOT"; }; - "OBJ_13" = { - isa = "PBXGroup"; - children = ( - ); - name = "Dependencies"; - path = ""; - sourceTree = ""; - }; - "OBJ_14" = { + "OBJ_15" = { isa = "PBXGroup"; children = ( "CommonMark::CommonMark::Product" @@ -90,15 +110,55 @@ sourceTree = "BUILT_PRODUCTS_DIR"; }; "OBJ_17" = { + isa = "PBXFileReference"; + path = "Package.pins"; + sourceTree = ""; + }; + "OBJ_18" = { + isa = "PBXFileReference"; + path = "README.md"; + sourceTree = ""; + }; + "OBJ_2" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_18", - "OBJ_19" + "OBJ_3", + "OBJ_4" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_18" = { + "OBJ_20" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_21", + "OBJ_22" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_21" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Debug"; + }; + "OBJ_22" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Release"; + }; + "OBJ_24" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_25", + "OBJ_26" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_25" = { isa = "XCBuildConfiguration"; buildSettings = { ENABLE_TESTABILITY = "YES"; @@ -108,13 +168,15 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/Ccmark.git-2580460302770786484" + "$(SRCROOT)/Sources/Ccmark" ); INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)", "-I/usr/local/include" @@ -135,12 +197,14 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "CommonMark"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Debug"; }; - "OBJ_19" = { + "OBJ_26" = { isa = "XCBuildConfiguration"; buildSettings = { ENABLE_TESTABILITY = "YES"; @@ -150,13 +214,15 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/Ccmark.git-2580460302770786484" + "$(SRCROOT)/Sources/Ccmark" ); INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)", "-I/usr/local/include" @@ -177,136 +243,131 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "CommonMark"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Release"; }; - "OBJ_2" = { - isa = "XCConfigurationList"; - buildConfigurations = ( - "OBJ_3", - "OBJ_4" - ); - defaultConfigurationIsVisible = "0"; - defaultConfigurationName = "Release"; - }; - "OBJ_20" = { + "OBJ_27" = { isa = "PBXSourcesBuildPhase"; files = ( - "OBJ_21", - "OBJ_22", - "OBJ_23" + "OBJ_28", + "OBJ_29", + "OBJ_30" ); }; - "OBJ_21" = { + "OBJ_28" = { isa = "PBXBuildFile"; - fileRef = "OBJ_9"; + fileRef = "OBJ_11"; }; - "OBJ_22" = { + "OBJ_29" = { isa = "PBXBuildFile"; - fileRef = "OBJ_10"; + fileRef = "OBJ_12"; + }; + "OBJ_3" = { + isa = "XCBuildConfiguration"; + buildSettings = { + CLANG_ENABLE_OBJC_ARC = "YES"; + COMBINE_HIDPI_IMAGES = "YES"; + COPY_PHASE_STRIP = "NO"; + DEBUG_INFORMATION_FORMAT = "dwarf"; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_NS_ASSERTIONS = "YES"; + GCC_OPTIMIZATION_LEVEL = "0"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1", + "DEBUG=1" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + ONLY_ACTIVE_ARCH = "YES"; + OTHER_SWIFT_FLAGS = ( + "-DXcode" + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = "macosx"; + SUPPORTED_PLATFORMS = ( + "macosx", + "iphoneos", + "iphonesimulator", + "appletvos", + "appletvsimulator", + "watchos", + "watchsimulator" + ); + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE", + "DEBUG" + ); + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + USE_HEADERMAP = "NO"; + }; + name = "Debug"; }; - "OBJ_23" = { + "OBJ_30" = { isa = "PBXBuildFile"; - fileRef = "OBJ_11"; + fileRef = "OBJ_13"; }; - "OBJ_24" = { + "OBJ_31" = { isa = "PBXFrameworksBuildPhase"; files = ( ); }; - "OBJ_26" = { + "OBJ_33" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_27", - "OBJ_28" + "OBJ_34", + "OBJ_35" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_27" = { + "OBJ_34" = { isa = "XCBuildConfiguration"; buildSettings = { LD = "/usr/bin/true"; OTHER_SWIFT_FLAGS = ( "-swift-version", - "4.2", + "5", "-I", "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", "-target", "x86_64-apple-macosx10.10", "-sdk", - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" + "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; }; name = "Debug"; }; - "OBJ_28" = { + "OBJ_35" = { isa = "XCBuildConfiguration"; buildSettings = { LD = "/usr/bin/true"; OTHER_SWIFT_FLAGS = ( "-swift-version", - "4.2", + "5", "-I", "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", "-target", "x86_64-apple-macosx10.10", "-sdk", - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" + "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; }; name = "Release"; }; - "OBJ_29" = { + "OBJ_36" = { isa = "PBXSourcesBuildPhase"; files = ( - "OBJ_30" + "OBJ_37" ); }; - "OBJ_3" = { - isa = "XCBuildConfiguration"; - buildSettings = { - CLANG_ENABLE_OBJC_ARC = "YES"; - COMBINE_HIDPI_IMAGES = "YES"; - COPY_PHASE_STRIP = "NO"; - DEBUG_INFORMATION_FORMAT = "dwarf"; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = "YES"; - GCC_OPTIMIZATION_LEVEL = "0"; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)" - ); - MACOSX_DEPLOYMENT_TARGET = "10.10"; - ONLY_ACTIVE_ARCH = "YES"; - OTHER_SWIFT_FLAGS = ( - "-DXcode" - ); - PRODUCT_NAME = "$(TARGET_NAME)"; - SDKROOT = "macosx"; - SUPPORTED_PLATFORMS = ( - "macosx", - "iphoneos", - "iphonesimulator", - "appletvos", - "appletvsimulator", - "watchos", - "watchsimulator" - ); - SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( - "SWIFT_PACKAGE", - "DEBUG" - ); - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - USE_HEADERMAP = "NO"; - }; - name = "Debug"; - }; - "OBJ_30" = { + "OBJ_37" = { isa = "PBXBuildFile"; fileRef = "OBJ_6"; }; @@ -319,6 +380,10 @@ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DYLIB_INSTALL_NAME_BASE = "@rpath"; GCC_OPTIMIZATION_LEVEL = "s"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1" + ); MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_SWIFT_FLAGS = ( "-DXcode" @@ -335,6 +400,7 @@ "watchsimulator" ); SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", "SWIFT_PACKAGE" ); SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -347,9 +413,10 @@ children = ( "OBJ_6", "OBJ_7", - "OBJ_12", - "OBJ_13", - "OBJ_14" + "OBJ_14", + "OBJ_15", + "OBJ_17", + "OBJ_18" ); path = ""; sourceTree = ""; @@ -363,7 +430,8 @@ "OBJ_7" = { isa = "PBXGroup"; children = ( - "OBJ_8" + "OBJ_8", + "OBJ_10" ); name = "Sources"; path = ""; @@ -372,17 +440,15 @@ "OBJ_8" = { isa = "PBXGroup"; children = ( - "OBJ_9", - "OBJ_10", - "OBJ_11" + "OBJ_9" ); - name = "CommonMark"; - path = "Sources/CommonMark"; + name = "Ccmark"; + path = "Sources/Ccmark"; sourceTree = "SOURCE_ROOT"; }; "OBJ_9" = { isa = "PBXFileReference"; - path = "ASTOperations.swift"; + path = "module.modulemap"; sourceTree = ""; }; }; diff --git a/Package.resolved b/Package.resolved deleted file mode 100644 index 8f9a492..0000000 --- a/Package.resolved +++ /dev/null @@ -1,16 +0,0 @@ -{ - "object": { - "pins": [ - { - "package": "Ccmark", - "repositoryURL": "https://github.com/objcio/Ccmark.git", - "state": { - "branch": "master", - "revision": "2d2002db33c3f077635538031008c6893a4c0ec2", - "version": null - } - } - ] - }, - "version": 1 -} diff --git a/Package.swift b/Package.swift index a0d7ee3..e5e7905 100644 --- a/Package.swift +++ b/Package.swift @@ -1,16 +1,23 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( name: "CommonMark", products: [ - .library(name: "CommonMark", targets: ["CommonMark"]) - ], - dependencies: [ - .package(url: "https://github.com/objcio/Ccmark.git", .branch("master")) + .library(name: "CommonMark", targets: ["CommonMark"]), + .library(name: "CcMark", targets: ["Ccmark"]), + ], + dependencies: [], targets: [ - .target(name: "CommonMark") + .target(name: "CommonMark", dependencies: ["Ccmark"]), + .systemLibrary( + name: "Ccmark", + pkgConfig: "libcmark", + providers: [ + .brew(["commonmark"]) + ] + ) ] ) diff --git a/Sources/Ccmark/module.modulemap b/Sources/Ccmark/module.modulemap new file mode 100644 index 0000000..fd0db12 --- /dev/null +++ b/Sources/Ccmark/module.modulemap @@ -0,0 +1,5 @@ +module Ccmark [system] { + header "/usr/local/include/cmark.h" + link "libcmark" + export * +} diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index f8a6720..5ea0b11 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -51,6 +51,11 @@ extension String { } } +public struct Position { + public var line: Int32 + public var column: Int32 +} + /// A node in a Markdown document. /// /// Can represent a full Markdown document (i.e. the document's root node) or @@ -79,25 +84,25 @@ public class Node: CustomStringConvertible { cmark_node_free(node) } - var type: cmark_node_type { + public var type: cmark_node_type { return cmark_node_get_type(node) } - var listType: cmark_list_type { + public var listType: cmark_list_type { get { return cmark_node_get_list_type(node) } set { cmark_node_set_list_type(node, newValue) } } - var listStart: Int { + public var listStart: Int { get { return Int(cmark_node_get_list_start(node)) } set { cmark_node_set_list_start(node, Int32(newValue)) } } - var typeString: String { + public var typeString: String { return String(unsafeCString: cmark_node_get_type_string(node)) ?? "" } - var literal: String? { + public var literal: String? { get { return String(unsafeCString: cmark_node_get_literal(node)) } set { if let value = newValue { @@ -108,12 +113,19 @@ public class Node: CustomStringConvertible { } } - var headerLevel: Int { + public var start: Position { + return Position(line: cmark_node_get_start_line(node), column: cmark_node_get_start_column(node)) + } + public var end: Position { + return Position(line: cmark_node_get_start_line(node), column: cmark_node_get_start_column(node)) + } + + public var headerLevel: Int { get { return Int(cmark_node_get_heading_level(node)) } set { cmark_node_set_heading_level(node, Int32(newValue)) } } - var fenceInfo: String? { + public var fenceInfo: String? { get { return String(unsafeCString: cmark_node_get_fence_info(node)) } set { @@ -125,7 +137,7 @@ public class Node: CustomStringConvertible { } } - var urlString: String? { + public var urlString: String? { get { return String(unsafeCString: cmark_node_get_url(node)) } set { if let value = newValue { @@ -136,7 +148,7 @@ public class Node: CustomStringConvertible { } } - var title: String? { + public var title: String? { get { return String(unsafeCString: cmark_node_get_title(node)) } set { if let value = newValue { @@ -147,7 +159,7 @@ public class Node: CustomStringConvertible { } } - var children: [Node] { + public var children: [Node] { var result: [Node] = [] var child = cmark_node_first_child(node) From c513552584eda6507dbca5e2fcc8167c8f9f859e Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Fri, 8 Mar 2019 14:38:33 +0100 Subject: [PATCH 05/13] Fix --- Package.swift | 2 +- Sources/CommonMark/Node.swift | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index e5e7905..c2eb8ac 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ let package = Package( name: "CommonMark", products: [ .library(name: "CommonMark", targets: ["CommonMark"]), - .library(name: "CcMark", targets: ["Ccmark"]), + .library(name: "Ccmark", targets: ["Ccmark"]), ], dependencies: [], diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 5ea0b11..99eb4f6 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -51,6 +51,7 @@ extension String { } } +/// A position in a Markdown document. Note that both `line` and `column` are 1-based. public struct Position { public var line: Int32 public var column: Int32 @@ -117,7 +118,7 @@ public class Node: CustomStringConvertible { return Position(line: cmark_node_get_start_line(node), column: cmark_node_get_start_column(node)) } public var end: Position { - return Position(line: cmark_node_get_start_line(node), column: cmark_node_get_start_column(node)) + return Position(line: cmark_node_get_end_line(node), column: cmark_node_get_end_column(node)) } public var headerLevel: Int { From 32d9017120ac52249debbea87f3e1c5c4023f4d3 Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Tue, 2 Apr 2019 13:32:14 +0200 Subject: [PATCH 06/13] Algebras. --- CommonMark.xcodeproj/project.pbxproj | 845 +++++++++++++-------------- Sources/CommonMark/Reduce.swift | 112 ++++ Sources/CommonMark/SwiftAST.swift | 8 +- 3 files changed, 508 insertions(+), 457 deletions(-) create mode 100644 Sources/CommonMark/Reduce.swift diff --git a/CommonMark.xcodeproj/project.pbxproj b/CommonMark.xcodeproj/project.pbxproj index 58e2506..93933e3 100644 --- a/CommonMark.xcodeproj/project.pbxproj +++ b/CommonMark.xcodeproj/project.pbxproj @@ -1,456 +1,395 @@ // !$*UTF8*$! { - archiveVersion = "1"; - objectVersion = "46"; - objects = { - "CommonMark::CcMark::ProductTarget" = { - isa = "PBXAggregateTarget"; - buildConfigurationList = "OBJ_20"; - buildPhases = ( - ); - dependencies = ( - ); - name = "CcMark"; - productName = "CcMark"; - }; - "CommonMark::CommonMark" = { - isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_24"; - buildPhases = ( - "OBJ_27", - "OBJ_31" - ); - dependencies = ( - ); - name = "CommonMark"; - productName = "CommonMark"; - productReference = "CommonMark::CommonMark::Product"; - productType = "com.apple.product-type.framework"; - }; - "CommonMark::CommonMark::Product" = { - isa = "PBXFileReference"; - path = "CommonMark.framework"; - sourceTree = "BUILT_PRODUCTS_DIR"; - }; - "CommonMark::SwiftPMPackageDescription" = { - isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_33"; - buildPhases = ( - "OBJ_36" - ); - dependencies = ( - ); - name = "CommonMarkPackageDescription"; - productName = "CommonMarkPackageDescription"; - productType = "com.apple.product-type.framework"; - }; - "OBJ_1" = { - isa = "PBXProject"; - attributes = { - LastSwiftMigration = "9999"; - LastUpgradeCheck = "9999"; - }; - buildConfigurationList = "OBJ_2"; - compatibilityVersion = "Xcode 3.2"; - developmentRegion = "English"; - hasScannedForEncodings = "0"; - knownRegions = ( - "en" - ); - mainGroup = "OBJ_5"; - productRefGroup = "OBJ_15"; - projectDirPath = "."; - targets = ( - "CommonMark::CcMark::ProductTarget", - "CommonMark::CommonMark", - "CommonMark::SwiftPMPackageDescription" - ); - }; - "OBJ_10" = { - isa = "PBXGroup"; - children = ( - "OBJ_11", - "OBJ_12", - "OBJ_13" - ); - name = "CommonMark"; - path = "Sources/CommonMark"; - sourceTree = "SOURCE_ROOT"; - }; - "OBJ_11" = { - isa = "PBXFileReference"; - path = "ASTOperations.swift"; - sourceTree = ""; - }; - "OBJ_12" = { - isa = "PBXFileReference"; - path = "Node.swift"; - sourceTree = ""; - }; - "OBJ_13" = { - isa = "PBXFileReference"; - path = "SwiftAST.swift"; - sourceTree = ""; - }; - "OBJ_14" = { - isa = "PBXGroup"; - children = ( - ); - name = "Tests"; - path = ""; - sourceTree = "SOURCE_ROOT"; - }; - "OBJ_15" = { - isa = "PBXGroup"; - children = ( - "CommonMark::CommonMark::Product" - ); - name = "Products"; - path = ""; - sourceTree = "BUILT_PRODUCTS_DIR"; - }; - "OBJ_17" = { - isa = "PBXFileReference"; - path = "Package.pins"; - sourceTree = ""; - }; - "OBJ_18" = { - isa = "PBXFileReference"; - path = "README.md"; - sourceTree = ""; - }; - "OBJ_2" = { - isa = "XCConfigurationList"; - buildConfigurations = ( - "OBJ_3", - "OBJ_4" - ); - defaultConfigurationIsVisible = "0"; - defaultConfigurationName = "Release"; - }; - "OBJ_20" = { - isa = "XCConfigurationList"; - buildConfigurations = ( - "OBJ_21", - "OBJ_22" - ); - defaultConfigurationIsVisible = "0"; - defaultConfigurationName = "Release"; - }; - "OBJ_21" = { - isa = "XCBuildConfiguration"; - buildSettings = { - }; - name = "Debug"; - }; - "OBJ_22" = { - isa = "XCBuildConfiguration"; - buildSettings = { - }; - name = "Release"; - }; - "OBJ_24" = { - isa = "XCConfigurationList"; - buildConfigurations = ( - "OBJ_25", - "OBJ_26" - ); - defaultConfigurationIsVisible = "0"; - defaultConfigurationName = "Release"; - }; - "OBJ_25" = { - isa = "XCBuildConfiguration"; - buildSettings = { - ENABLE_TESTABILITY = "YES"; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PLATFORM_DIR)/Developer/Library/Frameworks" - ); - HEADER_SEARCH_PATHS = ( - "$(inherited)", - "$(SRCROOT)/Sources/Ccmark" - ); - INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = "8.0"; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" - ); - MACOSX_DEPLOYMENT_TARGET = "10.10"; - OTHER_CFLAGS = ( - "$(inherited)", - "-I/usr/local/include" - ); - OTHER_LDFLAGS = ( - "$(inherited)", - "-L/usr/local/lib", - "-lcmark" - ); - OTHER_SWIFT_FLAGS = ( - "$(inherited)", - "-I/usr/local/include" - ); - PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; - PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = "YES"; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( - "$(inherited)" - ); - SWIFT_VERSION = "5.0"; - TARGET_NAME = "CommonMark"; - TVOS_DEPLOYMENT_TARGET = "9.0"; - WATCHOS_DEPLOYMENT_TARGET = "2.0"; - }; - name = "Debug"; - }; - "OBJ_26" = { - isa = "XCBuildConfiguration"; - buildSettings = { - ENABLE_TESTABILITY = "YES"; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PLATFORM_DIR)/Developer/Library/Frameworks" - ); - HEADER_SEARCH_PATHS = ( - "$(inherited)", - "$(SRCROOT)/Sources/Ccmark" - ); - INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = "8.0"; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" - ); - MACOSX_DEPLOYMENT_TARGET = "10.10"; - OTHER_CFLAGS = ( - "$(inherited)", - "-I/usr/local/include" - ); - OTHER_LDFLAGS = ( - "$(inherited)", - "-L/usr/local/lib", - "-lcmark" - ); - OTHER_SWIFT_FLAGS = ( - "$(inherited)", - "-I/usr/local/include" - ); - PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; - PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = "YES"; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( - "$(inherited)" - ); - SWIFT_VERSION = "5.0"; - TARGET_NAME = "CommonMark"; - TVOS_DEPLOYMENT_TARGET = "9.0"; - WATCHOS_DEPLOYMENT_TARGET = "2.0"; - }; - name = "Release"; - }; - "OBJ_27" = { - isa = "PBXSourcesBuildPhase"; - files = ( - "OBJ_28", - "OBJ_29", - "OBJ_30" - ); - }; - "OBJ_28" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_11"; - }; - "OBJ_29" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_12"; - }; - "OBJ_3" = { - isa = "XCBuildConfiguration"; - buildSettings = { - CLANG_ENABLE_OBJC_ARC = "YES"; - COMBINE_HIDPI_IMAGES = "YES"; - COPY_PHASE_STRIP = "NO"; - DEBUG_INFORMATION_FORMAT = "dwarf"; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = "YES"; - GCC_OPTIMIZATION_LEVEL = "0"; - GCC_PREPROCESSOR_DEFINITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE=1", - "DEBUG=1" - ); - MACOSX_DEPLOYMENT_TARGET = "10.10"; - ONLY_ACTIVE_ARCH = "YES"; - OTHER_SWIFT_FLAGS = ( - "-DXcode" - ); - PRODUCT_NAME = "$(TARGET_NAME)"; - SDKROOT = "macosx"; - SUPPORTED_PLATFORMS = ( - "macosx", - "iphoneos", - "iphonesimulator", - "appletvos", - "appletvsimulator", - "watchos", - "watchsimulator" - ); - SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE", - "DEBUG" - ); - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - USE_HEADERMAP = "NO"; - }; - name = "Debug"; - }; - "OBJ_30" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_13"; - }; - "OBJ_31" = { - isa = "PBXFrameworksBuildPhase"; - files = ( - ); - }; - "OBJ_33" = { - isa = "XCConfigurationList"; - buildConfigurations = ( - "OBJ_34", - "OBJ_35" - ); - defaultConfigurationIsVisible = "0"; - defaultConfigurationName = "Release"; - }; - "OBJ_34" = { - isa = "XCBuildConfiguration"; - buildSettings = { - LD = "/usr/bin/true"; - OTHER_SWIFT_FLAGS = ( - "-swift-version", - "5", - "-I", - "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", - "-target", - "x86_64-apple-macosx10.10", - "-sdk", - "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" - ); - SWIFT_VERSION = "5.0"; - }; - name = "Debug"; - }; - "OBJ_35" = { - isa = "XCBuildConfiguration"; - buildSettings = { - LD = "/usr/bin/true"; - OTHER_SWIFT_FLAGS = ( - "-swift-version", - "5", - "-I", - "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", - "-target", - "x86_64-apple-macosx10.10", - "-sdk", - "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" - ); - SWIFT_VERSION = "5.0"; - }; - name = "Release"; - }; - "OBJ_36" = { - isa = "PBXSourcesBuildPhase"; - files = ( - "OBJ_37" - ); - }; - "OBJ_37" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_6"; - }; - "OBJ_4" = { - isa = "XCBuildConfiguration"; - buildSettings = { - CLANG_ENABLE_OBJC_ARC = "YES"; - COMBINE_HIDPI_IMAGES = "YES"; - COPY_PHASE_STRIP = "YES"; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - GCC_OPTIMIZATION_LEVEL = "s"; - GCC_PREPROCESSOR_DEFINITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE=1" - ); - MACOSX_DEPLOYMENT_TARGET = "10.10"; - OTHER_SWIFT_FLAGS = ( - "-DXcode" - ); - PRODUCT_NAME = "$(TARGET_NAME)"; - SDKROOT = "macosx"; - SUPPORTED_PLATFORMS = ( - "macosx", - "iphoneos", - "iphonesimulator", - "appletvos", - "appletvsimulator", - "watchos", - "watchsimulator" - ); - SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE" - ); - SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - USE_HEADERMAP = "NO"; - }; - name = "Release"; - }; - "OBJ_5" = { - isa = "PBXGroup"; - children = ( - "OBJ_6", - "OBJ_7", - "OBJ_14", - "OBJ_15", - "OBJ_17", - "OBJ_18" - ); - path = ""; - sourceTree = ""; - }; - "OBJ_6" = { - isa = "PBXFileReference"; - explicitFileType = "sourcecode.swift"; - path = "Package.swift"; - sourceTree = ""; - }; - "OBJ_7" = { - isa = "PBXGroup"; - children = ( - "OBJ_8", - "OBJ_10" - ); - name = "Sources"; - path = ""; - sourceTree = "SOURCE_ROOT"; - }; - "OBJ_8" = { - isa = "PBXGroup"; - children = ( - "OBJ_9" - ); - name = "Ccmark"; - path = "Sources/Ccmark"; - sourceTree = "SOURCE_ROOT"; - }; - "OBJ_9" = { - isa = "PBXFileReference"; - path = "module.modulemap"; - sourceTree = ""; - }; - }; - rootObject = "OBJ_1"; + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXAggregateTarget section */ + "CommonMark::CcMark::ProductTarget" /* CcMark */ = { + isa = PBXAggregateTarget; + buildConfigurationList = OBJ_20 /* Build configuration list for PBXAggregateTarget "CcMark" */; + buildPhases = ( + ); + dependencies = ( + ); + name = CcMark; + productName = CcMark; + }; +/* End PBXAggregateTarget section */ + +/* Begin PBXBuildFile section */ + 830B48C022537DCC00C9FCE3 /* Reduce.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830B48BF22537DCC00C9FCE3 /* Reduce.swift */; }; + OBJ_28 /* ASTOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* ASTOperations.swift */; }; + OBJ_29 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* Node.swift */; }; + OBJ_30 /* SwiftAST.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* SwiftAST.swift */; }; + OBJ_37 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 830B48BF22537DCC00C9FCE3 /* Reduce.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reduce.swift; sourceTree = ""; }; + "CommonMark::CommonMark::Product" /* CommonMark.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CommonMark.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + OBJ_11 /* ASTOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASTOperations.swift; sourceTree = ""; }; + OBJ_12 /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; + OBJ_13 /* SwiftAST.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftAST.swift; sourceTree = ""; }; + OBJ_17 /* Package.pins */ = {isa = PBXFileReference; lastKnownFileType = text; path = Package.pins; sourceTree = ""; }; + OBJ_18 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; + OBJ_9 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + OBJ_31 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + OBJ_10 /* CommonMark */ = { + isa = PBXGroup; + children = ( + OBJ_11 /* ASTOperations.swift */, + OBJ_12 /* Node.swift */, + OBJ_13 /* SwiftAST.swift */, + 830B48BF22537DCC00C9FCE3 /* Reduce.swift */, + ); + name = CommonMark; + path = Sources/CommonMark; + sourceTree = SOURCE_ROOT; + }; + OBJ_14 /* Tests */ = { + isa = PBXGroup; + children = ( + ); + name = Tests; + sourceTree = SOURCE_ROOT; + }; + OBJ_15 /* Products */ = { + isa = PBXGroup; + children = ( + "CommonMark::CommonMark::Product" /* CommonMark.framework */, + ); + name = Products; + sourceTree = BUILT_PRODUCTS_DIR; + }; + OBJ_5 /* */ = { + isa = PBXGroup; + children = ( + OBJ_6 /* Package.swift */, + OBJ_7 /* Sources */, + OBJ_14 /* Tests */, + OBJ_15 /* Products */, + OBJ_17 /* Package.pins */, + OBJ_18 /* README.md */, + ); + name = ""; + sourceTree = ""; + }; + OBJ_7 /* Sources */ = { + isa = PBXGroup; + children = ( + OBJ_8 /* Ccmark */, + OBJ_10 /* CommonMark */, + ); + name = Sources; + sourceTree = SOURCE_ROOT; + }; + OBJ_8 /* Ccmark */ = { + isa = PBXGroup; + children = ( + OBJ_9 /* module.modulemap */, + ); + name = Ccmark; + path = Sources/Ccmark; + sourceTree = SOURCE_ROOT; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + "CommonMark::CommonMark" /* CommonMark */ = { + isa = PBXNativeTarget; + buildConfigurationList = OBJ_24 /* Build configuration list for PBXNativeTarget "CommonMark" */; + buildPhases = ( + OBJ_27 /* Sources */, + OBJ_31 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CommonMark; + productName = CommonMark; + productReference = "CommonMark::CommonMark::Product" /* CommonMark.framework */; + productType = "com.apple.product-type.framework"; + }; + "CommonMark::SwiftPMPackageDescription" /* CommonMarkPackageDescription */ = { + isa = PBXNativeTarget; + buildConfigurationList = OBJ_33 /* Build configuration list for PBXNativeTarget "CommonMarkPackageDescription" */; + buildPhases = ( + OBJ_36 /* Sources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CommonMarkPackageDescription; + productName = CommonMarkPackageDescription; + productType = "com.apple.product-type.framework"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + OBJ_1 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftMigration = 9999; + LastUpgradeCheck = 9999; + }; + buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "CommonMark" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + English, + en, + ); + mainGroup = OBJ_5 /* */; + productRefGroup = OBJ_15 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + "CommonMark::CcMark::ProductTarget" /* CcMark */, + "CommonMark::CommonMark" /* CommonMark */, + "CommonMark::SwiftPMPackageDescription" /* CommonMarkPackageDescription */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + OBJ_27 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 0; + files = ( + OBJ_28 /* ASTOperations.swift in Sources */, + OBJ_29 /* Node.swift in Sources */, + OBJ_30 /* SwiftAST.swift in Sources */, + 830B48C022537DCC00C9FCE3 /* Reduce.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + OBJ_36 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 0; + files = ( + OBJ_37 /* Package.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + OBJ_21 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Debug; + }; + OBJ_22 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Release; + }; + OBJ_25 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ENABLE_TESTABILITY = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks", + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark", + ); + INFOPLIST_FILE = CommonMark.xcodeproj/CommonMark_Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; + MACOSX_DEPLOYMENT_TARGET = 10.10; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include", + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark", + ); + OTHER_SWIFT_FLAGS = "$(inherited) -I/usr/local/include"; + PRODUCT_BUNDLE_IDENTIFIER = CommonMark; + PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; + SWIFT_VERSION = 5.0; + TARGET_NAME = CommonMark; + TVOS_DEPLOYMENT_TARGET = 9.0; + WATCHOS_DEPLOYMENT_TARGET = 2.0; + }; + name = Debug; + }; + OBJ_26 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ENABLE_TESTABILITY = YES; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks", + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark", + ); + INFOPLIST_FILE = CommonMark.xcodeproj/CommonMark_Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; + MACOSX_DEPLOYMENT_TARGET = 10.10; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include", + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark", + ); + OTHER_SWIFT_FLAGS = "$(inherited) -I/usr/local/include"; + PRODUCT_BUNDLE_IDENTIFIER = CommonMark; + PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; + SWIFT_VERSION = 5.0; + TARGET_NAME = CommonMark; + TVOS_DEPLOYMENT_TARGET = 9.0; + WATCHOS_DEPLOYMENT_TARGET = 2.0; + }; + name = Release; + }; + OBJ_3 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_ARC = YES; + COMBINE_HIDPI_IMAGES = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_NS_ASSERTIONS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1", + "DEBUG=1", + ); + MACOSX_DEPLOYMENT_TARGET = 10.10; + ONLY_ACTIVE_ARCH = YES; + OTHER_SWIFT_FLAGS = "-DXcode"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE DEBUG"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + USE_HEADERMAP = NO; + }; + name = Debug; + }; + OBJ_34 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + LD = /usr/bin/true; + OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + OBJ_35 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + LD = /usr/bin/true; + OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + OBJ_4 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_OBJC_ARC = YES; + COMBINE_HIDPI_IMAGES = YES; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + GCC_OPTIMIZATION_LEVEL = s; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1", + ); + MACOSX_DEPLOYMENT_TARGET = 10.10; + OTHER_SWIFT_FLAGS = "-DXcode"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + USE_HEADERMAP = NO; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + OBJ_2 /* Build configuration list for PBXProject "CommonMark" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + OBJ_3 /* Debug */, + OBJ_4 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + OBJ_20 /* Build configuration list for PBXAggregateTarget "CcMark" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + OBJ_21 /* Debug */, + OBJ_22 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + OBJ_24 /* Build configuration list for PBXNativeTarget "CommonMark" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + OBJ_25 /* Debug */, + OBJ_26 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + OBJ_33 /* Build configuration list for PBXNativeTarget "CommonMarkPackageDescription" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + OBJ_34 /* Debug */, + OBJ_35 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = OBJ_1 /* Project object */; } diff --git a/Sources/CommonMark/Reduce.swift b/Sources/CommonMark/Reduce.swift new file mode 100644 index 0000000..3bfd8a5 --- /dev/null +++ b/Sources/CommonMark/Reduce.swift @@ -0,0 +1,112 @@ +// +// Reduce.swift +// CommonMark +// +// Created by Chris Eidhof on 02.04.19. +// + +import Foundation +import Ccmark + +/// An algebra for a block-level element +public struct InlineAlgebra { + public var text: (_ text: String) -> A + public var softBreak: A + public var lineBreak: A + public var code: (_ text: String) -> A + public var html: (_ text: String) -> A + public var emphasis: (_ children: [A]) -> A + public var strong: (_ children: [A]) -> A + public var custom: (_ literal: String) -> A + public var link: (_ children: [A], _ title: String?, _ url: String?) -> A + public var image: (_ children: [A], _ title: String?, _ url: String?) -> A +} + +/// An algebra for a block-level element +public struct BlockAlgebra { + public var inline: InlineAlgebra + public var list: (_ items: [A], _ type: ListType) -> A + public var listItem: (_ children: [A]) -> A + public var blockQuote: (_ items: [A]) -> A + public var codeBlock: (_ text: String, _ language: String?) -> A + public var html: (_ text: String) -> A + public var paragraph: (_ text: [A]) -> A + public var heading: (_ text: [A], _ level: Int) -> A + public var custom: (_ literal: String) -> A + public var thematicBreak: A + public var document: (_ children: [A]) -> A + public var defaultValue: A +} + +extension Node { + public func reduce(_ b: BlockAlgebra) -> R { + func r(_ node: Node) -> R { + var children: [R] { return node.children.map(r) } + var lit: String { return node.literal ?? "" } + switch node.type { + case CMARK_NODE_DOCUMENT: return b.document(children) + case CMARK_NODE_BLOCK_QUOTE: return b.blockQuote(children) + case CMARK_NODE_LIST: return b.list(children, node.listType == CMARK_BULLET_LIST ? .unordered : .ordered) + case CMARK_NODE_ITEM: return b.listItem(children) + case CMARK_NODE_CODE_BLOCK: return b.codeBlock(lit, node.fenceInfo) + case CMARK_NODE_HTML_BLOCK: return b.html(lit) + case CMARK_NODE_CUSTOM_BLOCK: return b.custom(lit) + case CMARK_NODE_PARAGRAPH: return b.paragraph(children) + case CMARK_NODE_HEADING: return b.heading(children, node.headerLevel) + case CMARK_NODE_THEMATIC_BREAK: return b.thematicBreak + case CMARK_NODE_FIRST_BLOCK: return b.defaultValue + case CMARK_NODE_LAST_BLOCK: return b.defaultValue + + /* Inline */ + case CMARK_NODE_TEXT: return b.inline.text(lit) + case CMARK_NODE_SOFTBREAK: return b.inline.softBreak + case CMARK_NODE_LINEBREAK: return b.inline.lineBreak + case CMARK_NODE_CODE: return b.inline.code(lit) + case CMARK_NODE_HTML_INLINE: return b.inline.html(lit) + case CMARK_NODE_CUSTOM_INLINE: return b.inline.custom(lit) + case CMARK_NODE_EMPH: return b.inline.emphasis(children) + case CMARK_NODE_STRONG: return b.inline.strong(children) + case CMARK_NODE_LINK: return b.inline.link(children, node.title, node.urlString) + case CMARK_NODE_IMAGE: return b.inline.image(children, node.title, node.urlString) + default: + return b.defaultValue + } + } + return r(self) + } +} + +public protocol Monoid { + init() + static func +(lhs: Self, rhs: Self) -> Self + mutating func append(_ value: Self) +} + +extension Monoid { + public mutating func append(_ value: Self) { + self = self + value + } +} + +extension Array: Monoid { } + +extension Array where Element: Monoid { + public func flatten() -> Element { + return reduce(into: .init(), { $0.append($1) }) + } +} + +extension String: Monoid { } + +/// This collects all elements into a result `M`. +/// +/// For example, to collect all links in a document: +/// +/// var links: BlockAlgebra<[String]> = collect() +/// links.inline.link = { _, _, url in url.map { [$0] } ?? [] } +/// let allLinks = Node(markdown: string)!.reduce(links) +public func collect() -> BlockAlgebra { + let inline: InlineAlgebra = InlineAlgebra(text: { _ in .init() }, softBreak: .init(), lineBreak: .init(), code: { _ in .init()}, html: { _ in .init() }, emphasis: { $0.flatten() }, strong: { $0.flatten() }, custom: { _ in .init() }, link: { x,_,_ in x.flatten() }, image: { x,_, _ in x.flatten() }) + + return BlockAlgebra(inline: inline, list: { x, _ in x.flatten() }, listItem: { $0.flatten() }, blockQuote: { $0.flatten() }, codeBlock: { _,_ in .init() }, html: { _ in .init() }, paragraph: { $0.flatten() }, heading: { x,_ in x.flatten() }, custom: { _ in .init() }, thematicBreak: .init(), document: { $0.flatten() }, defaultValue: .init()) +} diff --git a/Sources/CommonMark/SwiftAST.swift b/Sources/CommonMark/SwiftAST.swift index f25174e..ff03b91 100644 --- a/Sources/CommonMark/SwiftAST.swift +++ b/Sources/CommonMark/SwiftAST.swift @@ -11,8 +11,8 @@ import Ccmark /// The type of a list in Markdown, represented by `Block.List`. public enum ListType { - case Unordered - case Ordered + case unordered + case ordered } /// An inline element in a Markdown abstract syntax tree. @@ -96,7 +96,7 @@ extension Block { case CMARK_NODE_BLOCK_QUOTE: self = .blockQuote(items: parseBlockChildren()) case CMARK_NODE_LIST: - let type = node.listType == CMARK_BULLET_LIST ? ListType.Unordered : ListType.Ordered + let type = node.listType == CMARK_BULLET_LIST ? ListType.unordered : ListType.ordered self = .list(items: node.children.map { $0.listItem }, type: type) case CMARK_NODE_CODE_BLOCK: self = .codeBlock(text: node.literal!, language: node.fenceInfo) @@ -211,7 +211,7 @@ extension Node { case let .list(items, type): let listItems = items.map { Node(type: CMARK_NODE_ITEM, blocks: $0) } self.init(type: CMARK_NODE_LIST, children: listItems) - listType = type == .Unordered ? CMARK_BULLET_LIST : CMARK_ORDERED_LIST + listType = type == .unordered ? CMARK_BULLET_LIST : CMARK_ORDERED_LIST case .blockQuote(let items): self.init(type: CMARK_NODE_BLOCK_QUOTE, blocks: items) case let .codeBlock(text, language): From edfae6b26e26218c6877acc6574b08566d49a136 Mon Sep 17 00:00:00 2001 From: Ole Begemann Date: Tue, 30 Apr 2019 12:17:01 +0200 Subject: [PATCH 07/13] Re-add test target to package manifest --- Package.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Package.swift b/Package.swift index c2eb8ac..4b74c71 100644 --- a/Package.swift +++ b/Package.swift @@ -12,12 +12,12 @@ let package = Package( dependencies: [], targets: [ .target(name: "CommonMark", dependencies: ["Ccmark"]), - .systemLibrary( - name: "Ccmark", - pkgConfig: "libcmark", - providers: [ - .brew(["commonmark"]) - ] - ) + .systemLibrary( + name: "Ccmark", + pkgConfig: "libcmark", + providers: [ + .brew(["commonmark"]) + ]), + .testTarget(name: "CommonMarkTests", dependencies: ["CommonMark"]), ] ) From 785e9c90ff7c705f3adaad12f9a34cf2e9c22ac5 Mon Sep 17 00:00:00 2001 From: Ole Begemann Date: Tue, 30 Apr 2019 12:18:24 +0200 Subject: [PATCH 08/13] Rename markdowntoHtml to markdowntoHTML For consistency with the Swift API Design Guidelines. --- Sources/CommonMark/Node.swift | 2 +- Tests/CommonMarkTests/CommonMarkTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 99eb4f6..6a1383c 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -11,7 +11,7 @@ import Ccmark -func markdownToHtml(string: String) -> String { +func markdowntoHTML(string: String) -> String { let outString = cmark_markdown_to_html(string, string.utf8.count, 0)! defer { free(outString) } return String(cString: outString) diff --git a/Tests/CommonMarkTests/CommonMarkTests.swift b/Tests/CommonMarkTests/CommonMarkTests.swift index 09795b5..16512e4 100644 --- a/Tests/CommonMarkTests/CommonMarkTests.swift +++ b/Tests/CommonMarkTests/CommonMarkTests.swift @@ -12,7 +12,7 @@ class CommonMarkTests: XCTestCase { func testMarkdownToHTML() { let markdown = "*Hello World*" - let html = markdownToHtml(string: markdown) + let html = markdowntoHTML(string: markdown) XCTAssertEqual(html, "

Hello World

\n") } From e0f0182eb00c2b8ec4fd9b52227d2f09a439a3cc Mon Sep 17 00:00:00 2001 From: Ole Begemann Date: Tue, 30 Apr 2019 12:25:32 +0200 Subject: [PATCH 09/13] Make Node.init(markdown:) non-failable I went through the cmark source. Unless malloc fails, cmark_parse_document never returns NULL. This makes sense because any input string is valid Markdown. --- Sources/CommonMark/Node.swift | 4 ++-- Sources/CommonMark/SwiftAST.swift | 2 +- Tests/CommonMarkTests/CommonMarkTests.swift | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 6a1383c..232d6c1 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -73,9 +73,9 @@ public class Node: CustomStringConvertible { self.node = node } - public init?(markdown: String) { + public init(markdown: String) { guard let node = cmark_parse_document(markdown, markdown.utf8.count, 0) else { - return nil + fatalError("cmark_parse_document returned NULL. Should never happen.") } self.node = node } diff --git a/Sources/CommonMark/SwiftAST.swift b/Sources/CommonMark/SwiftAST.swift index ff03b91..777f24c 100644 --- a/Sources/CommonMark/SwiftAST.swift +++ b/Sources/CommonMark/SwiftAST.swift @@ -163,7 +163,7 @@ extension Node { } func tableOfContents(document: String) -> [Block] { - let blocks = Node(markdown: document)?.children.map(Block.init) ?? [] + let blocks = Node(markdown: document).children.map(Block.init) return blocks.filter { switch $0 { case .heading(_, let level) where level < 3: return true diff --git a/Tests/CommonMarkTests/CommonMarkTests.swift b/Tests/CommonMarkTests/CommonMarkTests.swift index 16512e4..69cc7a0 100644 --- a/Tests/CommonMarkTests/CommonMarkTests.swift +++ b/Tests/CommonMarkTests/CommonMarkTests.swift @@ -19,7 +19,7 @@ class CommonMarkTests: XCTestCase { func testMarkdownToNode() { let markdown = "*Hello World*" let rootNode = Node(markdown: markdown) - XCTAssertNotNil(rootNode) + XCTAssertEqual(rootNode.elements.count, 1) } func testMarkdownToArrayOfBlocks() { @@ -33,7 +33,7 @@ class CommonMarkTests: XCTestCase { * List item 1 * List item 2 """ - let rootNode = Node(markdown: markdown)! + let rootNode = Node(markdown: markdown) let blocks = rootNode.elements XCTAssertEqual(blocks.count, 4) } From 6559f528e1502d4aed8e31e518dfb4df7f787c9e Mon Sep 17 00:00:00 2001 From: Ole Begemann Date: Tue, 30 Apr 2019 12:33:21 +0200 Subject: [PATCH 10/13] Don't crash when passing invalid filename to Node.init(filename:) cmark_parse_file crashes when passed a NULL FILE pointer. Catch this condition before calling cmark_parse_file. --- Sources/CommonMark/Node.swift | 3 ++- Tests/CommonMarkTests/CommonMarkTests.swift | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 232d6c1..cd0bc18 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -69,7 +69,8 @@ public class Node: CustomStringConvertible { } public init?(filename: String) { - guard let node = cmark_parse_file(fopen(filename, "r"), 0) else { return nil } + guard let file = fopen(filename, "r"), + let node = cmark_parse_file(file, 0) else { return nil } self.node = node } diff --git a/Tests/CommonMarkTests/CommonMarkTests.swift b/Tests/CommonMarkTests/CommonMarkTests.swift index 69cc7a0..a07b599 100644 --- a/Tests/CommonMarkTests/CommonMarkTests.swift +++ b/Tests/CommonMarkTests/CommonMarkTests.swift @@ -37,4 +37,10 @@ class CommonMarkTests: XCTestCase { let blocks = rootNode.elements XCTAssertEqual(blocks.count, 4) } + + func testReadMarkdownFromNonInvalidFilenameReturnsNil() { + let nonExistentFilename = "/lkjhgfdsa" + let rootNode = Node(filename: nonExistentFilename) + XCTAssertNil(rootNode) + } } From 77df8f44eb9dea4466ebdbd5b9b4605bd79ad3f8 Mon Sep 17 00:00:00 2001 From: Ole Begemann Date: Tue, 30 Apr 2019 12:35:20 +0200 Subject: [PATCH 11/13] Generate/update test manifests for Linux --- Tests/CommonMarkTests/XCTestManifests.swift | 21 +++++++++++++++++++++ Tests/LinuxMain.swift | 8 ++++++++ 2 files changed, 29 insertions(+) create mode 100644 Tests/CommonMarkTests/XCTestManifests.swift create mode 100644 Tests/LinuxMain.swift diff --git a/Tests/CommonMarkTests/XCTestManifests.swift b/Tests/CommonMarkTests/XCTestManifests.swift new file mode 100644 index 0000000..b7cd9f5 --- /dev/null +++ b/Tests/CommonMarkTests/XCTestManifests.swift @@ -0,0 +1,21 @@ +#if !canImport(ObjectiveC) +import XCTest + +extension CommonMarkTests { + // DO NOT MODIFY: This is autogenerated, use: + // `swift test --generate-linuxmain` + // to regenerate. + static let __allTests__CommonMarkTests = [ + ("testMarkdownToArrayOfBlocks", testMarkdownToArrayOfBlocks), + ("testMarkdownToHTML", testMarkdownToHTML), + ("testMarkdownToNode", testMarkdownToNode), + ("testReadMarkdownFromNonInvalidFilenameReturnsNil", testReadMarkdownFromNonInvalidFilenameReturnsNil), + ] +} + +public func __allTests() -> [XCTestCaseEntry] { + return [ + testCase(CommonMarkTests.__allTests__CommonMarkTests), + ] +} +#endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..20912dc --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,8 @@ +import XCTest + +import CommonMarkTests + +var tests = [XCTestCaseEntry]() +tests += CommonMarkTests.__allTests() + +XCTMain(tests) From 475f00196f21b0ffd0bfd7fc921900e71bd1bd54 Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Tue, 2 Jul 2019 10:31:22 +0200 Subject: [PATCH 12/13] Adding rendering options. --- CommonMark.xcodeproj/project.pbxproj | 1062 +++++++++++------ .../xcschemes/CommonMark-Package.xcscheme | 10 + Sources/CommonMark/Node.swift | 35 +- 3 files changed, 707 insertions(+), 400 deletions(-) diff --git a/CommonMark.xcodeproj/project.pbxproj b/CommonMark.xcodeproj/project.pbxproj index 93933e3..c95f937 100644 --- a/CommonMark.xcodeproj/project.pbxproj +++ b/CommonMark.xcodeproj/project.pbxproj @@ -1,395 +1,673 @@ // !$*UTF8*$! { - archiveVersion = 1; - classes = { - }; - objectVersion = 46; - objects = { - -/* Begin PBXAggregateTarget section */ - "CommonMark::CcMark::ProductTarget" /* CcMark */ = { - isa = PBXAggregateTarget; - buildConfigurationList = OBJ_20 /* Build configuration list for PBXAggregateTarget "CcMark" */; - buildPhases = ( - ); - dependencies = ( - ); - name = CcMark; - productName = CcMark; - }; -/* End PBXAggregateTarget section */ - -/* Begin PBXBuildFile section */ - 830B48C022537DCC00C9FCE3 /* Reduce.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830B48BF22537DCC00C9FCE3 /* Reduce.swift */; }; - OBJ_28 /* ASTOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* ASTOperations.swift */; }; - OBJ_29 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* Node.swift */; }; - OBJ_30 /* SwiftAST.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* SwiftAST.swift */; }; - OBJ_37 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; }; -/* End PBXBuildFile section */ - -/* Begin PBXFileReference section */ - 830B48BF22537DCC00C9FCE3 /* Reduce.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reduce.swift; sourceTree = ""; }; - "CommonMark::CommonMark::Product" /* CommonMark.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CommonMark.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - OBJ_11 /* ASTOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASTOperations.swift; sourceTree = ""; }; - OBJ_12 /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = ""; }; - OBJ_13 /* SwiftAST.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftAST.swift; sourceTree = ""; }; - OBJ_17 /* Package.pins */ = {isa = PBXFileReference; lastKnownFileType = text; path = Package.pins; sourceTree = ""; }; - OBJ_18 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; - OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; - OBJ_9 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - OBJ_31 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 0; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - OBJ_10 /* CommonMark */ = { - isa = PBXGroup; - children = ( - OBJ_11 /* ASTOperations.swift */, - OBJ_12 /* Node.swift */, - OBJ_13 /* SwiftAST.swift */, - 830B48BF22537DCC00C9FCE3 /* Reduce.swift */, - ); - name = CommonMark; - path = Sources/CommonMark; - sourceTree = SOURCE_ROOT; - }; - OBJ_14 /* Tests */ = { - isa = PBXGroup; - children = ( - ); - name = Tests; - sourceTree = SOURCE_ROOT; - }; - OBJ_15 /* Products */ = { - isa = PBXGroup; - children = ( - "CommonMark::CommonMark::Product" /* CommonMark.framework */, - ); - name = Products; - sourceTree = BUILT_PRODUCTS_DIR; - }; - OBJ_5 /* */ = { - isa = PBXGroup; - children = ( - OBJ_6 /* Package.swift */, - OBJ_7 /* Sources */, - OBJ_14 /* Tests */, - OBJ_15 /* Products */, - OBJ_17 /* Package.pins */, - OBJ_18 /* README.md */, - ); - name = ""; - sourceTree = ""; - }; - OBJ_7 /* Sources */ = { - isa = PBXGroup; - children = ( - OBJ_8 /* Ccmark */, - OBJ_10 /* CommonMark */, - ); - name = Sources; - sourceTree = SOURCE_ROOT; - }; - OBJ_8 /* Ccmark */ = { - isa = PBXGroup; - children = ( - OBJ_9 /* module.modulemap */, - ); - name = Ccmark; - path = Sources/Ccmark; - sourceTree = SOURCE_ROOT; - }; -/* End PBXGroup section */ - -/* Begin PBXNativeTarget section */ - "CommonMark::CommonMark" /* CommonMark */ = { - isa = PBXNativeTarget; - buildConfigurationList = OBJ_24 /* Build configuration list for PBXNativeTarget "CommonMark" */; - buildPhases = ( - OBJ_27 /* Sources */, - OBJ_31 /* Frameworks */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = CommonMark; - productName = CommonMark; - productReference = "CommonMark::CommonMark::Product" /* CommonMark.framework */; - productType = "com.apple.product-type.framework"; - }; - "CommonMark::SwiftPMPackageDescription" /* CommonMarkPackageDescription */ = { - isa = PBXNativeTarget; - buildConfigurationList = OBJ_33 /* Build configuration list for PBXNativeTarget "CommonMarkPackageDescription" */; - buildPhases = ( - OBJ_36 /* Sources */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = CommonMarkPackageDescription; - productName = CommonMarkPackageDescription; - productType = "com.apple.product-type.framework"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - OBJ_1 /* Project object */ = { - isa = PBXProject; - attributes = { - LastSwiftMigration = 9999; - LastUpgradeCheck = 9999; - }; - buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "CommonMark" */; - compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; - hasScannedForEncodings = 0; - knownRegions = ( - English, - en, - ); - mainGroup = OBJ_5 /* */; - productRefGroup = OBJ_15 /* Products */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - "CommonMark::CcMark::ProductTarget" /* CcMark */, - "CommonMark::CommonMark" /* CommonMark */, - "CommonMark::SwiftPMPackageDescription" /* CommonMarkPackageDescription */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXSourcesBuildPhase section */ - OBJ_27 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 0; - files = ( - OBJ_28 /* ASTOperations.swift in Sources */, - OBJ_29 /* Node.swift in Sources */, - OBJ_30 /* SwiftAST.swift in Sources */, - 830B48C022537DCC00C9FCE3 /* Reduce.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - OBJ_36 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 0; - files = ( - OBJ_37 /* Package.swift in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin XCBuildConfiguration section */ - OBJ_21 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - }; - name = Debug; - }; - OBJ_22 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - }; - name = Release; - }; - OBJ_25 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ENABLE_TESTABILITY = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PLATFORM_DIR)/Developer/Library/Frameworks", - ); - HEADER_SEARCH_PATHS = ( - "$(inherited)", - "$(SRCROOT)/Sources/Ccmark", - ); - INFOPLIST_FILE = CommonMark.xcodeproj/CommonMark_Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; - MACOSX_DEPLOYMENT_TARGET = 10.10; - OTHER_CFLAGS = ( - "$(inherited)", - "-I/usr/local/include", - ); - OTHER_LDFLAGS = ( - "$(inherited)", - "-L/usr/local/lib", - "-lcmark", - ); - OTHER_SWIFT_FLAGS = "$(inherited) -I/usr/local/include"; - PRODUCT_BUNDLE_IDENTIFIER = CommonMark; - PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = YES; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; - SWIFT_VERSION = 5.0; - TARGET_NAME = CommonMark; - TVOS_DEPLOYMENT_TARGET = 9.0; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Debug; - }; - OBJ_26 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ENABLE_TESTABILITY = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PLATFORM_DIR)/Developer/Library/Frameworks", - ); - HEADER_SEARCH_PATHS = ( - "$(inherited)", - "$(SRCROOT)/Sources/Ccmark", - ); - INFOPLIST_FILE = CommonMark.xcodeproj/CommonMark_Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx"; - MACOSX_DEPLOYMENT_TARGET = 10.10; - OTHER_CFLAGS = ( - "$(inherited)", - "-I/usr/local/include", - ); - OTHER_LDFLAGS = ( - "$(inherited)", - "-L/usr/local/lib", - "-lcmark", - ); - OTHER_SWIFT_FLAGS = "$(inherited) -I/usr/local/include"; - PRODUCT_BUNDLE_IDENTIFIER = CommonMark; - PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; - PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; - SKIP_INSTALL = YES; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; - SWIFT_VERSION = 5.0; - TARGET_NAME = CommonMark; - TVOS_DEPLOYMENT_TARGET = 9.0; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Release; - }; - OBJ_3 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - CLANG_ENABLE_OBJC_ARC = YES; - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = dwarf; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = YES; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE=1", - "DEBUG=1", - ); - MACOSX_DEPLOYMENT_TARGET = 10.10; - ONLY_ACTIVE_ARCH = YES; - OTHER_SWIFT_FLAGS = "-DXcode"; - PRODUCT_NAME = "$(TARGET_NAME)"; - SDKROOT = macosx; - SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator"; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE DEBUG"; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - USE_HEADERMAP = NO; - }; - name = Debug; - }; - OBJ_34 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - LD = /usr/bin/true; - OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk"; - SWIFT_VERSION = 5.0; - }; - name = Debug; - }; - OBJ_35 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - LD = /usr/bin/true; - OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk"; - SWIFT_VERSION = 5.0; - }; - name = Release; - }; - OBJ_4 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - CLANG_ENABLE_OBJC_ARC = YES; - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = YES; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - GCC_OPTIMIZATION_LEVEL = s; - GCC_PREPROCESSOR_DEFINITIONS = ( - "$(inherited)", - "SWIFT_PACKAGE=1", - ); - MACOSX_DEPLOYMENT_TARGET = 10.10; - OTHER_SWIFT_FLAGS = "-DXcode"; - PRODUCT_NAME = "$(TARGET_NAME)"; - SDKROOT = macosx; - SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator"; - SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) SWIFT_PACKAGE"; - SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - USE_HEADERMAP = NO; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - OBJ_2 /* Build configuration list for PBXProject "CommonMark" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - OBJ_3 /* Debug */, - OBJ_4 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - OBJ_20 /* Build configuration list for PBXAggregateTarget "CcMark" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - OBJ_21 /* Debug */, - OBJ_22 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - OBJ_24 /* Build configuration list for PBXNativeTarget "CommonMark" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - OBJ_25 /* Debug */, - OBJ_26 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - OBJ_33 /* Build configuration list for PBXNativeTarget "CommonMarkPackageDescription" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - OBJ_34 /* Debug */, - OBJ_35 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - }; - rootObject = OBJ_1 /* Project object */; + archiveVersion = "1"; + objectVersion = "46"; + objects = { + "CommonMark::Ccmark::ProductTarget" = { + isa = "PBXAggregateTarget"; + buildConfigurationList = "OBJ_25"; + buildPhases = ( + ); + dependencies = ( + ); + name = "Ccmark"; + productName = "Ccmark"; + }; + "CommonMark::CommonMark" = { + isa = "PBXNativeTarget"; + buildConfigurationList = "OBJ_29"; + buildPhases = ( + "OBJ_32", + "OBJ_37" + ); + dependencies = ( + ); + name = "CommonMark"; + productName = "CommonMark"; + productReference = "CommonMark::CommonMark::Product"; + productType = "com.apple.product-type.framework"; + }; + "CommonMark::CommonMark::Product" = { + isa = "PBXFileReference"; + path = "CommonMark.framework"; + sourceTree = "BUILT_PRODUCTS_DIR"; + }; + "CommonMark::CommonMarkPackageTests::ProductTarget" = { + isa = "PBXAggregateTarget"; + buildConfigurationList = "OBJ_45"; + buildPhases = ( + ); + dependencies = ( + "OBJ_48" + ); + name = "CommonMarkPackageTests"; + productName = "CommonMarkPackageTests"; + }; + "CommonMark::CommonMarkTests" = { + isa = "PBXNativeTarget"; + buildConfigurationList = "OBJ_50"; + buildPhases = ( + "OBJ_53", + "OBJ_56" + ); + dependencies = ( + "OBJ_58" + ); + name = "CommonMarkTests"; + productName = "CommonMarkTests"; + productReference = "CommonMark::CommonMarkTests::Product"; + productType = "com.apple.product-type.bundle.unit-test"; + }; + "CommonMark::CommonMarkTests::Product" = { + isa = "PBXFileReference"; + path = "CommonMarkTests.xctest"; + sourceTree = "BUILT_PRODUCTS_DIR"; + }; + "CommonMark::SwiftPMPackageDescription" = { + isa = "PBXNativeTarget"; + buildConfigurationList = "OBJ_39"; + buildPhases = ( + "OBJ_42" + ); + dependencies = ( + ); + name = "CommonMarkPackageDescription"; + productName = "CommonMarkPackageDescription"; + productType = "com.apple.product-type.framework"; + }; + "OBJ_1" = { + isa = "PBXProject"; + attributes = { + LastSwiftMigration = "9999"; + LastUpgradeCheck = "9999"; + }; + buildConfigurationList = "OBJ_2"; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = "English"; + hasScannedForEncodings = "0"; + knownRegions = ( + "en" + ); + mainGroup = "OBJ_5"; + productRefGroup = "OBJ_19"; + projectDirPath = "."; + targets = ( + "CommonMark::Ccmark::ProductTarget", + "CommonMark::CommonMark", + "CommonMark::SwiftPMPackageDescription", + "CommonMark::CommonMarkPackageTests::ProductTarget", + "CommonMark::CommonMarkTests" + ); + }; + "OBJ_10" = { + isa = "PBXGroup"; + children = ( + "OBJ_11", + "OBJ_12", + "OBJ_13", + "OBJ_14" + ); + name = "CommonMark"; + path = "Sources/CommonMark"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_11" = { + isa = "PBXFileReference"; + path = "ASTOperations.swift"; + sourceTree = ""; + }; + "OBJ_12" = { + isa = "PBXFileReference"; + path = "Node.swift"; + sourceTree = ""; + }; + "OBJ_13" = { + isa = "PBXFileReference"; + path = "Reduce.swift"; + sourceTree = ""; + }; + "OBJ_14" = { + isa = "PBXFileReference"; + path = "SwiftAST.swift"; + sourceTree = ""; + }; + "OBJ_15" = { + isa = "PBXGroup"; + children = ( + "OBJ_16" + ); + name = "Tests"; + path = ""; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_16" = { + isa = "PBXGroup"; + children = ( + "OBJ_17", + "OBJ_18" + ); + name = "CommonMarkTests"; + path = "Tests/CommonMarkTests"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_17" = { + isa = "PBXFileReference"; + path = "CommonMarkTests.swift"; + sourceTree = ""; + }; + "OBJ_18" = { + isa = "PBXFileReference"; + path = "XCTestManifests.swift"; + sourceTree = ""; + }; + "OBJ_19" = { + isa = "PBXGroup"; + children = ( + "CommonMark::CommonMarkTests::Product", + "CommonMark::CommonMark::Product" + ); + name = "Products"; + path = ""; + sourceTree = "BUILT_PRODUCTS_DIR"; + }; + "OBJ_2" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_3", + "OBJ_4" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_22" = { + isa = "PBXFileReference"; + path = "Package.pins"; + sourceTree = ""; + }; + "OBJ_23" = { + isa = "PBXFileReference"; + path = "README.md"; + sourceTree = ""; + }; + "OBJ_25" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_26", + "OBJ_27" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_26" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Debug"; + }; + "OBJ_27" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Release"; + }; + "OBJ_29" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_30", + "OBJ_31" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_3" = { + isa = "XCBuildConfiguration"; + buildSettings = { + CLANG_ENABLE_OBJC_ARC = "YES"; + COMBINE_HIDPI_IMAGES = "YES"; + COPY_PHASE_STRIP = "NO"; + DEBUG_INFORMATION_FORMAT = "dwarf"; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_NS_ASSERTIONS = "YES"; + GCC_OPTIMIZATION_LEVEL = "0"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1", + "DEBUG=1" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + ONLY_ACTIVE_ARCH = "YES"; + OTHER_SWIFT_FLAGS = ( + "-DXcode" + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = "macosx"; + SUPPORTED_PLATFORMS = ( + "macosx", + "iphoneos", + "iphonesimulator", + "appletvos", + "appletvsimulator", + "watchos", + "watchsimulator" + ); + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE", + "DEBUG" + ); + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + USE_HEADERMAP = "NO"; + }; + name = "Debug"; + }; + "OBJ_30" = { + isa = "XCBuildConfiguration"; + buildSettings = { + ENABLE_TESTABILITY = "YES"; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks" + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark" + ); + INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark" + ); + OTHER_SWIFT_FLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; + PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = "YES"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)" + ); + SWIFT_VERSION = "5.0"; + TARGET_NAME = "CommonMark"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; + }; + name = "Debug"; + }; + "OBJ_31" = { + isa = "XCBuildConfiguration"; + buildSettings = { + ENABLE_TESTABILITY = "YES"; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks" + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark" + ); + INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMark_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark" + ); + OTHER_SWIFT_FLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + PRODUCT_BUNDLE_IDENTIFIER = "CommonMark"; + PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = "YES"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)" + ); + SWIFT_VERSION = "5.0"; + TARGET_NAME = "CommonMark"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; + }; + name = "Release"; + }; + "OBJ_32" = { + isa = "PBXSourcesBuildPhase"; + files = ( + "OBJ_33", + "OBJ_34", + "OBJ_35", + "OBJ_36" + ); + }; + "OBJ_33" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_11"; + }; + "OBJ_34" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_12"; + }; + "OBJ_35" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_13"; + }; + "OBJ_36" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_14"; + }; + "OBJ_37" = { + isa = "PBXFrameworksBuildPhase"; + files = ( + ); + }; + "OBJ_39" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_40", + "OBJ_41" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_4" = { + isa = "XCBuildConfiguration"; + buildSettings = { + CLANG_ENABLE_OBJC_ARC = "YES"; + COMBINE_HIDPI_IMAGES = "YES"; + COPY_PHASE_STRIP = "YES"; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + GCC_OPTIMIZATION_LEVEL = "s"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + OTHER_SWIFT_FLAGS = ( + "-DXcode" + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = "macosx"; + SUPPORTED_PLATFORMS = ( + "macosx", + "iphoneos", + "iphonesimulator", + "appletvos", + "appletvsimulator", + "watchos", + "watchsimulator" + ); + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE" + ); + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + USE_HEADERMAP = "NO"; + }; + name = "Release"; + }; + "OBJ_40" = { + isa = "XCBuildConfiguration"; + buildSettings = { + LD = "/usr/bin/true"; + OTHER_SWIFT_FLAGS = ( + "-swift-version", + "5", + "-I", + "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", + "-target", + "x86_64-apple-macosx10.10", + "-sdk", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" + ); + SWIFT_VERSION = "5.0"; + }; + name = "Debug"; + }; + "OBJ_41" = { + isa = "XCBuildConfiguration"; + buildSettings = { + LD = "/usr/bin/true"; + OTHER_SWIFT_FLAGS = ( + "-swift-version", + "5", + "-I", + "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", + "-target", + "x86_64-apple-macosx10.10", + "-sdk", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" + ); + SWIFT_VERSION = "5.0"; + }; + name = "Release"; + }; + "OBJ_42" = { + isa = "PBXSourcesBuildPhase"; + files = ( + "OBJ_43" + ); + }; + "OBJ_43" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_6"; + }; + "OBJ_45" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_46", + "OBJ_47" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_46" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Debug"; + }; + "OBJ_47" = { + isa = "XCBuildConfiguration"; + buildSettings = { + }; + name = "Release"; + }; + "OBJ_48" = { + isa = "PBXTargetDependency"; + target = "CommonMark::CommonMarkTests"; + }; + "OBJ_5" = { + isa = "PBXGroup"; + children = ( + "OBJ_6", + "OBJ_7", + "OBJ_15", + "OBJ_19", + "OBJ_22", + "OBJ_23" + ); + path = ""; + sourceTree = ""; + }; + "OBJ_50" = { + isa = "XCConfigurationList"; + buildConfigurations = ( + "OBJ_51", + "OBJ_52" + ); + defaultConfigurationIsVisible = "0"; + defaultConfigurationName = "Release"; + }; + "OBJ_51" = { + isa = "XCBuildConfiguration"; + buildSettings = { + CLANG_ENABLE_MODULES = "YES"; + EMBEDDED_CONTENT_CONTAINS_SWIFT = "YES"; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks" + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark" + ); + INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMarkTests_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@loader_path/../Frameworks", + "@loader_path/Frameworks" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark" + ); + OTHER_SWIFT_FLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)" + ); + SWIFT_VERSION = "5.0"; + TARGET_NAME = "CommonMarkTests"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; + }; + name = "Debug"; + }; + "OBJ_52" = { + isa = "XCBuildConfiguration"; + buildSettings = { + CLANG_ENABLE_MODULES = "YES"; + EMBEDDED_CONTENT_CONTAINS_SWIFT = "YES"; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PLATFORM_DIR)/Developer/Library/Frameworks" + ); + HEADER_SEARCH_PATHS = ( + "$(inherited)", + "$(SRCROOT)/Sources/Ccmark" + ); + INFOPLIST_FILE = "CommonMark.xcodeproj/CommonMarkTests_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@loader_path/../Frameworks", + "@loader_path/Frameworks" + ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; + OTHER_CFLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-L/usr/local/lib", + "-lcmark" + ); + OTHER_SWIFT_FLAGS = ( + "$(inherited)", + "-I/usr/local/include" + ); + SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)" + ); + SWIFT_VERSION = "5.0"; + TARGET_NAME = "CommonMarkTests"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; + }; + name = "Release"; + }; + "OBJ_53" = { + isa = "PBXSourcesBuildPhase"; + files = ( + "OBJ_54", + "OBJ_55" + ); + }; + "OBJ_54" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_17"; + }; + "OBJ_55" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_18"; + }; + "OBJ_56" = { + isa = "PBXFrameworksBuildPhase"; + files = ( + "OBJ_57" + ); + }; + "OBJ_57" = { + isa = "PBXBuildFile"; + fileRef = "CommonMark::CommonMark::Product"; + }; + "OBJ_58" = { + isa = "PBXTargetDependency"; + target = "CommonMark::CommonMark"; + }; + "OBJ_6" = { + isa = "PBXFileReference"; + explicitFileType = "sourcecode.swift"; + path = "Package.swift"; + sourceTree = ""; + }; + "OBJ_7" = { + isa = "PBXGroup"; + children = ( + "OBJ_8", + "OBJ_10" + ); + name = "Sources"; + path = ""; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_8" = { + isa = "PBXGroup"; + children = ( + "OBJ_9" + ); + name = "Ccmark"; + path = "Sources/Ccmark"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_9" = { + isa = "PBXFileReference"; + path = "module.modulemap"; + sourceTree = ""; + }; + }; + rootObject = "OBJ_1"; } diff --git a/CommonMark.xcodeproj/xcshareddata/xcschemes/CommonMark-Package.xcscheme b/CommonMark.xcodeproj/xcshareddata/xcschemes/CommonMark-Package.xcscheme index a26dbab..34787e8 100644 --- a/CommonMark.xcodeproj/xcshareddata/xcschemes/CommonMark-Package.xcscheme +++ b/CommonMark.xcodeproj/xcshareddata/xcschemes/CommonMark-Package.xcscheme @@ -28,6 +28,16 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> + + + + diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index cd0bc18..29e77ca 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -57,6 +57,23 @@ public struct Position { public var column: Int32 } +public struct RenderingOptions: OptionSet { + public var rawValue: Int32 + public init(rawValue: Int32 = CMARK_OPT_DEFAULT) { + self.rawValue = rawValue + } + + static public let sourcePos = RenderingOptions(rawValue: CMARK_OPT_SOURCEPOS) + static public let hardBreaks = RenderingOptions(rawValue: CMARK_OPT_HARDBREAKS) + static public let safe = RenderingOptions(rawValue: CMARK_OPT_SAFE) + static public let unsafe = RenderingOptions(rawValue: CMARK_OPT_UNSAFE) + static public let noBreaks = RenderingOptions(rawValue: CMARK_OPT_NOBREAKS) + static public let normalize = RenderingOptions(rawValue: CMARK_OPT_NORMALIZE) + static public let validateUTF8 = RenderingOptions(rawValue: CMARK_OPT_VALIDATE_UTF8) + static public let smart = RenderingOptions(rawValue: CMARK_OPT_SMART) +} + + /// A node in a Markdown document. /// /// Can represent a full Markdown document (i.e. the document's root node) or @@ -173,23 +190,25 @@ public class Node: CustomStringConvertible { } /// Renders the HTML representation - public var html: String { - return String(freeingCString: cmark_render_html(node, 0)) ?? "" + /// + + public func html(options: RenderingOptions = RenderingOptions()) -> String { + return String(freeingCString: cmark_render_html(node, options.rawValue)) ?? "" } /// Renders the XML representation - public var xml: String { - return String(freeingCString: cmark_render_xml(node, 0)) ?? "" + public func xml(options: RenderingOptions = RenderingOptions()) -> String { + return String(freeingCString: cmark_render_xml(node, options.rawValue)) ?? "" } /// Renders the CommonMark representation - public var commonMark: String { - return String(freeingCString: cmark_render_commonmark(node, CMARK_OPT_DEFAULT, 80)) ?? "" + public func commonMark(options: RenderingOptions = RenderingOptions()) -> String { + return String(freeingCString: cmark_render_commonmark(node, options.rawValue, 80)) ?? "" } /// Renders the LaTeX representation - public var latex: String { - return String(freeingCString: cmark_render_latex(node, CMARK_OPT_DEFAULT, 80)) ?? "" + public func latex(options: RenderingOptions = RenderingOptions()) -> String { + return String(freeingCString: cmark_render_latex(node, options.rawValue, 80)) ?? "" } public var description: String { From 5b1de5a072f2bb89b7b1cf1ecb69ce4582561efa Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Mon, 11 Oct 2021 09:28:14 +0200 Subject: [PATCH 13/13] Version bump --- Package.swift | 2 +- Sources/CommonMark/Node.swift | 4 ++-- Sources/CommonMark/SwiftAST.swift | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 4b74c71..529e2d9 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.5 import PackageDescription diff --git a/Sources/CommonMark/Node.swift b/Sources/CommonMark/Node.swift index 29e77ca..1d82a8e 100644 --- a/Sources/CommonMark/Node.swift +++ b/Sources/CommonMark/Node.swift @@ -33,7 +33,7 @@ struct Markdown { extension String { // We're going through Data instead of using init(cstring:) because that leaks memory on Linux. - init?(unsafeCString: UnsafePointer!) { + init?(unsafeCString: UnsafePointer!) { guard let cString = unsafeCString else { return nil } let data = cString.withMemoryRebound(to: UInt8.self, capacity: strlen(cString), { p in return Data(UnsafeBufferPointer(start: p, count: strlen(cString))) @@ -41,7 +41,7 @@ extension String { self.init(data: data, encoding: .utf8) } - init?(freeingCString str: UnsafeMutablePointer?) { + init?(freeingCString str: UnsafeMutablePointer?) { guard let cString = str else { return nil } let data = cString.withMemoryRebound(to: UInt8.self, capacity: strlen(cString), { p in return Data(UnsafeBufferPointer(start: p, count: strlen(cString))) diff --git a/Sources/CommonMark/SwiftAST.swift b/Sources/CommonMark/SwiftAST.swift index 777f24c..6de8480 100644 --- a/Sources/CommonMark/SwiftAST.swift +++ b/Sources/CommonMark/SwiftAST.swift @@ -25,8 +25,8 @@ public enum Inline { case emphasis(children: [Inline]) case strong(children: [Inline]) case custom(literal: String) - case link(children: [Inline], title: String?, url: String?) - case image(children: [Inline], title: String?, url: String?) + case link(children: [Inline], title: String?, url: String) + case image(children: [Inline], title: String?, url: String) } extension Inline: ExpressibleByStringLiteral { @@ -158,7 +158,7 @@ extension Node { /// The abstract syntax tree representation of a Markdown document. /// - returns: an array of block-level elements. public var elements: [Block] { - return children.map(Block.init) + children.map(Block.init) } }