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

1.39      frystyk     1: /*                                                                      HTML.c
                      2: **     STRUCTURED STREAM TO RICH HYPERTEXT CONVERTER
                      3: **
                      4: **     (c) COPYRIGHT CERN 1994.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
1.2       timbl       7: **     This generates of a hypertext object.  It converts from the
                      8: **     structured stream interface fro HTMl events into the style-
                      9: **     oriented iunterface of the HText.h interface.  This module is
                     10: **     only used in clients and shouldnot be linked into servers.
1.1       timbl      11: **
1.6       timbl      12: **     Override this module if making a new GUI browser.
1.1       timbl      13: **
1.35      duns       14: ** HISTORY:
                     15: **      8 Jul 94  FM   Insulate free() from _free structure element.
                     16: **
1.1       timbl      17: */
1.16      timbl      18: 
1.41    ! frystyk    19: /* Library include files */
        !            20: #include "tcp.h"
        !            21: #include "HTUtils.h"
        !            22: #include "HTString.h"
1.1       timbl      23: #include "HTAtom.h"
                     24: #include "HTChunk.h"
                     25: #include "HText.h"
                     26: #include "HTStyle.h"
1.3       timbl      27: #include "HTAlert.h"
1.4       timbl      28: #include "HTMLGen.h"
1.8       timbl      29: #include "HTParse.h"
1.41    ! frystyk    30: #include "HTML.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.38      frystyk   446:     while (l-- > 0)
                    447:        HTML_put_character(me, *s++);
