Skip to content

Commit 64fb3ff

Browse files
authored
Merge pull request #880 from twostraws/markup-string-interpolation
Markup string interpolation
2 parents 0b6e2f6 + 5ab5f5a commit 64fb3ff

15 files changed

Lines changed: 180 additions & 85 deletions

Sources/Ignite/Elements/Image.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct Image: InlineElement, LazyLoadable {
120120
return Markup("<img\(attributes) />")
121121
}
122122

123-
var output = "<picture>"
123+
var output = Markup("<picture>")
124124

125125
if let darkSourceSet = generateSourceSet(darkVariants), let value = darkSourceSet.value {
126126
output += "<source media=\"(prefers-color-scheme: dark)\" srcset=\"\(value)\">"
@@ -129,7 +129,7 @@ public struct Image: InlineElement, LazyLoadable {
129129
// Add the fallback img tag
130130
output += "<img\(attributes) />"
131131
output += "</picture>"
132-
return Markup(output)
132+
return output
133133
}
134134

135135
/// Renders this element using publishing context passed in.

Sources/Ignite/Elements/Include.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct Include: HTML {
3434

3535
do {
3636
let string = try String(contentsOf: fileURL)
37-
return Markup(string)
37+
return Markup(verbatim: string)
3838
} catch {
3939
publishingContext.addWarning("""
4040
Failed to find \(filename) in Includes folder; \

Sources/Ignite/Elements/List.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public struct List: HTML {
162162
public func markup() -> Markup {
163163
let listAttributes = getAttributes()
164164

165-
var output = "<\(listElementName)\(listAttributes)>"
165+
var output: Markup = "<\(listElementName)\(listAttributes)>"
166166

167167
for originalItem in items {
168168
var item = originalItem
@@ -173,7 +173,7 @@ public struct List: HTML {
173173
if listStyle != .automatic {
174174
listableItem.attributes.append(classes: "list-group-item")
175175
}
176-
output += listableItem.listMarkup().string
176+
output += listableItem.listMarkup()
177177
} else {
178178
let styleClass = listStyle == .automatic ? "" : " class=\"list-group-item\""
179179
item.attributes.append(classes: "m-0")
@@ -183,6 +183,6 @@ public struct List: HTML {
183183

184184
output += "</\(listElementName)>"
185185

186-
return Markup(output)
186+
return output
187187
}
188188
}

Sources/Ignite/Elements/PlainDocument.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public struct PlainDocument: Document, HTML {
4242
// Deferred head rendering to accommodate for context updates during body rendering
4343
let headMarkup = head.markup()
4444

45-
var output = "<!doctype html>"
45+
var output: Markup = "<!doctype html>"
4646
output += "<html\(attributes)>"
47-
output += headMarkup.string
48-
output += bodyMarkup.string
47+
output += headMarkup
48+
output += bodyMarkup
4949
output += "</html>"
50-
return Markup(output)
50+
return output
5151
}
5252
}

Sources/Ignite/Elements/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ extension String: InlineElement, FormItem {
1414
/// Renders this element using publishing context passed in.
1515
/// - Returns: The HTML for this element.
1616
public func markup() -> Markup {
17-
Markup(self)
17+
Markup(verbatim: self)
1818
}
1919
}

Sources/Ignite/Elements/Table.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public struct Table: HTML {
164164
tableAttributes.append(classes: ["table-striped-columns"])
165165
}
166166

167-
var output = ""
167+
var output = Markup()
168168

169169
if let filterTitle {
170170
tableAttributes.id = "table-\(UUID().uuidString.truncatedHash)"
@@ -190,9 +190,9 @@ public struct Table: HTML {
190190
}
191191

192192
output += "<tbody>"
193-
output += rows.markupString()
193+
output += Markup(verbatim: rows.markupString())
194194
output += "</tbody>"
195195
output += "</table>"
196-
return Markup(output)
196+
return output
197197
}
198198
}

Sources/Ignite/Elements/Text.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,7 @@ public struct Text: HTML, DropdownItem {
160160
.attributes(attributes)
161161
.markup()
162162
} else {
163-
Markup(
164-
"<\(font.rawValue)\(attributes)>" +
165-
content.markupString() +
166-
"</\(font.rawValue)>"
167-
)
163+
Markup("<\(font.rawValue)\(attributes)>\(content.markupString())</\(font.rawValue)>")
168164
}
169165
}
170166
}

Sources/Ignite/Elements/Video.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct Video: InlineElement, LazyLoadable {
3434
/// - files: The user videos to render.
3535
/// - Returns: The HTML for this element.
3636
private func render(files: [String]) -> Markup {
37-
var output = "<video controls\(attributes)>"
37+
var output: Markup = "<video controls\(attributes)>"
3838

3939
for filename in files {
4040
if let fileType = videoType(for: filename) {
@@ -44,7 +44,7 @@ public struct Video: InlineElement, LazyLoadable {
4444

4545
output += "Your browser does not support the video tag."
4646
output += "</video>"
47-
return Markup(output)
47+
return output
4848
}
4949

5050
/// Renders this element using publishing context passed in.

Sources/Ignite/Framework/Analytics/Analytics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ public struct Analytics: HeadElement {
2727
public func markup() -> Markup {
2828
switch service {
2929
case .googleAnalytics(let measurementID):
30-
Markup(googleAnalyticsCode(for: measurementID))
30+
Markup(verbatim: googleAnalyticsCode(for: measurementID))
3131

3232
case .plausible(let domain, let measurements):
33-
Markup(plausibleCode(for: domain, using: measurements))
33+
Markup(verbatim: plausibleCode(for: domain, using: measurements))
3434

3535
case .fathom(let siteID):
36-
Markup(fathomCode(for: siteID))
36+
Markup(verbatim: fathomCode(for: siteID))
3737

3838
case .clicky(let siteID):
39-
Markup(clickyCode(for: siteID))
39+
Markup(verbatim: clickyCode(for: siteID))
4040

4141
case .telemetryDeck(let siteID):
42-
Markup(telemetryDeckCode(for: siteID))
42+
Markup(verbatim: telemetryDeckCode(for: siteID))
4343

4444
case .custom(let code):
45-
Markup(code)
45+
Markup(verbatim: code)
4646
}
4747
}
4848

Sources/Ignite/Framework/CoreAttributes.swift

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,10 @@
66
//
77

88
import Foundation
9-
import OrderedCollections
10-
11-
// A typealias that allows us to use `OrderedSet` without importing OrderedCollections
12-
public typealias OrderedSet<Element: Hashable> = OrderedCollections.OrderedSet<Element>
13-
14-
// A typealias that allows us to use `OrderedDictionary` without importing OrderedCollections
15-
public typealias OrderedDictionary<Key: Hashable, Value> = OrderedCollections.OrderedDictionary<Key, Value>
16-
17-
// A typealias that allows us to use `UUID` without importing Foundation
18-
public typealias UUID = Foundation.UUID
19-
20-
// A typealias that allows us to use `URL` without importing Foundation
21-
public typealias URL = Foundation.URL
22-
23-
// A typealias that allows us to use `Data` without importing Foundation
24-
public typealias Data = Foundation.Data
25-
26-
// A typealias that allows us to use `Date` without importing Foundation
27-
public typealias Date = Foundation.Date
28-
29-
/// A publishing-time registration required by rendered attributes.
30-
enum PublishingRegistration: Hashable, Equatable, Sendable {
31-
case fontFamily(Font)
32-
case responsiveFont(ResponsiveValues<LengthUnit>)
33-
case responsiveVisibility(ResponsiveValues<Bool>)
34-
35-
func apply(to context: PublishingContext) {
36-
switch self {
37-
case .fontFamily(let font):
38-
context.cssManager.registerFontFamily(font)
39-
case .responsiveFont(let values):
40-
_ = context.cssManager.registerFont(values)
41-
case .responsiveVisibility(let values):
42-
_ = context.cssManager.registerStyles(values)
43-
}
44-
}
45-
}
469

4710
/// A handful of attributes that all HTML types must support, either for
4811
/// rendering or for publishing purposes.
49-
public struct CoreAttributes: Equatable, Sendable, CustomStringConvertible {
12+
public struct CoreAttributes: Equatable, Sendable {
5013
/// A unique identifier. Can be empty.
5114
var id = ""
5215

@@ -75,10 +38,10 @@ public struct CoreAttributes: Equatable, Sendable, CustomStringConvertible {
7538
/// Whether this set of attributes is empty.
7639
var isEmpty: Bool { self == CoreAttributes() }
7740

78-
/// All core attributes collapsed down to a single string for easy application.
79-
public var description: String {
80-
registerPublishingRequirements()
81-
return "\(idString)\(customAttributeString)\(classString)\(styleString)\(dataString)\(ariaString)\(eventString)"
41+
/// All core attributes collapsed down to a single string for inclusion
42+
/// in HTML markup output.
43+
var markupAttributeString: String {
44+
"\(idString)\(customAttributeString)\(classString)\(styleString)\(dataString)\(ariaString)\(eventString)"
8245
}
8346

8447
/// The ID of this element, if set.
@@ -337,12 +300,11 @@ public struct CoreAttributes: Equatable, Sendable, CustomStringConvertible {
337300
publishingRegistrations.formUnion(other.publishingRegistrations)
338301
}
339302

340-
/// Applies publishing-time registrations when a publishing context is active.
341-
private func registerPublishingRequirements() {
342-
guard let context = PublishingContext.current else { return }
303+
}
343304

344-
for registration in publishingRegistrations {
345-
registration.apply(to: context)
346-
}
305+
extension String.StringInterpolation {
306+
@available(*, deprecated, message: "Interpolate CoreAttributes into Markup, not String — String interpolation drops publishing-time registrations.")
307+
public mutating func appendInterpolation(_ attributes: CoreAttributes) {
308+
appendLiteral(attributes.markupAttributeString)
347309
}
348310
}

0 commit comments

Comments
 (0)