Annotation of libwww/Library/src/HTML.c, revision 1.36

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

Webmaster