Skip to content

A Swift DSL for type-safe HTML & CSS, integrating swift-css and pointfree-html.

License

Notifications You must be signed in to change notification settings

coenttb/swift-html

Repository files navigation

swift-html

A Swift DSL for HTML and CSS that streamlines building HTML documents, built upon coenttb/pointfree-html and integrated with swift-css.

Development Status

This package is currently in active development and is subject to frequent changes. Features and APIs may change without prior notice until a stable release is available.

Examples

You can create HTML documents using a declarative, SwiftUI-like syntax, with support for type-safe CSS:

import HTML

let document = HTMLPreview.modern {
  h1 { "Type-safe HTML" }
    .color(.blue)
    .fontSize(24.px)
  p { "With type-safe CSS!" }
    .margin(top: 10.px, bottom: 10.px)
}

For a quick start, use HTMLPreview.modern to give your document a modern look. Alternatively, create a custom implementation by conforming your HTMLDocument struct or enum to the HTMLDocument protocol.

SwiftUI Preview Example

You can preview your HTML directly using SwiftUI:

#if canImport(SwiftUI)
import SwiftUI
import HTML

#Preview {
  HTMLPreview.modern {
    h1 { "Welcome!" }
      .color(.blue)
      .fontSize(24.px)
    p { "You've found our site!" }
      .margin(top: 10.px, bottom: 10.px)
  }
}
#endif

HTML & CSS domain specific language in Swift

This DSL leverages Swift’s type system to make constructing HTML documents and elements safer and more reliable. The APIs enforce proper types, minimizing the use of raw strings and reducing common errors.

The integration with swift-css ensures a type-safe system that allows that only valid CSS properties to be applied to HTML elements, helping catch styling mistakes at compile time.

FAQ

Can I use this with existing Swift web frameworks like Vapor?

Yes, you can! See coenttb-com-server for an example implementation.

Related projects

  • coenttb-html: Builds on swift-html and includes additional modules related to html components, html-to-pdf, markdown, and email.
  • swift-css: A Swift DSL for CSS.
  • swift-web: A collection of types and functions for dealing with common web server concerns. Forked from PointFree and updated where necessary.
  • coenttb-web: Builds on swift-web and includes additional modules related to web development.
  • coenttb-com-server: The coentt.com website, built with swift-html.
  • swift-languages: A package for for cross platform translations using Swift.

Installation

You can add swift-html to an Xcode project by including it as a package dependency:

Repository URL: https://github.com/coenttb/swift-html

For a Swift Package Manager project, add the dependency in your Package.swift file:

dependencies: [
  .package(url: "https://github.com/coenttb/swift-html", branch: "main")
]

Related projects

The coenttb stack

PointFree foundations

Feedback is much appreciated!

If you’re working on your own Swift project, feel free to learn, fork, and contribute.

Got thoughts? Found something you love? Something you hate? Let me know! Your feedback helps make this project better for everyone. Open an issue or start a discussion—I’m all ears.

Subscribe to my newsletter

Follow me on X

Link on Linkedin

License

This project is licensed by coenttb under the Apache 2.0 License. See LICENSE for details.

About

A Swift DSL for type-safe HTML & CSS, integrating swift-css and pointfree-html.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Languages