Annotation of libwww/Library/src/HTML.c, revision 1.63
1.39 frystyk 1: /* HTML.c
2: ** STRUCTURED STREAM TO RICH HYPERTEXT CONVERTER
3: **
1.43 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.39 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.63 ! frystyk 6: ** @(#) $Id: HTML.c,v 1.62 1996/05/21 00:44:39 frystyk Exp $
1.1 timbl 7: **
1.2 timbl 8: ** This generates of a hypertext object. It converts from the
9: ** structured stream interface fro HTMl events into the style-
1.47 frystyk 10: ** oriented iunterface of the HText interface. This module is
1.2 timbl 11: ** only used in clients and shouldnot be linked into servers.
1.1 timbl 12: **
1.6 timbl 13: ** Override this module if making a new GUI browser.
1.1 timbl 14: **
1.35 duns 15: ** HISTORY:
16: ** 8 Jul 94 FM Insulate free() from _free structure element.
17: **
1.1 timbl 18: */
1.16 timbl 19:
1.41 frystyk 20: /* Library include files */
1.60 frystyk 21: #include "sysdep.h"
1.63 ! frystyk 22: #include "WWWUtil.h"
! 23: #include "WWWCore.h"
! 24: #include "WWWHTML.h"
1.1 timbl 25: #include "HText.h"
26: #include "HTStyle.h"
1.41 frystyk 27: #include "HTML.h"
1.1 timbl 28:
29: extern HTStyleSheet * styleSheet; /* Application-wide */
30:
31: /* Module-wide style cache
32: */
33: PRIVATE int got_styles = 0;
1.16 timbl 34: PRIVATE HTStyle *styles[HTMLP_ELEMENTS];
1.2 timbl 35: PRIVATE HTStyle *default_style;
1.1 timbl 36:
1.62 frystyk 37: #define TAB '\0'
1.1 timbl 38:
39: /* HTML Object
40: ** -----------
41: */
1.2 timbl 42: #define MAX_NESTING 20 /* Should be checked by parser */
43:
44: typedef struct _stack_element {
45: HTStyle * style;
46: int tag_number;
47: } stack_element;
48:
49: struct _HTStructured {
1.60 frystyk 50: const HTStructuredClass * isa;
1.54 frystyk 51: HTRequest * request;
1.2 timbl 52: HTParentAnchor * node_anchor;
53: HText * text;
54:
55: HTStream* target; /* Output stream */
56: HTStreamClass targetClass; /* Output routines */
57:
1.56 frystyk 58: HTChunk * title; /* Grow by 128 */
1.2 timbl 59:
60: char * comment_start; /* for literate programming */
61: char * comment_end;
1.16 timbl 62:
1.60 frystyk 63: const SGML_dtd* dtd;
1.16 timbl 64:
1.2 timbl 65: HTTag * current_tag;
66: BOOL style_change;
67: HTStyle * new_style;
68: HTStyle * old_style;
69: BOOL in_word; /* Have just had a non-white char */
1.44 frystyk 70:
71: stack_element stack[MAX_NESTING];
72: stack_element *sp; /* Style stack pointer */
73: int overflow; /* Keep track of overflow nesting */
1.1 timbl 74: };
75:
1.2 timbl 76: struct _HTStream {
1.60 frystyk 77: const HTStreamClass * isa;
1.2 timbl 78: /* .... */
79: };
1.1 timbl 80:
81: /* Forward declarations of routines
82: */
1.52 frystyk 83: PRIVATE void get_styles (void);
1.1 timbl 84:
85:
1.52 frystyk 86: PRIVATE void actually_set_style (HTStructured * me);
87: PRIVATE void change_paragraph_style (HTStructured * me, HTStyle * style);
1.1 timbl 88:
89: /* Style buffering avoids dummy paragraph begin/ends.
90: */
1.4 timbl 91: #define UPDATE_STYLE if (me->style_change) { actually_set_style(me); }
1.1 timbl 92:
1.2 timbl 93: /* Entity values -- for ISO Latin 1 local representation
94: **
95: ** This MUST match exactly the table referred to in the DTD!
96: */
97: static char * ISO_Latin1[] = {
98: "\306", /* capital AE diphthong (ligature) */
99: "\301", /* capital A, acute accent */
100: "\302", /* capital A, circumflex accent */
101: "\300", /* capital A, grave accent */
102: "\305", /* capital A, ring */
103: "\303", /* capital A, tilde */
104: "\304", /* capital A, dieresis or umlaut mark */
105: "\307", /* capital C, cedilla */
106: "\320", /* capital Eth, Icelandic */
107: "\311", /* capital E, acute accent */
108: "\312", /* capital E, circumflex accent */
109: "\310", /* capital E, grave accent */
110: "\313", /* capital E, dieresis or umlaut mark */
111: "\315", /* capital I, acute accent */
112: "\316", /* capital I, circumflex accent */
113: "\314", /* capital I, grave accent */
114: "\317", /* capital I, dieresis or umlaut mark */
115: "\321", /* capital N, tilde */
116: "\323", /* capital O, acute accent */
117: "\324", /* capital O, circumflex accent */
118: "\322", /* capital O, grave accent */
119: "\330", /* capital O, slash */
120: "\325", /* capital O, tilde */
121: "\326", /* capital O, dieresis or umlaut mark */
122: "\336", /* capital THORN, Icelandic */
123: "\332", /* capital U, acute accent */
124: "\333", /* capital U, circumflex accent */
125: "\331", /* capital U, grave accent */
126: "\334", /* capital U, dieresis or umlaut mark */
127: "\335", /* capital Y, acute accent */
128: "\341", /* small a, acute accent */
129: "\342", /* small a, circumflex accent */
130: "\346", /* small ae diphthong (ligature) */
131: "\340", /* small a, grave accent */
132: "\046", /* ampersand */
133: "\345", /* small a, ring */
134: "\343", /* small a, tilde */
135: "\344", /* small a, dieresis or umlaut mark */
136: "\347", /* small c, cedilla */
137: "\351", /* small e, acute accent */
138: "\352", /* small e, circumflex accent */
139: "\350", /* small e, grave accent */
140: "\360", /* small eth, Icelandic */
141: "\353", /* small e, dieresis or umlaut mark */
142: "\076", /* greater than */
143: "\355", /* small i, acute accent */
144: "\356", /* small i, circumflex accent */
145: "\354", /* small i, grave accent */
146: "\357", /* small i, dieresis or umlaut mark */
147: "\074", /* less than */
1.62 frystyk 148: "\040", /* non-breaking space */
1.2 timbl 149: "\361", /* small n, tilde */
150: "\363", /* small o, acute accent */
151: "\364", /* small o, circumflex accent */
152: "\362", /* small o, grave accent */
153: "\370", /* small o, slash */
154: "\365", /* small o, tilde */
155: "\366", /* small o, dieresis or umlaut mark */
1.36 frystyk 156: "\042", /* double quote sign - June 94 */
1.2 timbl 157: "\337", /* small sharp s, German (sz ligature) */
158: "\376", /* small thorn, Icelandic */
159: "\372", /* small u, acute accent */
160: "\373", /* small u, circumflex accent */
161: "\371", /* small u, grave accent */
162: "\374", /* small u, dieresis or umlaut mark */
163: "\375", /* small y, acute accent */
164: "\377", /* small y, dieresis or umlaut mark */
1.1 timbl 165: };
166:
1.2 timbl 167:
168: /* Entity values -- for NeXT local representation
169: **
170: ** This MUST match exactly the table referred to in the DTD!
171: **
172: */
173: static char * NeXTCharacters[] = {
174: "\341", /* capital AE diphthong (ligature) */
175: "\202", /* capital A, acute accent */
176: "\203", /* capital A, circumflex accent */
177: "\201", /* capital A, grave accent */
178: "\206", /* capital A, ring */
179: "\204", /* capital A, tilde */
180: "\205", /* capital A, dieresis or umlaut mark */
181: "\207", /* capital C, cedilla */
182: "\220", /* capital Eth, Icelandic */
183: "\211", /* capital E, acute accent */
184: "\212", /* capital E, circumflex accent */
185: "\210", /* capital E, grave accent */
186: "\213", /* capital E, dieresis or umlaut mark */
187: "\215", /* capital I, acute accent */
188: "\216", /* capital I, circumflex accent these are */
189: "\214", /* capital I, grave accent ISO -100 hex */
190: "\217", /* capital I, dieresis or umlaut mark */
191: "\221", /* capital N, tilde */
192: "\223", /* capital O, acute accent */
193: "\224", /* capital O, circumflex accent */
194: "\222", /* capital O, grave accent */
195: "\351", /* capital O, slash 'cept this */
196: "\225", /* capital O, tilde */
197: "\226", /* capital O, dieresis or umlaut mark */
198: "\234", /* capital THORN, Icelandic */
199: "\230", /* capital U, acute accent */
200: "\231", /* capital U, circumflex accent */
201: "\227", /* capital U, grave accent */
202: "\232", /* capital U, dieresis or umlaut mark */
203: "\233", /* capital Y, acute accent */
204: "\326", /* small a, acute accent */
205: "\327", /* small a, circumflex accent */
206: "\361", /* small ae diphthong (ligature) */
207: "\325", /* small a, grave accent */
208: "\046", /* ampersand */
209: "\332", /* small a, ring */
210: "\330", /* small a, tilde */
211: "\331", /* small a, dieresis or umlaut mark */
212: "\333", /* small c, cedilla */
213: "\335", /* small e, acute accent */
214: "\336", /* small e, circumflex accent */
215: "\334", /* small e, grave accent */
216: "\346", /* small eth, Icelandic */
217: "\337", /* small e, dieresis or umlaut mark */
218: "\076", /* greater than */
219: "\342", /* small i, acute accent */
220: "\344", /* small i, circumflex accent */
221: "\340", /* small i, grave accent */
222: "\345", /* small i, dieresis or umlaut mark */
223: "\074", /* less than */
1.62 frystyk 224: "\040", /* non-breaking space */
1.2 timbl 225: "\347", /* small n, tilde */
226: "\355", /* small o, acute accent */
227: "\356", /* small o, circumflex accent */
228: "\354", /* small o, grave accent */
229: "\371", /* small o, slash */
230: "\357", /* small o, tilde */
231: "\360", /* small o, dieresis or umlaut mark */
1.36 frystyk 232: "\042", /* double quote sign - June 94 */
1.2 timbl 233: "\373", /* small sharp s, German (sz ligature) */
234: "\374", /* small thorn, Icelandic */
235: "\363", /* small u, acute accent */
236: "\364", /* small u, circumflex accent */
237: "\362", /* small u, grave accent */
238: "\366", /* small u, dieresis or umlaut mark */
239: "\367", /* small y, acute accent */
240: "\375", /* small y, dieresis or umlaut mark */
1.1 timbl 241: };
242:
1.2 timbl 243: /* Entity values -- for IBM/PC Code Page 850 (International)
244: **
245: ** This MUST match exactly the table referred to in the DTD!
246: **
247: */
248: /* @@@@@@@@@@@@@@@@@ TBD */
249:
250:
251:
252: /* Set character set
253: ** ----------------
254: */
255:
256: PRIVATE char** p_entity_values = ISO_Latin1; /* Pointer to translation */
1.1 timbl 257:
1.53 frystyk 258: PUBLIC void HTMLUseCharacterSet (HTMLCharacterSet i)
1.2 timbl 259: {
260: p_entity_values = (i == HTML_NEXT_CHARS) ? NeXTCharacters
261: : ISO_Latin1;
262: }
1.1 timbl 263:
264:
265: /* Flattening the style structure
266: ** ------------------------------
267: **
268: On the NeXT, and on any read-only browser, it is simpler for the text to have
269: a sequence of styles, rather than a nested tree of styles. In this
270: case we have to flatten the structure as it arrives from SGML tags into
271: a sequence of styles.
272: */
273:
274: /* If style really needs to be set, call this
275: */
1.53 frystyk 276: PRIVATE void actually_set_style (HTStructured * me)
1.1 timbl 277: {
1.4 timbl 278: if (!me->text) { /* First time through */
1.54 frystyk 279: me->text = HText_new2(me->request, me->node_anchor, me->target);
1.4 timbl 280: HText_beginAppend(me->text);
281: HText_setStyle(me->text, me->new_style);
282: me->in_word = NO;
1.1 timbl 283: } else {
1.4 timbl 284: HText_setStyle(me->text, me->new_style);
1.1 timbl 285: }
1.4 timbl 286: me->old_style = me->new_style;
287: me->style_change = NO;
1.1 timbl 288: }
289:
290: /* If you THINK you need to change style, call this
291: */
292:
1.53 frystyk 293: PRIVATE void change_paragraph_style (HTStructured * me, HTStyle *style)
1.1 timbl 294: {
1.4 timbl 295: if (me->new_style!=style) {
296: me->style_change = YES;
297: me->new_style = style;
1.1 timbl 298: }
1.11 timbl 299: me->in_word = NO;
1.1 timbl 300: }
301:
1.2 timbl 302: /*_________________________________________________________________________
303: **
304: ** A C T I O N R O U T I N E S
305: */
306:
1.63 ! frystyk 307: PRIVATE int HTML_write (HTStructured * me, const char * b, int l)
1.1 timbl 308: {
1.63 ! frystyk 309: while (l-- > 0) {
! 310: char c = *b++;
1.4 timbl 311: switch (me->sp[0].tag_number) {
1.2 timbl 312: case HTML_COMMENT:
313: break; /* Do Nothing */
314:
315: case HTML_TITLE:
1.56 frystyk 316: HTChunk_putb(me->title, &c, 1);
1.2 timbl 317: break;
318:
319:
320: case HTML_LISTING: /* Litteral text */
321: case HTML_XMP:
322: case HTML_PLAINTEXT:
323: case HTML_PRE:
324: /* We guarrantee that the style is up-to-date in begin_litteral
325: */
1.4 timbl 326: HText_appendCharacter(me->text, c);
1.2 timbl 327: break;
328:
329: default: /* Free format text */
1.4 timbl 330: if (me->style_change) {
1.42 frystyk 331: if ((c=='\n') || (c==' ')) return HT_OK; /* Ignore it */
1.2 timbl 332: UPDATE_STYLE;
333: }
1.62 frystyk 334: if (c == TAB)
335: HText_appendCharacter(me->text, '\t');
336: else if (WHITE(c)) {
1.4 timbl 337: if (me->in_word) {
338: HText_appendCharacter(me->text, ' ');
339: me->in_word = NO;
1.2 timbl 340: }
341: } else {
1.4 timbl 342: HText_appendCharacter(me->text, c);
343: me->in_word = YES;
1.2 timbl 344: }
345: } /* end switch */
1.63 ! frystyk 346: }
1.42 frystyk 347: return HT_OK;
1.1 timbl 348: }
349:
1.60 frystyk 350: PRIVATE int HTML_put_string (HTStructured * me, const char* s)
1.1 timbl 351: {
1.63 ! frystyk 352: return HTML_write(me, s, (int) strlen(s));
1.1 timbl 353: }
354:
1.63 ! frystyk 355: PRIVATE int HTML_put_character (HTStructured * me, char c)
1.1 timbl 356: {
1.63 ! frystyk 357: return HTML_write(me, &c, 1);
1.1 timbl 358: }
1.2 timbl 359:
360:
361: /* Start Element
362: ** -------------
363: */
1.53 frystyk 364: PRIVATE void HTML_start_element (
365: HTStructured * me,
366: int element_number,
1.60 frystyk 367: const BOOL* present,
368: const char ** value)
1.2 timbl 369: {
370: switch (element_number) {
371: case HTML_A:
372: {
1.8 timbl 373: HTChildAnchor * source;
1.9 timbl 374: char * href = NULL;
1.42 frystyk 375: if (present[HTML_A_HREF])
1.9 timbl 376: StrAllocCopy(href, value[HTML_A_HREF]);
1.8 timbl 377: source = HTAnchor_findChildAndLink(
1.4 timbl 378: me->node_anchor, /* parent */
1.2 timbl 379: present[HTML_A_NAME] ? value[HTML_A_NAME] : 0, /* Tag */
1.9 timbl 380: present[HTML_A_HREF] ? href : 0, /* Addresss */
1.16 timbl 381: present[HTML_A_REL] && value[HTML_A_REL] ?
1.54 frystyk 382: (HTLinkType) HTAtom_for(value[HTML_A_REL])
383: : 0);
1.2 timbl 384:
1.63 ! frystyk 385: if (present[HTML_A_TITLE] && value[HTML_A_TITLE]) {
! 386: HTLink * link = HTAnchor_mainLink((HTAnchor *) source);
! 387: HTParentAnchor *dest=HTAnchor_parent(HTLink_destination(link));
1.2 timbl 388: if (!HTAnchor_title(dest))
389: HTAnchor_setTitle(dest, value[HTML_A_TITLE]);
390: }
391: UPDATE_STYLE;
1.4 timbl 392: HText_beginAnchor(me->text, source);
1.58 frystyk 393: HT_FREE(href); /* Leak fix Henrik 17/02-94 */
1.2 timbl 394: }
395: break;
396:
1.63 ! frystyk 397: case HTML_LINK:
! 398:
! 399: /* MORE */
! 400:
! 401: break;
! 402:
1.2 timbl 403: case HTML_TITLE:
1.56 frystyk 404: HTChunk_clear(me->title);
1.2 timbl 405: break;
406:
407: case HTML_NEXTID:
408: /* if (present[NEXTID_N] && value[NEXTID_N])
1.4 timbl 409: HText_setNextId(me->text, atoi(value[NEXTID_N])); */
1.2 timbl 410: break;
411:
412: case HTML_ISINDEX:
1.4 timbl 413: HTAnchor_setIndex(me->node_anchor);
1.2 timbl 414: break;
415:
1.15 timbl 416: case HTML_BR:
417: UPDATE_STYLE;
418: HText_appendCharacter(me->text, '\n');
419: me->in_word = NO;
420: break;
421:
422: case HTML_HR:
423: UPDATE_STYLE;
424: HText_appendCharacter(me->text, '\n');
1.16 timbl 425: HText_appendText(me->text, "___________________________________");
1.15 timbl 426: HText_appendCharacter(me->text, '\n');
427: me->in_word = NO;
428: break;
429:
1.2 timbl 430: case HTML_P:
431: UPDATE_STYLE;
1.4 timbl 432: HText_appendParagraph(me->text);
433: me->in_word = NO;
1.2 timbl 434: break;
435:
436: case HTML_DL:
1.11 timbl 437: change_paragraph_style(me, present && present[DL_COMPACT]
1.16 timbl 438: ? styles[HTML_DL]
1.2 timbl 439: : styles[HTML_DL]);
440: break;
441:
442: case HTML_DT:
1.4 timbl 443: if (!me->style_change) {
444: HText_appendParagraph(me->text);
445: me->in_word = NO;
1.2 timbl 446: }
447: break;
448:
449: case HTML_DD:
450: UPDATE_STYLE;
1.62 frystyk 451: HTML_put_character(me, TAB); /* Just tab out one stop */
1.4 timbl 452: me->in_word = NO;
453: break;
1.2 timbl 454:
455: case HTML_UL:
456: case HTML_OL:
457: case HTML_MENU:
458: case HTML_DIR:
1.11 timbl 459: change_paragraph_style(me, styles[element_number]);
1.2 timbl 460: break;
461:
462: case HTML_LI:
463: UPDATE_STYLE;
1.7 timbl 464: if (me->sp[0].tag_number != HTML_DIR)
1.4 timbl 465: HText_appendParagraph(me->text);
1.2 timbl 466: else
1.62 frystyk 467: HText_appendCharacter(me->text, TAB);
1.4 timbl 468: me->in_word = NO;
1.2 timbl 469: break;
470:
471: case HTML_LISTING: /* Litteral text */
472: case HTML_XMP:
473: case HTML_PLAINTEXT:
474: case HTML_PRE:
1.11 timbl 475: change_paragraph_style(me, styles[element_number]);
1.2 timbl 476: UPDATE_STYLE;
1.4 timbl 477: if (me->comment_end)
478: HText_appendText(me->text, me->comment_end);
1.2 timbl 479: break;
1.11 timbl 480:
1.23 frystyk 481: case HTML_IMG: /* Images */
482: {
483: HTChildAnchor *source;
484: char *src = NULL;
1.49 frystyk 485: if (present[HTML_IMG_SRC])
1.23 frystyk 486: StrAllocCopy(src, value[HTML_IMG_SRC]);
487: source = HTAnchor_findChildAndLink(
488: me->node_anchor, /* parent */
489: 0, /* Tag */
490: src ? src : 0, /* Addresss */
491: 0);
492: UPDATE_STYLE;
493: HText_appendImage(me->text, source,
1.24 frystyk 494: present[HTML_IMG_ALT] ? value[HTML_IMG_ALT] : NULL,
495: present[HTML_IMG_ALIGN] ? value[HTML_IMG_ALIGN] : NULL,
496: present[HTML_IMG_ISMAP] ? YES : NO);
1.58 frystyk 497: HT_FREE(src);
1.24 frystyk 498: }
499: break;
500:
1.63 ! frystyk 501: case HTML_BASE: /* Base header */
! 502: if (present[HTML_BASE_HREF]) {
! 503: char * base = (char *) value[HTML_BASE_HREF];
! 504: if (base) {
! 505: HTAnchor_setBase(me->node_anchor, base);
! 506: if (SGML_TRACE) HTTrace("HTML Parser. New base `%s\'\n", base);
! 507: } else {
! 508: if (SGML_TRACE) HTTrace("HTML Parser. No base found\n");
! 509: }
! 510: }
! 511: break;
! 512:
1.24 frystyk 513: case HTML_HTML: /* Ignore these altogether */
514: case HTML_HEAD:
515: case HTML_BODY:
1.62 frystyk 516: break;
1.24 frystyk 517:
1.10 timbl 518: case HTML_TT: /* Physical character highlighting */
519: case HTML_B: /* Currently ignored */
520: case HTML_I:
521: case HTML_U:
1.62 frystyk 522: UPDATE_STYLE;
523: HText_appendCharacter(me->text, '_');
524: me->in_word = NO;
525: break;
1.10 timbl 526:
527: case HTML_EM: /* Logical character highlighting */
528: case HTML_STRONG: /* Currently ignored */
529: case HTML_CODE:
530: case HTML_SAMP:
531: case HTML_KBD:
532: case HTML_VAR:
533: case HTML_DFN:
534: case HTML_CITE:
535: break;
536:
1.11 timbl 537: case HTML_H1: /* paragraph styles */
538: case HTML_H2:
539: case HTML_H3:
540: case HTML_H4:
541: case HTML_H5:
542: case HTML_H6:
543: case HTML_H7:
544: case HTML_ADDRESS:
545: case HTML_BLOCKQUOTE:
546: change_paragraph_style(me, styles[element_number]); /* May be postponed */
1.2 timbl 547: break;
548:
549: } /* end switch */
550:
1.16 timbl 551: if (me->dtd->tags[element_number].contents!= SGML_EMPTY) {
1.13 timbl 552: if (me->sp == me->stack) {
1.44 frystyk 553: if (SGML_TRACE)
1.63 ! frystyk 554: HTTrace("HTML Parser. Maximum nesting of %d exceded!\n",
1.44 frystyk 555: MAX_NESTING);
556: me->overflow++;
1.12 timbl 557: return;
558: }
1.4 timbl 559: --(me->sp);
560: me->sp[0].style = me->new_style; /* Stack new style */
561: me->sp[0].tag_number = element_number;
1.10 timbl 562: }
1.1 timbl 563: }
1.10 timbl 564:
1.2 timbl 565:
1.1 timbl 566: /* End Element
1.2 timbl 567: ** -----------
1.1 timbl 568: **
1.2 timbl 569: */
570: /* When we end an element, the style must be returned to that
1.1 timbl 571: ** in effect before that element. Note that anchors (etc?)
572: ** don't have an associated style, so that we must scan down the
573: ** stack for an element with a defined style. (In fact, the styles
574: ** should be linked to the whole stack not just the top one.)
575: ** TBL 921119
1.6 timbl 576: **
577: ** We don't turn on "CAREFUL" check because the parser produces
578: ** (internal code errors apart) good nesting. The parser checks
579: ** incoming code errors, not this module.
1.1 timbl 580: */
1.53 frystyk 581: PRIVATE void HTML_end_element (HTStructured * me, int element_number)
1.1 timbl 582: {
1.2 timbl 583: #ifdef CAREFUL /* parser assumed to produce good nesting */
1.4 timbl 584: if (element_number != me->sp[0].tag_number) {
1.59 eric 585: HTTrace("HTMLText: end of element %s when expecting end of %s\n",
1.16 timbl 586: me->dtd->tags[element_number].name,
587: me->dtd->tags[me->sp->tag_number].name);
1.6 timbl 588: /* panic */
1.1 timbl 589: }
1.2 timbl 590: #endif
1.44 frystyk 591:
592: /* HFN, If overflow of nestings, we need to get back to reality */
593: if (me->overflow > 0) {
594: me->overflow--;
595: return;
596: }
597:
1.4 timbl 598: me->sp++; /* Pop state off stack */
1.44 frystyk 599:
1.2 timbl 600: switch(element_number) {
601:
602: case HTML_A:
603: UPDATE_STYLE;
1.4 timbl 604: HText_endAnchor(me->text);
1.2 timbl 605: break;
606:
607: case HTML_TITLE:
1.56 frystyk 608: HTAnchor_setTitle(me->node_anchor, HTChunk_data(me->title));
1.2 timbl 609: break;
610:
1.62 frystyk 611: case HTML_TT: /* Physical character highlighting */
612: case HTML_B: /* Currently ignored */
613: case HTML_I:
614: case HTML_U:
615: UPDATE_STYLE;
616: HText_appendCharacter(me->text, '_');
617: break;
618:
1.2 timbl 619: case HTML_LISTING: /* Litteral text */
620: case HTML_XMP:
621: case HTML_PLAINTEXT:
622: case HTML_PRE:
1.4 timbl 623: if (me->comment_start)
624: HText_appendText(me->text, me->comment_start);
1.2 timbl 625: /* Fall through */
626:
627: default:
1.44 frystyk 628:
629: /* Often won't really change */
630: change_paragraph_style(me, me->sp->style);
1.2 timbl 631: break;
632:
633: } /* switch */
1.1 timbl 634: }
635:
1.2 timbl 636:
637: /* Expanding entities
638: ** ------------------
639: */
640: /* (In fact, they all shrink!)
1.1 timbl 641: */
1.2 timbl 642:
1.53 frystyk 643: PRIVATE void HTML_put_entity (HTStructured * me, int entity_number)
1.1 timbl 644: {
1.4 timbl 645: HTML_put_string(me, ISO_Latin1[entity_number]); /* @@ Other representations */
1.1 timbl 646: }
1.2 timbl 647:
1.42 frystyk 648: /* Flush an HTML object
649: ** --------------------
650: */
1.53 frystyk 651: PUBLIC int HTML_flush (HTStructured * me)
1.42 frystyk 652: {
653: UPDATE_STYLE; /* Creates empty document here! */
1.57 frystyk 654: if (me->comment_end) HTML_put_string(me,me->comment_end);
655: return me->target ? (*me->targetClass.flush)(me->target) : HT_OK;
1.42 frystyk 656: }
1.2 timbl 657:
658: /* Free an HTML object
659: ** -------------------
660: **
1.4 timbl 661: ** If the document is empty, the text object will not yet exist.
662: So we could in fact abandon creating the document and return
663: an error code. In fact an empty document is an important type
664: of document, so we don't.
665: **
666: ** If non-interactive, everything is freed off. No: crashes -listrefs
1.2 timbl 667: ** Otherwise, the interactive object is left.
668: */
1.53 frystyk 669: PUBLIC int HTML_free (HTStructured * me)
1.1 timbl 670: {
1.4 timbl 671: UPDATE_STYLE; /* Creates empty document here! */
672: if (me->comment_end)
673: HTML_put_string(me,me->comment_end);
674: HText_endAppend(me->text);
675:
676: if (me->target) {
1.35 duns 677: (*me->targetClass._free)(me->target);
1.2 timbl 678: }
1.56 frystyk 679: HTChunk_delete(me->title);
1.58 frystyk 680: HT_FREE(me);
1.42 frystyk 681: return HT_OK;
1.1 timbl 682: }
683:
684:
1.53 frystyk 685: PRIVATE int HTML_abort (HTStructured * me, HTList * e)
1.1 timbl 686:
1.14 timbl 687: {
688: if (me->target) {
689: (*me->targetClass.abort)(me->target, e);
690: }
1.56 frystyk 691: HTChunk_delete(me->title);
1.58 frystyk 692: HT_FREE(me);
1.42 frystyk 693: return HT_ERROR;
1.1 timbl 694: }
695:
1.2 timbl 696:
697: /* Get Styles from style sheet
698: ** ---------------------------
699: */
1.53 frystyk 700: PRIVATE void get_styles (void)
1.1 timbl 701: {
1.2 timbl 702: got_styles = YES;
703:
704: default_style = HTStyleNamed(styleSheet, "Normal");
1.1 timbl 705:
1.2 timbl 706: styles[HTML_H1] = HTStyleNamed(styleSheet, "Heading1");
707: styles[HTML_H2] = HTStyleNamed(styleSheet, "Heading2");
708: styles[HTML_H3] = HTStyleNamed(styleSheet, "Heading3");
709: styles[HTML_H4] = HTStyleNamed(styleSheet, "Heading4");
710: styles[HTML_H5] = HTStyleNamed(styleSheet, "Heading5");
711: styles[HTML_H6] = HTStyleNamed(styleSheet, "Heading6");
712: styles[HTML_H7] = HTStyleNamed(styleSheet, "Heading7");
713:
714: styles[HTML_DL] = HTStyleNamed(styleSheet, "Glossary");
715: styles[HTML_UL] =
716: styles[HTML_OL] = HTStyleNamed(styleSheet, "List");
717: styles[HTML_MENU] = HTStyleNamed(styleSheet, "Menu");
718: styles[HTML_DIR] = HTStyleNamed(styleSheet, "Dir");
1.16 timbl 719: /* styles[HTML_DLC] = HTStyleNamed(styleSheet, "GlossaryCompact"); */
1.2 timbl 720: styles[HTML_ADDRESS]= HTStyleNamed(styleSheet, "Address");
721: styles[HTML_BLOCKQUOTE]= HTStyleNamed(styleSheet, "BlockQuote");
722: styles[HTML_PLAINTEXT] =
723: styles[HTML_XMP] = HTStyleNamed(styleSheet, "Example");
724: styles[HTML_PRE] = HTStyleNamed(styleSheet, "Preformatted");
725: styles[HTML_LISTING] = HTStyleNamed(styleSheet, "Listing");
726: }
727: /* P U B L I C
728: */
729:
730: /* Structured Object Class
731: ** -----------------------
732: */
1.60 frystyk 733: PRIVATE const HTStructuredClass HTMLPresentation = /* As opposed to print etc */
1.2 timbl 734: {
735: "text/html",
1.42 frystyk 736: HTML_flush,
1.2 timbl 737: HTML_free,
1.14 timbl 738: HTML_abort,
1.2 timbl 739: HTML_put_character, HTML_put_string, HTML_write,
740: HTML_start_element, HTML_end_element,
741: HTML_put_entity
742: };
1.1 timbl 743:
1.4 timbl 744:
1.2 timbl 745: /* New Structured Text object
746: ** --------------------------
747: **
1.16 timbl 748: ** The structured stream can generate either presentation,
1.4 timbl 749: ** or plain text, or HTML.
1.1 timbl 750: */
1.53 frystyk 751: PRIVATE HTStructured* HTML_new (HTRequest * request,
752: void * param,
753: HTFormat input_format,
754: HTFormat output_format,
755: HTStream * output_stream)
1.1 timbl 756: {
757:
1.4 timbl 758: HTStructured * me;
759:
1.47 frystyk 760: #if 0
1.16 timbl 761: if (output_format != WWW_PLAINTEXT
762: && output_format != WWW_PRESENT
763: && output_format != HTAtom_for("text/x-c")) {
1.37 frystyk 764: HTStream * intermediate = HTStreamStack(WWW_HTML, output_format,
765: output_stream, request, NO);
1.6 timbl 766: if (intermediate) return HTMLGenerator(intermediate);
1.44 frystyk 767: if (SGML_TRACE)
1.63 ! frystyk 768: HTTrace("HTML Parser. Can't parse HTML to %s\n",
1.44 frystyk 769: HTAtom_name(output_format));
1.4 timbl 770: exit (-99);
771: }
1.47 frystyk 772: #endif
1.4 timbl 773:
1.58 frystyk 774: if ((me = (HTStructured *) HT_CALLOC(1, sizeof(*me))) == NULL)
775: HT_OUTOFMEM("HTML_new");
1.1 timbl 776:
777: if (!got_styles) get_styles();
778:
1.4 timbl 779: me->isa = &HTMLPresentation;
1.47 frystyk 780: me->dtd = &HTMLP_dtd;
1.54 frystyk 781: me->request = request;
1.48 frystyk 782: me->node_anchor = HTRequest_anchor(request);
1.56 frystyk 783: me->title = HTChunk_new(128);
1.4 timbl 784: me->text = 0;
785: me->style_change = YES; /* Force check leading to text creation */
786: me->new_style = default_style;
787: me->old_style = 0;
788: me->sp = me->stack + MAX_NESTING - 1;
789: me->sp->tag_number = -1; /* INVALID */
790: me->sp->style = default_style; /* INVALID */
1.1 timbl 791:
1.4 timbl 792: me->comment_start = NULL;
793: me->comment_end = NULL;
1.16 timbl 794: me->target = output_stream;
795: if (output_stream) me->targetClass = *output_stream->isa; /* Copy pointers */
1.1 timbl 796:
1.4 timbl 797: return (HTStructured*) me;
1.1 timbl 798: }
799:
800:
1.2 timbl 801: /* HTConverter for HTML to plain text
802: ** ----------------------------------
1.1 timbl 803: **
1.2 timbl 804: ** This will convert from HTML to presentation or plain text.
1.1 timbl 805: */
1.53 frystyk 806: PUBLIC HTStream* HTMLToPlain (
807: HTRequest * request,
808: void * param,
809: HTFormat input_format,
810: HTFormat output_format,
811: HTStream * output_stream)
1.1 timbl 812: {
1.47 frystyk 813: return SGML_new(&HTMLP_dtd, HTML_new(
1.16 timbl 814: request, NULL, input_format, output_format, output_stream));
1.1 timbl 815: }
816:
817:
1.2 timbl 818: /* HTConverter for HTML to C code
819: ** ------------------------------
820: **
1.36 frystyk 821: ** C code is like plain text but all non-preformatted code
1.2 timbl 822: ** is commented out.
823: ** This will convert from HTML to presentation or plain text.
824: */
1.53 frystyk 825: PUBLIC HTStream* HTMLToC (
826: HTRequest * request,
827: void * param,
828: HTFormat input_format,
829: HTFormat output_format,
830: HTStream * output_stream)
1.1 timbl 831: {
1.4 timbl 832:
833: HTStructured * html;
834:
1.36 frystyk 835: (*output_stream->isa->put_string)(output_stream, "/* "); /* Before title */
1.16 timbl 836: html = HTML_new(request, NULL, input_format, output_format, output_stream);
1.45 frystyk 837: html->comment_start = "\n/* ";
1.47 frystyk 838: html->dtd = &HTMLP_dtd;
1.2 timbl 839: html->comment_end = " */\n"; /* Must start in col 1 for cpp */
1.47 frystyk 840: return SGML_new(&HTMLP_dtd, html);
1.1 timbl 841: }
842:
843:
1.2 timbl 844: /* Presenter for HTML
845: ** ------------------
846: **
847: ** This will convert from HTML to presentation or plain text.
848: **
849: ** Override this if you have a windows version
1.1 timbl 850: */
1.2 timbl 851: #ifndef GUI
1.53 frystyk 852: PUBLIC HTStream* HTMLPresent (
853: HTRequest * request,
854: void * param,
855: HTFormat input_format,
856: HTFormat output_format,
857: HTStream * output_stream)
1.1 timbl 858: {
1.47 frystyk 859: return SGML_new(&HTMLP_dtd, HTML_new(
1.16 timbl 860: request, NULL, input_format, output_format, output_stream));
1.1 timbl 861: }
1.2 timbl 862: #endif
1.29 frystyk 863:
Webmaster