1.1       timbl     448: }
1.2       timbl     449: 
                    450: 
                    451: /*     Start Element
                    452: **     -------------
                    453: */
                    454: PRIVATE void HTML_start_element ARGS4(
1.4       timbl     455:        HTStructured *,         me,
1.16      timbl     456:        int,                    element_number,
1.3       timbl     457:        CONST BOOL*,            present,
1.16      timbl     458:        CONST char **,          value)
1.2       timbl     459: {
                    460:     switch (element_number) {
                    461:     case HTML_A:
                    462:        {
1.8       timbl     463:            HTChildAnchor * source;
1.9       timbl     464:            char * href = NULL;
                    465:            if (present[HTML_A_HREF]) {
                    466:                StrAllocCopy(href, value[HTML_A_HREF]);
1.36      frystyk   467: #ifdef OLD_CODE
1.9       timbl     468:                HTSimplify(href);
1.36      frystyk   469: #endif
1.9       timbl     470:            }
1.8       timbl     471:            source = HTAnchor_findChildAndLink(
1.4       timbl     472:                me->node_anchor,                                /* parent */
1.2       timbl     473:                present[HTML_A_NAME] ? value[HTML_A_NAME] : 0,  /* Tag */
1.9       timbl     474:                present[HTML_A_HREF] ? href : 0,                /* Addresss */
1.16      timbl     475:                present[HTML_A_REL] && value[HTML_A_REL] ? 
                    476:                        (HTLinkType*)HTAtom_for(value[HTML_A_REL])
1.2       timbl     477:                                                : 0);
                    478:            
                    479:            if (present[HTML_A_TITLE] && value[HTML_A_TITLE]) {
                    480:                HTParentAnchor * dest = 
                    481:                    HTAnchor_parent(
                    482:                        HTAnchor_followMainLink((HTAnchor*)source)
                    483:                                    );
                    484:                if (!HTAnchor_title(dest))
                    485:                        HTAnchor_setTitle(dest, value[HTML_A_TITLE]);
                    486:            }
                    487:            UPDATE_STYLE;
1.4       timbl     488:            HText_beginAnchor(me->text, source);
1.18      frystyk   489:            free(href);                 /* Leak fix Henrik 17/02-94 */
1.2       timbl     490:        }
                    491:        break;
                    492:        
                    493:     case HTML_TITLE:
1.4       timbl     494:         HTChunkClear(&me->title);
1.2       timbl     495:        break;
                    496:        
                    497:     case HTML_NEXTID:
                    498:        /* if (present[NEXTID_N] && value[NEXTID_N])
1.4       timbl     499:                HText_setNextId(me->text, atoi(value[NEXTID_N])); */
1.2       timbl     500:        break;
                    501:        
                    502:     case HTML_ISINDEX:
1.4       timbl     503:        HTAnchor_setIndex(me->node_anchor);
1.2       timbl     504:        break;
                    505:        
1.15      timbl     506:     case HTML_BR: 
                    507:        UPDATE_STYLE;
                    508:        HText_appendCharacter(me->text, '\n');
                    509:        me->in_word = NO;
                    510:        break;
                    511:        
                    512:     case HTML_HR: 
                    513:        UPDATE_STYLE;
                    514:        HText_appendCharacter(me->text, '\n');
1.16      timbl     515:        HText_appendText(me->text, "___________________________________");
1.15      timbl     516:        HText_appendCharacter(me->text, '\n');
                    517:        me->in_word = NO;
                    518:        break;
                    519:        
1.2       timbl     520:     case HTML_P:
                    521:        UPDATE_STYLE;
1.4       timbl     522:        HText_appendParagraph(me->text);
                    523:        me->in_word = NO;
1.2       timbl     524:        break;
                    525: 
                    526:     case HTML_DL:
1.11      timbl     527:         change_paragraph_style(me, present && present[DL_COMPACT]
1.16      timbl     528:                ? styles[HTML_DL]
1.2       timbl     529:                : styles[HTML_DL]);
                    530:        break;
                    531:        
                    532:     case HTML_DT:
1.4       timbl     533:         if (!me->style_change) {
                    534:            HText_appendParagraph(me->text);
                    535:            me->in_word = NO;
1.2       timbl     536:        }
                    537:        break;
                    538:        
                    539:     case HTML_DD:
                    540:         UPDATE_STYLE;
1.4       timbl     541:        HTML_put_character(me, '\t');   /* Just tab out one stop */
                    542:        me->in_word = NO;
                    543:        break;
1.2       timbl     544: 
                    545:     case HTML_UL:
                    546:     case HTML_OL:
                    547:     case HTML_MENU:
                    548:     case HTML_DIR:
1.11      timbl     549:        change_paragraph_style(me, styles[element_number]);
1.2       timbl     550:        break;
                    551:        
                    552:     case HTML_LI:
                    553:         UPDATE_STYLE;
1.7       timbl     554:        if (me->sp[0].tag_number != HTML_DIR)
1.4       timbl     555:            HText_appendParagraph(me->text);
1.2       timbl     556:        else
1.4       timbl     557:            HText_appendCharacter(me->text, '\t');      /* Tab @@ nl for UL? */
                    558:        me->in_word = NO;
1.2       timbl     559:        break;
                    560:        
                    561:     case HTML_LISTING:                         /* Litteral text */
                    562:     case HTML_XMP:
                    563:     case HTML_PLAINTEXT:
                    564:     case HTML_PRE:
1.11      timbl     565:        change_paragraph_style(me, styles[element_number]);
1.2       timbl     566:        UPDATE_STYLE;
1.4       timbl     567:        if (me->comment_end)
                    568:            HText_appendText(me->text, me->comment_end);
1.2       timbl     569:        break;
1.11      timbl     570: 
1.23      frystyk   571:     case HTML_IMG:                     /* Images */
                    572:        {
                    573:            HTChildAnchor *source;
                    574:            char *src = NULL;
                    575:            if (present[HTML_IMG_SRC]) {
                    576:                StrAllocCopy(src, value[HTML_IMG_SRC]);
1.36      frystyk   577: #ifdef OLD_CODE
1.23      frystyk   578:                HTSimplify(src);
1.36      frystyk   579: #endif
1.23      frystyk   580:            }
                    581:            source = HTAnchor_findChildAndLink(
                    582:                                               me->node_anchor,    /* parent */
                    583:                                               0,                     /* Tag */
                    584:                                               src ? src : 0,    /* Addresss */
                    585:                                               0);
                    586:            UPDATE_STYLE;
                    587:            HText_appendImage(me->text, source,
1.24      frystyk   588:                      present[HTML_IMG_ALT] ? value[HTML_IMG_ALT] : NULL,
                    589:                      present[HTML_IMG_ALIGN] ? value[HTML_IMG_ALIGN] : NULL,
                    590:                      present[HTML_IMG_ISMAP] ? YES : NO);
1.23      frystyk   591:            free(src);
1.24      frystyk   592:        }       
                    593:        break;
                    594: 
                    595:     case HTML_HTML:                    /* Ignore these altogether */
                    596:     case HTML_HEAD:
                    597:     case HTML_BODY:
                    598:     
1.10      timbl     599:     case HTML_TT:                      /* Physical character highlighting */
                    600:     case HTML_B:                       /* Currently ignored */
                    601:     case HTML_I:
                    602:     case HTML_U:
                    603:     
                    604:     case HTML_EM:                      /* Logical character highlighting */
                    605:     case HTML_STRONG:                  /* Currently ignored */
                    606:     case HTML_CODE:
                    607:     case HTML_SAMP:
                    608:     case HTML_KBD:
                    609:     case HTML_VAR:
                    610:     case HTML_DFN:
                    611:     case HTML_CITE:
                    612:        break;
                    613:        
1.11      timbl     614:     case HTML_H1:                      /* paragraph styles */
                    615:     case HTML_H2:
                    616:     case HTML_H3:
                    617:     case HTML_H4:
                    618:     case HTML_H5:
                    619:     case HTML_H6:
                    620:     case HTML_H7:
                    621:     case HTML_ADDRESS:
                    622:     case HTML_BLOCKQUOTE:
                    623:        change_paragraph_style(me, styles[element_number]);     /* May be postponed */
1.2       timbl     624:        break;
                    625: 
                    626:     } /* end switch */
                    627: 
1.16      timbl     628:     if (me->dtd->tags[element_number].contents!= SGML_EMPTY) {
1.13      timbl     629:         if (me->sp == me->stack) {
1.41    ! frystyk   630:            fprintf(TDEST, "HTML: ****** Maximum nesting of %d exceded!\n",
1.12      timbl     631:            MAX_NESTING); 
                    632:            return;
                    633:        }
1.4       timbl     634:        --(me->sp);
                    635:        me->sp[0].style = me->new_style;        /* Stack new style */
                    636:        me->sp[0].tag_number = element_number;
1.10      timbl     637:     }  
1.1       timbl     638: }
1.10      timbl     639: 
1.2       timbl     640: 
1.1       timbl     641: /*             End Element
1.2       timbl     642: **             -----------
1.1       timbl     643: **
1.2       timbl     644: */
                    645: /*     When we end an element, the style must be returned to that
1.1       timbl     646: **     in effect before that element.  Note that anchors (etc?)
                    647: **     don't have an associated style, so that we must scan down the
                    648: **     stack for an element with a defined style. (In fact, the styles
                    649: **     should be linked to the whole stack not just the top one.)
                    650: **     TBL 921119
1.6       timbl     651: **
                    652: **     We don't turn on "CAREFUL" check because the parser produces
                    653: **     (internal code errors apart) good nesting. The parser checks
                    654: **     incoming code errors, not this module.
1.1       timbl     655: */
1.4       timbl     656: PRIVATE void HTML_end_element ARGS2(HTStructured *, me, int , element_number)
1.1       timbl     657: {
1.2       timbl     658: #ifdef CAREFUL                 /* parser assumed to produce good nesting */
1.4       timbl     659:     if (element_number != me->sp[0].tag_number) {
1.41    ! frystyk   660:         fprintf(TDEST, "HTMLText: end of element %s when expecting end of %s\n",
1.16      timbl     661:                me->dtd->tags[element_number].name,
                    662:                me->dtd->tags[me->sp->tag_number].name);
1.6       timbl     663:                /* panic */
1.1       timbl     664:     }
1.2       timbl     665: #endif
                    666:     
1.4       timbl     667:     me->sp++;                          /* Pop state off stack */
1.2       timbl     668:     
                    669:     switch(element_number) {
                    670: 
                    671:     case HTML_A:
                    672:        UPDATE_STYLE;
1.4       timbl     673:        HText_endAnchor(me->text);
1.2       timbl     674:        break;
                    675: 
                    676:     case HTML_TITLE:
1.4       timbl     677:         HTChunkTerminate(&me->title);
                    678:        HTAnchor_setTitle(me->node_anchor, me->title.data);
1.2       timbl     679:        break;
                    680:        
                    681:     case HTML_LISTING:                         /* Litteral text */
                    682:     case HTML_XMP:
                    683:     case HTML_PLAINTEXT:
                    684:     case HTML_PRE:
1.4       timbl     685:        if (me->comment_start)
                    686:            HText_appendText(me->text, me->comment_start);
1.2       timbl     687:        /* Fall through */
                    688:        
                    689:     default:
                    690:     
1.11      timbl     691:        change_paragraph_style(me, me->sp->style);      /* Often won't really change */
1.2       timbl     692:        break;
                    693:        
                    694:     } /* switch */
1.1       timbl     695: }
                    696: 
