Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
This was extracted from https://github.com/jasonzurita/swiftynotes
in order to make the swift website dsl available for use outside the
swiftynotes.com site.
  • Loading branch information
jasonzurita committed Jan 4, 2024
0 parents commit b6bcb29
Show file tree
Hide file tree
Showing 96 changed files with 1,431 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Run Tests

on:
push:
branches:
- main

jobs:
tests:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Checking out code
uses: actions/checkout@v2

- name: Run tests
run: swift test
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store

# vim swap files
*.sw[nop]

# Swift Package Manager
/.build
xcuserdata/
.swiftpm
.swiftpm/xcode/xcuserdata
*.xcuserstate
*.xcworkspacedata
*.orig
_site/index.html
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.9
5 changes: 5 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--disable wrapMultilineStatementBraces
--enable isEmpty
--header strip
--commas always
--indent 4
27 changes: 27 additions & 0 deletions Modules/ExampleSwiftWebsite/src/Site/Header.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import SwiftWebsiteDSL

struct SiteHeader: HtmlProvider {
public let html: HtmlNode = {
Header {
Img(src: "images/logo.svg", alt: "let hello = \"world\"")
.padding([.top], 50)
.width(500)
P("One minute Swift reads to get Swifty-er")
.color(.white)
Div {
// TODO: pull the swift value in from .swift-version
P("Updated for Swift 5.8")
}
.textAlign(.right)
.color(.lightGray)
.padding([.bottom], 1)
.padding([.top], 25)
.padding([.trailing], 18)
}
.background(
.linearGradient(.init(degree: 180, first: (.headerTopBlue, 0), second: (.headerBottomBlue, 100)))
)
// TODO: work on this quirk where we shouldn't need to call `.html` after
.html
}()
}
30 changes: 30 additions & 0 deletions Modules/ExampleSwiftWebsite/src/Site/RootSite.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import SwiftWebsiteDSL

// TODO: custom style guide for code

func renderHtml() -> String {
let site =
Html {
Head(title: "ExampleSwiftWebsite", cssStyleFileName: "")
Body {
H1("Hello World!")
.padding([.top], 48)
.color(.white)
Footer {
P("Built using the Swift Language and is ") {
A(copy: "open source.", url: "https://github.com/jasonzurita/swift-website-dsl")
}
}
.textAlign(.center)
.color(.lightGray)
}
.font(.apple)
.textAlign(.center)
.margin(0)
.padding(0)
.background(
.linearGradient(.init(degree: 180, first: (.headerTopBlue, 0), second: (.headerBottomBlue, 100)))
)
}
return site.html.render
}
11 changes: 11 additions & 0 deletions Modules/ExampleSwiftWebsite/src/Support/Colors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SwiftWebsiteDSL

// https://rgbacolorpicker.com/rgba-to-hex
extension Color {
static let white = Color(hex: "FFFFFF")
static let lightGray = Color(hex: "F2F2F2")
static let mediumGray = Color(hex: "8a8a8a")
static let darkGray = Color(hex: "333333")
static let headerTopBlue = Color(hex: "459bd0a8")
static let headerBottomBlue = Color(hex: "2F80ED")
}
22 changes: 22 additions & 0 deletions Modules/ExampleSwiftWebsite/src/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Usage: Go to root directory then `swift run`
import Foundation

let siteDirectory = FileManager.default.currentDirectoryPath + "/_site"
let outputFilePath = siteDirectory + "/index.html"

print("🛠️ Starting to generate the example website...")

do {
print("🖍️ Generating HTML...")
let html = renderHtml() // This is a free function from this module
if let data = html.data(using: .utf8) {
try data.write(to: URL(fileURLWithPath: outputFilePath), options: [])
print("🚀 Finished generating site! Output is in: \(siteDirectory)")
} else {
print("❌ Failed to write generated site out to: \(outputFilePath)")
exit(1)
}
} catch {
print("❌ Failed to build and generate site. Error: \(error)")
exit(1)
}
130 changes: 130 additions & 0 deletions Modules/SwiftWebsiteDSL/Tests/AnyElement.test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Foundation
import SnapshotTesting
@testable import SwiftWebsiteDSL
import XCTest

