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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/* HTML Generator
** ==============
**
** This version of the HTML object sends HTML markup to the output stream.
**
** Bugs: Line wrapping is not done at all.
** All data handled as PCDATA.
** Should convert old XMP, LISTING and PLAINTEXT to PRE.
**
** It is not obvious to me right now whether the HEAD should be generated
** from the incomming data or the anchor. Currently it is from the former
** which is cleanest.
*/
#include "HTUtils.h"
#include "tcp.h"
#define BUFFER_SIZE 80 /* Line buffer attempts to make neat breaks */
/* Implements:
*/
#include "HTMLGen.h"
#include <stdio.h>
#include "HTMLDTD.h"
#include "HTStream.h"
#include "SGML.h"
#include "HTFormat.h"
#include "LYLeaks.h"
#define FREE(x) if (x) {free(x); x = NULL;}
#define PUTC(c) (*me->targetClass.put_character)(me->target, c)
/* #define PUTS(s) (*me->targetClass.put_string)(me->target, s) */
#define PUTB(s,l) (*me->targetClass.put_block)(me->target, s, l)
/* HTML Object
** -----------
*/
struct _HTStream {
CONST HTStreamClass * isa;
HTStream * target;
HTStreamClass targetClass; /* COPY for speed */
};
struct _HTStructured {
CONST HTStructuredClass * isa;
HTStream * target;
HTStreamClass targetClass; /* COPY for speed */
char buffer[BUFFER_SIZE+1]; /* 1for NL */
char * write_pointer;
char * line_break;
int cleanness;
BOOL delete_line_break_char;
BOOL preformatted;
};
/* Flush Buffer
** ------------
*/
PRIVATE void HTMLGen_flush ARGS1(
HTStructured *, me)
{
(*me->targetClass.put_block)(me->target,
me->buffer,
me->write_pointer - me->buffer);
me->write_pointer = me->buffer;
me->line_break = me->buffer;
me->cleanness = 0;
me->delete_line_break_char = NO;
}
/* Character handling
** ------------------
**
** The tricky bits are the line break handling. This attempts
** to synchrononise line breaks on sentence or phrase ends. This
** is important if one stores SGML files in a line-oriented code
** repository, so that if a small change is made, line ends don't
** shift in a ripple-through to apparently change a large part of the
** file. We give extra "cleanness" to spaces appearing directly
** after periods (full stops), [semi]colons and commas.
** This should make the source files easier to read and modify
** by hand, too, though this is not a primary design consideration.
*/
PRIVATE void HTMLGen_put_character ARGS2(
HTStructured *, me,
char, c)
{
*me->write_pointer++ = c;
if (c == '\n') {
HTMLGen_flush(me);
return;
}
if ((!me->preformatted && c == ' ')) {
int new_cleanness = 1;
if (me->write_pointer > (me->buffer + 1)) {
char delims[5];
char * p;
strcpy(delims, ",;:."); /* @@ english bias */
p = strchr(delims, me->write_pointer[-2]);
if (p) new_cleanness = p - delims + 2;
}
if (new_cleanness >= me->cleanness) {
me->line_break = me->write_pointer - 1; /* Point to space */
me->cleanness = new_cleanness;
me->delete_line_break_char = YES;
}
}
/*
* Flush buffer out when full.
*/
if (me->write_pointer == me->buffer + BUFFER_SIZE) {
if (me->cleanness) {
char line_break_char = me->line_break[0];
char * saved = me->line_break;
if (me->delete_line_break_char) saved++;
me->line_break[0] = '\n';
(*me->targetClass.put_block)(me->target,
me->buffer,
me->line_break - me->buffer + 1);
me->line_break[0] = line_break_char;
{ /* move next line in */
char * p = saved;
char *q;
for (q = me->buffer; p < me->write_pointer; )
*q++ = *p++;
}
me->cleanness = 0;
me->delete_line_break_char = 0;
me->write_pointer = me->write_pointer - (saved-me->buffer);
} else {
(*me->targetClass.put_block)(me->target,
me->buffer,
BUFFER_SIZE);
me->write_pointer = me->buffer;
}
me->line_break = me->buffer;
}
}
/* String handling
** ---------------
*/
PRIVATE void HTMLGen_put_string ARGS2(
HTStructured *, me,
CONST char *, s)
{
CONST char * p;
for (p = s; *p; p++)
HTMLGen_put_character(me, *p);
}
PRIVATE void HTMLGen_write ARGS3(
HTStructured *, me,
CONST char *, s,
int, l)
{
CONST char * p;
for (p = s; p < (s + l); p++)
HTMLGen_put_character(me, *p);
}
/* Start Element
** -------------
**
** Within the opening tag, there may be spaces
** and the line may be broken at these spaces.
*/
PRIVATE void HTMLGen_start_element ARGS5(
HTStructured *, me,
int, element_number,
CONST BOOL*, present,
CONST char **, value,
char **, insert)
{
int i;
BOOL was_preformatted = me->preformatted;
HTTag * tag = &HTML_dtd.tags[element_number];
me->preformatted = NO; /* free text within tags */
HTMLGen_put_character(me, '<');
HTMLGen_put_string(me, tag->name);
if (present) {
for (i = 0; i < tag->number_of_attributes; i++) {
if (present[i]) {
HTMLGen_put_character(me, ' ');
HTMLGen_put_string(me, tag->attributes[i].name);
if (value[i]) {
HTMLGen_put_string(me, "=\"");
HTMLGen_put_string(me, value[i]);
HTMLGen_put_character(me, '"');
}
}
}
}
HTMLGen_put_string(me, ">"); /* got rid of \n LJM */
/*
* Make very specific HTML assumption that PRE can't be nested!
*/
me->preformatted = (element_number == HTML_PRE) ? YES : was_preformatted;
/*
* Can break after element start.
*/
if (!me->preformatted && tag->contents != SGML_EMPTY) {
me->line_break = me->write_pointer; /* Don't you hate SGML? */
me->cleanness = 1;
me->delete_line_break_char = NO;
}
}
/* End Element
** -----------
**
*/
/* When we end an element, the style must be returned to that
** in effect before that element. Note that anchors (etc?)
** don't have an associated style, so that we must scan down the
** stack for an element with a defined style. (In fact, the styles
** should be linked to the whole stack not just the top one.)
** TBL 921119
*/
PRIVATE void HTMLGen_end_element ARGS3(
HTStructured *, me,
int, element_number,
char **, insert)
{
if (!me->preformatted &&
HTML_dtd.tags[element_number].contents != SGML_EMPTY) {
/*
* Can break before element end.
*/
me->line_break = me->write_pointer; /* Don't you hate SGML? */
me->cleanness = 1;
me->delete_line_break_char = NO;
}
HTMLGen_put_string(me, "</");
HTMLGen_put_string(me, HTML_dtd.tags[element_number].name);
HTMLGen_put_character(me, '>');
if (element_number == HTML_PRE) {
me->preformatted = NO;
}
}
/* Expanding entities
** ------------------
**
*/
PRIVATE int HTMLGen_put_entity ARGS2(
HTStructured *, me,
int, entity_number)
{
int nent = HTML_dtd.number_of_entities;
HTMLGen_put_character(me, '&');
if (entity_number < nent)
HTMLGen_put_string(me, HTML_dtd.entity_names[entity_number]);
#ifdef EXP_CHARTRANS
else
HTMLGen_put_string(me,
HTML_dtd.extra_entity_info[entity_number-nent].name);
#endif
HTMLGen_put_character(me, ';');
return HT_OK;
}
/* Free an HTML object
** -------------------
**
*/
PRIVATE void HTMLGen_free ARGS1(
HTStructured *, me)
{
(*me->targetClass.put_character)(me->target, '\n');
HTMLGen_flush(me);
(*me->targetClass._free)(me->target); /* ripple through */
FREE(me);
}
PRIVATE void PlainToHTML_free ARGS1(
HTStructured *, me)
{
HTMLGen_end_element(me, HTML_PRE, 0);
HTMLGen_free(me);
}
PRIVATE void HTMLGen_abort ARGS2(
HTStructured *, me,
HTError, e)
{
HTMLGen_free(me);
}
PRIVATE void PlainToHTML_abort ARGS2(
HTStructured *, me,
HTError, e)
{
PlainToHTML_free(me);
}
/* Structured Object Class
** -----------------------
*/
PRIVATE CONST HTStructuredClass HTMLGeneration = /* As opposed to print etc */
{
"text/html",
HTMLGen_free,
HTMLGen_abort,
HTMLGen_put_character, HTMLGen_put_string, HTMLGen_write,
HTMLGen_start_element, HTMLGen_end_element,
HTMLGen_put_entity
};
/* Subclass-specific Methods
** -------------------------
*/
PUBLIC HTStructured * HTMLGenerator ARGS1(
HTStream *, output)
{
HTStructured* me = (HTStructured*)malloc(sizeof(*me));
if (me == NULL)
outofmem(__FILE__, "HTMLGenerator");
me->isa = &HTMLGeneration;
me->target = output;
me->targetClass = *me->target->isa; /* Copy pointers to routines for speed*/
me->write_pointer = me->buffer;
me->line_break = me->buffer;
me->cleanness = 0;
me->delete_line_break_char = NO;
me->preformatted = NO;
return me;
}
/* Stream Object Class
** -------------------
**
** This object just converts a plain text stream into HTML
** It is officially a structured strem but only the stream bits exist.
** This is just the easiest way of typecasting all the routines.
*/
PRIVATE CONST HTStructuredClass PlainToHTMLConversion =
{
"plaintexttoHTML",
HTMLGen_free,
PlainToHTML_abort,
HTMLGen_put_character,
HTMLGen_put_string,
HTMLGen_write,
NULL, /* Structured stuff */
NULL,
NULL
};
/* HTConverter from plain text to HTML Stream
** ------------------------------------------
*/
PUBLIC HTStream* HTPlainToHTML ARGS3(
HTPresentation *, pres,
HTParentAnchor *, anchor,
HTStream *, sink)
{
HTStructured* me = (HTStructured*)malloc(sizeof(*me));
if (me == NULL)
outofmem(__FILE__, "PlainToHTML");
me->isa = (HTStructuredClass*) &PlainToHTMLConversion;
/*
* Copy pointers to routines for speed.
*/
me->target = sink;
me->targetClass = *me->target->isa;
HTMLGen_put_string(me, "<HTML>\n<BODY>\n<PRE>\n");
me->preformatted = YES;
return (HTStream*) me;
}
|