This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: bumping version to 3.2.2 * added SwiftRichString dependency; adding parameter to control HTML parsing mode * implementing HTMLRenderer * adding XMLDynamicAttributesResolver * pointing to SwiftRichString branch * passing HTMLStyleParams * some refactoring and tidy up; allow the optimized HTML rendering to be passed on the creation of the RTV * cleanup * resolved package * some tidy up * fixing tests and lint warnings; adding the files to the workspace * updating podspec to 3.3.0 * adding SwiftRichString as a dependency * PR fix: nicer implementation of renderHTML * removing extra blank space
- Loading branch information
1 parent
2e51877
commit c34d4d8
Showing
14 changed files
with
316 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "Down", | ||
"repositoryURL": "https://github.com/johnxnguyen/Down", | ||
"state": { | ||
"branch": null, | ||
"revision": "f34b166be1f1db4aa8f573067e901d72f2a6be57", | ||
"version": "0.11.0" | ||
} | ||
}, | ||
{ | ||
"package": "iosMath", | ||
"repositoryURL": "https://github.com/tophatmonocle/iosMath", | ||
"state": { | ||
"branch": null, | ||
"revision": "8c65dab2295100c02d9d844f6dc5646ab9d57b42", | ||
"version": "1.1.2" | ||
} | ||
}, | ||
{ | ||
"package": "SnapKit", | ||
"repositoryURL": "https://github.com/SnapKit/SnapKit", | ||
"state": { | ||
"branch": null, | ||
"revision": "d458564516e5676af9c70b4f4b2a9178294f1bc6", | ||
"version": "5.0.1" | ||
} | ||
}, | ||
{ | ||
"package": "SwiftRichString", | ||
"repositoryURL": "https://github.com/tophatmonocle/SwiftRichString", | ||
"state": { | ||
"branch": "master", | ||
"revision": "e0b72d5c96968d7802856d2be096202c9798e8d1", | ||
"version": null | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// DynamicAttributesResolver.swift | ||
// | ||
// | ||
// Created by Maicon Brauwers on 2022-02-24. | ||
// | ||
|
||
import UIKit | ||
import SwiftRichString | ||
|
||
class DynamicAttributesResolver: XMLDynamicAttributesResolver { | ||
|
||
func styleForUnknownXMLTag(_ tag: String, to attributedString: inout SwiftRichString.AttributedString, attributes: [String: String]?, fromStyle: StyleXML) { | ||
} | ||
|
||
func applyDynamicAttributes(to attributedString: inout SwiftRichString.AttributedString, xmlStyle: XMLDynamicStyle, fromStyle: StyleXML) { | ||
let finalStyleToApply = Style() | ||
xmlStyle.enumerateAttributes { key, value in | ||
switch key { | ||
case "color": // color support | ||
finalStyleToApply.color = Color(hexString: value) | ||
case "style": | ||
// THIS IS ALL HORRIBLE AND SHOULD BE REFACTOR USING A STRING SCANNER OR REGEX!!! | ||
if value.starts(with: "color: rgb(") { | ||
let substr = value.suffix(from: "color: rgb(".endIndex).dropLast() | ||
let rgbValues = substr.split(separator: ",") | ||
if rgbValues.count == 3, | ||
let red = Float(rgbValues[0].trimmingCharacters(in: .whitespacesAndNewlines)), | ||
let green = Float(rgbValues[1].trimmingCharacters(in: .whitespacesAndNewlines)), | ||
let blue = Float(rgbValues[2].trimmingCharacters(in: .whitespacesAndNewlines)) { | ||
finalStyleToApply.color = Color(red: CGFloat(red)/255, green: CGFloat(green)/255, blue: CGFloat(blue)/255, alpha: 1.0) | ||
} | ||
} | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
attributedString.add(style: finalStyleToApply) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// HTMLRenderer.swift | ||
// | ||
// | ||
// Created by Maicon Brauwers on 2022-02-22. | ||
// | ||
|
||
import UIKit | ||
import SwiftRichString | ||
|
||
/* | ||
* Renders an HTML string into a NSAttributedString by using the SwiftRichString dependency | ||
*/ | ||
|
||
class HTMLRenderer { | ||
|
||
var cachedStyles: [HTMLStyleParams: StyleXML] = [:] | ||
static let shared = HTMLRenderer() | ||
|
||
func renderHTML(html: String, styleParams: HTMLStyleParams) -> NSAttributedString { | ||
let style: StyleXML | ||
|
||
if let cachedStyle = self.cachedStyles[styleParams] { | ||
style = cachedStyle | ||
} else { | ||
style = HTMLStyleBuilder().buildStyles(styleParams: styleParams) | ||
cachedStyles[styleParams] = style | ||
} | ||
|
||
let htmlReplacingBr = html.replacingOccurrences(of: "<br>", with: "\n") | ||
return htmlReplacingBr.set(style: style) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// HTMLStyleBuilder.swift | ||
// | ||
// | ||
// Created by Maicon Brauwers on 2022-02-24. | ||
// | ||
|
||
import SwiftRichString | ||
|
||
// This class is responsible to create the styles that are using to style the HTML tags | ||
|
||
struct HTMLStyleBuilder { | ||
|
||
// swiftlint:disable function_body_length | ||
func buildStyles(styleParams: HTMLStyleParams) -> StyleGroup { | ||
|
||
let baseStyle = Style { | ||
$0.font = styleParams.baseFont | ||
$0.minimumLineHeight = 28 | ||
} | ||
|
||
let h1Style = Style { | ||
$0.font = styleParams.h1Font | ||
$0.traitVariants = .bold | ||
} | ||
|
||
let h2Style = Style { | ||
$0.font = styleParams.h2Font | ||
$0.traitVariants = .bold | ||
} | ||
|
||
let h3Style = Style { | ||
$0.font = styleParams.h3Font | ||
$0.traitVariants = .bold | ||
} | ||
|
||
let italicStyle = baseStyle.byAdding { | ||
$0.traitVariants = .italic | ||
} | ||
|
||
let boldStyle = baseStyle.byAdding { | ||
$0.traitVariants = .bold | ||
} | ||
|
||
let superStyle = Style { | ||
$0.font = styleParams.baseFont.font(size: 8.0) | ||
$0.baselineOffset = 20.0 / 3.5 | ||
} | ||
|
||
let subStyle = baseStyle.byAdding { | ||
$0.font = styleParams.baseFont.font(size: 8.0) | ||
$0.baselineOffset = -20.0 / 3.5 | ||
} | ||
|
||
let codeStyle = baseStyle.byAdding { | ||
$0.backColor = Color(red: 249/255, green: 249/255, blue: 249/255, alpha: 1.0) | ||
$0.color = Color(red: 0.69, green: 0.231, blue: 0, alpha: 1) | ||
} | ||
|
||
let groupStyle = StyleXML( | ||
base: baseStyle, | ||
[ | ||
"p": baseStyle, | ||
"div": baseStyle, | ||
"h1": h1Style, | ||
"h2": h2Style, | ||
"h3": h3Style, | ||
"i": italicStyle, | ||
"em": italicStyle, | ||
"italic": italicStyle, | ||
"bold": boldStyle, | ||
"strong": boldStyle, | ||
"b": boldStyle, | ||
"sub": subStyle, | ||
"sup": superStyle, | ||
"span": baseStyle, | ||
"code": codeStyle | ||
] | ||
) | ||
groupStyle.xmlAttributesResolver = DynamicAttributesResolver() | ||
return groupStyle | ||
} | ||
// swiftlint:enable function_body_length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// HTMLStyleParams.swift | ||
// | ||
// | ||
// Created by Maicon Brauwers on 2022-02-24. | ||
// | ||
|
||
import UIKit | ||
|
||
public struct HTMLStyleParams: Hashable { | ||
let baseFont: UIFont | ||
let h1Font: UIFont | ||
let h2Font: UIFont | ||
let h3Font: UIFont | ||
|
||
public init(baseFont: UIFont, h1Font: UIFont, h2Font: UIFont, h3Font: UIFont) { | ||
self.baseFont = baseFont | ||
self.h1Font = h1Font | ||
self.h2Font = h2Font | ||
self.h3Font = h3Font | ||
} | ||
} |
Oops, something went wrong.