Skip to content

Commit

Permalink
Merge pull request #8 from vapor-community/vapor3
Browse files Browse the repository at this point in the history
Vapor 3
  • Loading branch information
0xTim authored Aug 10, 2018
2 parents 554143d + 8cd9f4e commit 1e34264
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 199 deletions.
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
included:
- Sources
- Tests
11 changes: 2 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ language: generic
sudo: required
dist: trusty

osx_image: xcode9
osx_image: xcode9.4
before_install:
- if [ $TRAVIS_OS_NAME == "osx" ]; then
HOMEBREW_NO_AUTO_UPDATE=1 brew install vapor/tap/vapor;
brew install vapor/tap/vapor;
else
eval "$(curl -sL https://apt.vapor.sh)";
sudo apt-get install vapor;
Expand All @@ -19,13 +19,6 @@ script:
- swift build
- swift build -c release
- swift test
- if [ $TRAVIS_OS_NAME != "osx" ]; then
sudo apt-get remove vapor;
sudo apt-get install swift=3.1.1;
swift build;
swift build -c release;
swift test;
fi

after_success:
- eval "$(curl -sL https://raw.githubusercontent.com/vapor-community/swift/swift-4-codecov/codecov-swift4)"
17 changes: 13 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "MarkdownProvider",
name: "LeafMarkdown",
products: [
.library(name: "LeafMarkdown", targets: ["LeafMarkdown"]),
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.Package(url: "https://github.com/vapor/leaf-provider.git", majorVersion: 1),
.Package(url: "https://github.com/vapor-community/markdown.git", majorVersion: 0)
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/leaf.git", from: "3.0.0"),
.package(url: "https://github.com/vapor-community/markdown.git", .upToNextMajor(from: "0.4.0")),
],
targets: [
.target(name: "LeafMarkdown", dependencies: ["Vapor", "Leaf", "SwiftMarkdown"]),
.testTarget(name: "LeafMarkdownTests", dependencies: ["LeafMarkdown"]),
]
)
19 changes: 0 additions & 19 deletions [email protected]

This file was deleted.

40 changes: 16 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Markdown Provider
# Leaf Markdown

[![Language](https://img.shields.io/badge/Swift-4-brightgreen.svg)](http://swift.org)
[![Build Status](https://travis-ci.org/vapor-community/markdown-provider.svg?branch=master)](https://travis-ci.org/vapor-community/markdown-provider)
[![codecov](https://codecov.io/gh/vapor-community/markdown-provider/branch/master/graph/badge.svg)](https://codecov.io/gh/vapor-community/mmarkdown-provider)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/vapor-community/markdown-provider/master/LICENSE)
[![Build Status](https://travis-ci.org/vapor-community/leaf-markdown.svg?branch=master)](https://travis-ci.org/vapor-community/leaf-markdown)
[![codecov](https://codecov.io/gh/vapor-community/leaf-markdown/branch/master/graph/badge.svg)](https://codecov.io/gh/vapor-community/leaf-markdown)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/vapor-community/leaf-markdown/master/LICENSE)

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 support [Github Flavored Markdown](https://guides.github.com/features/mastering-markdown/)
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:

Expand All @@ -27,34 +27,26 @@ Check out my *awesome* markdown! It is easy to use in `tags`

### Add as dependency

Add Markdown Provider as a dependency in your `Package.swift` file:
Add Leaf Markdown as a dependency in your `Package.swift` file:

```swift
dependencies: [
...,
.package(url: "https://github.com/vapor-community/markdown-provider.git", .upToNextMajor(from: "1.1.0"))
.package(url: "https://github.com/vapor-community/leaf-markdown.git", .upToNextMajor(from: "2.0.0"))
]
```

### Add the Provider

You can add a provider to you `Config`, which will do all of the setup for you and register your tag. Just add it as so:

```swift
let config = try Config()
try config.addProvider(MarkdownProvider.Provider.self)
let drop = try Droplet(config)
```


### 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`.
33 changes: 33 additions & 0 deletions Sources/LeafMarkdown/Tag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Leaf
import SwiftMarkdown
import Async

public final class Markdown: TagRenderer {

public enum Error: Swift.Error {
case invalidArgument(TemplateData?)
}

public let name = "markdown"

public init() {}

public func render(tag: TagContext) throws -> Future<TemplateData> {

var markdown = ""

if let markdownArgument = tag.parameters.first, !markdownArgument.isNull {
guard let markdownArgumentValue = markdownArgument.string else {
throw Error.invalidArgument(tag.parameters.first)
}
markdown = markdownArgumentValue
}

let markdownHTML = try markdownToHTML(markdown)

return Future.map(on: tag) {
.string(markdownHTML)
}
}

}
21 changes: 0 additions & 21 deletions Sources/MarkdownProvider/Provider.swift

This file was deleted.

33 changes: 0 additions & 33 deletions Sources/MarkdownProvider/Tag.swift

This file was deleted.

44 changes: 44 additions & 0 deletions Tests/LeafMarkdownTests/LeafTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import XCTest
import Vapor

@testable import Leaf
import LeafMarkdown

class LeafTests: XCTestCase {
static var allTests = [
("testRunTag", testRunTag),
("testNilParameterDoesNotCrashLeaf", testNilParameterDoesNotCrashLeaf)
]

var renderer: LeafRenderer!
let template = "#markdown(data)"

override func setUp() {
let queue = EmbeddedEventLoop()
let container = BasicContainer(config: .init(), environment: .testing, services: .init(), on: queue)
let tag = Markdown()
var leafTagConfig = LeafTagConfig.default()
leafTagConfig.use(tag, as: tag.name)
self.renderer = LeafRenderer(config: LeafConfig(tags: leafTagConfig, viewsDir: "", shouldCache: false),
using: container)
}

func testRunTag() throws {
let inputMarkdown = "# This is a test\n\nWe have some text in a tag"
let data = TemplateData.dictionary(["data": .string(inputMarkdown)])
let expectedHtml = "<h1>This is a test</h1>\n<p>We have some text in a tag</p>\n"

let result = try renderer.render(template: template.data(using: .utf8)!, data).wait()
let resultString = String(data: result.data, encoding: .utf8)!
XCTAssertEqual(resultString, expectedHtml)
}

func testNilParameterDoesNotCrashLeaf() throws {
let data = TemplateData.dictionary(["data": .null])
let expectedHtml = ""

let result = try renderer.render(template: template.data(using: .utf8)!, data).wait()
let resultString = String(data: result.data, encoding: .utf8)!
XCTAssertEqual(resultString, expectedHtml)
}
}
23 changes: 23 additions & 0 deletions Tests/LeafMarkdownTests/ServicesTests.swift
Original file line number Diff line number Diff line change
@@ -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])
}
}
4 changes: 2 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import XCTest

@testable import MarkdownProviderTests
@testable import LeafMarkdownTests

XCTMain([
testCase(LeafTests.allTests),
testCase(ProviderTests.allTests)
testCase(ServicesTests.allTests)
])
51 changes: 0 additions & 51 deletions Tests/MarkdownProviderTests/LeafTests.swift

This file was deleted.

35 changes: 0 additions & 35 deletions Tests/MarkdownProviderTests/ProviderTests.swift

This file was deleted.

0 comments on commit 1e34264

Please sign in to comment.