66import Foundation
77import 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: " & " )
@@ -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: " < " )
0 commit comments