Skip to content

Commit 85815ce

Browse files
general cleanup before release
1 parent 96c9b88 commit 85815ce

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ let package = Package(
9696
"HTMLKitParse",
9797
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
9898
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
99+
//.product(name: "SwiftLexicalLookup", package: "swift-syntax"),
99100
.product(name: "SwiftSyntax", package: "swift-syntax"),
100101
.product(name: "SwiftSyntaxMacros", package: "swift-syntax")
101102
]

Sources/HTMLKit/HTMLKit.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ public macro anyRawHTML(
8181
) -> any CustomStringConvertible & Sendable = #externalMacro(module: "HTMLKitMacros", type: "RawHTML")
8282

8383
// MARK: HTML Context
84-
//@freestanding(expression)
85-
//public macro htmlContext() = #externalMacro(module: "HTMLKitMacros", type: "RawHTML")
84+
@freestanding(expression)
85+
macro htmlContext<T: CustomStringConvertible & Sendable>(
86+
_ value: () -> T
87+
) -> T = #externalMacro(module: "HTMLKitMacros", type: "HTMLContext")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// HTMLContext.swift
3+
//
4+
//
5+
// Created by Evan Anderson on 3/29/25.
6+
//
7+
8+
import HTMLKitParse
9+
import HTMLKitUtilities
10+
//import SwiftLexicalLookup
11+
import SwiftSyntax
12+
import SwiftSyntaxMacros
13+
14+
enum HTMLContext : ExpressionMacro {
15+
static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
16+
return "\"\""
17+
}
18+
}

Sources/HTMLKitMacros/HTMLKitMacros.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct HTMLKitMacros : CompilerPlugin {
1313
let providingMacros:[any Macro.Type] = [
1414
HTMLElementMacro.self,
1515
EscapeHTML.self,
16-
RawHTML.self
16+
RawHTML.self,
17+
HTMLContext.self
1718
]
1819
}

Sources/HTMLKitUtilityMacros/HTMLElements.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ enum HTMLElements : DeclarationMacro {
132132
initializers += "}"
133133
string += initializers
134134

135-
var render = "\npublic var description : String {\n"
135+
var referencedStringDelimiter:Bool = false
136+
var render = "\n@inlinable public var description : String {\n"
136137
var attributes_func = ""
137138
var itemsArray:String = ""
138139
if !attributes.isEmpty {
@@ -152,6 +153,7 @@ enum HTMLElements : DeclarationMacro {
152153
default: break
153154
}
154155
if valueType.first == "[" {
156+
referencedStringDelimiter = true
155157
itemsArray += "if let _\(variableName):String = "
156158
let separator = separator(key: key)
157159
switch valueType {
@@ -160,7 +162,7 @@ enum HTMLElements : DeclarationMacro {
160162
case "[Int]", "[Float]":
161163
itemsArray += "\(key)?.map({ \"\\($0)\" })"
162164
default:
163-
itemsArray += "\(key)?.compactMap({ return $0.htmlValue(encoding: encoding, forMacro: fromMacro) })"
165+
itemsArray += "\(key)?.compactMap({ $0.htmlValue(encoding: encoding, forMacro: fromMacro) })"
164166
}
165167
itemsArray += ".joined(separator: \"\(separator)\") {\n"
166168
itemsArray += #"let k:String = _\#(variableName).isEmpty ? "" : "=" + sd + _\#(variableName) + sd"#
@@ -171,18 +173,20 @@ enum HTMLElements : DeclarationMacro {
171173
case "Bool":
172174
itemsArray += "if \(key) { items.append(\"\(keyLiteral)\") }\n"
173175
case "String", "Int", "Float", "Double":
176+
referencedStringDelimiter = true
174177
let value = valueType == "String" ? key : "String(describing: \(key))"
175178
itemsArray += #"if let \#(key) { items.append("\#(keyLiteral)=" + sd + \#(value) + sd) }"#
176179
itemsArray += "\n"
177180
default:
181+
referencedStringDelimiter = true
178182
itemsArray += "if let \(key), let v = \(key).htmlValue(encoding: encoding, forMacro: fromMacro) {\n"
179183
itemsArray += #"let s = \#(key).htmlValueIsVoidable && v.isEmpty ? "" : "=" + sd + v + sd"#
180184
itemsArray += "\nitems.append(\"\(keyLiteral)\" + s)"
181185
itemsArray += "\n}\n"
182186
}
183187
}
184188
}
185-
render += attributes_func + itemsArray
189+
render += (!referencedStringDelimiter ? "" : attributes_func) + itemsArray
186190
render += "return render("
187191
if tag == "html" {
188192
render += "prefix: \"!DOCTYPE html\", "

Tests/HTMLKitTests/InterpolationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct InterpolationTests {
2323

2424
expected_result = #html(div(attributes: [.id("sheesh-dude")], "sheesh-dude"))
2525
test = "dude"
26-
let result:String = #html(div(attributes:[.id("sheesh-\(test)")], "sheesh-\(test)"))
26+
let result:String = #html(div(attributes: [.id("sheesh-\(test)")], "sheesh-\(test)"))
2727
#expect(result == expected_result)
2828
}
2929

Tests/HTMLKitTests/LexicalLookupTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
#if compiler(>=6.0)
99

1010
import Testing
11-
import HTMLKit
11+
@testable import HTMLKit
1212

1313
struct LexicalLookupTests {
1414
@Test
1515
func lexicalLookup() {
16-
let placeholder:String = #html(p("gottem"))
17-
let value:String = #html(html(placeholder))
16+
//let placeholder:String = #html(p("gottem"))
17+
//let value:String = #html(html(placeholder))
18+
19+
let contextValue:String = #htmlContext {
20+
let placeholder:String = #html(p("gottem"))
21+
return #html(html(placeholder))
22+
}
1823
}
1924
}
2025

0 commit comments

Comments
 (0)