Skip to content

Commit

Permalink
👋 hello, swep-perfect!
Browse files Browse the repository at this point in the history
  • Loading branch information
Alja7dali committed Aug 29, 2021
0 parents commit 781155e
Show file tree
Hide file tree
Showing 17 changed files with 739 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log
All notable changes to this project will be documented in this file.

<hr align="center" style="background-color: #393939; height: 2px">
111 changes: 111 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<h1>Contributing</h1>

Thank you for your interest in Swep! There are multiple ways you can
contribute to this project. We welcome contributions in all areas, with special
attention to:

- [General rules](#general-rules)
- [File an Issue](#file-an-issue)
- [Style Guides](#style-guides)
- [Fork the Repository](#fork-the-repository)
- [Make the Pull Request](#make-the-pull-request)
- [Performance improvements](#performance-improvements)
- [Developer's Certificate of Origin 1.1](#developers-certificate-of-origin-11)

Please take the time to carefully read the following guide. These rules help
make the best out of your time, the code reviewer's time and the general
consistency of the project.

<hr align="center" style="background-color: #393939; height: 2px">

## General rules

1. File an issue to notify the maintainers about what you're working on.
2. Fork the repo, develop and test your code changes, add docs.
3. Make sure that your commit messages clearly describe the changes.
4. Send a pull request.

### File an Issue

Use the [issue tracker][Issues] to start the discussion. It is possible that
someone else is already working on your idea, your approach is not quite right,
or that the functionality exists already. The ticket you file in the issue
tracker will be used to hash that all out.

### Style Guides

1. Write in UTF-8 in Swift 5.4
2. User modular architecture to group similar functions, classes, etc.
3. Always use 2 spaces for indentation (don't use tabs)
4. Try to limit line length to 80 characters
5. Class / Struct names should always be capitalized
6. Function names should always be camelized
7. Look at the existing style and adhere accordingly

### Fork the Repository

Be sure to add the relevant tests before making the pull request. Docs will be
updated automatically when we merge to `master`, but you should also build
the docs yourself and make sure they're readable.

### Make the Pull Request

Once you have made all your changes, tests, and updated the documentation,
make a pull request to move everything back into the main branch of the
`repository`. Be sure to reference the original issue in the pull request.
Expect some back-and-forth with regards to style and compliance of these
rules.

<hr align="center" style="background-color: #393939; height: 2px">

## Performance improvements

We take performance very much to heart. Performance improvements are always
welcome! If you identified a bottleneck, please make sure you follow the
performance fix procedure:

* Prepare a reproducible case that highlights the performance issue, if possible.
At least, the case should provide a testable timing result.
* Submit a PR with a fix that provides a measurable performance improvement
* Think hard about all the use cases! Threading and concurrency are important to
think about when it comes to performance, make sure your fix doesn't come with
a performance regression in some use cases.

As previously highlighted, discussing the matter via an issue is a preferred
starting point. This will allow other contributors to join and express their
point of view, allowing for a smooth glide from problem description to resolution.

Thanks for caring about performance!

<hr align="center" style="background-color: #393939; height: 2px">

## Developer's Certificate of Origin 1.1

> By making a contribution to this project, I certify that:
>
> - (a) The contribution was created in whole or in part by me and I
> have the right to submit it under the open source license
> indicated in the file; or
>
> - (b) The contribution is based upon previous work that, to the best
> of my knowledge, is covered under an appropriate open source
> license and I have the right under that license to submit that
> work with modifications, whether created in whole or in part
> by me, under the same open source license (unless I am
> permitted to submit under a different license), as indicated
> in the file; or
>
> - (c) The contribution was provided directly to me by some other
> person who certified (a), (b) or (c) and I have not modified
> it.
>
> - (d) I understand and agree that this project and the contribution
> are public and that a record of the contribution (including all
> personal information I submit with it, including my sign-off) is
> maintained indefinitely and may be redistributed consistent with
> this project or the open source license(s) involved.
*Wording of statement copied from [elinux.org][elinux.org]*

[Issues]: <https://github.com/alja7dali/swift-web-page/issues>
[elinux.org]: <http://elinux.org/Developer_Certificate_Of_Origin>
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT) Copyright (c) 2021 **Abdullah Aljahdali** Alja7dali@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"object": {
"pins": [
{
"package": "COpenSSL",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-COpenSSL-Linux.git",
"state": {
"branch": null,
"revision": "ebf8dc885a0f86b93632b362433d7db554d4f7db",
"version": "4.0.2"
}
},
{
"package": "PerfectCrypto",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-Crypto.git",
"state": {
"branch": null,
"revision": "98373fa3a6c3d551e498c1babe4f25f650fa1310",
"version": "3.3.0"
}
},
{
"package": "PerfectCZlib",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-CZlib-src.git",
"state": {
"branch": null,
"revision": "b63e46909aad937c6434766f8f8c6720d8f3bd05",
"version": "0.0.4"
}
},
{
"package": "PerfectHTTP",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-HTTP.git",
"state": {
"branch": null,
"revision": "122affd0bb5090c61bdc1e50e7f32ab6457d228e",
"version": "3.3.0"
}
},
{
"package": "PerfectHTTPServer",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-HTTPServer",
"state": {
"branch": null,
"revision": "4843cd54762d59b7e70841663ae561b279b80946",
"version": "3.0.23"
}
},
{
"package": "LinuxBridge",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-LinuxBridge.git",
"state": {
"branch": null,
"revision": "d6e64c48e6b06b6f1ab7ab9338280447baa8ca5c",
"version": "3.1.0"
}
},
{
"package": "PerfectNet",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-Net.git",
"state": {
"branch": null,
"revision": "d00b2b9f4cccb4c398e1bcf18f767d6b8add6af7",
"version": "3.3.0"
}
},
{
"package": "PerfectThread",
"repositoryURL": "https://github.com/PerfectlySoft/Perfect-Thread.git",
"state": {
"branch": null,
"revision": "a50b9a33f08fce638f33ae3121613fe499cc23ec",
"version": "3.0.7"
}
},
{
"package": "PerfectLib",
"repositoryURL": "https://github.com/PerfectlySoft/PerfectLib.git",
"state": {
"branch": null,
"revision": "5a08c2a596df23a3ae30b6ac9cb509a38384166c",
"version": "3.1.4"
}
},
{
"package": "swift-web-page",
"repositoryURL": "https://github.com/alja7dali/swift-web-page",
"state": {
"branch": null,
"revision": "48a5f31dccb5803f8b1d68c6b8be0500793777b2",
"version": "0.0.1"
}
}
]
},
"version": 1
}
24 changes: 24 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version:5.3
import PackageDescription

let Swep: Target.Dependency = .product(name: "Swep", package: "swift-web-page")

let package = Package(
name: "swift-web-page-perfect",
products: [
.library(name: "SwepPerfectSupport", targets: ["SwepPerfectSupport"]),
.executable(name: "SwepPerfectSupportExample", targets: ["SwepPerfectSupportExample"]),
],
dependencies: [
.package(url: "https://github.com/alja7dali/swift-web-page", from: "0.0.1"),
.package(name: "PerfectHTTPServer", url: "https://github.com/PerfectlySoft/Perfect-HTTPServer", from: "3.0.0"),
],
targets: [
.target(name: "SwepPerfectSupport", dependencies: [Swep, "PerfectHTTPServer"]),
.target(name: "SwepPerfectSupportExample", dependencies: [Swep, "PerfectHTTPServer", "SwepPerfectSupport"]),
.testTarget(name: "SwepPerfectSupportTests", dependencies: ["SwepPerfectSupport"]),
],
swiftLanguageVersions: [
.version("5"),
]
)
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

# 🦅 swift-web-page-perfect

<p align="left">
<img src="https://img.shields.io/badge/Swift_Version-5.1-orange.svg?style=flat&logo=Swift" alt="Swift Version: 5.1"/>
<a href="https://swift.org/package-manager">
<img src="https://img.shields.io/badge/SwiftPM-Compatible-darkgreen.svg?style=flat" alt="Swift Package Manager"/>
</a>
<img src="https://img.shields.io/badge/Platforms-macOS,%20iOS,%20Linux-darkgreen.svg?style=flat" alt="Swift Package Manager"/>
</p>

[Perfect](https://www.perfect.org) plugin for type-safe HTML/CSS views using [Swep](https://github.com/alja7dali/swift-web-page).

## Motivation

The most popular choice for rendering HTML/CSS in a Perfect web app is to use the Mustache templating language, but it exposes your application to **runtime errors** and **invalid HTML/CSS**. Our plugin prevents these runtime issues at compile-time by embedding HTML/CSS directly into Swift’s powerful type system. It uses the [Swep](https://github.com/alja7dali/swift-web-page) DSL for constructing HTML/CSS documents using plain Swift data structures.

## Usage

To use the plugin all you have to do is return a `Document` or `Stylesheet` value from your router callback:

``` swift
import Swep
import PerfectHTTP
import PerfectHTTPServer
import SwepPerfectSupport

let router = Router()

router.add(method: .get, uri: "/") { request, response in
response.append(
document {
head {
style {
selector("*") {
margin(.px(5))
backgroundColor(.hex(0xd1d1d1))
}
}
link()
.rel(.stylesheet)
.href("style.min.css")
}
body {
h1("Hello, type-safe HTML/CSS on Perfect!")
.color(.green)
}
}
)
response.completed()
}

router.add(method: .get, uri: "style.min.css") { request, response in
response.append(
stylesheet {
selector("body") {
padding(.rem(0.5))
lineHeight(1.35)
fontFamily("SanFranciscoDisplay-Regular")
}
selector("h1") {
marginTop(.rem(2))
marginBottom(.px(0))
}
}
)
response.completed()
}

let server = try HTTPServer.launch(name: "localhost", port: 8080, routes: routes)

defer { server.terminate() }
```

## Take it for a spin

We've included a sample Perfect application in this repo to show off its usage. To run the app immediately, simply do:

* `swift run SwepPerfectSupportExample`
* Open your browser to `http://localhost:8080`

The HTML/CSS for that page is constructed and rendered with Swep!!

## Installation

### Swift Package Manager (SPM)

If you want to use *swift-web-page-perfect* in a project that uses [SPM](https://swift.org/package-manager/), it's as simple as adding a `dependencies` clause to your `Package.swift`:

``` swift
dependencies: [
.package(url: "https://github.com/alja7dali/swift-web-page-perfect.git", from: "0.0.1")
]
```

From there you can add `SwepPerfectSupport` as target dependencies.

``` swift
let SwepPerfectSupport: Target.Dependency = .product(name: "SwepPerfectSupport", package: "swift-web-page-perfect")
...
targets: [
.taradd(method: .get, uri: name: "yourProject", dependencies: [SwepPerfectSupport]),
]
```

## License

All modules are released under the MIT license. See [LICENSE](./LICENSE.md) for details.
14 changes: 14 additions & 0 deletions Sources/SwepPerfectSupport/SwepPerfectSupport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Swep
import PerfectHTTP

extension HTTPResponse {
public func append(_ node: Document) {
self.setHeader(.contentType, value: "text/html; charset=utf-8")
self.appendBody(string: node.render())
}

public func append(_ node: Stylesheet) {
self.setHeader(.contentType, value: "text/css; charset=utf-8")
self.appendBody(string: node.render())
}
}
Loading

0 comments on commit 781155e

Please sign in to comment.