Skip to content

Commit 4e99e83

Browse files
committed
Add superscript formatter and converters.
1 parent 04df855 commit 4e99e83

File tree

6 files changed

+84
-3
lines changed

6 files changed

+84
-3
lines changed

Aztec.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@
239239
FF7A1C511E5651EA00C4C7C8 /* LineAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7A1C501E5651EA00C4C7C8 /* LineAttachment.swift */; };
240240
FF7C89B01E3BC52F000472A8 /* NSAttributedString+FontTraits.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7C89AF1E3BC52F000472A8 /* NSAttributedString+FontTraits.swift */; };
241241
FF7EAEC4234D253B007A26E0 /* FontProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7EAEC3234D253B007A26E0 /* FontProvider.swift */; };
242+
FF94935E245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */; };
243+
FF949360245740250085ABB3 /* SuperscriptFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */; };
242244
FFA61E891DF18F3D00B71BF6 /* ParagraphStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */; };
243245
FFA61EC21DF6C1C900B71BF6 /* NSAttributedString+Archive.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */; };
244246
FFB5D29720BEB21A0038DCFB /* CiteFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */; };
@@ -523,6 +525,8 @@
523525
FF7A1C501E5651EA00C4C7C8 /* LineAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineAttachment.swift; sourceTree = "<group>"; };
524526
FF7C89AF1E3BC52F000472A8 /* NSAttributedString+FontTraits.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+FontTraits.swift"; sourceTree = "<group>"; };
525527
FF7EAEC3234D253B007A26E0 /* FontProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontProvider.swift; sourceTree = "<group>"; };
528+
FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperscriptStringAttributeConverter.swift; sourceTree = "<group>"; };
529+
FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuperscriptFormatter.swift; sourceTree = "<group>"; };
526530
FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParagraphStyle.swift; sourceTree = "<group>"; };
527531
FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+Archive.swift"; sourceTree = "<group>"; };
528532
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CiteFormatter.swift; sourceTree = "<group>"; };
@@ -1004,6 +1008,7 @@
10041008
F12F585F1EF20394008AE298 /* StrikethroughFormatter.swift */,
10051009
F12F58601EF20394008AE298 /* TextListFormatter.swift */,
10061010
F12F58611EF20394008AE298 /* UnderlineFormatter.swift */,
1011+
FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */,
10071012
FF61909D202481F4004BCD0A /* CodeFormatter.swift */,
10081013
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */,
10091014
);
@@ -1089,6 +1094,7 @@
10891094
F15BA6082151501300424120 /* BoldStringAttributeConverter.swift */,
10901095
F1656FDD2152A6A6009C7E3A /* CiteStringAttributeConverter.swift */,
10911096
F15BA60C215159A600424120 /* ItalicStringAttributeConverter.swift */,
1097+
FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */,
10921098
F15BA60E21515C0F00424120 /* UnderlineStringAttributeConverter.swift */,
10931099
);
10941100
path = Implementations;
@@ -1587,6 +1593,7 @@
15871593
F16A2AD520CC437E00BF3A0A /* LineAttachmentToElementConverter.swift in Sources */,
15881594
B574F4A41FB0CF3B0048F355 /* NSAttributedStringKey+Conversion.swift in Sources */,
15891595
F12F58631EF20394008AE298 /* AttributeFormatter.swift in Sources */,
1596+
FF949360245740250085ABB3 /* SuperscriptFormatter.swift in Sources */,
15901597
F19544051F588F1A00671B73 /* CSSParser.swift in Sources */,
15911598
599F254E1D8BC9A1002871D6 /* FormatBarDelegate.swift in Sources */,
15921599
F109873F214FF4C400983B6A /* ElementAttributeConverter.swift in Sources */,
@@ -1684,6 +1691,7 @@
16841691
F12F586F1EF20394008AE298 /* PreFormatter.swift in Sources */,
16851692
F18A1EA921C0586E00F1AA9E /* NSAttributedString+ParagraphRange.swift in Sources */,
16861693
B5B86D371DA3EC250083DB3F /* NSRange+Helpers.swift in Sources */,
1694+
FF94935E245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift in Sources */,
16871695
F15BA6172151693F00424120 /* BoldCSSAttributeMatcher.swift in Sources */,
16881696
F1E2323420C18055008DA49F /* FormatterElementConverter.swift in Sources */,
16891697
F15BA60B2151519C00424120 /* CSSAttributeType.swift in Sources */,

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/GenericElementConverter.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class GenericElementConverter: ElementConverter {
3434
lazy var unorderedListFormatter = TextListFormatter(style: .unordered, increaseDepth: true)
3535
lazy var codeFormatter = CodeFormatter()
3636
lazy var liFormatter = LiFormatter()
37+
lazy var superscriptFormatter = SuperscriptFormatter()
3738

3839
public lazy var elementFormattersMap: [Element: AttributeFormatter] = {
3940
return [
@@ -55,7 +56,8 @@ class GenericElementConverter: ElementConverter {
5556
.p: self.paragraphFormatter,
5657
.pre: self.preFormatter,
5758
.code: self.codeFormatter,
58-
.li: self.liFormatter
59+
.li: self.liFormatter,
60+
.sup: self.superscriptFormatter,
5961
]
6062
}()
6163

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Foundation
2+
import UIKit
3+
4+
5+
/// Converts the superscript style information from string attributes and aggregates it into an
6+
/// existing array of element nodes.
7+
///
8+
open class SuperscriptStringAttributeConverter: StringAttributeConverter {
9+
10+
private let toggler = HTMLStyleToggler(defaultElement: .sup, cssAttributeMatcher: NeverCSSAttributeMatcher())
11+
12+
public func convert(
13+
attributes: [NSAttributedString.Key: Any],
14+
andAggregateWith elementNodes: [ElementNode]) -> [ElementNode] {
15+
16+
var elementNodes = elementNodes
17+
18+
// We add the representation right away, if it exists... as it could contain attributes beyond just this
19+
// style. The enable and disable methods below can modify this as necessary.
20+
//
21+
if let representation = attributes[NSAttributedString.Key.supHtmlRepresentation] as? HTMLRepresentation,
22+
case let .element(representationElement) = representation.kind {
23+
24+
elementNodes.append(representationElement.toElementNode())
25+
}
26+
27+
if shouldEnable(for: attributes) {
28+
return toggler.enable(in: elementNodes)
29+
} else {
30+
return toggler.disable(in: elementNodes)
31+
}
32+
}
33+
34+
// MARK: - Style Detection
35+
36+
func shouldEnable(for attributes: [NSAttributedString.Key : Any]) -> Bool {
37+
return hasTraits(for: attributes)
38+
}
39+
40+
func hasTraits(for attributes: [NSAttributedString.Key : Any]) -> Bool {
41+
guard let baselineOffset = attributes[.baselineOffset] as? NSNumber else {
42+
return false
43+
}
44+
45+
return baselineOffset.intValue > 0;
46+
}
47+
}
48+

Aztec/Classes/Formatters/Base/StandardAttributeFormatter.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ class StandardAttributeFormatter: AttributeFormatter {
1111

1212
let htmlRepresentationKey: NSAttributedString.Key
1313

14+
let needsToMatchValue: Bool
15+
1416
// MARK: - Init
1517

16-
init(attributeKey: NSAttributedString.Key, attributeValue: Any, htmlRepresentationKey: NSAttributedString.Key) {
18+
init(attributeKey: NSAttributedString.Key, attributeValue: Any, htmlRepresentationKey: NSAttributedString.Key, needsToMatchValue: Bool = false) {
1719
self.attributeKey = attributeKey
1820
self.attributeValue = attributeValue
1921
self.htmlRepresentationKey = htmlRepresentationKey
22+
self.needsToMatchValue = needsToMatchValue
2023
}
2124

2225
func applicationRange(for range: NSRange, in text: NSAttributedString) -> NSRange {
@@ -43,7 +46,16 @@ class StandardAttributeFormatter: AttributeFormatter {
4346

4447
func present(in attributes: [NSAttributedString.Key: Any]) -> Bool {
4548
let enabled = attributes[attributeKey] != nil
46-
return enabled
49+
if (!needsToMatchValue) {
50+
return enabled
51+
}
52+
53+
if let value = attributes[attributeKey] as? NSObject,
54+
let attributeValue = attributeValue as? NSObject {
55+
return value.isEqual(attributeValue)
56+
}
57+
58+
return false
4759
}
4860
}
4961

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import UIKit
2+
3+
class SuperscriptFormatter: StandardAttributeFormatter {
4+
5+
init() {
6+
super.init(attributeKey: .baselineOffset,
7+
attributeValue: NSNumber(4),
8+
htmlRepresentationKey: .supHtmlRepresentation)
9+
}
10+
}

Aztec/Classes/NSAttributedString/Conversions/AttributedStringParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AttributedStringParser {
2727
BoldStringAttributeConverter(),
2828
ConditionalItalicStringAttributeConverter(),
2929
UnderlineStringAttributeConverter(),
30+
SuperscriptStringAttributeConverter(),
3031
]
3132

3233
// MARK: - Attachment Converters

0 commit comments

Comments
 (0)