Skip to content

Commit

Permalink
Merge pull request #3 from brokenhandsio/master
Browse files Browse the repository at this point in the history
Merge Broken Hands work
  • Loading branch information
0xTim authored May 18, 2017
2 parents 7620fd1 + 90e209d commit f1dd75f
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 145 deletions.
4 changes: 4 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage:
range: "0...100"
ignore:
- "Tests/"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.build
/Packages
/*.xcodeproj
Package.pins
DerivedData/
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-08-18-a
3.1
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
os:
- linux
- osx
language: generic
sudo: required
dist: trusty

osx_image: xcode8.3
before_install:
- if [ $TRAVIS_OS_NAME == "osx" ]; then
brew tap vapor/tap;
brew update;
brew install vapor;
else
eval "$(curl -sL https://apt.vapor.sh)";
sudo apt-get install vapor;
sudo chmod -R a+rx /usr/;
fi

script:
- swift build
- swift build -c release
- swift test

after_success:
- eval "$(curl -sL https://swift.vapor.sh/codecov)"
10 changes: 4 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import PackageDescription

let package = Package(
name: "vapor-markdown",
targets: [
Target(name: "vapor-markdown-example", dependencies: ["vapor-markdown"])
],
name: "MarkdownProvider",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 17),
.Package(url: "https://github.com/czechboy0/cmark.swift.git", majorVersion: 0, minor: 1)
.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)
]
)
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
# vapor-markdown
# Markdown Provider

> Markdown renderer for Vapor
[![Language](https://img.shields.io/badge/Swift-3.1-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)

WIP
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/)

## Use

Once set up, you can use it in your Leaf template files like any other tag:

```swift
#markdown(myMarkdown)
```

Where you have passed `myMarkdown` into the view as something like:

```markdown
# Hey #

Check out my *awesome* markdown! It is easy to use in `tags`
```

## Setup

### Add as dependency

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

```swift
dependencies: [
...,
.Package(url: "https://github.com/vapor-community/markdown-provider", majorVersion: 0)
]
```

### Add the Provider

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

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


### Register with Leaf

Alternatively, you can also directly add the Tag onto your `LeafRenderer` if desired. During your setup (for example, in `main.swift`), register your tag as so:

```swift
if let leaf = drop.view as? LeafRenderer {
leaf.stem.register(Markdown())
}
```

Don't forget to import MarkdownProvider in the file you register the tag or add the provider in with `import MarkdownProvider`
7 changes: 0 additions & 7 deletions Resources/Views/leaf/index.leaf

This file was deleted.

11 changes: 0 additions & 11 deletions Resources/Views/md/welcome.md

This file was deleted.

21 changes: 21 additions & 0 deletions Sources/MarkdownProvider/Provider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Vapor
import LeafProvider

public struct Provider: Vapor.Provider {

public static let repositoryName = "markdown-provider"

public func boot(_ drop: Droplet) {
guard let renderer = drop.view as? LeafRenderer else {
print("LeafMarkdown only supports Leaf as a renderer")
return
}

renderer.stem.register(Markdown())
}

public init(config: Config) throws {}
public init() {}
public func boot(_ config: Config) throws {}
public func beforeRun(_: Vapor.Droplet) {}
}
33 changes: 33 additions & 0 deletions Sources/MarkdownProvider/Tag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Leaf
import SwiftMarkdown

public final class Markdown: Tag {

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

public init() { }

public let name = "markdown"

public func shouldRender(stem: Stem, context: Context, tagTemplate: TagTemplate, arguments: [Argument], value: Node?) -> Bool{
return true
}

public func run(tagTemplate: TagTemplate, arguments: ArgumentList) throws -> Node? {
var markdown = ""

if let markdownArgument = arguments.first {
guard let markdownArgumentValue = markdownArgument.string else {
throw Error.invalidArgument(arguments.list.first)
}
markdown = markdownArgumentValue
}

let markdownHtml = try markdownToHTML(markdown)
let unescaped = markdownHtml.bytes
return .bytes(unescaped)
}

}
11 changes: 0 additions & 11 deletions Sources/vapor-markdown-example/main.swift

This file was deleted.

19 changes: 0 additions & 19 deletions Sources/vapor-markdown/LeafMarkdownRenderer.swift

This file was deleted.

18 changes: 0 additions & 18 deletions Sources/vapor-markdown/MarkdownProvider.swift

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/vapor-markdown/MarkdownRenderer.swift

This file was deleted.

41 changes: 0 additions & 41 deletions Sources/vapor-markdown/MarkdownTag.swift

This file was deleted.

8 changes: 8 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

@testable import MarkdownProviderTests

XCTMain([
testCase(LeafTests.allTests),
testCase(ProviderTests.allTests)
])
51 changes: 51 additions & 0 deletions Tests/MarkdownProviderTests/LeafTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import XCTest
import Vapor

@testable import Leaf
@testable import MarkdownProvider

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

func testRunTag() {
let tag = Markdown()
let inputMarkdown = "# This is a test\n\nWe have some text in a tag"
let expectedHtml = "<h1>This is a test</h1>\n<p>We have some text in a tag</p>\n"

do {
let node = try run(tag: tag, context: inputMarkdown.makeNode(in: nil), arguments: [.constant(Leaf(raw: inputMarkdown, components: [.raw(inputMarkdown.makeBytes())]))])
XCTAssertEqual(node?.string, expectedHtml)
}
catch {
XCTFail()
}
}

func testNilParameterDoesNotCrashLeaf() {
let tag = Markdown()
let expectedHtml = ""

do {
let node = try run(tag: tag, context: nil, arguments: [])
XCTAssertEqual(node?.string, expectedHtml)
}
catch {
XCTFail("Markdown Tag threw exception")
}
}
}

extension LeafTests {
func run(tag: Tag, context node: Node, arguments: [Argument]) throws -> Node? {
let context = Context(node)
let argumentList = ArgumentList(list: arguments, stem: Stem(DataFile(workDir: "")), context: context)

return try tag.run(
tagTemplate: TagTemplate(name: "", parameters: [], body: nil),
arguments: argumentList
)
}
}
Loading

0 comments on commit f1dd75f

Please sign in to comment.