Skip to content

micheltlutz/Winged-Swift

Repository files navigation

WingedSwift

main codecov codebeat badge Reviewed by Hound

Swift Versions Platform Compatibility

=============

WingedSwift is an innovative Domain-Specific Language (DSL) library for efficient HTML writing in Swift. Mirroring its Python counterpart, WingedSwift is based on the DSL concept, focusing on simplification and specificity in HTML generation. Using the Composite design pattern, the library enables developers to construct HTML structures in a logical, organized, and reusable manner.

This library is created to be fully independent, not requiring integration with specific server frameworks or front-end libraries. This offers developers the freedom to use WingedSwift across a variety of projects, from simple static pages to complex web applications, keeping the code clean, readable, and efficient.

Table of Contents

Demo

Installation

Swift Package Manager

To add WingedSwift to your project, add the following line to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/micheltlutz/Winged-Swift.git", from: "1.1.0")
]

And include WingedSwift as a dependency in your target:

targets: [
    .target(
        name: "YourTarget",
        dependencies: ["WingedSwift"]),
]

To include in Vapor project use this line code in executableTarget.

.product(name: "WingedSwift", package: "Winged-Swift")

Usage

WingedSwift allows you to build HTML documents using a DSL syntax in Swift. Here are some examples of how to use the library.

Basic Example

import WingedSwift

let document = html {
    Head(children: [
        Meta(name: "description", content: "A description of the page"),
        Link(href: "styles.css", rel: "stylesheet")
    ])
    Body(children: [
        Header(children: [
            Nav(children: [
                A(href: "#home", content: "Home"),
                A(href: "#about", content: "About"),
                A(href: "#contact", content: "Contact")
            ])
        ]),
        Main(children: [
            P(content: "Welcome to our website!")
        ]),
        Footer(children: [
            P(content: "© 2024 Company, Inc.")
        ])
    ])
}

print(document.render())

Working with Forms

let form = Form(attributes: [Attribute(key: "action", value: "/submit")], children: [
    Fieldset(children: [
        Label(for: "name", content: "Name"),
        Input(type: "text", name: "name")
    ]),
    Fieldset(children: [
        Label(for: "message", content: "Message"),
        Textarea(name: "message")
    ]),
    Input(type: "submit", name: "submit", value: "Send")
])

print(form.render())

Code Structure

let pre = Pre(content: """
This is preformatted text.
It preserves whitespace and line breaks.
""")

print(pre.render())

let code = Code(content: """
let x = 10
print(x)
""")

print(code.render())

let embed = Embed(src: "video.mp4", type: "video/mp4")

print(embed.render())

Documentation

The complete documentation is available here soon.

Generating the Documentation

To generate the DocC documentation, use the following command in the terminal:

swift package generate-documentation --target WingedSwift --output-path ./docs
open ./docs/index.html

Preview Documentation

swift package --disable-sandbox preview-documentation --target WingedSwift

Contribution

Contributions are welcome! Please follow the steps below to contribute:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/new-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

Swift HTML Made Simple and Powerful

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages