Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 248 lines (195 sloc) 6.744 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
de567f7 added method to output JavaScript (via Dan)
Terrence Talbot authored
152 #pragma mark javascript
153
154 - (void)writeScriptSrc:(NSString *)src // Note: You should either use src OR contents, not both.
155 orContents:(NSString *)contents // However you can specify contents for comments, which is OK.
156 useCDATA:(BOOL)useCDATA;
157 {
158 [self openTag:@"script"];
159
160 [self writeAttribute:@"type" value:@"text/javascript"];
161 if (src) [self writeAttribute:@"src" value:src];
162 [self didStartElement];
163 if (contents)
164 {
165 // DO NOT USE startNewline since we don't want indentation
166 [self writeString:useCDATA ? @"\n/* <![CDATA[ */\n" : @"\n"];
167 [self writeString:contents];
168 [self writeString:useCDATA ? @"\n/* ]]> */\n" : @"\n"];
169 }
170
171 [self endElement];
172 }
173
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
174 #pragma mark Style
175
176 - (void)startStyleElementWithType:(NSString *)type;
177 {
178 [self openTag:@"style"];
179 if (type) [self writeAttribute:@"type" value:type];
180 [self didStartElement];
181 }
182
183 #pragma mark Elements Stack
184
185 - (BOOL)topElementIsList;
186 {
187 NSString *tagName = [self topElement];
188 BOOL result = [tagName isEqualToString:@"UL"] || [tagName isEqualToString:@"OL"];
189 return result;
190 }
191
192 #pragma mark Inline Writing
193
194 - (BOOL)canWriteElementInline:(NSString *)tagName;
195 {
196 switch ([tagName length])
197 {
198 case 1:
199 if ([tagName caseInsensitiveCompare:@"A"] == NSOrderedSame ||
200 [tagName caseInsensitiveCompare:@"B"] == NSOrderedSame ||
201 [tagName caseInsensitiveCompare:@"I"] == NSOrderedSame) return YES;
202 break;
203
204 case 2:
205 if ([tagName caseInsensitiveCompare:@"BR"] == NSOrderedSame ||
206 [tagName caseInsensitiveCompare:@"EM"] == NSOrderedSame) return YES;
207 break;
208
209 case 3:
210 if ([tagName caseInsensitiveCompare:@"IMG"] == NSOrderedSame ||
211 [tagName caseInsensitiveCompare:@"SUP"] == NSOrderedSame ||
212 [tagName caseInsensitiveCompare:@"SUB"] == NSOrderedSame ||
213 [tagName caseInsensitiveCompare:@"BIG"] == NSOrderedSame) return YES;
214 break;
215
216 case 4:
217 if ([tagName caseInsensitiveCompare:@"SPAN"] == NSOrderedSame ||
218 [tagName caseInsensitiveCompare:@"FONT"] == NSOrderedSame) return YES;
219 break;
220
221 case 5:
222 if ([tagName caseInsensitiveCompare:@"SMALL"] == NSOrderedSame) return YES;
223 break;
224
225 case 6:
226 if ([tagName caseInsensitiveCompare:@"STRONG"] == NSOrderedSame) return YES;
227 break;
228 }
229
230 return [super canWriteElementInline:tagName];
231 }
232
233 #pragma mark Element Primitives
234
235 - (void)closeEmptyElementTag; // /> OR > depending on -isXHTML
236 {
237 if ([self isXHTML])
238 {
239 [super closeEmptyElementTag];
240 }
241 else
242 {
243 [self writeString:@">"];
244 }
245 }
246
247 @end
Something went wrong with that request. Please try again.