1.2       timbl     697: 
                    698: /*             Expanding entities
                    699: **             ------------------
                    700: */
                    701: /*     (In fact, they all shrink!)
1.1       timbl     702: */
1.2       timbl     703: 
1.4       timbl     704: PRIVATE void HTML_put_entity ARGS2(HTStructured *, me, int, entity_number)
1.1       timbl     705: {
1.4       timbl     706:     HTML_put_string(me, ISO_Latin1[entity_number]);    /* @@ Other representations */
1.1       timbl     707: }
1.2       timbl     708: 
                    709: 
                    710: /*     Free an HTML object
                    711: **     -------------------
                    712: **
1.4       timbl     713: ** If the document is empty, the text object will not yet exist.
                    714:    So we could in fact abandon creating the document and return
                    715:    an error code.  In fact an empty document is an important type
                    716:    of document, so we don't.
                    717: **
                    718: **     If non-interactive, everything is freed off.   No: crashes -listrefs
1.2       timbl     719: **     Otherwise, the interactive object is left.      
                    720: */
1.37      frystyk   721: PUBLIC int HTML_free ARGS1(HTStructured *, me)
1.1       timbl     722: {
1.4       timbl     723:     UPDATE_STYLE;              /* Creates empty document here! */
                    724:     if (me->comment_end)
                    725:                HTML_put_string(me,me->comment_end);
                    726:     HText_endAppend(me->text);
                    727: 
                    728:     if (me->target) {
1.35      duns      729:         (*me->targetClass._free)(me->target);
1.2       timbl     730:     }
1.19      frystyk   731:     HTChunkClear(&me->title);  /* Henrik 18/02-94 */
1.4       timbl     732:     free(me);
1.37      frystyk   733:     return 0;
1.1       timbl     734: }
                    735: 
                    736: 
