Coder Social home page Coder Social logo

vaux's Introduction

Vaux

Swift Version Swift PM Compatible Apache License

Vaux is a library that allows you to generate HTML using Swift. It includes a domain-specific language written in Swift for HTML, and its purpose is to allow developers to write HTML, but in Swift.

Warning: If you are reading this, this is alpha software based on Swift 5.1, which is currently only downloadable from swift.org or in Xcode 11 Beta. This is a work in progress in every sense of the phrase.

Requirements

  • Swift 5.1

Motivation

At WWDC2019, Apple announced functionality for a new feature in Swift called Function Builders. The core functionality of Vaux, which includes a Swift DSL for HTML, is heavily inspired by this proposal and corresponding examples. As such, this library exists with two primary goals:

  • Provide an underpinning for future potential opinionated frameworks for web interfaces written in Swift (for example, this).
  • Learn more about function builders.

At the time of this library's first open source release, it is standing tall on the shoulders of giants, and gratitude is in order.

Example

Let's say you want to write the following HTML:

<html>
  <head>
    <title>
      Page title
    </title>
  </head>
  <body>
    <div>
      Page body
    </div>
  </body>
</html>

Rather than write this out by hand, you can first write a function that will return HTML using Vaux:

var pageTitle = "Page title"
var pageBody = "Page body"

func simplePage() -> HTML {
  html {
    head {
      title(pageTitle)
    }
    body {
      div {
        pageBody
      }
    }
  }
}

Then, you can render the function result into a static html file using Vaux like so:

let vaux = Vaux()
vaux.outputLocation = .file(name: "testing", path: "/tmp/")
do {
  try vaux.render(html)
} catch let error {
  print("Uh-oh, something happened: \(error.localizedDescription)")
}

The end result is that you have a file called testing.html in your /tmp/ directory.

Creating HTML elements

Note: For a series of real use cases for Vaux, check out VauxTests.swift to see examples of different tags.

Write a function that returns HTML, and add your builders inside this function:

func buildPage() -> HTML {

}

Documentation

Coming soon...

Importing This Library

Vaux is available as a Swift package on GitHub. If you are using the Xcode 11 beta, you can simply add this url (https://github.com/dokun1/Vaux) as a dependency. Otherwise, you must include this as a dependency in your Package.swift file and use the command line to to create a project for this.

vaux's People

Contributors

bamchoh avatar dokun1 avatar maxdesiatov avatar mbarnach avatar stkent avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vaux's Issues

Attribute/StyleAttribute internal initializers

In looking the definitions for Attribute and StyleAttribute, is there a reason that public initializers are not provided? I suspect that they were just inadvertently omitted (I do it all the time; one of the less intuitive features of swift IMO).

public struct Attribute {
  public let key: String
  public let value: String?
}

public struct StyleAttribute {
  public let key: String
  public let value: String
}

tests fail for Swift 5.1.3 release

swift --version
# Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
# Target: x86_64-apple-darwin19.3.0

swift test
# …/Vaux/Tests/VauxTests/VauxTableTests.swift:94:190: 
#   error: 'String' is not convertible to 'String?'
#       link(…, label: "…").class("external").attr("rel", "noopener")
#                                                         ^~~~~~~~~~
# …/Vaux/Tests/VauxTests/VauxTests.swift:1703:13: 
#   error: expression type 'HTML' is ambiguous without more context
#             fieldset {
#             ^~~~~~~~~~
# …/Vaux/Tests/VauxTests/VauxTests.swift:1714:28: 
#   error: 'InputType' is not convertible to 'InputType?'
#               input(type: .text)
#                           ~^~~~
# …/Vaux/Tests/VauxTests/VauxTests.swift:2386:49: 
#   error: 'String' is not convertible to 'String?'
#               }.attr("for", "a b").attr("name", "x")
#                                                 ^~~
# …/Vaux/Tests/VauxTests/VauxTests.swift:2386:29: 
#   error: 'String' is not convertible to 'String?'
#               }.attr("for", "a b").attr("name", "x")
#                             ^~~~~

Output to variable?

I kinda expected this to work, but it doesn't (output is an empty string). Any idea how to get the result as a String?

import Vaux

var pageTitle = "Page title"
var pageBody = "Page body"

func simplePage() -> HTML {
  html {
    head {
      title(pageTitle)
    }
    body {
      div {
        pageBody
      }
    }
  }
}

let content = simplePage()

var output = ""
let stream = HTMLOutputStream(output, content.getTag())

content.renderAsHTML(into: stream, attributes: [])

print(output)

New release tag?

The last tag, 0.2.0, is quite old - there have been a bunch of commits since then. Could you tag a new release?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.