@@ -43,8 +43,8 @@ enum HTMLElements : DeclarationMacro {
43
43
public let tag:String = " \( tag) "
44
44
public var attributes:[HTMLAttribute]
45
45
public var innerHTML:[CustomStringConvertible & Sendable]
46
- private var encoding:HTMLEncoding = .string
47
- private var fromMacro:Bool = false
46
+ public private(set) var encoding:HTMLEncoding = .string
47
+ public private(set) var fromMacro:Bool = false
48
48
public let isVoid:Bool = \( isVoid)
49
49
public var trailingSlash:Bool = false
50
50
public var escaped:Bool = false
@@ -100,12 +100,12 @@ enum HTMLElements : DeclarationMacro {
100
100
initializers += " _ innerHTML: CustomStringConvertible & Sendable... \n ) { \n "
101
101
initializers += " self.attributes = attributes \n "
102
102
for (key, _, _) in attributes {
103
- var key_literal = key
104
- if key_literal . first == " ` " {
105
- key_literal . removeFirst ( )
106
- key_literal . removeLast ( )
103
+ var keyLiteral = key
104
+ if keyLiteral . first == " ` " {
105
+ keyLiteral . removeFirst ( )
106
+ keyLiteral . removeLast ( )
107
107
}
108
- initializers += " self. \( key_literal ) = \( key) \n "
108
+ initializers += " self. \( keyLiteral ) = \( key) \n "
109
109
}
110
110
initializers += " self.innerHTML = innerHTML \n } \n "
111
111
@@ -117,10 +117,10 @@ enum HTMLElements : DeclarationMacro {
117
117
}
118
118
initializers += " self.attributes = data.globalAttributes \n "
119
119
for (key, value_type, _) in attributes {
120
- var key_literal = key
121
- if key_literal . first == " ` " {
122
- key_literal . removeFirst ( )
123
- key_literal . removeLast ( )
120
+ var keyLiteral = key
121
+ if keyLiteral . first == " ` " {
122
+ keyLiteral . removeFirst ( )
123
+ keyLiteral . removeLast ( )
124
124
}
125
125
var value = " as? \( value_type) "
126
126
switch value_type {
@@ -129,80 +129,68 @@ enum HTMLElements : DeclarationMacro {
129
129
default :
130
130
break
131
131
}
132
- initializers += " self. \( key) = data.attributes[ \" \( key_literal ) \" ] " + value + " \n "
132
+ initializers += " self. \( key) = data.attributes[ \" \( keyLiteral ) \" ] " + value + " \n "
133
133
}
134
134
initializers += " self.innerHTML = data.innerHTML \n "
135
135
initializers += " } "
136
136
string += initializers
137
137
138
138
var render = " \n public var description : String { \n "
139
- var attributes_func = " func attributes() -> String { \n "
139
+ var attributes_func = " "
140
+ var itemsArray : String = " "
140
141
if !attributes. isEmpty {
141
142
attributes_func += " let sd = encoding.stringDelimiter(forMacro: fromMacro) \n "
142
- attributes_func += " var "
143
- } else {
144
- attributes_func += " let "
143
+ itemsArray += " var items:[String] = [] \n "
145
144
}
146
- attributes_func += " items:[String] = self.attributes.compactMap({ \n "
147
- attributes_func += " guard let v = $0.htmlValue(encoding: encoding, forMacro: fromMacro) else { return nil } \n "
148
- attributes_func += " let d = $0.htmlValueDelimiter(encoding: encoding, forMacro: fromMacro) \n "
149
- attributes_func += #"return $0.key + ($0.htmlValueIsVoidable && v.isEmpty ? "" : "=" + d + v + d)"#
150
- attributes_func += " \n }) \n "
151
145
for (key, value_type, _) in attributes {
152
- var key_literal = key
153
- if key_literal . first == " ` " {
154
- key_literal . removeFirst ( )
155
- key_literal . removeLast ( )
146
+ var keyLiteral = key
147
+ if keyLiteral . first == " ` " {
148
+ keyLiteral . removeFirst ( )
149
+ keyLiteral . removeLast ( )
156
150
}
157
- let variable_name = key_literal
158
- if key_literal == " httpEquiv " {
159
- key_literal = " http-equiv "
160
- } else if key_literal == " acceptCharset " {
161
- key_literal = " accept-charset "
151
+ let variable_name = keyLiteral
152
+ if keyLiteral == " httpEquiv " {
153
+ keyLiteral = " http-equiv "
154
+ } else if keyLiteral == " acceptCharset " {
155
+ keyLiteral = " accept-charset "
162
156
}
163
157
if value_type == " Bool " {
164
- attributes_func += " if \( key) { items.append( \" \( key_literal ) \" ) } \n "
158
+ itemsArray += " if \( key) { items.append( \" \( keyLiteral ) \" ) } \n "
165
159
} else if value_type. first == " [ " {
166
- attributes_func += " if let _ \( variable_name) :String = "
160
+ itemsArray += " if let _ \( variable_name) :String = "
167
161
let separator = separator ( key: key)
168
162
switch value_type {
169
163
case " [String] " :
170
- attributes_func += " \( key) ? "
164
+ itemsArray += " \( key) ? "
171
165
case " [Int] " , " [Float] " :
172
- attributes_func += " \( key) ?.map({ \" \\ ($0) \" }) "
166
+ itemsArray += " \( key) ?.map({ \" \\ ($0) \" }) "
173
167
default :
174
- attributes_func += " \( key) ?.compactMap({ return $0.htmlValue(encoding: encoding, forMacro: fromMacro) }) "
168
+ itemsArray += " \( key) ?.compactMap({ return $0.htmlValue(encoding: encoding, forMacro: fromMacro) }) "
175
169
}
176
- attributes_func += " .joined(separator: \" \( separator) \" ) { \n "
177
- attributes_func += #"let k:String = _ \#( variable_name) .isEmpty ? "" : "=" + sd + _ \#( variable_name) + sd"#
178
- attributes_func += " \n items.append( \" \( key_literal ) \" + k) "
179
- attributes_func += " \n } \n "
170
+ itemsArray += " .joined(separator: \" \( separator) \" ) { \n "
171
+ itemsArray += #"let k:String = _ \#( variable_name) .isEmpty ? "" : "=" + sd + _ \#( variable_name) + sd"#
172
+ itemsArray += " \n items.append( \" \( keyLiteral ) \" + k) "
173
+ itemsArray += " \n } \n "
180
174
} else if value_type == " String " || value_type == " Int " || value_type == " Float " || value_type == " Double " {
181
175
let value = value_type == " String " ? key : " String(describing: \( key) ) "
182
- attributes_func += #"if let \#( key) { items.append(" \#( key_literal ) =" + sd + \#( value) + sd) }"#
183
- attributes_func += " \n "
176
+ itemsArray += #"if let \#( key) { items.append(" \#( keyLiteral ) =" + sd + \#( value) + sd) }"#
177
+ itemsArray += " \n "
184
178
} else {
185
- attributes_func += " if let \( key) , let v = \( key) .htmlValue(encoding: encoding, forMacro: fromMacro) { \n "
186
- attributes_func += #"let s = \#( key) .htmlValueIsVoidable && v.isEmpty ? "" : "=" + sd + v + sd"#
187
- attributes_func += " \n items.append( \" \( key_literal ) \" + s) "
188
- attributes_func += " \n } \n "
179
+ itemsArray += " if let \( key) , let v = \( key) .htmlValue(encoding: encoding, forMacro: fromMacro) { \n "
180
+ itemsArray += #"let s = \#( key) .htmlValueIsVoidable && v.isEmpty ? "" : "=" + sd + v + sd"#
181
+ itemsArray += " \n items.append( \" \( keyLiteral ) \" + s) "
182
+ itemsArray += " \n } \n "
189
183
}
190
184
}
191
- attributes_func += " return (items.isEmpty ? \" \" : \" \" ) + items.joined(separator: \" \" ) \n } \n "
192
- render += attributes_func
193
- render += " let string:String = innerHTML.map({ String(describing: $0) }).joined() \n "
194
- let trailing_slash = isVoid ? " + (trailingSlash ? \" / \" : \" \" ) " : " "
195
- render += """
196
- let l:String, g:String
197
- if escaped {
198
- l = " < "
199
- g = " > "
200
- } else {
201
- l = " < "
202
- g = " > "
185
+ render += attributes_func + itemsArray
186
+ render += " return render( "
187
+ if tag == " html " {
188
+ render += " prefix: \" !DOCTYPE html \" , "
203
189
}
204
- """
205
- render += " return \( tag == " html " ? " l + \" !DOCTYPE html \" + g + " : " " ) l + tag + attributes() \( trailing_slash) + g + string " + ( isVoid ? " " : " + l + \" / \" + tag + g " )
190
+ if !isVoid {
191
+ render += " suffix: \" / \" + tag, "
192
+ }
193
+ render += " items: \( itemsArray. isEmpty ? " [] " : " items " ) ) "
206
194
render += " } "
207
195
208
196
string += render
0 commit comments