1.37      frystyk   737: PRIVATE int 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.37      frystyk   745:     return EOF;
1.1       timbl     746: }
                    747: 
1.2       timbl     748: 
                    749: /*     Get Styles from style sheet
                    750: **     ---------------------------
                    751: */
                    752: PRIVATE void get_styles NOARGS
1.1       timbl     753: {
1.2       timbl     754:     got_styles = YES;
                    755:     
                    756:     default_style =            HTStyleNamed(styleSheet, "Normal");
1.1       timbl     757: 
1.2       timbl     758:     styles[HTML_H1] =          HTStyleNamed(styleSheet, "Heading1");
                    759:     styles[HTML_H2] =          HTStyleNamed(styleSheet, "Heading2");
                    760:     styles[HTML_H3] =          HTStyleNamed(styleSheet, "Heading3");
                    761:     styles[HTML_H4] =          HTStyleNamed(styleSheet, "Heading4");
                    762:     styles[HTML_H5] =          HTStyleNamed(styleSheet, "Heading5");
                    763:     styles[HTML_H6] =          HTStyleNamed(styleSheet, "Heading6");
                    764:     styles[HTML_H7] =          HTStyleNamed(styleSheet, "Heading7");
                    765: 
                    766:     styles[HTML_DL] =          HTStyleNamed(styleSheet, "Glossary");
                    767:     styles[HTML_UL] =
                    768:     styles[HTML_OL] =          HTStyleNamed(styleSheet, "List");
                    769:     styles[HTML_MENU] =                HTStyleNamed(styleSheet, "Menu");
                    770:     styles[HTML_DIR] =         HTStyleNamed(styleSheet, "Dir");    
1.16      timbl     771: /*  styles[HTML_DLC] =         HTStyleNamed(styleSheet, "GlossaryCompact"); */
1.2       timbl     772:     styles[HTML_ADDRESS]=      HTStyleNamed(styleSheet, "Address");
                    773:     styles[HTML_BLOCKQUOTE]=   HTStyleNamed(styleSheet, "BlockQuote");
                    774:     styles[HTML_PLAINTEXT] =
                    775:     styles[HTML_XMP] =         HTStyleNamed(styleSheet, "Example");
                    776:     styles[HTML_PRE] =         HTStyleNamed(styleSheet, "Preformatted");
                    777:     styles[HTML_LISTING] =     HTStyleNamed(styleSheet, "Listing");
                    778: }
                    779: /*                             P U B L I C
                    780: */
                    781: 
                    782: /*     Structured Object Class
                    783: **     -----------------------
                    784: */
                    785: PUBLIC CONST HTStructuredClass HTMLPresentation = /* As opposed to print etc */
                    786: {              
                    787:        "text/html",
                    788:        HTML_free,
1.14      timbl     789:        HTML_abort,
1.2       timbl     790:        HTML_put_character,     HTML_put_string,  HTML_write,
                    791:        HTML_start_element,     HTML_end_element,
                    792:        HTML_put_entity
                    793: }; 
