Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 226 lines (177 sloc) 6.045 kB
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
1 //
2 // KSHTMLWriter.m
3 // Sandvox
4 //
5 // Created by Mike on 23/02/2010.
6 // Copyright 2010 Karelia Software. All rights reserved.
7 //
8
9 #import "KSHTMLWriter.h"
10
11
12 @implementation KSHTMLWriter
13
72adfa1 @mikeabdullah Documenting .isXHTML generation better.
mikeabdullah authored
14 #pragma mark Creating an HTML Writer
15
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
16 - (id)initWithOutputWriter:(id <KSWriter>)stream;
17 {
18 [super initWithOutputWriter:stream];
19 _isXHTML = YES;
20 return self;
21 }
22
23 - (id)initWithOutputWriter:(id <KSWriter>)stream isXHTML:(BOOL)isXHTML;
24 {
25 if (self = [self initWithOutputWriter:stream])
26 {
27 _isXHTML = isXHTML;
28 }
29
30 return self;
31 }
32
72adfa1 @mikeabdullah Documenting .isXHTML generation better.
mikeabdullah authored
33 #pragma mark XHTML
34
35 @synthesize XHTML = _isXHTML;
36
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
37 #pragma mark Document
38
39 - (void)startDocument:(NSString *)DTD isXHTML:(BOOL)isXHTML;
40 {
41 _isXHTML = isXHTML;
42 [self startDocument:DTD];
43 }
44
45 #pragma mark HTML Fragments
46
47 - (void)writeHTMLString:(NSString *)html;
48 {
49 [self writeString:html];
50 }
51
52 - (void)writeHTMLFormat:(NSString *)format , ...
53 {
54 va_list argList;
55 va_start(argList, format);
56 NSString *aString = [[[NSString alloc] initWithFormat:format arguments:argList] autorelease];
57 va_end(argList);
58
59 [self writeHTMLString:aString];
60 }
61
62 #pragma mark General
63
64 - (void)startElement:(NSString *)tagName;
65 {
66 [self startElement:tagName className:nil];
67 }
68
69 - (void)startElement:(NSString *)tagName className:(NSString *)className;
70 {
71 [self startElement:tagName idName:nil className:className];
72 }
73
74 - (void)startElement:(NSString *)tagName idName:(NSString *)idName className:(NSString *)className;
75 {
76 [self openTag:tagName];
77 if (idName) [self writeAttribute:@"id" value:idName];
78 if (className) [self writeAttribute:@"class" value:className];
79 [self didStartElement];
80 }
81
82 #pragma mark Line Break
83
84 - (void)writeLineBreak;
85 {
86 [self startElement:@"br"];
87 [self endElement];
88 }
89
90 #pragma mark Higher-level Tag Writing
91
92 - (void)startAnchorElementWithHref:(NSString *)href title:(NSString *)titleString target:(NSString *)targetString rel:(NSString *)relString;
93 {
94 [self openTag:@"a"];
95 if (href) [self writeAttribute:@"href" value:href];
96 if (targetString) [self writeAttribute:@"target" value:targetString];
97 if (titleString) [self writeAttribute:@"title" value:titleString];
98 if (relString) [self writeAttribute:@"rel" value:relString];
99 [self didStartElement];
100 }
101
102 - (void)writeImageWithIdName:(NSString *)idName
103 className:(NSString *)className
104 src:(NSString *)src
105 alt:(NSString *)alt
106 width:(NSString *)width
107 height:(NSString *)height;
108 {
109 [self openTag:@"img"];
110
111 if (idName) [self writeAttribute:@"id" value:idName];
112 if (className) [self writeAttribute:@"class" value:className];
113
114 [self writeAttribute:@"src" value:src];
115 [self writeAttribute:@"alt" value:alt];
116 if (width) [self writeAttribute:@"width" value:width];
117 if (height) [self writeAttribute:@"height" value:height];
118
119 [self didStartElement];
120 [self endElement];
121 }
122
123 // TODO: disable indentation & newlines when we are in an anchor tag, somehow.
124
125 #pragma mark Link
126
127 - (void)writeLinkWithHref:(NSString *)href
128 type:(NSString *)type
129 rel:(NSString *)rel
130 title:(NSString *)title
131 media:(NSString *)media;
132 {
133 [self openTag:@"link"];
134
135 if (rel) [self writeAttribute:@"rel" value:rel];
136 if (type) [self writeAttribute:@"type" value:type];
137 [self writeAttribute:@"href" value:href];
138 if (title) [self writeAttribute:@"title" value:title];
139 if (media) [self writeAttribute:@"media" value:media];
140
141 [self didStartElement];
142 [self endElement];
143 }
144
145 - (void)writeLinkToStylesheet:(NSString *)href
146 title:(NSString *)title
147 media:(NSString *)media;
148 {
149 [self writeLinkWithHref:href type:@"text/css" rel:@"stylesheet" title:title media:media];
150 }
151
152 #pragma mark Style
153
154 - (void)startStyleElementWithType:(NSString *)type;
155 {
156 [self openTag:@"style"];
157 if (type) [self writeAttribute:@"type" value:type];
158 [self didStartElement];
159 }
160
161 #pragma mark Elements Stack
162
163 - (BOOL)topElementIsList;
164 {
165 NSString *tagName = [self topElement];
166 BOOL result = [tagName isEqualToString:@"UL"] || [tagName isEqualToString:@"OL"];
167 return result;
168 }
169
170 #pragma mark Inline Writing
171
172 - (BOOL)canWriteElementInline:(NSString *)tagName;
173 {
174 switch ([tagName length])
175 {
176 case 1:
177 if ([tagName caseInsensitiveCompare:@"A"] == NSOrderedSame ||
178 [tagName caseInsensitiveCompare:@"B"] == NSOrderedSame ||
179 [tagName caseInsensitiveCompare:@"I"] == NSOrderedSame) return YES;
180 break;
181
182 case 2:
183 if ([tagName caseInsensitiveCompare:@"BR"] == NSOrderedSame ||
184 [tagName caseInsensitiveCompare:@"EM"] == NSOrderedSame) return YES;
185 break;
186
187 case 3:
188 if ([tagName caseInsensitiveCompare:@"IMG"] == NSOrderedSame ||
189 [tagName caseInsensitiveCompare:@"SUP"] == NSOrderedSame ||
190 [tagName caseInsensitiveCompare:@"SUB"] == NSOrderedSame ||
191 [tagName caseInsensitiveCompare:@"BIG"] == NSOrderedSame) return YES;
192 break;
193
194 case 4:
195 if ([tagName caseInsensitiveCompare:@"SPAN"] == NSOrderedSame ||
196 [tagName caseInsensitiveCompare:@"FONT"] == NSOrderedSame) return YES;
197 break;
198
199 case 5:
200 if ([tagName caseInsensitiveCompare:@"SMALL"] == NSOrderedSame) return YES;
201 break;
202
203 case 6:
204 if ([tagName caseInsensitiveCompare:@"STRONG"] == NSOrderedSame) return YES;
205 break;
206 }
207
208 return [super canWriteElementInline:tagName];
209 }
210
211 #pragma mark Element Primitives
212
213 - (void)closeEmptyElementTag; // /> OR > depending on -isXHTML
214 {
215 if ([self isXHTML])
216 {
217 [super closeEmptyElementTag];
218 }
219 else
220 {
221 [self writeString:@">"];
222 }
223 }
224
225 @end
Something went wrong with that request. Please try again.