Skip to content

Commit

Permalink
Merge pull request dokun1#5 from mbarnach/heading_enum
Browse files Browse the repository at this point in the history
Use an enum for the heading
  • Loading branch information
dokun1 authored Jun 12, 2019
2 parents 664443d + c047e46 commit 0f146f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ In HTML, a heading is used to specify the weight of certain text, to create a na
```swift
func buildPage() -> HTML {
html {
heading(weight: 2) {
heading(.h2) {
"This is a header"
}
}
Expand Down
19 changes: 13 additions & 6 deletions Sources/Vaux/Builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

import Foundation

/// Heading weight for the heading tag.
public enum HeadingWeight {
case h1
case h2
case h3
case h4
case h5
case h6
}

/// Inserts the top level `<html>` element into the HTML document, and closes with `</html>` after the contents of the closure.
/// - Parameters:
/// - child: `HTML` content to go inside the `<html></html>` element.
Expand Down Expand Up @@ -49,13 +59,10 @@ public func lineBreak() -> HTML {

/// Inserts a `<h1>` element (given the specified weight) into the
/// - Parameters:
/// - weight: The weight of the heading, such as `<h1>` or `<h4>`. The weight must be specified as between 1 and 6.
/// - weight: The weight of the heading, such as `<h1>` or `<h4>`.
/// - child: `HTML` content to go inside the `<h1></h1>` element.
public func heading(weight: Int, @HTMLBuilder child: () -> HTML) -> HTML {
var weight = weight
if weight < 1 { weight = 1 }
if weight > 6 { weight = 6 }
return HTMLNode(tag: "h\(weight)", child: child())
public func heading(_ weight: HeadingWeight, @HTMLBuilder child: () -> HTML) -> HTML {
return HTMLNode(tag: "\(weight)", child: child())
}

/// Inserts a `<p>` element into the HTML document, and closes with `</p>` after the contents of the closure.
Expand Down
11 changes: 4 additions & 7 deletions Tests/VauxTests/VauxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ final class VauxTests: XCTestCase {
html {
body {
link(url: url, label: "google")
lineBreak()
}
}
}
Expand Down Expand Up @@ -214,16 +213,14 @@ final class VauxTests: XCTestCase {
func pageWithHeading() -> HTML {
html {
body {
heading(weight: 1) {
heading(.h1) {
"This is a heading of weight 1"
}
heading(weight: 3) {
heading(.h3) {
"This is a heading of weight 3"
}
paragraph {
"Four score and "
emphasis { "seven" }
" years ago..."
"Four score and seven years ago..."
}
}.style([StyleAttribute(key: "background-color", value: "blue"),
StyleAttribute(key: "color", value: "red")])
Expand All @@ -240,7 +237,7 @@ final class VauxTests: XCTestCase {
This is a heading of weight 3
</h3>
<p>
Four score and <em>seven</em> years ago...
Four score and seven years ago...
</p>
</body>
</html>
Expand Down

0 comments on commit 0f146f4

Please sign in to comment.