final class AnyElementTests: XCTestCase {
// MARK: - Styles

func testWidthStyle() {
// given
let element = AnyElement(element: "fake-element", attrs: [:], copy: "", nodes: [])
.width(789)

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testColorStyle() {
// given
let element = AnyElement(element: "fake-element", attrs: [:], copy: "", nodes: [])
.color(.init(hex: "123456"))

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testMarginAutoStyle() {
// given
let element = AnyElement(element: "fake-margin-auto-element", attrs: [:], copy: "", nodes: [])
.margin([.leading, .top], .auto)

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBackgroundColor() {
// given
let element = AnyElement(element: "fake-background-color-element", attrs: [:], copy: "", nodes: [])
.background(.color(.init(hex: "123456")))

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBackgroundLinearGradient() {
// given
let gradient: BackgroundType.LinearGradient = .init(
degree: 77,
first: (.init(hex: "ASDFGH"), 4),
second: (.init(hex: "QWERTY"), 99)
)

let element = AnyElement(element: "fake-background-linear-gradient-element", attrs: [:], copy: "", nodes: [])
.background(.linearGradient(gradient))

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBorderRadius() {
// given
let element = AnyElement(element: "fake-border-radius-element", attrs: [:], copy: "", nodes: [])
.borderRadius(px: 16)

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testLineHeight() {
// given
let element = AnyElement(element: "fake-line-height-element", attrs: [:], copy: "", nodes: [])
.lineHeight(2.3)

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testMostGeneralStylesWithPElement() {
// given
let element = P("Super cool copy here")
.color(.init(hex: "1234567"))
.margin(0)
.padding([.top], 7)
// .width(13) // TODO: this fails because width for a p element should be in the style section

// when
let rendered = element.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testCopyIsNotLostWithModifiers() {
// given
let copy = "mic test"
let element = P(copy)
// when
.color(.init(hex: "1234567"))
.margin(1)
.padding([.top], 0)

switch element.html {
case let .element(tag, attrs: _, copy, _):
// then
XCTAssertEqual(tag, "p")
XCTAssertFalse(copy.isEmpty)
}
}
}
104 changes: 104 additions & 0 deletions Modules/SwiftWebsiteDSL/Tests/BodyUnit.test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Foundation
import SnapshotTesting
@testable import SwiftWebsiteDSL
import XCTest

final class BodyUnitTests: XCTestCase {
func testEmptyBody() {
// given
let body = Body(attrs: [:], nodes: [])

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBodyWithEachMargin() {
Side.allCases.forEach {
// given
let body = Body(attrs: [:], nodes: []).margin([$0], 7.1)

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}
}

func testBodyWithAllMargins() {
// given
let body = Body(attrs: [:], nodes: []).margin(Side.allCases, 7.1)

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBodyWithEachPadding() {
Side.allCases.forEach {
// given
let body = Body(attrs: [:], nodes: []).padding([$0], 7.1)

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}
}

func testBodyWithAllPaddings() {
// given
let body = Body(attrs: [:], nodes: []).padding(7.1)

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBodyWithBackground() {
// given
let body = Body(attrs: [:], nodes: [])
.background(.color(.init(hex: "ASDFGH")))

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBodyWithFont() {
// given
let body = Body(attrs: [:], nodes: []).font(.apple)

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}

func testBodyWithMultipleStyles() {
// given
let body = Body(attrs: [:], nodes: [])
.font(.apple)
.textAlign(.left)
.background(.color(.init(hex: "ASDFGH")))
.margin(11)
.color(.init(hex: "asdfgh"))

// when
let rendered = body.html.render

// then
assertSnapshot(matching: rendered, as: .lines)
}
}
Loading

0 comments on commit b6bcb29

Please sign in to comment.