-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathETTextExample.m
155 lines (148 loc) · 3.28 KB
/
ETTextExample.m
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
#import <EtoileFoundation/EtoileFoundation.h>
//#import <CoreObject/CoreObject.h>
#import "EtoileText.h"
@interface Visitor : NSObject <ETTextVisitor>
{
NSUInteger depth;
NSMutableString *tex;
}
@end
@implementation Visitor
- (id)init
{
SUPERINIT;
tex = [NSMutableString new];
return self;
}
- (void)startTextNode: (id<ETText>)aNode
{
NSString *typeName = [aNode.textType valueForKey: kETTextStyleName];
if (nil != typeName)
{
[tex appendFormat: @"\\%@{", typeName];
}
depth++;
}
- (void)visitTextNode: (id<ETText>)aNode
{
[tex appendString: [aNode stringValue]];
}
- (void)endTextNode: (id<ETText>)aNode
{
;
if (nil != [aNode.textType valueForKey: kETTextStyleName])
{
[tex appendString: @"}"];
}
depth--;
if (depth == 0)
{
NSLog(@"%@", tex);
}
}
@end
@interface HTMLParser : NSObject
{
id<ETText>html;
}
@end
@implementation HTMLParser
- (id<ETText>)parseHTMLFromString: (NSString*)aString
{
html = nil;
ETXMLParser *parser = [ETXMLParser new];
ETXMLTextParser *delegate =
[[ETXMLTextParser alloc] initWithXMLParser: parser
parent: (id)self
key: @"HTML"];
delegate.document = [ETTextDocument new];
[parser parseFromSource: aString];
[parser release];
return html;
}
- (void)addChild: (id<ETText>)aNode forKey: (NSString*)aKey
{
html = [[aNode retain] autorelease];
}
@end
@interface TeXScannerDelegate : NSObject <ETTeXScannerDelegate>
{
@public
NSMutableSet *commands;
}
@end
@implementation TeXScannerDelegate
- (id)init
{
SUPERINIT;
commands = [NSMutableSet new];
return self;
}
- (void)dealloc
{
NSLog(@"Used commands: %@", commands);
[commands release];
[super dealloc];
}
- (void)beginCommand: (NSString*)aCommand
{
[commands addObject: aCommand];
NSLog(@"Command: %@", aCommand);
}
- (void)beginOptArg
{
NSLog(@"[");
}
- (void)endOptArg
{
NSLog(@"]");
}
- (void)beginArgument
{
NSLog(@"{");
}
- (void)endArgument
{
NSLog(@"}");
}
- (void)handleText: (NSString*)aString
{
NSLog(@"Text: %@", aString);
}
@end
int main(void)
{
[NSAutoreleasePool new];
ETTextFragment *text = [[ETTextFragment alloc] initWithString: @"This is a test"];
text.textType = D(@"p", kETTextStyleName);
NSLog(@"%@", text);
id<ETText> part1 = [text splitAtIndex: 5];
NSLog(@"%@", part1);
NSLog(@"%@", text);
ETTextTree *tree = [ETTextTree textTreeWithChildren: A(part1, text)];
tree.textType = D(@"div", kETTextStyleName);
[tree visitWithVisitor: [Visitor new]];
NSLog(@"%@", tree);
id<ETText>tree1 = [tree splitAtIndex: 8];
NSLog(@"%@%@", tree1, tree);
HTMLParser *parser = [HTMLParser new];
id<ETText> html = [parser parseHTMLFromString: @"<p>This is a string containing <b>bold</b> text</p>"];
NSLog(@"%@", html);
[html visitWithVisitor: [Visitor new]];
TeXScannerDelegate *d = [TeXScannerDelegate new];
ETTeXScanner *s = [ETTeXScanner new];
NSError *error = nil;
NSString *tex = [NSString stringWithContentsOfFile: @"/tmp/tex"
encoding: NSUTF8StringEncoding
error: &error];
NSCAssert1(error == nil, @"%@", error);
s.delegate = d;
[s parseString: tex];
[d release];
ETTeXParser *d2 = [ETTeXParser new];
d2.scanner = s;
s.delegate = d2;
[s parseString: tex];
NSLog(@"Parsed TeX: %@", d2.builder.textTree);
return 0;
}