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

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

Webmaster