|
| 1 | +enum Plist2HTML { |
| 2 | + |
| 3 | + private static let whiteHTMLColor = "white" |
| 4 | + private static let blackHTMLColor = "black" |
| 5 | + |
| 6 | + static let blankLine = "<br>" |
| 7 | + static let ackKey = "PreferenceSpecifiers" |
| 8 | + static let titleKey = "Title" |
| 9 | + static let licenseKey = "License" |
| 10 | + static let footerKey = "FooterText" |
| 11 | + |
| 12 | + static func body(with content: String) -> String { |
| 13 | + |
| 14 | + let color = Plist2HTML.blackHTMLColor |
| 15 | + let fullBody = ("<body style=\"background-color:\(color);\">" + content + "</body>") |
| 16 | + |
| 17 | + // Adding style this way to make it more readable and easier to be modified |
| 18 | + let style = """ |
| 19 | + <style> html, body { font-family: Calibri,\"PT Sans\",sans-serif; padding: 15px; } |
| 20 | + ol { |
| 21 | + padding-left: 15px; |
| 22 | + } |
| 23 | + li { |
| 24 | + font-size: 16px; |
| 25 | + } |
| 26 | + @media (prefers-color-scheme: dark) { |
| 27 | + body { |
| 28 | + background: #333; |
| 29 | + color: #fff; |
| 30 | + } |
| 31 | + a { |
| 32 | + color:#999; |
| 33 | + } |
| 34 | + } |
| 35 | + </style> |
| 36 | + """ |
| 37 | + |
| 38 | + return (fullBody + style) |
| 39 | + } |
| 40 | + |
| 41 | + static func header(with title: String) -> String { |
| 42 | + |
| 43 | + let color = Plist2HTML.whiteHTMLColor |
| 44 | + return ("<h1 style=\"color:\(color);\">" + title + "</h1>") |
| 45 | + } |
| 46 | + |
| 47 | + static func subheader(with subtitle: String) -> String { |
| 48 | + |
| 49 | + let color = Plist2HTML.whiteHTMLColor |
| 50 | + return ("<h3 style=\"color:\(color);\">" + subtitle + "</h3>") |
| 51 | + } |
| 52 | + |
| 53 | + static func paragraph(with text: String) -> String { |
| 54 | + |
| 55 | + let color = Plist2HTML.whiteHTMLColor |
| 56 | + return ("<p style=\"color:\(color);\">" + text + "</p>") |
| 57 | + } |
| 58 | + |
| 59 | + /// Transforms the plist file containing the acknowledgments into an HTML |
| 60 | + /// - Parameter url: URL pointing to acknowledgments file |
| 61 | + static func transformPlistFile(from url: URL) -> String { |
| 62 | + |
| 63 | + guard |
| 64 | + let plistDict = NSDictionary(contentsOfFile: url.path), |
| 65 | + let acknowledgmentsMultiList = plistDict[Plist2HTML.ackKey] as? NSArray else { |
| 66 | + return "" |
| 67 | + } |
| 68 | + |
| 69 | + var htmlString = "" |
| 70 | + |
| 71 | + acknowledgmentsMultiList.forEach { (acknowledgment: Any) in |
| 72 | + |
| 73 | + guard let ackDict = acknowledgment as? NSDictionary else { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if let _title = ackDict[Plist2HTML.titleKey] as? String { |
| 78 | + htmlString += Plist2HTML.header(with: _title) |
| 79 | + } |
| 80 | + |
| 81 | + if let _license = ackDict[Plist2HTML.licenseKey] as? String { |
| 82 | + htmlString += Plist2HTML.subheader(with: _license) |
| 83 | + } |
| 84 | + |
| 85 | + if let _footerText = ackDict[Plist2HTML.footerKey] as? String { |
| 86 | + htmlString += Plist2HTML.paragraph(with: _footerText.replacingOccurrences(of: "\n", with: Plist2HTML.blankLine)) |
| 87 | + } |
| 88 | + |
| 89 | + htmlString += Plist2HTML.blankLine |
| 90 | + } |
| 91 | + |
| 92 | + let styledHTMLString = Plist2HTML.body(with: htmlString) |
| 93 | + |
| 94 | + return styledHTMLString |
| 95 | + } |
| 96 | +} |
0 commit comments