1.1       timbl     794: 
1.4       timbl     795: 
1.2       timbl     796: /*             New Structured Text object
                    797: **             --------------------------
                    798: **
1.16      timbl     799: **     The structured stream can generate either presentation,
1.4       timbl     800: **     or plain text, or HTML.
1.1       timbl     801: */
1.16      timbl     802: PUBLIC HTStructured* HTML_new ARGS5(
                    803:        HTRequest *,            request,
                    804:        void *,                 param,
                    805:        HTFormat,               input_format,
                    806:        HTFormat,               output_format,
                    807:        HTStream *,             output_stream)
1.1       timbl     808: {
                    809: 
1.4       timbl     810:     HTStructured * me;
                    811:     
1.16      timbl     812:     if (output_format != WWW_PLAINTEXT
                    813:        && output_format != WWW_PRESENT
                    814:        && output_format != HTAtom_for("text/x-c")) {
1.37      frystyk   815:         HTStream * intermediate = HTStreamStack(WWW_HTML, output_format,
                    816:                                                output_stream, request, NO);
1.6       timbl     817:        if (intermediate) return HTMLGenerator(intermediate);
1.41    ! frystyk   818:         fprintf(TDEST, "** Internal error: can't parse HTML to %s\n",
1.16      timbl     819:                        HTAtom_name(output_format));
1.4       timbl     820:        exit (-99);
                    821:     }
                    822: 
                    823:     me = (HTStructured*) malloc(sizeof(*me));
                    824:     if (me == NULL) outofmem(__FILE__, "HTML_new");
1.1       timbl     825: 
                    826:     if (!got_styles) get_styles();
                    827: 
1.4       timbl     828:     me->isa = &HTMLPresentation;
1.16      timbl     829:     me->dtd = &DTD;
                    830:     me->node_anchor =  request->anchor;
1.4       timbl     831:     me->title.size = 0;
                    832:     me->title.growby = 128;
                    833:     me->title.allocated = 0;
                    834:     me->title.data = 0;
                    835:     me->text = 0;
                    836:     me->style_change = YES; /* Force check leading to text creation */
                    837:     me->new_style = default_style;
                    838:     me->old_style = 0;
                    839:     me->sp = me->stack + MAX_NESTING - 1;
                    840:     me->sp->tag_number = -1;                           /* INVALID */
                    841:     me->sp->style = default_style;                     /* INVALID */
1.1       timbl     842:     
1.4       timbl     843:     me->comment_start = NULL;
                    844:     me->comment_end = NULL;
1.16      timbl     845:     me->target = output_stream;
                    846:     if (output_stream) me->targetClass = *output_stream->isa;  /* Copy pointers */
1.1       timbl     847:     
1.4       timbl     848:     return (HTStructured*) me;
1.1       timbl     849: }
                    850: 
                    851: 
1.2       timbl     852: /*     HTConverter for HTML to plain text
                    853: **     ----------------------------------
1.1       timbl     854: **
1.2       timbl     855: **     This will convert from HTML to presentation or plain text.
1.1       timbl     856: */
1.16      timbl     857: PUBLIC HTStream* HTMLToPlain ARGS5(
                    858:        HTRequest *,            request,
                    859:        void *,                 param,
                    860:        HTFormat,               input_format,
                    861:        HTFormat,               output_format,
                    862:        HTStream *,             output_stream)
