Skip to content

Commit

Permalink
Added more documentation, and + function
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsMoll committed Apr 16, 2019
1 parent 2296fec commit 9e6e367
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 43 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ struct SimpleView: ContextualTemplate {
}
```

Note: You can also use `+` or in most cases `,` to concat multiple elements. Eg:
```swift
// Using `+`
div.child(
"Some text " + variable(\.name) + p.child("View More")
)

// Using `,`
div.child(
"Some text ", variable(\.name), p.child("View More")
)
```

### Now how do we render the views?

Expand Down Expand Up @@ -154,8 +166,10 @@ struct SomeView: ContextualTemplate {
runtimeIf(
\.values.count > 0,

forEach(in: \.values,
render: SimpleView())
forEach(
in: \.values,
render: SimpleView()
)
).else(
p.child(
"There is no values!"
Expand Down
19 changes: 19 additions & 0 deletions Sources/HTMLKit/CompiledTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@ extension UUID: CompiledTemplate {
formula.add(string: self.uuidString)
}
}


/// Concats two values
public func + (lhs: CompiledTemplate, rhs: CompiledTemplate) -> CompiledTemplate {
var output: Array<CompiledTemplate> = []

if let list = lhs as? Array<CompiledTemplate> {
output.append(contentsOf: list)
} else {
output.append(lhs)
}

if let list = rhs as? Array<CompiledTemplate> {
output.append(list)
} else {
output.append(rhs)
}
return output
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ extension HTML.AttributeNode: CompiledTemplate {
}
}

extension HTML.AttributeNode: CustomDebugStringConvertible {
public var debugDescription: String {
if let value = value {
return "Tag: \(attribute), Value: \(value)"
}
return "Tag: " + attribute
}
}

extension HTML.AttributeNode {

// MARK: - Non HTML standard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ extension HTML.ContentNode: AttributableNode, DynamicAttributable {
return .init(name: name, attributes: attributes, content: content, dynamicAttributes: [dynamicAttributes, ifCondition])
}
}

extension HTML.ContentNode: CustomDebugStringConvertible {
public var debugDescription: String {
return "Tag: \(name), Attributes: \(attributes), Content: \(content)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ extension TemplateVariable: CompiledTemplate {
}
}

extension TemplateVariable: CustomDebugStringConvertible {
public var debugDescription: String {
return "Variable: \(referance), escaping: \(escaping)"
}
}


extension ContextualTemplate {

/// References an optional variable in the `Context` type
Expand Down
9 changes: 8 additions & 1 deletion Sources/HTMLKit/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import Foundation
///
/// - unsafeNone: No escaping. This will render the variable as given
/// - safeHTML: This will escape characters that may cause security problems
public enum EscapingOption {
public enum EscapingOption: CustomDebugStringConvertible {
case unsafeNone
case safeHTML

public var debugDescription: String {
switch self {
case .unsafeNone: return "Unsafe"
case .safeHTML: return "Safe"
}
}
}

enum ContextReferance<T, Value> where T : ContextualTemplate {
Expand Down
2 changes: 0 additions & 2 deletions Sources/HTMLKit/HTMLKitProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Mats Mollestad on 11/03/2019.
//

#if canImport(Service)
import Service

extension HTMLRenderer: Service {}
Expand All @@ -27,4 +26,3 @@ public final class HTMLKitProvider: Provider {
return .done(on: container)
}
}
#endif
8 changes: 8 additions & 0 deletions Sources/HTMLKit/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import Lingo
import Foundation

/// A compiled template that returnes a localized string
struct Localize<T: ContextualTemplate, C: Encodable>: CompiledTemplate {

/// The key/text to localize
let key: String

/// The path to the local
let localePath: KeyPath<T.Context, String>

/// The path to the content needed to render the string, if needed
let contentPath: KeyPath<T.Context, C>?

// View `CompiledTempalte`
func render<T>(with manager: HTMLRenderer.ContextManager<T>) throws -> String {

let locale = try manager.value(at: localePath)
Expand All @@ -31,6 +38,7 @@ struct Localize<T: ContextualTemplate, C: Encodable>: CompiledTemplate {
}
}

// View `Brewable`
func brew<T>(_ formula: HTMLRenderer.Formula<T>) throws where T : ContextualTemplate {
formula.add(mappable: self)
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/HTMLKit/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

import SwiftMarkdown

/// A compiled template that converts markdown to html
struct Markdown: CompiledTemplate {

/// The markdown to convert
let markdown: CompiledTemplate

/// The rendering options
let options: MarkdownOptions

// View `Brewable`
func brew<T>(_ formula: HTMLRenderer.Formula<T>) throws where T : ContextualTemplate {
formula.add(mappable: self)
}

// View `CompiledTemplate`
func render<T>(with manager: HTMLRenderer.ContextManager<T>) throws -> String {
return try markdownToHTML(markdown.render(with: manager), options: options)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/HTMLKitTests/HTMLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class HTMLKitTests: XCTestCase {
try! FileManager().createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)

try! HTMLKitTests.enLocalizations.write(toFile: path + "/en.json", atomically: true, encoding: .utf8)
try! HTMLKitTests.noLocalizations.write(toFile: path + "/no.json", atomically: true, encoding: .utf8)
try! HTMLKitTests.nbLocalizations.write(toFile: path + "/nb.json", atomically: true, encoding: .utf8)

var renderer = HTMLRenderer()

Expand Down Expand Up @@ -48,7 +48,7 @@ final class HTMLKitTests: XCTestCase {
let twoDynamic = try renderer.renderRaw(DynamicAttribute.self, with: .init(isChecked: true, isActive: true, isOptional: false))
let markdown = try renderer.renderRaw(MarkdownView.self, with: .init(title: "Hello", description: "World"))
let english = try renderer.renderRaw(LocalizedView.self, with: .init(local: "en", description: .init(numberTest: 1)))
let norwegian = try renderer.renderRaw(LocalizedView.self, with: .init(local: "no", description: .init(numberTest: 2)))
let norwegian = try renderer.renderRaw(LocalizedView.self, with: .init(local: "nb", description: .init(numberTest: 2)))

let simpleRender = try renderer.renderRaw(SimpleView.self)
let chainedRender = try renderer.renderRaw(ChainedEqualAttributes.self)
Expand Down Expand Up @@ -87,7 +87,7 @@ final class HTMLKitTests: XCTestCase {
"""
}

private static var noLocalizations: String {
private static var nbLocalizations: String {
return """
{
"hello.world": "Hei Verden!",
Expand Down
38 changes: 3 additions & 35 deletions Tests/HTMLKitTests/HTMLTestDocuments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct StaticEmbedView: ContextualTemplate {
func build() -> CompiledTemplate {
return
div.child(
SimpleView(),
SimpleView() +
p.child(
variable(\.string)
),
) +

runtimeIf(
\.int != nil,
Expand All @@ -44,13 +44,6 @@ struct StaticEmbedView: ContextualTemplate {
)
)
)
// div(
// SimpleView.build(),
// p( variable(\.string)),
// runtimeIf(\.int != nil,
// small( variable(\.int))
// )
// )
}
}

Expand All @@ -77,12 +70,6 @@ struct BaseView: ContextualTemplate {
// Used to check for an error ocurring when embedding two different `ContextualTemplate`s and a `localFormula` is involved
runtimeIf(\.title == "May Cause an error when embedding multiple views", div)
)
// html(
// head(
// title(variable(\.title))
// ),
// body(context.body)
// )
}
}

Expand Down Expand Up @@ -153,7 +140,7 @@ struct IFView: ContextualTemplate {
\.name == "Mats",

p.child(
"My name is: ", variable(\.name), "!"
"My name is: " + variable(\.name) + "!"
)
),

Expand All @@ -178,25 +165,6 @@ struct IFView: ContextualTemplate {
)

)
// div(
// runtimeIf(\.name == "Mats",
// p("My name is: ", variable(\.name), "!")
// ),
//
// runtimeIf(\.age < 20,
// "I am a child"
// ).elseIf(\.age > 20,
// "I am older"
// ).else(render:
// "I am growing"
// ),
//
// runtimeIf(\.nullable != nil,
// b( variable(\.nullable))
// ).elseIf(\.bool,
// p("Simple bool")
// )
// )
}
}

Expand Down

0 comments on commit 9e6e367

Please sign in to comment.