Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed May 23, 2022
1 parent dd8ad03 commit a08d7e4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Sources/TokamakCore/MountedViews/MountedElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class MountedElement<R: Renderer> {

public internal(set) var environmentValues: EnvironmentValues

weak private(set) var parent: MountedElement<R>?
private(set) weak var parent: MountedElement<R>?

var preferenceStore: _PreferenceStore = .init()

Expand Down
22 changes: 11 additions & 11 deletions Sources/TokamakStaticHTML/Preferences/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
import TokamakCore

public struct HTMLTitlePreferenceKey: PreferenceKey {
public static var defaultValue: String = ""
public static var defaultValue: String = ""

public static func reduce(value: inout String, nextValue: () -> String) {
value = nextValue()
}
public static func reduce(value: inout String, nextValue: () -> String) {
value = nextValue()
}
}

public struct HTMLMetaPreferenceKey: PreferenceKey {
public static var defaultValue: [HTMLMeta.MetaTag] = []
public static var defaultValue: [HTMLMeta.MetaTag] = []

public static func reduce(
value: inout [HTMLMeta.MetaTag],
nextValue: () -> [HTMLMeta.MetaTag]
) {
value += nextValue()
}
public static func reduce(
value: inout [HTMLMeta.MetaTag],
nextValue: () -> [HTMLMeta.MetaTag]
) {
value += nextValue()
}
}
11 changes: 5 additions & 6 deletions Sources/TokamakStaticHTML/StaticHTMLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ struct HTMLBody: AnyHTML {
extension HTMLMeta.MetaTag {
func outerHTML() -> String {
switch self {
case .charset(let charset):
case let .charset(charset):
return #"<meta charset="\#(charset)">"#
case .name(let name, let content):
case let .name(name, content):
return #"<meta name="\#(name)" content="\#(content)">"#
case .property(let property, let content):
case let .property(property, content):
return #"<meta property="\#(property)" content="\#(content)">"#
case .httpEquiv(let httpEquiv, let content):
case let .httpEquiv(httpEquiv, content):
return #"<meta http-equiv="\#(httpEquiv)" content="\#(content)">"#
}
}
Expand All @@ -91,7 +91,7 @@ public final class StaticHTMLRenderer: Renderer {
<html>
<head>
<title>\(title)</title>
\(meta.map({ $0.outerHTML() }).joined(separator: "\n "))
\(meta.map { $0.outerHTML() }.joined(separator: "\n "))
<style>
\(tokamakStyles)
</style>
Expand All @@ -107,7 +107,6 @@ public final class StaticHTMLRenderer: Renderer {
}

public init<V: View>(_ view: V, _ rootEnvironment: EnvironmentValues? = nil) {

rootTarget = HTMLTarget(view, HTMLBody())

reconciler = StackReconciler(
Expand Down
109 changes: 54 additions & 55 deletions Sources/TokamakStaticHTML/Views/Head/Meta.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,66 @@
import TokamakCore

public struct HTMLMeta: View {
public enum MetaTag: Equatable, Hashable {
case charset(_ charset: String)
case name(_ name: String, content: String)
case property(_ property: String, content: String)
case httpEquiv(_ httpEquiv: String, content: String)
}

var meta: MetaTag

public init(_ value: MetaTag) {
meta = value
}

public init(charset: String) {
meta = .charset(charset)
}

public init(name: String, content: String) {
meta = .name(name, content: content)
}

public init(property: String, content: String) {
meta = .property(property, content: content)
}

public init(httpEquiv: String, content: String) {
meta = .httpEquiv(httpEquiv, content: content)
}

public var body: some View {
EmptyView()
.preference(key: HTMLMetaPreferenceKey.self, value: [meta])
}
public enum MetaTag: Equatable, Hashable {
case charset(_ charset: String)
case name(_ name: String, content: String)
case property(_ property: String, content: String)
case httpEquiv(_ httpEquiv: String, content: String)
}

var meta: MetaTag

public init(_ value: MetaTag) {
meta = value
}

public init(charset: String) {
meta = .charset(charset)
}

public init(name: String, content: String) {
meta = .name(name, content: content)
}

public init(property: String, content: String) {
meta = .property(property, content: content)
}

public init(httpEquiv: String, content: String) {
meta = .httpEquiv(httpEquiv, content: content)
}

public var body: some View {
EmptyView()
.preference(key: HTMLMetaPreferenceKey.self, value: [meta])
}
}

extension View {

public func htmlMeta(_ value: HTMLMeta.MetaTag) -> some View {
htmlMeta(.init(value))
}
public extension View {
func htmlMeta(_ value: HTMLMeta.MetaTag) -> some View {
htmlMeta(.init(value))
}

public func htmlMeta(charset: String) -> some View {
htmlMeta(.init(charset: charset))
}
func htmlMeta(charset: String) -> some View {
htmlMeta(.init(charset: charset))
}

public func htmlMeta(name: String, content: String) -> some View {
htmlMeta(.init(name: name, content: content))
}
func htmlMeta(name: String, content: String) -> some View {
htmlMeta(.init(name: name, content: content))
}

public func htmlMeta(property: String, content: String) -> some View {
htmlMeta(.init(property: property, content: content))
}
func htmlMeta(property: String, content: String) -> some View {
htmlMeta(.init(property: property, content: content))
}

public func htmlMeta(httpEquiv: String, content: String) -> some View {
htmlMeta(.init(httpEquiv: httpEquiv, content: content))
}
func htmlMeta(httpEquiv: String, content: String) -> some View {
htmlMeta(.init(httpEquiv: httpEquiv, content: content))
}

public func htmlMeta(_ meta: HTMLMeta) -> some View {
Group {
self
meta
}
func htmlMeta(_ meta: HTMLMeta) -> some View {
Group {
self
meta
}
}
}
36 changes: 17 additions & 19 deletions Sources/TokamakStaticHTML/Views/Head/Title.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@
import TokamakCore

public struct HTMLTitle: View {
var title: String

var title: String
public init(_ title: String) {
self.title = title
}

public init(_ title: String) {
self.title = title
}

public var body: some View {
EmptyView()
.preference(key: HTMLTitlePreferenceKey.self, value: title)
}
public var body: some View {
EmptyView()
.preference(key: HTMLTitlePreferenceKey.self, value: title)
}
}

extension View {

public func htmlTitle(_ title: String) -> some View {
htmlTitle(.init(title))
}
public extension View {
func htmlTitle(_ title: String) -> some View {
htmlTitle(.init(title))
}

public func htmlTitle(_ title: HTMLTitle) -> some View {
Group {
self
title
}
func htmlTitle(_ title: HTMLTitle) -> some View {
Group {
self
title
}
}
}
8 changes: 4 additions & 4 deletions Tests/TokamakStaticHTMLTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ final class HTMLTests: XCTestCase {
}

func testPreferencePropagation() {
var title0: String = ""
var title1: String = ""
var title2: String = ""
var title3: String = ""
var title0 = ""
var title1 = ""
var title2 = ""
var title3 = ""

let resultingHTML = StaticHTMLRenderer(
VStack {
Expand Down

0 comments on commit a08d7e4

Please sign in to comment.