-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Joannis/master
[WIP] Leaf 4 support
- Loading branch information
Showing
6 changed files
with
86 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ coverage: | |
range: "0...100" | ||
ignore: | ||
- "Tests/" | ||
- ".build/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
// swift-tools-version:5.1 | ||
// swift-tools-version:5.2 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "LeafMarkdown", | ||
platforms: [ | ||
.macOS(.v10_15) | ||
], | ||
products: [ | ||
.library(name: "LeafMarkdown", targets: ["LeafMarkdown"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/vapor/template-kit.git", from: "1.4.0"), | ||
.package(url: "https://github.com/vapor-community/markdown.git", from: "0.5.0"), | ||
.package(url: "https://github.com/vapor/leaf.git", from: "3.0.0"), | ||
.package(name: "SwiftMarkdown", url: "https://github.com/vapor-community/markdown.git", from: "0.5.0"), | ||
.package(url: "https://github.com/vapor/leaf-kit.git", from: "1.0.0-rc.1"), | ||
], | ||
targets: [ | ||
.target(name: "LeafMarkdown", dependencies: ["TemplateKit", "SwiftMarkdown"]), | ||
.testTarget(name: "LeafMarkdownTests", dependencies: ["LeafMarkdown", "Leaf"]), | ||
.target(name: "LeafMarkdown", dependencies: [ | ||
.product(name: "LeafKit", package: "leaf-kit"), | ||
.product(name: "SwiftMarkdown", package: "SwiftMarkdown"), | ||
]), | ||
.testTarget(name: "LeafMarkdownTests", dependencies: [ | ||
.target(name: "LeafMarkdown"), | ||
.product(name: "LeafKit", package: "leaf-kit"), | ||
]), | ||
] | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,34 @@ | ||
import TemplateKit | ||
import LeafKit | ||
import SwiftMarkdown | ||
|
||
public final class Markdown: TagRenderer { | ||
|
||
public struct Markdown: LeafTag { | ||
public enum Error: Swift.Error { | ||
case invalidArgument(TemplateData?) | ||
case invalidArgument(LeafData?) | ||
} | ||
|
||
public let name = "markdown" | ||
|
||
|
||
private let options: MarkdownOptions? | ||
|
||
public init(options: MarkdownOptions? = nil) { | ||
self.options = options | ||
} | ||
|
||
public func render(tag: TagContext) throws -> Future<TemplateData> { | ||
|
||
|
||
public func render(_ ctx: LeafContext) throws -> LeafData { | ||
var markdown = "" | ||
|
||
if let markdownArgument = tag.parameters.first, !markdownArgument.isNull { | ||
if let markdownArgument = ctx.parameters.first, !markdownArgument.isNull { | ||
guard let markdownArgumentValue = markdownArgument.string else { | ||
throw Error.invalidArgument(tag.parameters.first) | ||
throw Error.invalidArgument(ctx.parameters.first) | ||
} | ||
markdown = markdownArgumentValue | ||
} | ||
|
||
let markdownHTML: String = try { | ||
if let options = options { | ||
return try markdownToHTML(markdown, options: options) | ||
} else { | ||
return try markdownToHTML(markdown) | ||
} | ||
}() | ||
|
||
return Future.map(on: tag) { | ||
.string(markdownHTML) | ||
let markdownHTML: String | ||
if let options = options { | ||
markdownHTML = try markdownToHTML(markdown, options: options) | ||
} else { | ||
markdownHTML = try markdownToHTML(markdown) | ||
} | ||
} | ||
|
||
return .string(markdownHTML) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,89 @@ | ||
import XCTest | ||
@testable import Leaf | ||
@testable import LeafKit | ||
import LeafMarkdown | ||
|
||
class LeafTests: XCTestCase { | ||
|
||
class MarkdownTests: XCTestCase { | ||
// MARK: - Properties | ||
|
||
var renderer: LeafRenderer! | ||
let template = "#markdown(data)" | ||
var template: ResolvedDocument! | ||
|
||
// MARK: - Overrides | ||
|
||
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) | ||
let loop = EmbeddedEventLoop() | ||
let config = LeafConfiguration(rootDirectory: Process().currentDirectoryPath) | ||
var tags = defaultTags | ||
tags["markdown"] = Markdown() | ||
self.renderer = LeafRenderer( | ||
configuration: config, | ||
tags: tags, | ||
files: NIOLeafFiles(fileio: .init(threadPool: .init(numberOfThreads: 1))), | ||
eventLoop: loop | ||
) | ||
|
||
do { | ||
var lexer = LeafLexer(name: "markdowntest", template: "#markdown(data)") | ||
let tokens = try lexer.lex() | ||
var parser = LeafParser(name: "markdowntest", tokens: tokens) | ||
let syntax = try parser.parse() | ||
let unresolved = UnresolvedDocument(name: "markdowntest", raw: syntax) | ||
let resolver = ExtendResolver(document: unresolved, dependencies: []) | ||
template = try resolver.resolve(rootDirectory: config.rootDirectory) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
|
||
// MARK: - Tests | ||
|
||
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)! | ||
let result = try renderer.render(template, context: ["data": .string(inputMarkdown)]) | ||
let resultString = result.getString(at: 0, length: result.readableBytes) | ||
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)! | ||
let result = try renderer.render(template, context: ["data": .null]) | ||
let resultString = result.getString(at: 0, length: result.readableBytes) | ||
XCTAssertEqual(resultString, expectedHtml) | ||
} | ||
|
||
func testStripHtml() throws { | ||
let inputMarkdown = "<br>" | ||
let data = TemplateData.dictionary(["data": .string(inputMarkdown)]) | ||
let expectedHtml = "<!-- raw HTML omitted -->\n" | ||
|
||
let result = try renderer.render(template: template.data(using: .utf8)!, data).wait() | ||
let resultString = String(data: result.data, encoding: .utf8)! | ||
let result = try renderer.render(template, context: ["data": .string(inputMarkdown)]) | ||
let resultString = result.getString(at: 0, length: result.readableBytes) | ||
XCTAssertEqual(resultString, expectedHtml) | ||
} | ||
|
||
func testRejectBadData() throws { | ||
let data = LeafData.lazy { .null } | ||
XCTAssertThrowsError(try renderer.render(template, context: ["data": data])) | ||
} | ||
|
||
func testDoNotStripHtml() throws { | ||
|
||
let queue = EmbeddedEventLoop() | ||
let container = BasicContainer(config: .init(), environment: .testing, services: .init(), on: queue) | ||
let tag = Markdown(options: []) | ||
var leafTagConfig = LeafTagConfig.default() | ||
leafTagConfig.use(tag, as: tag.name) | ||
let renderer = LeafRenderer(config: LeafConfig(tags: leafTagConfig, viewsDir: "", shouldCache: false), | ||
using: container) | ||
let loop = EmbeddedEventLoop() | ||
let config = LeafConfiguration(rootDirectory: Process().currentDirectoryPath) | ||
var tags = defaultTags | ||
tags["markdown"] = Markdown(options: [.unsafe]) | ||
let renderer = LeafRenderer( | ||
configuration: config, | ||
tags: tags, | ||
files: NIOLeafFiles(fileio: .init(threadPool: .init(numberOfThreads: 1))), | ||
eventLoop: loop | ||
) | ||
|
||
let inputMarkdown = "<br>" | ||
let data = TemplateData.dictionary(["data": .string(inputMarkdown)]) | ||
let expectedHtml = "<br>\n" | ||
|
||
let result = try renderer.render(template: template.data(using: .utf8)!, data).wait() | ||
let resultString = String(data: result.data, encoding: .utf8)! | ||
let result = try renderer.render(template, context: ["data": .string(inputMarkdown)]) | ||
let resultString = result.getString(at: 0, length: result.readableBytes) | ||
XCTAssertEqual(resultString, expectedHtml) | ||
} | ||
} |