Skip to content

Commit c37a52f

Browse files
author
Sanel Zsivics
committed
Refactor Plist2HTML to make it more reusable
1 parent 93214f2 commit c37a52f

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1-
enum Plist2HTML {
1+
import Foundation
22

3-
private static let whiteHTMLColor = "white"
4-
private static let blackHTMLColor = "black"
3+
struct Plist2HTML {
4+
5+
enum HTMLStyle: String {
6+
7+
case light
8+
case dark
9+
}
10+
11+
private let style: HTMLStyle
12+
13+
init(with style: HTMLStyle) {
14+
self.style = style
15+
}
16+
17+
private var textColor: String {
18+
return (self.style == .light) ? self.blackHTMLColor : self.whiteHTMLColor
19+
}
20+
21+
private var backgroundColor: String {
22+
return (self.style == .light) ? self.whiteHTMLColor : self.blackHTMLColor
23+
}
24+
25+
private let whiteHTMLColor = "white"
26+
private let blackHTMLColor = "black"
527

628
static let blankLine = "<br>"
729
static let ackKey = "PreferenceSpecifiers"
830
static let titleKey = "Title"
931
static let licenseKey = "License"
1032
static let footerKey = "FooterText"
1133

12-
static func body(with content: String) -> String {
13-
let color = Plist2HTML.blackHTMLColor
14-
let fullBody = ("<body style=\"background-color:\(color);\">" + content + "</body>")
34+
func body(with content: String) -> String {
35+
let fullBody = ("<body style=\"background-color:\(self.backgroundColor);\">" + content + "</body>")
1536

1637
let style = """
1738
<style> html, body { font-family: Calibri,\"PT Sans\",sans-serif; padding: 15px; }
@@ -36,24 +57,21 @@ enum Plist2HTML {
3657
return (fullBody + style)
3758
}
3859

39-
static func header(with title: String) -> String {
40-
let color = Plist2HTML.whiteHTMLColor
41-
return ("<h1 style=\"color:\(color);\">" + title + "</h1>")
60+
func header(with title: String) -> String {
61+
return ("<h1 style=\"color:\(self.textColor);\">" + title + "</h1>")
4262
}
4363

44-
static func subheader(with subtitle: String) -> String {
45-
let color = Plist2HTML.whiteHTMLColor
46-
return ("<h3 style=\"color:\(color);\">" + subtitle + "</h3>")
64+
func subheader(with subtitle: String) -> String {
65+
return ("<h3 style=\"color:\(self.textColor);\">" + subtitle + "</h3>")
4766
}
4867

49-
static func paragraph(with text: String) -> String {
50-
let color = Plist2HTML.whiteHTMLColor
51-
return ("<p style=\"color:\(color);\">" + text + "</p>")
68+
func paragraph(with text: String) -> String {
69+
return ("<p style=\"color:\(self.textColor);\">" + text + "</p>")
5270
}
5371

5472
/// Transforms the plist file containing the acknowledgments into an HTML
5573
/// - Parameter url: URL pointing to acknowledgments file
56-
static func transformPlistFile(from url: URL) -> String {
74+
func transformPlistFile(from url: URL) -> String {
5775
guard
5876
let plistDict = NSDictionary(contentsOfFile: url.path),
5977
let acknowledgmentsMultiList = plistDict[Plist2HTML.ackKey] as? NSArray else {
@@ -69,20 +87,20 @@ enum Plist2HTML {
6987
}
7088

7189
if let _title = ackDict[Plist2HTML.titleKey] as? String {
72-
htmlString += Plist2HTML.header(with: _title)
90+
htmlString += self.header(with: _title)
7391
}
7492

7593
if let _license = ackDict[Plist2HTML.licenseKey] as? String {
76-
htmlString += Plist2HTML.subheader(with: _license)
94+
htmlString += self.subheader(with: _license)
7795
}
7896

7997
if let _footerText = ackDict[Plist2HTML.footerKey] as? String {
80-
htmlString += Plist2HTML.paragraph(with: _footerText.replacingOccurrences(of: "\n", with: Plist2HTML.blankLine))
98+
htmlString += self.paragraph(with: _footerText.replacingOccurrences(of: "\n", with: Plist2HTML.blankLine))
8199
}
82100

83101
htmlString += Plist2HTML.blankLine
84102
}
85103

86-
return Plist2HTML.body(with: htmlString)
104+
return self.body(with: htmlString)
87105
}
88106
}

0 commit comments

Comments
 (0)