| 1 |
/* |
| 2 |
* $LynxId: HTMLGen.c,v 1.48 2025/01/06 18:02:15 tom Exp $ |
| 3 |
* |
| 4 |
* HTML Generator |
| 5 |
* ============== |
| 6 |
* |
| 7 |
* This version of the HTML object sends HTML markup to the output stream. |
| 8 |
* |
| 9 |
* Bugs: Line wrapping is not done at all. |
| 10 |
* All data handled as PCDATA. |
| 11 |
* Should convert old XMP, LISTING and PLAINTEXT to PRE. |
| 12 |
* |
| 13 |
* It is not obvious to me right now whether the HEAD should be generated |
| 14 |
* from the incoming data or the anchor. Currently it is from the former |
| 15 |
* which is cleanest. |
| 16 |
*/ |
| 17 |
|
| 18 |
#define HTSTREAM_INTERNAL 1 |
| 19 |
|
| 20 |
#include <HTUtils.h> |
| 21 |
|
| 22 |
#define BUFFER_SIZE 200 /* Line buffer attempts to make neat breaks */ |
| 23 |
#define MAX_CLEANNESS 20 |
| 24 |
|
| 25 |
/* Implements: |
| 26 |
*/ |
| 27 |
#include <HTMLGen.h> |
| 28 |
|
| 29 |
#include <HTMLDTD.h> |
| 30 |
#include <HTStream.h> |
| 31 |
#include <SGML.h> |
| 32 |
#include <HTFormat.h> |
| 33 |
|
| 34 |
#ifdef USE_COLOR_STYLE |
| 35 |
#include <LYCharUtils.h> |
| 36 |
#include <AttrList.h> |
| 37 |
#include <LYHash.h> |
| 38 |
#include <LYStyle.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <LYGlobalDefs.h> |
| 42 |
#include <LYCurses.h> |
| 43 |
#include <LYLeaks.h> |
| 44 |
|
| 45 |
#ifdef USE_COLOR_STYLE |
| 46 |
char class_string[TEMPSTRINGSIZE + 1]; |
| 47 |
|
| 48 |
static char *Style_className = NULL; |
| 49 |
static int hcode; |
| 50 |
#endif |
| 51 |
|
| 52 |
/* HTML Object |
| 53 |
* ----------- |
| 54 |
*/ |
| 55 |
struct _HTStream { |
| 56 |
const HTStreamClass *isa; |
| 57 |
HTStream *target; |
| 58 |
HTStreamClass targetClass; /* COPY for speed */ |
| 59 |
}; |
| 60 |
|
| 61 |
struct _HTStructured { |
| 62 |
const HTStructuredClass *isa; |
| 63 |
HTStream *target; |
| 64 |
HTStreamClass targetClass; /* COPY for speed */ |
| 65 |
|
| 66 |
char buffer[BUFFER_SIZE + 1]; /* 1for NL */ |
| 67 |
int buffer_maxchars; |
| 68 |
char *write_pointer; |
| 69 |
char *line_break[MAX_CLEANNESS + 1]; |
| 70 |
int cleanness; |
| 71 |
BOOL overflowed; |
| 72 |
BOOL delete_line_break_char[MAX_CLEANNESS + 1]; |
| 73 |
BOOL preformatted; |
| 74 |
BOOL escape_specials; |
| 75 |
BOOL in_attrval; |
| 76 |
#ifdef USE_COLOR_STYLE |
| 77 |
HText *text; |
| 78 |
#endif |
| 79 |
}; |
| 80 |
|
| 81 |
/* Flush Buffer |
| 82 |
* ------------ |
| 83 |
*/ |
| 84 |
|
| 85 |
static void flush_breaks(HTStructured * me) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
|
| 89 |
for (i = 0; i <= MAX_CLEANNESS; i++) { |
| 90 |
me->line_break[i] = NULL; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static void HTMLGen_flush(HTStructured * me) |
| 95 |
{ |
| 96 |
(*me->targetClass.put_block) (me->target, |
| 97 |
me->buffer, |
| 98 |
(int) (me->write_pointer - me->buffer)); |
| 99 |
me->write_pointer = me->buffer; |
| 100 |
flush_breaks(me); |
| 101 |
me->cleanness = 0; |
| 102 |
me->delete_line_break_char[0] = NO; |
| 103 |
} |
| 104 |
|
| 105 |
#ifdef USE_COLOR_STYLE |
| 106 |
/* |
| 107 |
* We need to flush our buffer each time before we effect a color style change, |
| 108 |
* this also relies on the subsequent stage not doing any buffering - this is |
| 109 |
* currently true, in cases where it matters the target stream should be the |
| 110 |
* HTPlain converter. The flushing currently prevents reasonable line breaking |
| 111 |
* in lines with tags. Since color styles help visual scanning of displayed |
| 112 |
* source lines, and long lines are wrapped in GridText anyway, this is |
| 113 |
* probably acceptable (or even A Good Thing - more to see in one screenful). |
| 114 |
* The pointer to the HText structure is initialized here before we effect the |
| 115 |
* first style change. Getting it from the global HTMainText variable isn't |
| 116 |
* very clean, since it relies on the fact that HText_new() has already been |
| 117 |
* called for the current stream stack's document by the time we start |
| 118 |
* processing the first element; we rely on HTMLGenerator's callers |
| 119 |
* (HTMLParsedPresent in particular) to guarantee this when it matters. |
| 120 |
* Normally the target stream will have been setup by HTPlainPresent, which |
| 121 |
* does what we need in this respect. (A check whether we have the right |
| 122 |
* output stream could be done by checking that targetClass.name is |
| 123 |
* "PlainPresenter" or similar.) |
| 124 |
* |
| 125 |
* All special color style handling is only done if LYPreparsedSource is set. |
| 126 |
* We could always do it for displaying source generated by an internal |
| 127 |
* gateway, but this makes the rule more simple for the user: color styles are |
| 128 |
* applied to html source only with the -preparsed flag. - kw |
| 129 |
*/ |
| 130 |
static void do_cstyle_flush(HTStructured * me) |
| 131 |
{ |
| 132 |
if (!me->text && LYPreparsedSource) { |
| 133 |
me->text = HTMainText; |
| 134 |
} |
| 135 |
if (me->text) { |
| 136 |
HTMLGen_flush(me); |
| 137 |
} |
| 138 |
} |
| 139 |
#endif /* COLOR_STYLE */ |
| 140 |
|
| 141 |
/* Weighted optional line break |
| 142 |
* |
| 143 |
* We keep track of all the breaks for when we chop the line |
| 144 |
*/ |
| 145 |
|
| 146 |
static void allow_break(HTStructured * me, int new_cleanness, int dlbc) |
| 147 |
{ |
| 148 |
if (dlbc && me->write_pointer == me->buffer) |
| 149 |
dlbc = NO; |
| 150 |
me->line_break[new_cleanness] = |
| 151 |
dlbc ? me->write_pointer - 1 /* Point to space */ |
| 152 |
: me->write_pointer; /* point to gap */ |
| 153 |
me->delete_line_break_char[new_cleanness] = (BOOLEAN) dlbc; |
| 154 |
if (new_cleanness >= me->cleanness && |
| 155 |
(me->overflowed || me->line_break[new_cleanness] > me->buffer)) |
| 156 |
me->cleanness = new_cleanness; |
| 157 |
} |
| 158 |
|
| 159 |
/* Character handling |
| 160 |
* ------------------ |
| 161 |
* |
| 162 |
* The tricky bits are the line break handling. This attempts |
| 163 |
* to synchronize line breaks on sentence or phrase ends. This |
| 164 |
* is important if one stores SGML files in a line-oriented code |
| 165 |
* repository, so that if a small change is made, line ends don't |
| 166 |
* shift in a ripple-through to apparently change a large part of the |
| 167 |
* file. We give extra "cleanness" to spaces appearing directly |
| 168 |
* after periods (full stops), [semi]colons and commas. |
| 169 |
* This should make the source files easier to read and modify |
| 170 |
* by hand, too, though this is not a primary design consideration. TBL |
| 171 |
*/ |
| 172 |
static void HTMLGen_put_character(HTStructured * me, int c) |
| 173 |
{ |
| 174 |
if (me->escape_specials && UCH(c) < 32) { |
| 175 |
if (c == HT_NON_BREAK_SPACE || c == HT_EN_SPACE || |
| 176 |
c == LY_SOFT_HYPHEN) { /* recursion... */ |
| 177 |
HTMLGen_put_character(me, '&'); |
| 178 |
HTMLGen_put_character(me, '#'); |
| 179 |
HTMLGen_put_character(me, 'x'); |
| 180 |
switch (c) { |
| 181 |
case HT_NON_BREAK_SPACE: /*   */ |
| 182 |
HTMLGen_put_character(me, 'A'); |
| 183 |
HTMLGen_put_character(me, '0'); |
| 184 |
break; |
| 185 |
case HT_EN_SPACE: /*   */ |
| 186 |
HTMLGen_put_character(me, '2'); |
| 187 |
HTMLGen_put_character(me, '0'); |
| 188 |
HTMLGen_put_character(me, '0'); |
| 189 |
HTMLGen_put_character(me, '2'); |
| 190 |
break; |
| 191 |
case LY_SOFT_HYPHEN: /* ­ */ |
| 192 |
HTMLGen_put_character(me, 'A'); |
| 193 |
HTMLGen_put_character(me, 'D'); |
| 194 |
break; |
| 195 |
} |
| 196 |
c = ';'; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
*me->write_pointer++ = (char) c; |
| 201 |
|
| 202 |
if (c == '\n') { |
| 203 |
HTMLGen_flush(me); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
/* Figure our whether we can break at this point |
| 208 |
*/ |
| 209 |
if ((!me->preformatted && (c == ' ' || c == '\t'))) { |
| 210 |
int new_cleanness = 3; |
| 211 |
|
| 212 |
if (me->write_pointer > (me->buffer + 1)) { |
| 213 |
char delims[5]; |
| 214 |
char *p; |
| 215 |
|
| 216 |
strcpy(delims, ",;:."); /* @@ english bias */ |
| 217 |
p = StrChr(delims, me->write_pointer[-2]); |
| 218 |
if (p) |
| 219 |
new_cleanness = (int) (p - delims + 6); |
| 220 |
if (!me->in_attrval) |
| 221 |
new_cleanness += 10; |
| 222 |
} |
| 223 |
allow_break(me, new_cleanness, YES); |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* Flush buffer out when full, or whenever the line is over the nominal |
| 228 |
* maximum and we can break at all |
| 229 |
*/ |
| 230 |
if (me->write_pointer >= me->buffer + me->buffer_maxchars || |
| 231 |
(me->overflowed && me->cleanness)) { |
| 232 |
if (me->cleanness) { |
| 233 |
char line_break_char = me->line_break[me->cleanness][0]; |
| 234 |
char *saved = me->line_break[me->cleanness]; |
| 235 |
|
| 236 |
if (me->delete_line_break_char[me->cleanness]) |
| 237 |
saved++; |
| 238 |
me->line_break[me->cleanness][0] = '\n'; |
| 239 |
(*me->targetClass.put_block) (me->target, |
| 240 |
me->buffer, |
| 241 |
(int) (me->line_break[me->cleanness] - |
| 242 |
me->buffer + 1)); |
| 243 |
me->line_break[me->cleanness][0] = line_break_char; |
| 244 |
{ /* move next line in */ |
| 245 |
char *p = saved; |
| 246 |
char *q; |
| 247 |
|
| 248 |
for (q = me->buffer; p < me->write_pointer;) |
| 249 |
*q++ = *p++; |
| 250 |
} |
| 251 |
me->cleanness = 0; |
| 252 |
/* Now we have to check whether there are any perfectly good breaks |
| 253 |
* which weren't good enough for the last line but may be good |
| 254 |
* enough for the next |
| 255 |
*/ |
| 256 |
{ |
| 257 |
int i; |
| 258 |
|
| 259 |
for (i = 0; i <= MAX_CLEANNESS; i++) { |
| 260 |
if (me->line_break[i] != NULL && |
| 261 |
me->line_break[i] > saved) { |
| 262 |
me->line_break[i] = me->line_break[i] - |
| 263 |
(saved - me->buffer); |
| 264 |
me->cleanness = i; |
| 265 |
} else { |
| 266 |
me->line_break[i] = NULL; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
me->delete_line_break_char[0] = 0; |
| 272 |
me->write_pointer = me->write_pointer - (saved - me->buffer); |
| 273 |
me->overflowed = NO; |
| 274 |
|
| 275 |
} else { |
| 276 |
(*me->targetClass.put_block) (me->target, |
| 277 |
me->buffer, |
| 278 |
me->buffer_maxchars); |
| 279 |
me->write_pointer = me->buffer; |
| 280 |
flush_breaks(me); |
| 281 |
me->overflowed = YES; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/* String handling |
| 287 |
* --------------- |
| 288 |
*/ |
| 289 |
static void HTMLGen_put_string(HTStructured * me, const char *s) |
| 290 |
{ |
| 291 |
const char *p; |
| 292 |
|
| 293 |
for (p = s; *p; p++) |
| 294 |
HTMLGen_put_character(me, *p); |
| 295 |
} |
| 296 |
|
| 297 |
static void HTMLGen_write(HTStructured * me, const char *s, |
| 298 |
int l) |
| 299 |
{ |
| 300 |
const char *p; |
| 301 |
|
| 302 |
for (p = s; p < (s + l); p++) |
| 303 |
HTMLGen_put_character(me, *p); |
| 304 |
} |
| 305 |
|
| 306 |
/* Start Element |
| 307 |
* ------------- |
| 308 |
* |
| 309 |
* Within the opening tag, there may be spaces and the line may be broken at |
| 310 |
* these spaces. |
| 311 |
*/ |
| 312 |
static int HTMLGen_start_element(HTStructured * me, int element_number, |
| 313 |
const BOOL *present, |
| 314 |
STRING2PTR value, |
| 315 |
int charset GCC_UNUSED, |
| 316 |
char **insert GCC_UNUSED) |
| 317 |
{ |
| 318 |
int i; |
| 319 |
BOOL was_preformatted = me->preformatted; |
| 320 |
HTTag *tag = &HTML_dtd.tags[element_number]; |
| 321 |
|
| 322 |
#if defined(USE_COLOR_STYLE) |
| 323 |
char *title = NULL; |
| 324 |
char *title_tmp = NULL; |
| 325 |
const char *name; |
| 326 |
|
| 327 |
if (LYPreparsedSource && (name = tag->name) != NULL) { |
| 328 |
char *myHash = NULL; |
| 329 |
|
| 330 |
/* |
| 331 |
* Same logic as in HTML_start_element, copied from there. - kw |
| 332 |
*/ |
| 333 |
HTSprintf(&Style_className, ";%s", name); |
| 334 |
StrAllocCopy(myHash, name); |
| 335 |
if (class_string[0]) { |
| 336 |
StrAllocCat(myHash, "."); |
| 337 |
StrAllocCat(myHash, class_string); |
| 338 |
HTSprintf(&Style_className, ".%s", class_string); |
| 339 |
} |
| 340 |
class_string[0] = '\0'; |
| 341 |
strtolower(myHash); |
| 342 |
hcode = color_style_1(myHash); |
| 343 |
strtolower(Style_className); |
| 344 |
|
| 345 |
if (TRACE_STYLE) { |
| 346 |
fprintf(tfp, "CSSTRIM:%s -> %d", myHash, hcode); |
| 347 |
if (!hashStyles[hcode].used) { |
| 348 |
char *rp = strrchr(myHash, '.'); |
| 349 |
|
| 350 |
fprintf(tfp, " (undefined) %s\n", myHash); |
| 351 |
if (rp) { |
| 352 |
int hcd; |
| 353 |
|
| 354 |
*rp = '\0'; /* trim the class */ |
| 355 |
hcd = color_style_1(myHash); |
| 356 |
fprintf(tfp, "CSS:%s -> %d", myHash, hcd); |
| 357 |
if (!hashStyles[hcd].used) |
| 358 |
fprintf(tfp, " (undefined) %s\n", myHash); |
| 359 |
else |
| 360 |
fprintf(tfp, " ca=%d\n", hashStyles[hcd].color); |
| 361 |
} |
| 362 |
} else |
| 363 |
fprintf(tfp, " ca=%d\n", hashStyles[hcode].color); |
| 364 |
} |
| 365 |
|
| 366 |
if (displayStyles[element_number + STARTAT].color > -2) { |
| 367 |
CTRACE2(TRACE_STYLE, |
| 368 |
(tfp, "CSSTRIM: start_element: top <%s>\n", |
| 369 |
tag->name)); |
| 370 |
do_cstyle_flush(me); |
| 371 |
HText_characterStyle(me->text, hcode, 1); |
| 372 |
} |
| 373 |
FREE(myHash); |
| 374 |
} |
| 375 |
#endif /* USE_COLOR_STYLE */ |
| 376 |
me->preformatted = YES; /* free text within tags */ |
| 377 |
HTMLGen_put_character(me, '<'); |
| 378 |
HTMLGen_put_string(me, tag->name); |
| 379 |
if (present) { |
| 380 |
BOOL had_attr = NO; |
| 381 |
|
| 382 |
for (i = 0; i < tag->number_of_attributes; i++) { |
| 383 |
if (present[i]) { |
| 384 |
had_attr = YES; |
| 385 |
HTMLGen_put_character(me, ' '); |
| 386 |
allow_break(me, 11, YES); |
| 387 |
#ifdef USE_COLOR_STYLE |
| 388 |
/* |
| 389 |
* Try to mimic HTML_start_element's special handling for |
| 390 |
* HTML_LINK. If applicable, color the displayed attribute / |
| 391 |
* value pairs differently. - kw |
| 392 |
*/ |
| 393 |
if (LYPreparsedSource && |
| 394 |
element_number == HTML_LINK && !title && |
| 395 |
present[HTML_LINK_CLASS] && *value[HTML_LINK_CLASS] && |
| 396 |
!present[HTML_LINK_REV] && |
| 397 |
(present[HTML_LINK_REL] || present[HTML_LINK_HREF])) { |
| 398 |
if (present[HTML_LINK_TITLE] && *value[HTML_LINK_TITLE]) { |
| 399 |
StrAllocCopy(title, value[HTML_LINK_TITLE]); |
| 400 |
LYTrimHead(title); |
| 401 |
LYTrimTail(title); |
| 402 |
} |
| 403 |
if ((!title || *title == '\0') && present[HTML_LINK_REL]) { |
| 404 |
StrAllocCopy(title, value[HTML_LINK_REL]); |
| 405 |
} |
| 406 |
if (title && *title) { |
| 407 |
HTSprintf0(&title_tmp, "link.%s.%s", |
| 408 |
value[HTML_LINK_CLASS], title); |
| 409 |
CTRACE2(TRACE_STYLE, |
| 410 |
(tfp, "CSSTRIM:link=%s\n", title_tmp)); |
| 411 |
|
| 412 |
do_cstyle_flush(me); |
| 413 |
HText_characterStyle(me->text, |
| 414 |
color_style_1(title_tmp), 1); |
| 415 |
} |
| 416 |
} |
| 417 |
#endif |
| 418 |
HTMLGen_put_string(me, tag->attributes[i].name); |
| 419 |
if (value[i]) { |
| 420 |
me->preformatted = was_preformatted; |
| 421 |
me->in_attrval = YES; |
| 422 |
if (StrChr(value[i], '"') == NULL) { |
| 423 |
HTMLGen_put_string(me, "=\""); |
| 424 |
HTMLGen_put_string(me, value[i]); |
| 425 |
HTMLGen_put_character(me, '"'); |
| 426 |
} else if (StrChr(value[i], '\'') == NULL) { |
| 427 |
HTMLGen_put_string(me, "='"); |
| 428 |
HTMLGen_put_string(me, value[i]); |
| 429 |
HTMLGen_put_character(me, '\''); |
| 430 |
} else { /* attribute value has both kinds of quotes */ |
| 431 |
const char *p; |
| 432 |
|
| 433 |
HTMLGen_put_string(me, "=\""); |
| 434 |
for (p = value[i]; *p; p++) { |
| 435 |
if (*p != '"') { |
| 436 |
HTMLGen_put_character(me, *p); |
| 437 |
} else { |
| 438 |
HTMLGen_put_string(me, """); |
| 439 |
} |
| 440 |
} |
| 441 |
HTMLGen_put_character(me, '"'); |
| 442 |
} |
| 443 |
me->preformatted = YES; |
| 444 |
me->in_attrval = NO; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
#ifdef USE_COLOR_STYLE |
| 449 |
if (had_attr && LYPreparsedSource && element_number == HTML_LINK) { |
| 450 |
/* |
| 451 |
* Clean up after special HTML_LINK handling - kw |
| 452 |
*/ |
| 453 |
if (title && *title) { |
| 454 |
do_cstyle_flush(me); |
| 455 |
HText_characterStyle(me->text, color_style_1(title_tmp), 0); |
| 456 |
FREE(title_tmp); |
| 457 |
} |
| 458 |
FREE(title); |
| 459 |
} |
| 460 |
#endif |
| 461 |
if (had_attr) |
| 462 |
allow_break(me, 12, NO); |
| 463 |
} |
| 464 |
HTMLGen_put_string(me, ">"); /* got rid of \n LJM */ |
| 465 |
|
| 466 |
/* |
| 467 |
* Make very specific HTML assumption that PRE can't be nested! |
| 468 |
*/ |
| 469 |
me->preformatted = (BOOL) ((element_number == HTML_PRE |
| 470 |
|| element_number == HTML_XMP) |
| 471 |
? YES |
| 472 |
: was_preformatted); |
| 473 |
|
| 474 |
/* |
| 475 |
* Can break after element start. |
| 476 |
*/ |
| 477 |
if (!me->preformatted && tag->contents != SGML_EMPTY) { |
| 478 |
if (tag->contents == SGML_ELEMENT) |
| 479 |
allow_break(me, 15, NO); |
| 480 |
else |
| 481 |
allow_break(me, 2, NO); |
| 482 |
} |
| 483 |
#if defined(USE_COLOR_STYLE) |
| 484 |
/* |
| 485 |
* Same logic as in HTML_start_element, copied from there. - kw |
| 486 |
*/ |
| 487 |
|
| 488 |
/* end really empty tags straight away */ |
| 489 |
if (LYPreparsedSource && ReallyEmptyTagNum(element_number)) { |
| 490 |
CTRACE2(TRACE_STYLE, |
| 491 |
(tfp, "STYLE:begin_element:ending EMPTY element style\n")); |
| 492 |
do_cstyle_flush(me); |
| 493 |
HText_characterStyle(me->text, hcode, STACK_OFF); |
| 494 |
TrimColorClass(tag->name, Style_className, &hcode); |
| 495 |
} |
| 496 |
#endif /* USE_COLOR_STYLE */ |
| 497 |
if (element_number == HTML_OBJECT && tag->contents == SGML_LITTERAL) { |
| 498 |
/* |
| 499 |
* These conditions only approximate the ones used in HTML.c. Let our |
| 500 |
* SGML parser know that further content is to be parsed normally not |
| 501 |
* literally. - kw |
| 502 |
*/ |
| 503 |
if (!present) { |
| 504 |
return HT_PARSER_OTHER_CONTENT; |
| 505 |
} else if (!present[HTML_OBJECT_DECLARE] && |
| 506 |
!(present[HTML_OBJECT_NAME] && |
| 507 |
value[HTML_OBJECT_NAME] && *value[HTML_OBJECT_NAME])) { |
| 508 |
if (present[HTML_OBJECT_SHAPES] || |
| 509 |
!(present[HTML_OBJECT_USEMAP] && |
| 510 |
value[HTML_OBJECT_USEMAP] && *value[HTML_OBJECT_USEMAP])) |
| 511 |
return HT_PARSER_OTHER_CONTENT; |
| 512 |
} |
| 513 |
} |
| 514 |
return HT_OK; |
| 515 |
} |
| 516 |
|
| 517 |
/* End Element |
| 518 |
* ----------- |
| 519 |
* |
| 520 |
* When we end an element, the style must be returned to that in effect before |
| 521 |
* that element. Note that anchors (etc?) don't have an associated style, so |
| 522 |
* that we must scan down the stack for an element with a defined style. (In |
| 523 |
* fact, the styles should be linked to the whole stack not just the top one.) |
| 524 |
* TBL 921119 |
| 525 |
*/ |
| 526 |
static int HTMLGen_end_element(HTStructured * me, int element_number, |
| 527 |
char **insert GCC_UNUSED) |
| 528 |
{ |
| 529 |
if (!me->preformatted && |
| 530 |
HTML_dtd.tags[element_number].contents != SGML_EMPTY) { |
| 531 |
/* |
| 532 |
* Can break before element end. |
| 533 |
*/ |
| 534 |
if (HTML_dtd.tags[element_number].contents == SGML_ELEMENT) |
| 535 |
allow_break(me, 14, NO); |
| 536 |
else |
| 537 |
allow_break(me, 1, NO); |
| 538 |
} |
| 539 |
HTMLGen_put_string(me, "</"); |
| 540 |
HTMLGen_put_string(me, HTML_dtd.tags[element_number].name); |
| 541 |
HTMLGen_put_character(me, '>'); |
| 542 |
if (element_number == HTML_PRE || element_number == HTML_XMP) { |
| 543 |
me->preformatted = NO; |
| 544 |
} |
| 545 |
#ifdef USE_COLOR_STYLE |
| 546 |
/* |
| 547 |
* Same logic as in HTML_end_element, copied from there. - kw |
| 548 |
*/ |
| 549 |
TrimColorClass(HTML_dtd.tags[element_number].name, |
| 550 |
Style_className, &hcode); |
| 551 |
|
| 552 |
if (LYPreparsedSource && !ReallyEmptyTagNum(element_number)) { |
| 553 |
CTRACE2(TRACE_STYLE, |
| 554 |
(tfp, "STYLE:end_element: ending non-EMPTY style\n")); |
| 555 |
do_cstyle_flush(me); |
| 556 |
HText_characterStyle(me->text, hcode, STACK_OFF); |
| 557 |
} |
| 558 |
#endif /* USE_COLOR_STYLE */ |
| 559 |
return HT_OK; |
| 560 |
} |
| 561 |
|
| 562 |
/* Expanding entities |
| 563 |
* ------------------ |
| 564 |
* |
| 565 |
*/ |
| 566 |
static int HTMLGen_put_entity(HTStructured * me, int entity_number) |
| 567 |
{ |
| 568 |
int nent = (int) HTML_dtd.number_of_entities; |
| 569 |
|
| 570 |
HTMLGen_put_character(me, '&'); |
| 571 |
if (entity_number < nent) { |
| 572 |
HTMLGen_put_string(me, HTML_dtd.entity_names[entity_number]); |
| 573 |
} |
| 574 |
HTMLGen_put_character(me, ';'); |
| 575 |
return HT_OK; |
| 576 |
} |
| 577 |
|
| 578 |
/* Free an HTML object |
| 579 |
* ------------------- |
| 580 |
* |
| 581 |
*/ |
| 582 |
static void HTMLGen_free(HTStructured * me) |
| 583 |
{ |
| 584 |
(*me->targetClass.put_character) (me->target, '\n'); |
| 585 |
HTMLGen_flush(me); |
| 586 |
(*me->targetClass._free) (me->target); /* ripple through */ |
| 587 |
#ifdef USE_COLOR_STYLE |
| 588 |
FREE(Style_className); |
| 589 |
#endif |
| 590 |
FREE(me); |
| 591 |
} |
| 592 |
|
| 593 |
static void PlainToHTML_free(HTStructured * me) |
| 594 |
{ |
| 595 |
HTMLGen_end_element(me, HTML_PRE, NULL); |
| 596 |
HTMLGen_free(me); |
| 597 |
} |
| 598 |
|
| 599 |
static void HTMLGen_abort(HTStructured * me, HTError e GCC_UNUSED) |
| 600 |
{ |
| 601 |
HTMLGen_free(me); |
| 602 |
#ifdef USE_COLOR_STYLE |
| 603 |
FREE(Style_className); |
| 604 |
#endif |
| 605 |
} |
| 606 |
|
| 607 |
static void PlainToHTML_abort(HTStructured * me, HTError e GCC_UNUSED) |
| 608 |
{ |
| 609 |
PlainToHTML_free(me); |
| 610 |
} |
| 611 |
|
| 612 |
/* Structured Object Class |
| 613 |
* ----------------------- |
| 614 |
*/ |
| 615 |
static const HTStructuredClass HTMLGeneration = /* As opposed to print etc */ |
| 616 |
{ |
| 617 |
"HTMLGen", |
| 618 |
HTMLGen_free, |
| 619 |
HTMLGen_abort, |
| 620 |
HTMLGen_put_character, HTMLGen_put_string, HTMLGen_write, |
| 621 |
HTMLGen_start_element, HTMLGen_end_element, |
| 622 |
HTMLGen_put_entity |
| 623 |
}; |
| 624 |
|
| 625 |
/* Subclass-specific Methods |
| 626 |
* ------------------------- |
| 627 |
*/ |
| 628 |
HTStructured *HTMLGenerator(HTStream *output) |
| 629 |
{ |
| 630 |
HTStructured *me = (HTStructured *) malloc(sizeof(*me)); |
| 631 |
|
| 632 |
if (me == NULL) |
| 633 |
outofmem(__FILE__, "HTMLGenerator"); |
| 634 |
|
| 635 |
me->isa = &HTMLGeneration; |
| 636 |
|
| 637 |
me->target = output; |
| 638 |
me->targetClass = *me->target->isa; /* Copy pointers to routines for speed */ |
| 639 |
|
| 640 |
me->write_pointer = me->buffer; |
| 641 |
flush_breaks(me); |
| 642 |
me->line_break[0] = me->buffer; |
| 643 |
me->cleanness = 0; |
| 644 |
me->overflowed = NO; |
| 645 |
me->delete_line_break_char[0] = NO; |
| 646 |
me->preformatted = NO; |
| 647 |
me->in_attrval = NO; |
| 648 |
|
| 649 |
/* |
| 650 |
* For what line length should we attempt to wrap ? - kw |
| 651 |
*/ |
| 652 |
if (!LYPreparsedSource) { |
| 653 |
me->buffer_maxchars = 80; /* work as before - kw */ |
| 654 |
} else if (dump_output_width > 1) { |
| 655 |
me->buffer_maxchars = dump_output_width; /* try to honor -width - kw */ |
| 656 |
} else if (dump_output_immediately) { |
| 657 |
me->buffer_maxchars = 80; /* try to honor -width - kw */ |
| 658 |
} else { |
| 659 |
me->buffer_maxchars = (LYcolLimit - 1); |
| 660 |
if (me->buffer_maxchars < 38) /* too narrow, let GridText deal */ |
| 661 |
me->buffer_maxchars = 40; |
| 662 |
} |
| 663 |
if (me->buffer_maxchars > 900) /* likely not true - kw */ |
| 664 |
me->buffer_maxchars = 78; |
| 665 |
if (me->buffer_maxchars > BUFFER_SIZE) /* must not be larger! */ |
| 666 |
me->buffer_maxchars = BUFFER_SIZE - 2; |
| 667 |
|
| 668 |
/* |
| 669 |
* If dump_output_immediately is set, there likely isn't anything after |
| 670 |
* this stream to interpret the Lynx special chars. Also if they get |
| 671 |
* displayed via HTPlain, that will probably make non-breaking space chars |
| 672 |
* etc. invisible. So let's translate them to numerical character |
| 673 |
* references. For debugging purposes we'll use the new hex format. |
| 674 |
*/ |
| 675 |
me->escape_specials = LYPreparsedSource; |
| 676 |
|
| 677 |
#ifdef USE_COLOR_STYLE |
| 678 |
me->text = NULL; /* Will be initialized when first needed. - kw */ |
| 679 |
FREE(Style_className); |
| 680 |
class_string[0] = '\0'; |
| 681 |
#endif /* COLOR_STYLE */ |
| 682 |
|
| 683 |
return me; |
| 684 |
} |
| 685 |
|
| 686 |
/* Stream Object Class |
| 687 |
* ------------------- |
| 688 |
* |
| 689 |
* This object just converts a plain text stream into HTML |
| 690 |
* It is officially a structured stream but only the stream bits exist. |
| 691 |
* This is just the easiest way of typecasting all the routines. |
| 692 |
*/ |
| 693 |
static const HTStructuredClass PlainToHTMLConversion = |
| 694 |
{ |
| 695 |
"plaintexttoHTML", |
| 696 |
HTMLGen_free, |
| 697 |
PlainToHTML_abort, |
| 698 |
HTMLGen_put_character, |
| 699 |
HTMLGen_put_string, |
| 700 |
HTMLGen_write, |
| 701 |
NULL, /* Structured stuff */ |
| 702 |
NULL, |
| 703 |
NULL |
| 704 |
}; |
| 705 |
|
| 706 |
/* HTConverter from plain text to HTML Stream |
| 707 |
* ------------------------------------------ |
| 708 |
*/ |
| 709 |
HTStream *HTPlainToHTML(HTPresentation *pres GCC_UNUSED, |
| 710 |
HTParentAnchor *anchor GCC_UNUSED, |
| 711 |
HTStream *sink) |
| 712 |
{ |
| 713 |
HTStructured *me = (HTStructured *) malloc(sizeof(*me)); |
| 714 |
|
| 715 |
if (me == NULL) |
| 716 |
outofmem(__FILE__, "PlainToHTML"); |
| 717 |
|
| 718 |
me->isa = (const HTStructuredClass *) &PlainToHTMLConversion; |
| 719 |
|
| 720 |
/* |
| 721 |
* Copy pointers to routines for speed. |
| 722 |
*/ |
| 723 |
me->target = sink; |
| 724 |
me->targetClass = *me->target->isa; |
| 725 |
me->write_pointer = me->buffer; |
| 726 |
flush_breaks(me); |
| 727 |
me->cleanness = 0; |
| 728 |
me->overflowed = NO; |
| 729 |
me->delete_line_break_char[0] = NO; |
| 730 |
/* try to honor -width - kw */ |
| 731 |
me->buffer_maxchars = (dump_output_width > 1 ? |
| 732 |
dump_output_width : 80); |
| 733 |
|
| 734 |
HTMLGen_put_string(me, "<HTML>\n<BODY>\n<PRE>\n"); |
| 735 |
me->preformatted = YES; |
| 736 |
me->escape_specials = NO; |
| 737 |
me->in_attrval = NO; |
| 738 |
return (HTStream *) me; |
| 739 |
} |