HTML.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 25k
Category:

Browser Client

Development Platform:

Unix_Linux

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