27

GitHub - robb/HTML-DSL: A quick and dirty DSL for writing HTML in Swift

 5 years ago
source link: https://github.com/robb/HTML-DSL
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

HTML DSL

A quickly thrown together DSL for building HTML nodes with Swift's new function builders.

let myDocument = html(lang: "en-US") {
    head {
        meta(charset: "utf-8", content: "text/html", httpEquiv: "Content-Type")
    }
    body(customData: [ "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 lang="en-US">
  <head>
    <meta http-equiv="Content-Type" charset="utf-8" content="text/html" />
  </head>
  <body data-foo="bar">
    <article class="readme modern">
      <header>
        <h1>
          This is a great article.
        </h1>
      </header>
      <p>
        Hello World!
        <br/>
        How are you?
      </p>
      <p>
        This is a
        <a href="https://swift.org">
          link to the Swift website
        </a>
        .
      </p>
    </article>
  </body>
</html>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK