From ee51b75cee944c7f73d87d016cb320f4bcea1e61 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Fri, 4 May 2018 15:45:31 +0100 Subject: [PATCH] Remove provider and update README --- README.md | 15 +++++---- Sources/LeafMarkdown/Provider.swift | 36 --------------------- Tests/LeafMarkdownTests/ProviderTests.swift | 31 ------------------ Tests/LeafMarkdownTests/ServicesTests.swift | 23 +++++++++++++ Tests/LinuxMain.swift | 2 +- 5 files changed, 33 insertions(+), 74 deletions(-) delete mode 100644 Sources/LeafMarkdown/Provider.swift delete mode 100644 Tests/LeafMarkdownTests/ProviderTests.swift create mode 100644 Tests/LeafMarkdownTests/ServicesTests.swift diff --git a/README.md b/README.md index 92a31bc..8c6b8d3 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A Markdown renderer for Vapor and Leaf. This uses the [Vapor Markdown](https://github.com/vapor/markdown) package to wrap [cmark](https://github.com/jgm/cmark) (though a [fork](https://github.com/brokenhandsio/cmark-gfm) is used to make it work with Swift PM), so it understands [Common Mark](http://commonmark.org). A quick reference guide for Common Mark can be found [here](http://commonmark.org/help/). It also supports [Github Flavored Markdown](https://guides.github.com/features/mastering-markdown/). -## Use +## Usage Once set up, you can use it in your Leaf template files like any other tag: @@ -38,12 +38,15 @@ Add Leaf Markdown as a dependency in your `Package.swift` file: ### Register with Leaf -Alternatively, you can also directly add the Tag onto your `LeafRenderer` if desired. During your setup (for example, in `Droplet+Setup.swift`), register your tag as so: +To add the tag to Leaf, add it to your `LeafTagConfig`: ```swift -if let leaf = drop.view as? LeafRenderer { - leaf.stem.register(Markdown()) -} +try services.register(LeafProvider()) +var tags = LeafTagConfig.default() +tags.use(Markdown(), as: "markdown") +services.register(tags) ``` -Don't forget to import MarkdownProvider in the file you register the tag or add the provider in with `import MarkdownProvider` +**Note:** it's important that you register the `LeafProvider` first otherwise this will override your `LeafTagConfig`. + +Don't forget to import LeafMarkdown in the file you register the tag with `import LeafMarkdown`. diff --git a/Sources/LeafMarkdown/Provider.swift b/Sources/LeafMarkdown/Provider.swift deleted file mode 100644 index 6e952b5..0000000 --- a/Sources/LeafMarkdown/Provider.swift +++ /dev/null @@ -1,36 +0,0 @@ -import Vapor -import Leaf - -public struct Provider: Vapor.Provider { - - public static let repositoryName = "markdown-provider" - - public init() {} - - public func register(_ services: inout Services) throws { - var tags = LeafTagConfig.default() - tags.use(Markdown(), as: "markdown") - services.register(tags) - } - - public func boot(_ worker: Container) throws { - do { - _ = try worker.make(LeafRenderer.self) - } catch is ServiceError { - print("Markdown Provider only supports Leaf as a renderer - make sure the Leaf Provider has been registered") - } - } - - public func didBoot(_ container: Container) throws -> EventLoopFuture { - return Future.map(on: container) {} - } -// -// public func boot(_ drop: Droplet) { -// guard let renderer = drop.view as? LeafRenderer else { -// print("Markdown Provider only supports Leaf as a renderer") -// return -// } -// -// renderer.stem.register(Markdown()) -// } -} diff --git a/Tests/LeafMarkdownTests/ProviderTests.swift b/Tests/LeafMarkdownTests/ProviderTests.swift deleted file mode 100644 index 1ed9a83..0000000 --- a/Tests/LeafMarkdownTests/ProviderTests.swift +++ /dev/null @@ -1,31 +0,0 @@ -import XCTest -import Vapor -import Leaf - -import LeafMarkdown - -class ProviderTests: XCTestCase { - static var allTests = [ - ("testProviderCreation", testProviderAddsTagToLeaf), - ("testProviderGracefullyHandlesNonLeafRenderer", testProviderGracefullyHandlesNonLeafRenderer) - ] - - func testProviderAddsTagToLeaf() throws { - var services = Services.default() - let leafProvider = LeafProvider() - try services.register(leafProvider) - try services.register(LeafMarkdown.Provider()) - let app = try Application(services: services) - - let renderer = try app.make(LeafRenderer.self) - - XCTAssertNotNil(renderer.tags[Markdown().name]) - } - - func testProviderGracefullyHandlesNonLeafRenderer() throws { - var services = Services.default() - try services.register(LeafMarkdown.Provider()) - _ = try Application(services: services) - XCTAssert(true, "We should reach this point") - } -} diff --git a/Tests/LeafMarkdownTests/ServicesTests.swift b/Tests/LeafMarkdownTests/ServicesTests.swift new file mode 100644 index 0000000..58f4ca5 --- /dev/null +++ b/Tests/LeafMarkdownTests/ServicesTests.swift @@ -0,0 +1,23 @@ +import XCTest +import Vapor +import Leaf + +import LeafMarkdown + +class ServicesTests: XCTestCase { + static var allTests = [ + ("testTagsCanBeAddedToServices", testTagsCanBeAddedToServices), + ] + + func testTagsCanBeAddedToServices() throws { + var services = Services.default() + try services.register(LeafProvider()) + var tags = LeafTagConfig.default() + tags.use(Markdown(), as: "markdown") + services.register(tags) + let app = try Application(services: services) + let renderer = try app.make(LeafRenderer.self) + + XCTAssertNotNil(renderer.tags[Markdown().name]) + } +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 3d445ed..c3fbd0f 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -4,5 +4,5 @@ import XCTest XCTMain([ testCase(LeafTests.allTests), - testCase(ProviderTests.allTests) + testCase(ServicesTests.allTests) ])