Skip to content

Commit 18a47b8

Browse files
QOL stuff
- split some content into their own files for better compilation performance and maintainability - added some documentation - added `HTMLEncoding` (replaces existing system for encoding html as a different type)
1 parent fe40c3b commit 18a47b8

File tree

11 files changed

+1758
-1684
lines changed

11 files changed

+1758
-1684
lines changed

Sources/HTMLKit/HTMLKit.swift

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,7 @@ public macro escapeHTML<T: ExpressibleByStringLiteral>(_ innerHTML: T...) -> T =
3030
// MARK: HTML Representation
3131
@freestanding(expression)
3232
public macro html<T: ExpressibleByStringLiteral>(
33+
encoding: HTMLEncoding = .string,
3334
lookupFiles: [StaticString] = [],
34-
_ innerHTML: HTMLElement...
35-
) -> T = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
36-
37-
@freestanding(expression)
38-
public macro htmlUTF8Bytes<T: ExpressibleByStringLiteral>(lookupFiles: [StaticString] = [], attributes: [HTMLElementAttribute] = [], xmlns: T? = nil, _ innerHTML: T...) -> [UInt8] = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
39-
40-
@freestanding(expression)
41-
public macro htmlUTF16Bytes<T: ExpressibleByStringLiteral>(lookupFiles: [StaticString] = [], attributes: [HTMLElementAttribute] = [], xmlns: T? = nil, _ innerHTML: T...) -> [UInt16] = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
42-
43-
@freestanding(expression)
44-
public macro htmlUTF8CString<T: ExpressibleByStringLiteral>(lookupFiles: [StaticString] = [], attributes: [HTMLElementAttribute] = [], xmlns: T? = nil, _ innerHTML: T...) -> ContiguousArray<CChar> = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
45-
46-
#if canImport(Foundation)
47-
@freestanding(expression)
48-
public macro htmlData<T: ExpressibleByStringLiteral>(lookupFiles: [StaticString] = [], attributes: [HTMLElementAttribute] = [], xmlns: T? = nil, _ innerHTML: T...) -> Data = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
49-
#endif
50-
51-
@freestanding(expression)
52-
public macro htmlByteBuffer<T: ExpressibleByStringLiteral>(lookupFiles: [StaticString] = [], attributes: [HTMLElementAttribute] = [], xmlns: T? = nil, _ innerHTML: T...) -> ByteBuffer = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")
35+
_ innerHTML: CustomStringConvertible...
36+
) -> T = #externalMacro(module: "HTMLKitMacros", type: "HTMLElementMacro")

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,40 @@ import struct NIOCore.ByteBuffer
1919
enum HTMLElementMacro : ExpressionMacro {
2020
static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
2121
let string:String = expand_macro(context: context, macro: node.macroExpansion!)
22-
var set:Set<HTMLElementType?> = [.htmlUTF8Bytes, .htmlUTF16Bytes, .htmlUTF8CString, .htmlByteBuffer]
22+
let encoding:HTMLEncoding = HTMLEncoding.string
2323

24-
#if canImport(Foundation)
25-
set.insert(.htmlData)
26-
#endif
27-
28-
let elementType:HTMLElementType? = HTMLElementType(rawValue: node.macroName.text)
29-
if set.contains(elementType) {
24+
func has_no_interpolation() -> Bool {
3025
let has_interpolation:Bool = !string.ranges(of: try! Regex("\\((.*)\\)")).isEmpty
3126
guard !has_interpolation else {
3227
context.diagnose(Diagnostic(node: node, message: DiagnosticMsg(id: "interpolationNotAllowedForDataType", message: "String Interpolation is not allowed for this data type. Runtime values get converted to raw text, which is not the expected result.")))
33-
return ""
34-
}
35-
func bytes<T: FixedWidthInteger>(_ bytes: [T]) -> String {
36-
return "[" + bytes.map({ "\($0)" }).joined(separator: ",") + "]"
28+
return false
3729
}
38-
switch elementType {
39-
case .htmlUTF8Bytes:
40-
return "\(raw: bytes([UInt8](string.utf8)))"
41-
case .htmlUTF16Bytes:
42-
return "\(raw: bytes([UInt16](string.utf16)))"
43-
case .htmlUTF8CString:
44-
return "\(raw: string.utf8CString)"
30+
return true
31+
}
32+
func bytes<T: FixedWidthInteger>(_ bytes: [T]) -> String {
33+
return "[" + bytes.map({ "\($0)" }).joined(separator: ",") + "]"
34+
}
35+
switch encoding {
36+
case .utf8Bytes:
37+
guard has_no_interpolation() else { return "" }
38+
return "\(raw: bytes([UInt8](string.utf8)))"
39+
case .utf16Bytes:
40+
guard has_no_interpolation() else { return "" }
41+
return "\(raw: bytes([UInt16](string.utf16)))"
42+
case .utf8CString:
43+
return "\(raw: string.utf8CString)"
4544

46-
#if canImport(Foundation)
47-
case .htmlData:
48-
return "Data(\(raw: bytes([UInt8](string.utf8))))"
49-
#endif
45+
case .foundationData:
46+
guard has_no_interpolation() else { return "" }
47+
return "Data(\(raw: bytes([UInt8](string.utf8))))"
5048

51-
case .htmlByteBuffer:
52-
return "ByteBuffer(bytes: \(raw: bytes([UInt8](string.utf8))))"
49+
case .byteBuffer:
50+
guard has_no_interpolation() else { return "" }
51+
return "ByteBuffer(bytes: \(raw: bytes([UInt8](string.utf8))))"
5352

54-
default: break
55-
}
53+
case .string:
54+
return "\"\(raw: string)\""
5655
}
57-
return "\"\(raw: string)\""
5856
}
5957
}
6058

