-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAttributedRenderer.m
More file actions
257 lines (226 loc) · 9.31 KB
/
AttributedRenderer.m
File metadata and controls
257 lines (226 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#import "AttributedRenderer.h"
#import "NodeRenderer.h"
#import "RenderContext.h"
#import "MarkdownASTNode.h"
#import "SpacingUtils.h"
@interface ParagraphRenderer : NSObject <NodeRenderer>
@end
@interface TextRenderer : NSObject <NodeRenderer>
@end
@interface LinkRenderer : NSObject <NodeRenderer>
@end
@interface HeadingRenderer : NSObject <NodeRenderer>
@end
@interface AttributedRenderer (Helpers)
- (NSAttributedString *)createTextString:(NSString *)text
withFont:(UIFont *)font
color:(UIColor *)color;
- (void)renderChildrenOfNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context;
@end
@implementation AttributedRenderer
- (NSMutableAttributedString *)renderRoot:(MarkdownASTNode *)root
font:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
NSMutableAttributedString *out = [[NSMutableAttributedString alloc] init];
[self renderNodeRecursive:root into:out font:font color:color context:context isTopLevel:YES];
return out;
}
/**
* Recursively renders markdown AST nodes into attributed text.
*
* Uses recursive tree traversal to handle nested markdown elements like
* "**bold with [link](url) inside**". Each node type has its own renderer
* for modular, maintainable code. Performance: O(n) with shallow AST depth.
*/
- (void)renderNodeRecursive:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)out
font:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context
isTopLevel:(BOOL)isTopLevel {
id<NodeRenderer> renderer = [self rendererForNode:node];
if (renderer) {
[renderer renderNode:node into:out withFont:font color:color context:context];
return;
}
// Fallback: render children
for (NSUInteger i = 0; i < node.children.count; i++) {
MarkdownASTNode *child = node.children[i];
[self renderNodeRecursive:child into:out font:font color:color context:context isTopLevel:NO];
if (child.type == MarkdownNodeTypeParagraph && i < node.children.count - 1) {
NSAttributedString *spacing = createSpacing();
[out appendAttributedString:spacing];
}
}
}
- (id<NodeRenderer>)rendererForNode:(MarkdownASTNode *)node {
switch (node.type) {
case MarkdownNodeTypeParagraph: return [ParagraphRenderer new];
case MarkdownNodeTypeText: return [TextRenderer new];
case MarkdownNodeTypeLink: return [LinkRenderer new];
case MarkdownNodeTypeHeading: return [HeadingRenderer new];
default: return nil;
}
}
@end
@implementation AttributedRenderer (Helpers)
- (NSAttributedString *)createTextString:(NSString *)text
withFont:(UIFont *)font
color:(UIColor *)color {
return [[NSAttributedString alloc] initWithString:text
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
}
- (void)renderChildrenOfNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
for (MarkdownASTNode *child in node.children) {
id<NodeRenderer> renderer = [self rendererForNode:child];
if (renderer) {
[renderer renderNode:child
into:output
withFont:font
color:color
context:context];
}
}
}
@end
@implementation ParagraphRenderer
- (BOOL)canRender:(MarkdownASTNode *)node { return node.type == MarkdownNodeTypeParagraph; }
- (void)renderNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
for (MarkdownASTNode *child in node.children) {
switch (child.type) {
case MarkdownNodeTypeText:
if (child.content) {
NSAttributedString *text = [[NSAttributedString alloc]
initWithString:child.content
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
[output appendAttributedString:text];
}
break;
case MarkdownNodeTypeLink: {
LinkRenderer *linkRenderer = [LinkRenderer new];
[linkRenderer renderNode:child
into:output
withFont:font
color:color
context:context];
break;
}
case MarkdownNodeTypeLineBreak: {
NSAttributedString *br = [[NSAttributedString alloc]
initWithString:@"\n"
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
[output appendAttributedString:br];
break;
}
default:
// Fallback: render children
for (MarkdownASTNode *grand in child.children) {
if (grand.type == MarkdownNodeTypeText && grand.content) {
NSAttributedString *t = [[NSAttributedString alloc]
initWithString:grand.content
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
[output appendAttributedString:t];
}
}
break;
}
}
}
@end
@implementation TextRenderer
- (BOOL)canRender:(MarkdownASTNode *)node { return node.type == MarkdownNodeTypeText; }
- (void)renderNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
if (!node.content) return;
NSAttributedString *text = [[NSAttributedString alloc]
initWithString:node.content
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
[output appendAttributedString:text];
}
@end
@implementation LinkRenderer
- (BOOL)canRender:(MarkdownASTNode *)node { return node.type == MarkdownNodeTypeLink; }
- (void)renderNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
NSUInteger start = output.length;
for (MarkdownASTNode *child in node.children) {
if (child.type == MarkdownNodeTypeText && child.content) {
NSAttributedString *text = [[NSAttributedString alloc]
initWithString:child.content
attributes:@{
NSFontAttributeName: font
}];
[output appendAttributedString:text];
}
}
NSUInteger len = output.length - start;
if (len > 0) {
NSRange range = NSMakeRange(start, len);
NSString *url = node.attributes[@"url"] ?: @"";
[output addAttributes:@{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)
} range:range];
[output addAttribute:NSLinkAttributeName
value:url
range:range];
[context registerLinkRange:range url:url];
}
}
@end
@implementation HeadingRenderer
- (BOOL)canRender:(MarkdownASTNode *)node { return node.type == MarkdownNodeTypeHeading; }
- (void)renderNode:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
withFont:(UIFont *)font
color:(UIColor *)color
context:(RenderContext *)context {
UIFont *boldFont = [UIFont fontWithDescriptor:[font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize];
for (MarkdownASTNode *child in node.children) {
if (child.type == MarkdownNodeTypeText && child.content) {
NSAttributedString *text = [[NSAttributedString alloc]
initWithString:child.content
attributes:@{
NSFontAttributeName: boldFont,
NSForegroundColorAttributeName: color
}];
[output appendAttributedString:text];
}
}
NSAttributedString *spacing = createSpacing();
[output appendAttributedString:spacing];
}
@end