Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robb/Swim

A DSL for writing HTML in Swift
https://github.com/robb/Swim

dsl html swift swift-5

Last synced: 10 days ago
JSON representation

A DSL for writing HTML in Swift

Awesome Lists containing this project

README

        

# Swim – Swift Markup

A DSL for building HTML documents with Swift [function builders].

Currently in use on [my personal website].

```swift
import Swim
import HTML

let myDocument = html(lang: "en-US") {
head {
meta(charset: "utf-8", content: "text/html", httpEquiv: "Content-Type")
}
body(customAttributes: [ "data-foo": "bar" ]) {
article(classes: "readme", "modern") {
header {
h1 {
"This is a great article."
}
}

p {
"Hello World!"
br()
"How are you?"
}

p {
"This is a"
a(href: "https://swift.org") { "link to the Swift website" }
"."
}
}
}
}
```

By generating all words in the language according to the HTML specification, we can make sure that only valid HTML can be expressed.

For example, the above would generate this HTML:

```html








This is a great article.




Hello World!


How are you?



This is a

link to the Swift website

.



```

[function builders]: https://github.com/apple/swift-evolution/blob/9992cf3c11c2d5e0ea20bee98657d93902d5b174/proposals/XXXX-function-builders.md
[my personal website]: http://github.com/robb/robb.swift