@@ -20,7 +20,11 @@ public class HTMLConverter {
2020 }
2121
2222 // MARK: - Converters: HTML -> AttributedString
23-
23+
24+ /// If a value is set the character set will be used to replace any last empty line created by the converter.
25+ ///
26+ open var characterToReplaceLastEmtpyLine : Character ?
27+
2428 let htmlToTree = HTMLParser ( )
2529
2630 private( set) lazy var treeToAttributedString : AttributedStringSerializer = {
@@ -54,11 +58,27 @@ public class HTMLConverter {
5458 pluginManager. process ( htmlTree: rootNode)
5559
5660 let defaultAttributes = defaultAttributes ?? [ : ]
57- let attributedString = treeToAttributedString. serialize ( rootNode, defaultAttributes: defaultAttributes)
61+ var attributedString = treeToAttributedString. serialize ( rootNode, defaultAttributes: defaultAttributes)
62+
63+ if let characterToUse = characterToReplaceLastEmtpyLine {
64+ attributedString = replaceLastEmptyLine ( in: attributedString, with: characterToUse)
65+ }
5866
5967 return attributedString
6068 }
6169
70+ func replaceLastEmptyLine( in attributedString: NSAttributedString , with replacement: Character ) -> NSAttributedString {
71+ var result = attributedString
72+ let string = attributedString. string
73+ if string. isEmptyLineAtEndOfFile ( at: string. count) , !string. isEmpty, let location = string. location ( before: attributedString. length) {
74+ let mutableString = NSMutableAttributedString ( attributedString: attributedString)
75+ let attributes = mutableString. attributes ( at: location, effectiveRange: nil )
76+ mutableString. replaceCharacters ( in: NSRange ( location: location, length: attributedString. length- location) , with: NSAttributedString ( string: String ( replacement) , attributes: attributes) )
77+ result = mutableString
78+ }
79+ return result
80+ }
81+
6282
6383 /// Check if the given html string is supported to be parsed into Attributed Strings.
6484 ///
0 commit comments