1.1       timbl     863: {
1.16      timbl     864:     return SGML_new(&DTD, HTML_new(
                    865:        request, NULL, input_format, output_format, output_stream));
1.1       timbl     866: }
                    867: 
                    868: 
1.2       timbl     869: /*     HTConverter for HTML to C code
                    870: **     ------------------------------
                    871: **
1.36      frystyk   872: **     C code is like plain text but all non-preformatted code
1.2       timbl     873: **     is commented out.
                    874: **     This will convert from HTML to presentation or plain text.
                    875: */
1.16      timbl     876: PUBLIC HTStream* HTMLToC ARGS5(
                    877:        HTRequest *,            request,
                    878:        void *,                 param,
                    879:        HTFormat,               input_format,
                    880:        HTFormat,               output_format,
                    881:        HTStream *,             output_stream)
1.1       timbl     882: {
1.4       timbl     883:     
                    884:     HTStructured * html;
                    885:     
1.36      frystyk   886:     (*output_stream->isa->put_string)(output_stream, "/* "); /* Before title */
1.16      timbl     887:     html = HTML_new(request, NULL, input_format, output_format, output_stream);
1.2       timbl     888:     html->comment_start = "/* ";
1.16      timbl     889:     html->dtd = &DTD;
1.2       timbl     890:     html->comment_end = " */\n";       /* Must start in col 1 for cpp */
1.16      timbl     891:     return SGML_new(&DTD, html);
1.1       timbl     892: }
                    893: 
                    894: 
1.2       timbl     895: /*     Presenter for HTML
                    896: **     ------------------
                    897: **
                    898: **     This will convert from HTML to presentation or plain text.
                    899: **
                    900: **     Override this if you have a windows version
1.1       timbl     901: */
1.2       timbl     902: #ifndef GUI
1.16      timbl     903: PUBLIC HTStream* HTMLPresent ARGS5(
                    904:        HTRequest *,            request,
                    905:        void *,                 param,
                    906:        HTFormat,               input_format,
                    907:        HTFormat,               output_format,
                    908:        HTStream *,             output_stream)
1.1       timbl     909: {
1.16      timbl     910:     return SGML_new(&DTD, HTML_new(
                    911:        request, NULL, input_format, output_format, output_stream));
1.1       timbl     912: }
1.2       timbl     913: #endif
1.1       timbl     914: 
                    915: 
1.2       timbl     916: /*     Record error message as a hypertext object
                    917: **     ------------------------------------------
                    918: **
                    919: **     The error message should be marked as an error so that
                    920: **     it can be reloaded later.
                    921: **     This implementation just throws up an error message
                    922: **     and leaves the document unloaded.
1.9       timbl     923: **     A smarter implementation would load an error document,
                    924: **     marking at such so that it is retried on reload.
1.1       timbl     925: **
1.2       timbl     926: ** On entry,
                    927: **     sink    is a stream to the output device if any
                    928: **     number  is the HTTP error number
                    929: **     message is the human readable message.
1.9       timbl     930: **
                    931: ** On exit,
                    932: **     returns a negative number to indicate lack of success in the load.
1.1       timbl     933: */
1.2       timbl     934: 
                    935: PUBLIC int HTLoadError ARGS3(
1.17      luotonen  936:        HTRequest *,    req,
1.2       timbl     937:        int,            number,
                    938:        CONST char *,   message)
                    939: {
1.20      frystyk   940:     char *err = "Oh I screwed up!";            /* Dummy pointer not used (I hope) */
1.2       timbl     941:     HTAlert(message);          /* @@@@@@@@@@@@@@@@@@@ */
1.20      frystyk   942:     /* Clean up! Henrik 04/03-94 */
                    943:     if (req && req->output_stream)
                    944:        (*req->output_stream->isa->abort)(req->output_stream, err);
1.33      frystyk   945: #if OLD_CODE
1.25      luotonen  946:     HTClearErrors(req);
1.33      frystyk   947: #endif
1.2       timbl     948:     return -number;
                    949: } 
1.29      frystyk   950: 

Webmaster