Skip to content

Commit

Permalink
Stylesheet renderer (#1)
Browse files Browse the repository at this point in the history
* New stylesheet renderer object
* Minification & custom indentation support
* Write doubles as int if possible
* Updated unit tests & minor fixes
  • Loading branch information
tib authored Nov 21, 2021
1 parent 581082d commit 5ed92bd
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 255 deletions.
10 changes: 0 additions & 10 deletions Sources/SwiftCss/CSS/CSSRepresentable.swift

This file was deleted.

68 changes: 0 additions & 68 deletions Sources/SwiftCss/CSS/Stylesheet.swift

This file was deleted.

38 changes: 19 additions & 19 deletions Sources/SwiftCss/Components/CSSColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,69 @@

public struct CSSColor: ExpressibleByStringLiteral {

private var css: String
private var colorValue: String

init(raw: String) {
css = raw
colorValue = raw
}

// init(hex: String) {
// css = hex
// }

public init(stringLiteral value: StringLiteralType) {
css = value
colorValue = value
/// check if length is valid (000, #000, cafe00, #cafe00)
assert([3,4,6,7].contains(value.count), "Invalid hex string")

/// add # prefix if missing
if !css.hasPrefix("#") {
css = "#" + css
if !colorValue.hasPrefix("#") {
colorValue = "#" + colorValue
}
}

public init(r: Int, g: Int, b: Int, a: Double? = nil) {
css = "\(r),\(g),\(b)"
colorValue = "\(r),\(g),\(b)"
if let a = a {
css = "rgba(" + css + ", \(a))"
colorValue = "rgba(" + colorValue + ", \(a))"
}
else {
css = "rgb(" + css + ")"
colorValue = "rgb(" + colorValue + ")"
}
}

public init(r: Double, g: Double, b: Double, a: Double? = nil) {
css = "\(r)%,\(g)%,\(b)%"
colorValue = "\(r)%,\(g)%,\(b)%"
if let a = a {
css = "rgba(" + css + ", \(a))"
colorValue = "rgba(" + colorValue + ", \(a))"
}
else {
css = "rgb(" + css + ")"
colorValue = "rgb(" + colorValue + ")"
}
}

public init(h: Int, s: Int, l: Int, a: Double? = nil) {
css = "\(h),\(s),\(l)"
colorValue = "\(h),\(s),\(l)"
if let a = a {
css = "hsla(" + css + ", \(a))"
colorValue = "hsla(" + colorValue + ", \(a))"
}
else {
css = "hsl(" + css + ")"
colorValue = "hsl(" + colorValue + ")"
}
}

public init(h: Double, s: Double, l: Double, a: Double? = nil) {
css = "\(h)%,\(s)%,\(l)%"
colorValue = "\(h)%,\(s)%,\(l)%"
if let a = a {
css = "hsla(" + css + ", \(a))"
colorValue = "hsla(" + colorValue + ", \(a))"
}
else {
css = "hsl(" + css + ")"
colorValue = "hsl(" + colorValue + ")"
}
}

var rawValue: String {
css
colorValue
}
}

8 changes: 2 additions & 6 deletions Sources/SwiftCss/Components/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

/// https://www.w3schools.com/cssref/
public struct Property: CSSRepresentable {
public struct Property {
var name: String
var value: String
var isImportant: Bool = false
Expand All @@ -16,11 +16,7 @@ public struct Property: CSSRepresentable {
self.value = value
self.isImportant = isImportant
}

public var css: String {
"\t" + name + ": " + value + (isImportant ? " !important" : "") + ";\n"
}


public func important() -> Property {
guard !isImportant else {
return self
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftCss/Components/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Created by Tibor Bodecs on 2021. 07. 10..
//

public protocol Rule: CSSRepresentable {
public protocol Rule {

}
10 changes: 1 addition & 9 deletions Sources/SwiftCss/Components/Selector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

/// https://www.w3schools.com/cssref/css_selectors.asp
public struct Selector: CSSRepresentable {
public struct Selector {
var name: String
var properties: [Property]
var pseudo: String? = nil
Expand All @@ -21,12 +21,4 @@ public struct Selector: CSSRepresentable {
self.name = name
self.properties = builder()
}

public var css: String {
var suffix = ""
if let pseudo = pseudo {
suffix = pseudo
}
return name + suffix + " {\n" + properties.map(\.css).joined() + "}\n"
}
}
41 changes: 26 additions & 15 deletions Sources/SwiftCss/Components/Unit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
// Created by Tibor Bodecs on 2021. 07. 10..
//

fileprivate extension Double {

/// converts a double value into an integer if it has no fraction digits, otherwise it uses the double value
var asIntOrDouble: String {
if truncatingRemainder(dividingBy: 1) == 0 {
return String(Int(self))
}
return String(self)
}
}

public enum Unit {
case zero

Expand Down Expand Up @@ -45,35 +56,35 @@ public enum Unit {
case .zero:
return "0"
case .cm(let value):
return "\(value)cm"
return "\(value.asIntOrDouble)cm"
case .mm(let value):
return "\(value)mm"
return "\(value.asIntOrDouble)mm"
case .in(let value):
return "\(value)in"
return "\(value.asIntOrDouble)in"
case .px(let value):
return "\(value)px"
return "\(value.asIntOrDouble)px"
case .pt(let value):
return "\(value)pt"
return "\(value.asIntOrDouble)pt"
case .pc(let value):
return "\(value)pc"
return "\(value.asIntOrDouble)pc"
case .em(let value):
return "\(value)em"
return "\(value.asIntOrDouble)em"
case .ex(let value):
return "\(value)ex"
return "\(value.asIntOrDouble)ex"
case .ch(let value):
return "\(value)ch"
return "\(value.asIntOrDouble)ch"
case .rem(let value):
return "\(value)rem"
return "\(value.asIntOrDouble)rem"
case .vw(let value):
return "\(value)vw"
return "\(value.asIntOrDouble)vw"
case .vh(let value):
return "\(value)vh"
return "\(value.asIntOrDouble)vh"
case .vmin(let value):
return "\(value)vmin"
return "\(value.asIntOrDouble)vmin"
case .vmax(let value):
return "\(value)vmax"
return "\(value.asIntOrDouble)vmax"
case .percent(let value):
return "\(value)%"
return "\(value.asIntOrDouble)%"
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions Sources/SwiftCss/Rules/Charset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public struct Charset: Rule {
public init(_ name: String) {
self.name = name
}

/// @charset "UTF-8";
public var css: String {
#"@charset ""# + name + #"";"#
}
}
4 changes: 0 additions & 4 deletions Sources/SwiftCss/Rules/FontFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public struct FontFace: Rule {
self.properties = builder()
}

public var css: String {
let value = properties.map(\.css).joined()
return "@font-face {\n\t" + value.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
}
}
5 changes: 0 additions & 5 deletions Sources/SwiftCss/Rules/Import.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ public struct Import: Rule {
public init(_ name: String) {
self.name = name
}

/// @import "mobstyle.css" screen and (max-width: 768px);
public var css: String {
"@import " + name + ";"
}
}
5 changes: 0 additions & 5 deletions Sources/SwiftCss/Rules/Keyframes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,4 @@ public struct Keyframes: Rule {
self.name = name
self.selectors = builder()
}

public var css: String {
let value = selectors.map(\.css).joined()
return "@keyframes " + name + " {\n\t" + value.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
}
}
8 changes: 0 additions & 8 deletions Sources/SwiftCss/Rules/Media.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,4 @@ public struct Media: Rule {
public init(screen: Screen, @SelectorBuilder _ builder: () -> [Selector]) {
self.init(screen.rawValue, builder)
}

public var css: String {
let css = selectors.map(\.css).joined()
guard let query = query else {
return css
}
return "@media " + query + " {\n\t" + css.split(separator: "\n").joined(separator: "\n\t") + "\n}\n"
}
}
14 changes: 14 additions & 0 deletions Sources/SwiftCss/Stylesheet/Stylesheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Stylesheet.swift
// SwiftCss
//
// Created by Tibor Bodecs on 2021. 07. 09..
//

public struct Stylesheet {
let rules: [Rule]

public init(@RuleBuilder _ builder: () -> [Rule]) {
self.rules = builder()
}
}
Loading

0 comments on commit 5ed92bd

Please sign in to comment.