home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTMLGen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  5.3 KB  |  241 lines

  1. /*        HTML Generator
  2. **        ==============
  3. **
  4. **    This version of the HTML object sends HTML markup to the output stream.
  5. **
  6. ** Bugs:    Line wrapping is not done at all.
  7. **        All data handled as PCDATA.
  8. **        Should convert old XMP, LISTING and PLAINTEXT to PRE.
  9. **
  10. **    It is not obvious to me right now whether the HEAD should be generated
  11. **    from the incomming data or the anchor.  Currently it's from the former
  12. **    which is cleanest.
  13. */
  14.  
  15. /* Implements:
  16. */
  17. #include "HTMLGen.h"
  18.  
  19. #include <stdio.h>
  20. #include "HTMLDTD.h"
  21. #include "HTStream.h"
  22. #include "SGML.h"
  23. #include "HTFormat.h"
  24.  
  25. #define PUTC(c) (*me->targetClass.put_character)(me->target, c)
  26. #define PUTS(s) (*me->targetClass.put_string)(me->target, s)
  27. #define PUTB(s,l) (*me->targetClass.put_block)(me->target, s, l)
  28.  
  29. /*        HTML Object
  30. **        -----------
  31. */
  32.  
  33. struct _HTStream {
  34.     CONST HTStreamClass *        isa;    
  35.     HTStream *             target;
  36.     HTStreamClass            targetClass;    /* COPY for speed */
  37. };
  38.  
  39. struct _HTStructured {
  40.     CONST HTStructuredClass *    isa;
  41.     HTStream *             target;
  42.     HTStreamClass            targetClass;    /* COPY for speed */
  43. };
  44.  
  45.  
  46. /*    Character handling
  47. **    ------------------
  48. */
  49. PRIVATE void HTMLGen_put_character ARGS2(HTStructured *, me, char, c)
  50. {
  51.     PUTC(c);
  52. }
  53.  
  54.  
  55.  
  56. /*    String handling
  57. **    ---------------
  58. */
  59. PRIVATE void HTMLGen_put_string ARGS2(HTStructured *, me, CONST char*, s)
  60. {
  61.     PUTS(s);
  62. }
  63.  
  64. PRIVATE void HTMLGen_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
  65. {
  66.     PUTB(s,l);
  67. }
  68.  
  69.  
  70. /*    Start Element
  71. **    -------------
  72. */
  73. PRIVATE void HTMLGen_start_element ARGS4(
  74.     HTStructured *,     me,
  75.     int,            element_number,
  76.     CONST BOOL*,         present,
  77.     CONST char **,        value)
  78. {
  79.     int i;
  80.  
  81.     HTTag * tag = &HTML_dtd.tags[element_number];
  82.     PUTC('<');
  83.     PUTS(tag->name);
  84.     if (present) for (i=0; i< tag->number_of_attributes; i++) {
  85.         if (present[i]) {
  86.         PUTC(' ');
  87.         PUTS(tag->attributes[i].name);
  88.         if (value[i]) {
  89.          PUTS("=\"");
  90.         PUTS(value[i]);
  91.         PUTC('"');
  92.         }
  93.     }
  94.     }
  95.     PUTC('>');
  96. }
  97.  
  98.  
  99. /*        End Element
  100. **        -----------
  101. **
  102. */
  103. /*    When we end an element, the style must be returned to that
  104. **    in effect before that element.  Note that anchors (etc?)
  105. **    don't have an associated style, so that we must scan down the
  106. **    stack for an element with a defined style. (In fact, the styles
  107. **    should be linked to the whole stack not just the top one.)
  108. **    TBL 921119
  109. */
  110. PRIVATE void HTMLGen_end_element ARGS2(HTStructured *, me,
  111.             int , element_number)
  112. {
  113.     PUTS("</");
  114.     PUTS(HTML_dtd.tags[element_number].name);
  115.     PUTC('>');
  116. }
  117.  
  118.  
  119. /*        Expanding entities
  120. **        ------------------
  121. **
  122. */
  123.  
  124. PRIVATE void HTMLGen_put_entity ARGS2(HTStructured *, me, int, entity_number)
  125. {
  126.     PUTC('&');
  127.     PUTS(HTML_dtd.entity_names[entity_number]);
  128.     PUTC(';');
  129. }
  130.  
  131.  
  132.  
  133. /*    Free an HTML object
  134. **    -------------------
  135. **
  136. **    Note that the SGML parsing context is freed, but the created object is not,
  137. **    as it takes on an existence of its own unless explicitly freed.
  138. */
  139. PRIVATE void HTMLGen_free ARGS1(HTStructured *, me)
  140. {
  141.     (*me->targetClass.free)(me->target);    /* ripple through */
  142.     free(me);
  143. }
  144.  
  145.  
  146.  
  147. PRIVATE void HTMLGen_end_document ARGS1(HTStructured *, me)
  148. {
  149.     PUTC('\n');        /* Make sure ends with newline for sed etc etc */
  150.     (*me->targetClass.end_document)(me->target);
  151. }
  152.  
  153.  
  154. PRIVATE void HTMLGen_handle_interrupt ARGS1(HTStructured *, me)
  155. {
  156.     (*me->targetClass.handle_interrupt)(me->target);
  157. }
  158.  
  159.  
  160. PRIVATE void PlainToHTML_end_document ARGS1(HTStructured *, me)
  161. {
  162.     PUTS("</PRE></BODY>\n");/* Make sure ends with newline for sed etc etc */
  163.     (*me->targetClass.end_document)(me->target);
  164. }
  165.  
  166.  
  167.  
  168. /*    Structured Object Class
  169. **    -----------------------
  170. */
  171. PRIVATE CONST HTStructuredClass HTMLGeneration = /* As opposed to print etc */
  172. {        
  173.     "text/html",
  174.     HTMLGen_free,
  175.     HTMLGen_end_document, HTMLGen_handle_interrupt,
  176.     HTMLGen_put_character,     HTMLGen_put_string, HTMLGen_write,
  177.     HTMLGen_start_element,     HTMLGen_end_element,
  178.     HTMLGen_put_entity
  179. }; 
  180.  
  181.  
  182. /*    Subclass-specific Methods
  183. **    -------------------------
  184. */
  185.  
  186. PUBLIC HTStructured * HTMLGenerator ARGS1(HTStream *, output)
  187. {
  188.     HTStructured* me = (HTStructured*)malloc(sizeof(*me));
  189.     if (me == NULL) outofmem(__FILE__, "HTMLGenerator");
  190.     me->isa = &HTMLGeneration;       
  191.  
  192.     me->target = output;
  193.     me->targetClass = *me->target->isa; /* Copy pointers to routines for speed*/
  194.  
  195.     return me;
  196. }
  197.  
  198. /*    Stream Object Class
  199. **    -------------------
  200. **
  201. **    This object just converts a plain text stream into HTML
  202. **    It is officially a structured strem but only the stream bits exist.
  203. **    This is just the easiest way of typecasting all the routines.
  204. */
  205. PRIVATE CONST HTStructuredClass PlainToHTMLConversion =
  206. {        
  207.     "plaintexttoHTML",
  208.     HTMLGen_free,    
  209.     PlainToHTML_end_document,    
  210.         HTMLGen_handle_interrupt,
  211.     HTMLGen_put_character,
  212.     HTMLGen_put_string,
  213.     HTMLGen_write,
  214.     NULL,        /* Structured stuff */
  215.     NULL,
  216.     NULL
  217. }; 
  218.  
  219.  
  220. /*    HTConverter from plain text to HTML Stream
  221. **    ------------------------------------------
  222. */
  223.  
  224. PUBLIC HTStream* HTPlainToHTML ARGS5(
  225.     HTPresentation *,    pres,
  226.     HTParentAnchor *,    anchor,    
  227.     HTStream *,        sink,
  228.         HTFormat,               format_in,
  229.         int,                    compressed)
  230. {
  231.     HTStream* me = (HTStream*)malloc(sizeof(*me));
  232.     me->isa = (HTStreamClass*) &PlainToHTMLConversion;       
  233.  
  234.     me->target = sink;
  235.     me->targetClass = *me->target->isa;
  236.         /* Copy pointers to routines for speed*/
  237.     
  238.     PUTS("<BODY>\n<PRE>\n");
  239.     return me;
  240. }
  241.