@@ -65,56 +63,14 @@ private extension HTMLElementMacro {
6563
return "\(macro)"
6664
}
6765
let children:SyntaxChildren = macro.arguments.children(viewMode: .all)
68-
if elementType == .escapeHTML {
66+
/*if elementType == .escapeHTML {
6967
let array:[CustomStringConvertible] = children.compactMap({
7068
guard let child:LabeledExprSyntax = $0.labeled else { return nil }
7169
return HTMLKitUtilities.parse_inner_html(context: context, child: child, lookupFiles: [])
7270
})
7371
return array.map({ String(describing: $0) }).joined()
74-
}
72+
}*/
7573
let data:HTMLKitUtilities.ElementData = HTMLKitUtilities.parse_arguments(context: context, children: children)
7674
return data.innerHTML.map({ String(describing: $0) }).joined()
7775
}
78-
}
79-
80-
extension HTMLElementAttribute.Extra {
81-
enum CSSUnitType : String {
82-
case centimeters
83-
case millimeters
84-
case inches
85-
case pixels
86-
case points
87-
case picas
88-
89-
case em
90-
case ex
91-
case ch
92-
case rem
93-
case viewportWidth
94-
case viewportHeight
95-
case viewportMin
96-
case viewportMax
97-
case percent
98-
99-
var suffix : String {
100-
switch self {
101-
case .centimeters: return "cm"
102-
case .millimeters: return "mm"
103-
case .inches: return "in"
104-
case .pixels: return "px"
105-
case .points: return "pt"
106-
case .picas: return "pc"
107-
108-
case .em: return "em"
109-
case .ex: return "ex"
110-
case .ch: return "ch"
111-
case .rem: return "rem"
112-
case .viewportWidth: return "vw"
113-
case .viewportHeight: return "vh"
114-
case .viewportMin: return "vmin"
115-
case .viewportMax: return "vmax"
116-
case .percent: return "%"
117-
}
118-
}
119-
}
12076
}

0 commit comments

Comments
 (0)