Giter Club home page Giter Club logo

swim's Introduction

Swim โ€“ Swift Markup

A DSL for building HTML documents with Swift function builders.

Currently in use on my personal website.

import Swim
import HTML

let myDocument = html(lang: "en-US") {
    head {
        meta(charset: "utf-8", content: "text/html", httpEquiv: "Content-Type")
    }
    body(customAttributes: [ "data-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>

swim's People

Contributors

chriseidhof avatar kevinrenskers avatar robb 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

swim's Issues

Ideas for attributes

First of all, thanks for this library! I've only started to dig in recently, but am enjoying the syntax and the fact that I can just use map, compactMap, if statements etc, without having to worry about wrapping things in .group or .fragment types (as Plot does), or the need for a custom forEach element (like Vaux).

My biggest issue with this library however is that working with tag attributes is rather painful. The autocomplete is useless since there are so many parameters, of which the order actually matters.

Screen Shot 2021-02-10 at 11 15 22

Screen Shot 2021-02-10 at 11 15 07

Here's my initial idea for an improvement:

public enum Attribute {
  case `class`(String)
  case id(String)
  case custom(String, String)

  var name: String {
    switch self {
      case .class:
        return "class"
      case .id:
        return "id"
      case .custom(let name, _):
        return name
    }
  }

  var value: String {
    switch self {
      case .class(let value):
        return value
      case .id(let value):
        return value
      case .custom(_, let value):
        return value
    }
  }
}

public func p(_ attributes: Attribute...,
  @NodeBuilder children: () -> NodeConvertible = { Node.fragment([]) }
) -> Node {
  let attributesDict = attributes.reduce([:]) { (result, next) -> [String: String] in
    var result = result
    result[next.name] = next.value
    return result
  }

  return .element("p", attributesDict, children().asNode())
}

The result:

p(.id("id"), .class("class"), .custom("data-whatever", "foobar")) {
  "Hello"
}

This way you have simple autocompletion (just type . and you get a list of attributes), and the order doesn't matter. Of course in this simple naive way, all attributes would be available to be used on all tags, which is not ideal. But hey, this was my first idea which doesn't require a big architecture change of Swim.

Plot solves the same problem with protocols:

Screen Shot 2021-02-10 at 11 30 32

Vaux on the other hand goes a different route: you add attributes onto a tag:

div {
  paragraph { "Hello" }
}.class("article")

To be honest I'm not a big fan of that approach, it's much less readable in a long(ish) tree of tags. But the idea is the same: you limit what kind of attributes you can use per "type of tag", without having all those repeated function parameters in every function.

Finally, swift-html also uses an array of enum cases:

.div(
  attributes: [.class("article")],
  .p("Hello there")
)

If you look at the function definition of div, you can see their solution to limit which attributes can be used:

div(attributes: [Html.Attribute<Html.Tag.Div>] = [], _ content: Html.Node...) -> Html.Node

And that seems to be a winner to me? So the basic idea is this:

struct Attribute<Node> {
  let key: String
  let value: String?

  init(_ key: String, _ value: String?) {
    self.key = key
    self.value = value
  }
}

extension Attribute {
  static func id(_ value: String) -> Attribute {
    return .init("id", value)
  }
}

extension Attribute where Node: Linkable {
  static func href(_ value: String) -> Attribute {
    return .init("href", value)
  }
}

I'm curious to see what you think. Have the very long function signatures bothered you as well?

`textarea` whitespace

By default, the textarea tag uses all the whitespace inside of it and uses that for the value of the textarea. Since Swim's default approach for rendering an html tree to a string is to pretty print it with tons of whitespace (which in general is great!), this adds a bunch of whitespace in any textarea.

I'm not sure what the best way to fix this is:

  1. Special case textarea's printing?
  2. Have a printing mode that prints with no extra whitespace?
  3. Add another parameter to textarea for its value?

Minimum Swift version is now 5.4

Because of @resultBuilder, the minimum Swift version is now 5.4. So I think that means that Package.swift should be updated? Specifically the first line: swift-tools-version:5.1.

Swift-Package Version Number

I am trying to import this package into my project, but keep getting the same error.

because no versions of HTML-DSL match the requirement 0.0.1..<1.0.0 and root depends on HTML-DSL 0.0.1..<1.0.0, version solving failed`

It doesn't matter what version I set it to, it's still the same issue.

Thanks

How to deal with whitespace?

Consider this example:

div(id: "article_footer") {
  p {
    "Have feedback? Let me know on"
    a(href: "https://twitter.com/kevinrenskers") { "Twitter" }
    "."
  }
}

This will get rendered with a space between the link and the period. How would you prevent that from happening?

Missing media attribute for the meta tag

The "media" attribute is not available for meta tags, so something like this is currently impossible:

<meta name="theme-color" 
      content="#ecd96f" 
      media="(prefers-color-scheme: light)">
<meta name="theme-color" 
      content="#0b3e05" 
      media="(prefers-color-scheme: dark)">

Would you want me to create a PR?

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.