WingedSwift

main codecov codebeat badge Reviewed by Hound

Swift Versions Platform Compatibility

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

WingedSwift 是一个创新的领域特定语言 (DSL) 库,用于在 Swift 中高效编写 HTML。WingedSwift 借鉴了其 Python 对应版本,基于 DSL 概念,专注于 HTML 生成的简化和特异性。该库使用组合设计模式,使开发人员能够以逻辑化、有组织且可重用的方式构建 HTML 结构。

创建此库旨在完全独立,无需与特定的服务器框架或前端库集成。这使开发人员可以自由地在各种项目中使用 WingedSwift,从简单的静态页面到复杂的 Web 应用程序,保持代码的简洁、可读和高效。

目录

演示

安装

Swift Package Manager

要将 WingedSwift 添加到您的项目中,请将以下行添加到您的 Package.swift 文件中

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

并在您的目标中包含 WingedSwift 作为依赖项

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

要在 Vapor 项目中包含,请在 executableTarget 中使用此行代码。

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

使用方法

WingedSwift 允许您在 Swift 中使用 DSL 语法构建 HTML 文档。以下是一些关于如何使用该库的示例。

基本示例

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())

处理表单

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())

代码结构

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())

文档

完整的文档即将在此处提供

生成文档

要生成 DocC 文档,请在终端中使用以下命令

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

预览文档

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

贡献

欢迎贡献!请按照以下步骤进行贡献

  1. Fork 仓库
  2. 创建一个新分支 (git checkout -b feature/new-feature)
  3. 提交您的更改 (git commit -am 'Add new feature')
  4. 推送到分支 (git push origin feature/new-feature)
  5. 打开 Pull Request

许可证

本项目根据 MIT 许可证获得许可。有关更多详细信息,请参阅 LICENSE 文件。