Skip to content

Commit 57ec613

Browse files
authored
Prepare for release (#145)
* Make the features flag available to vapor * Tidy up a bit * Silence deprecation warning of nsdictionary * Update readme * Silence warning of shadowing type parameters
1 parent cb34198 commit 57ec613

File tree

14 files changed

+195
-144
lines changed

14 files changed

+195
-144
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ The HTMLKit framework consists of a list of features to create HTML templates in
2323
- **UI components** - Construct user interfaces with the components library.
2424
- **Symbol bundle** - Use the symbols which come within the framework.
2525

26-
The framework comes with an integration for the web framework [Vapor](https://swiftpackageindex.com/vapor/vapor). Interested? Lets get started: [Tutorials](https://swiftpackageindex.com/vapor-community/HTMLKit/3.0.0-alpha.8/documentation/htmlkit)
26+
The framework comes with an integration for the web framework [Vapor](https://github.com/vapor/vapor). Interested? Lets get started: [Tutorials](https://swiftpackageindex.com/vapor-community/htmlkit/main/tutorials/introduction)

Sources/HTMLKit/Framework/Environment/EnvironmentObject.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import Foundation
22

33
/// A property wrapper type to initate an environment object
4-
@frozen @propertyWrapper public struct EnvironmentObject<Value> {
4+
@frozen @propertyWrapper public struct EnvironmentObject<ObjectType> {
55

66
/// The wrapped value
7-
public var wrappedValue: Wrapper<Value>
7+
public var wrappedValue: Wrapper<ObjectType>
88

99
/// Converts the type into the wrapped value
10-
public init(_ type: Value.Type) {
10+
public init(_ type: ObjectType.Type) {
1111

1212
self.wrappedValue = .init()
1313
}
1414

1515
/// A type, that holds the environment object informationen
16-
@dynamicMemberLookup public struct Wrapper<Value> {
16+
@dynamicMemberLookup public struct Wrapper<WrapperType> {
1717

1818
/// The path of the parent
1919
internal var parent: AnyKeyPath?
@@ -24,7 +24,7 @@ import Foundation
2424
/// Initiates a wrapper
2525
public init() {
2626

27-
self.path = \Value.self
27+
self.path = \WrapperType.self
2828
}
2929

3030
/// Initiates a wrapper with the necessary information for the environment object
@@ -35,7 +35,7 @@ import Foundation
3535
}
3636

3737
/// Looks up for a containing property
38-
public subscript<T>(dynamicMember member: KeyPath<Value, T>) -> EnvironmentValue {
38+
public subscript<T>(dynamicMember member: KeyPath<WrapperType, T>) -> EnvironmentValue {
3939

4040
guard let newPath = self.path.appending(path: member) else {
4141
fatalError()
@@ -49,7 +49,7 @@ import Foundation
4949
}
5050

5151
/// Looks up for a containing model
52-
public subscript<T>(dynamicMember member: KeyPath<Value, T>) -> Wrapper<T> where T: ViewModel {
52+
public subscript<T>(dynamicMember member: KeyPath<WrapperType, T>) -> Wrapper<T> where T: ViewModel {
5353

5454
guard let newPath = self.path.appending(path: member) else {
5555
fatalError()

Sources/HTMLKit/Framework/Localization/Localization.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Localization {
1212
case missingTables
1313
case unkownTable
1414
case noFallback
15+
case loadingDataFailed
1516

1617
public var description: String {
1718

@@ -30,6 +31,9 @@ public class Localization {
3031

3132
case .noFallback:
3233
return "The fallback needs to be set up first."
34+
35+
case .loadingDataFailed:
36+
return "Unable to load data."
3337
}
3438
}
3539
}
@@ -81,11 +85,14 @@ public class Localization {
8185

8286
if var translationTables = localizationTables[locale] {
8387

84-
if let translations = NSDictionary(contentsOf: path) as? [String: String] {
85-
translationTables.append(TranslationTable(name: path.deletingPathExtension().lastPathComponent, translations: translations))
88+
if let data = try? Foundation.Data(contentsOf: path) {
89+
90+
if let translations = try? PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil) as? [String: String] {
91+
translationTables.append(TranslationTable(name: path.deletingPathExtension().lastPathComponent, translations: translations))
92+
}
93+
94+
localizationTables[locale] = translationTables
8695
}
87-
88-
localizationTables[locale] = translationTables
8996
}
9097
}
9198

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// An option set of features.
2+
public struct Features: OptionSet {
3+
4+
public var rawValue: Int
5+
6+
public static let markdown = Features(rawValue: 1 << 0)
7+
8+
public init(rawValue: Int) {
9+
self.rawValue = rawValue
10+
}
11+
}

Sources/HTMLKit/Framework/Rendering/Renderer+FeatureSet.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.

Sources/HTMLKit/Framework/Rendering/Renderer.swift

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@
66
import Foundation
77
import OrderedCollections
88

9-
/// A struct containing the different formulas for the different views.
109
@_documentation(visibility: internal)
11-
public class Renderer {
10+
public final class Renderer {
1211

13-
/// A enumeration of possible render errors
12+
/// An enumeration of possible rendering errors.
1413
public enum Errors: Error {
1514

15+
/// Indicates a casting error.
1616
case unableToCastEnvironmentValue
17+
18+
/// Indicates a wrong environment key.
1719
case unindendedEnvironmentKey
20+
21+
/// Indicates a missing environment object.
1822
case environmentObjectNotFound
23+
24+
/// Indicates a missing environment value.
1925
case environmentValueNotFound
26+
27+
/// Indicates a missing localization configuration.
2028
case missingLocalization
2129

30+
/// A brief error description.
2231
public var description: String {
2332

2433
switch self {
@@ -40,47 +49,35 @@ public class Renderer {
4049
}
4150
}
4251

52+
/// The context environment
4353
private var environment: Environment
4454

55+
/// The localization configuration
4556
private var localization: Localization?
4657

58+
/// The security configuration
4759
private var security: Security
4860

61+
/// The markdown parser
4962
private var markdown: Markdown
5063

51-
public var features: FeatureSet
64+
/// The feature flag used to manage the visibility of new and untested features.
65+
private var features: Features
5266

5367
/// Initiates the renderer.
54-
public init(localization: Localization? = nil) {
55-
56-
self.localization = localization
57-
self.environment = Environment()
58-
self.security = Security()
59-
self.markdown = Markdown()
60-
self.features = []
61-
}
62-
63-
/// Initiates the renderer.
64-
public init(localization: Localization? = nil, security: Security) {
65-
66-
self.localization = localization
67-
self.environment = Environment()
68-
self.security = security
69-
self.markdown = Markdown()
70-
self.features = []
71-
}
72-
73-
/// Initiates the renderer.
74-
public init(localization: Localization? = nil, environment: Environment, security: Security) {
68+
public init(localization: Localization? = nil,
69+
environment: Environment = Environment(),
70+
security: Security = Security(),
71+
features: Features = []) {
7572

7673
self.localization = localization
7774
self.environment = environment
7875
self.security = security
7976
self.markdown = Markdown()
80-
self.features = []
77+
self.features = features
8178
}
8279

83-
/// Renders a view
80+
/// Renders a view and transforms it into a string representation.
8481
public func render(view: some View) throws -> String {
8582

8683
var result = ""
@@ -92,7 +89,8 @@ public class Renderer {
9289
return result
9390
}
9491

95-
internal func render(contents: [Content]) throws -> String {
92+
/// Reads the view content and transforms it.
93+
private func render(contents: [Content]) throws -> String {
9694

9795
var result = ""
9896

@@ -156,8 +154,8 @@ public class Renderer {
156154
return result
157155
}
158156

159-
/// Renders a content element
160-
internal func render(element: some ContentNode) throws -> String {
157+
/// Renders a content element. A content element usually has descendants, which need to be rendered as well.
158+
private func render(element: some ContentNode) throws -> String {
161159

162160
var result = ""
163161

@@ -234,8 +232,8 @@ public class Renderer {
234232
return result
235233
}
236234

237-
/// Renders a empty element
238-
internal func render(element: some EmptyNode) throws -> String {
235+
/// Renders a empty element. An empty element has no descendants.
236+
private func render(element: some EmptyNode) throws -> String {
239237

240238
var result = ""
241239

@@ -250,11 +248,11 @@ public class Renderer {
250248
return result
251249
}
252250

253-
/// Renders a document element
254-
internal func render(element: some DocumentNode) -> String {
251+
/// Renders a document element. The document element holds the metadata.
252+
private func render(element: some DocumentNode) -> String {
255253

256254
var result = ""
257-
255+
258256
result += "<!DOCTYPE "
259257

260258
result += element.content
@@ -264,8 +262,8 @@ public class Renderer {
264262
return result
265263
}
266264

267-
/// Renders a comment element
268-
internal func render(element: some CommentNode) -> String {
265+
/// Renders a comment element.
266+
private func render(element: some CommentNode) -> String {
269267

270268
var result = ""
271269

@@ -278,8 +276,8 @@ public class Renderer {
278276
return result
279277
}
280278

281-
/// Renders a content element
282-
internal func render(element: some CustomNode) throws -> String {
279+
/// Renders a custom element.
280+
private func render(element: some CustomNode) throws -> String {
283281

284282
var result = ""
285283

@@ -352,8 +350,8 @@ public class Renderer {
352350
return result
353351
}
354352

355-
/// Renders a localized string key
356-
internal func render(stringkey: LocalizedStringKey) throws -> String {
353+
/// Renders a localized string key.
354+
private func render(stringkey: LocalizedStringKey) throws -> String {
357355

358356
guard let localization = self.localization else {
359357
throw Errors.missingLocalization
@@ -366,8 +364,8 @@ public class Renderer {
366364
return try localization.localize(key: stringkey.key, locale: environment.locale, interpolation: stringkey.interpolation)
367365
}
368366

369-
/// Renders a environment modifier
370-
internal func render(modifier: EnvironmentModifier) throws -> String {
367+
/// Renders a environment modifier.
368+
private func render(modifier: EnvironmentModifier) throws -> String {
371369

372370
if let value = modifier.value {
373371
self.environment.upsert(value, for: modifier.key)
@@ -401,8 +399,8 @@ public class Renderer {
401399
return try render(contents: modifier.content)
402400
}
403401

404-
/// Renders a environment value
405-
internal func render(value: EnvironmentValue) throws -> String {
402+
/// Renders a environment value.
403+
private func render(value: EnvironmentValue) throws -> String {
406404

407405
guard let parent = self.environment.retrieve(for: value.parentPath) else {
408406
throw Errors.environmentObjectNotFound
@@ -439,8 +437,8 @@ public class Renderer {
439437
}
440438
}
441439

442-
/// Renders the node attributes
443-
internal func render(attributes: OrderedDictionary<String, Any>) throws -> String {
440+
/// Renders the node attributes.
441+
private func render(attributes: OrderedDictionary<String, Any>) throws -> String {
444442

445443
var result = ""
446444

@@ -461,13 +459,13 @@ public class Renderer {
461459
return result
462460
}
463461

464-
/// Renders the markdown content
465-
internal func render(markdown: MarkdownString) throws -> String {
462+
/// Renders the markdown content.
463+
private func render(markdown: MarkdownString) throws -> String {
466464
return self.markdown.render(string: escape(content: markdown.raw))
467465
}
468466

469467
/// Converts specific charaters into encoded values.
470-
internal func escape(attribute value: String) -> String {
468+
private func escape(attribute value: String) -> String {
471469

472470
if security.autoEscaping {
473471
return value.replacingOccurrences(of: "&", with: "&amp;")
@@ -479,7 +477,7 @@ public class Renderer {
479477
}
480478

481479
/// Converts specific charaters into encoded values.
482-
internal func escape(content value: String) -> String {
480+
private func escape(content value: String) -> String {
483481

484482
if security.autoEscaping {
485483
return value.replacingOccurrences(of: "<", with: "&lt;")

Sources/HTMLKit/Framework/Security/Security.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Security {
1+
public final class Security {
22

33
public var autoEscaping: Bool
44

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import HTMLKit
2+
3+
/// Holds the renderer configuration
4+
public final class Configuration {
5+
6+
/// Holds the localization configuration
7+
internal var localization: HTMLKit.Localization
8+
9+
/// Holds the environment configuration
10+
internal var environment: HTMLKit.Environment
11+
12+
/// Holds the security configuration
13+
internal var security: HTMLKit.Security
14+
15+
/// Holds the enabled features
16+
internal var features: HTMLKit.Features
17+
18+
/// Creates a configuration
19+
public init() {
20+
21+
self.localization = Localization()
22+
self.environment = Environment()
23+
self.security = Security()
24+
self.features = []
25+
}
26+
}

0 commit comments

Comments
 (0)