Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonzurita committed Jan 4, 2024
0 parents commit b653a70
Show file tree
Hide file tree
Showing 98 changed files with 1,534 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
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store

# vim swap files
*.sw[nop]

# Swift Package Manager
/.build
.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>
103 changes: 103 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/SwiftWebsiteDSL.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ExampleSwiftWebsite"
BuildableName = "ExampleSwiftWebsite"
BlueprintName = "ExampleSwiftWebsite"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftWebsiteDSLTest"
BuildableName = "SwiftWebsiteDSLTest"
BlueprintName = "SwiftWebsiteDSLTest"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftWebsiteDSLTest"
BuildableName = "SwiftWebsiteDSLTest"
BlueprintName = "SwiftWebsiteDSLTest"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ExampleSwiftWebsite"
BuildableName = "ExampleSwiftWebsite"
BlueprintName = "ExampleSwiftWebsite"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ExampleSwiftWebsite"
BuildableName = "ExampleSwiftWebsite"
BlueprintName = "ExampleSwiftWebsite"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
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)
}
}
}
Loading

0 comments on commit b653a70

Please sign in to comment.