Annotation of libwww/Library/src/HTTeXGen.c, revision 2.10
2.10 ! frystyk 1: /* HTTeXGen.c
! 2: ** HTML -> LaTeX CONVERTER
! 3: **
! 4: ** (c) COPYRIGHT CERN 1994.
! 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.1 frystyk 6: **
7: ** This version of the HTML object sends LaTeX to the output stream.
8: ** No attributes are considered in the translation!
9: ** The module uses simple 1:1 table-conversions, but this COULD be
10: ** expanded to a stack-machine. This would then be in start_element and
11: ** end_element...
12: ** Henrik 07/03-94
2.7 duns 13: **
14: ** HISTORY:
15: ** 8 Jul 94 FM Insulate free() from _free structure element.
16: **
2.1 frystyk 17: */
18:
19: #define BUFFER_SIZE 80 /* Line buffer attempts to make neat breaks */
2.8 frystyk 20: #define WORD_DELIMITERS ",;:[]()"
2.1 frystyk 21:
22: /* Implements: */
23: #include "HTTeXGen.h"
24: #include "HTMLPDTD.h"
25: #include "HTStream.h"
26: #include "SGML.h"
27: #include "HTFormat.h"
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: CONST SGML_dtd * dtd;
44:
2.5 frystyk 45: char buffer[2*BUFFER_SIZE]; /* See note */
2.1 frystyk 46: char * write_pointer;
47: char * line_break;
48: BOOL sensitive; /* Can we put \n */
49: BOOL preformatted; /* Is it verbatim? */
50: BOOL markup; /* If doing LaTeX markup */
51: BOOL startup; /* To skip MIME header */
52: };
2.5 frystyk 53:
54: /* The buffer has to be bigger than 80 as latex markup might make the line
55: longer before we get to flush it. */
2.1 frystyk 56:
2.4 frystyk 57: PRIVATE char *TeX_names[HTMLP_ELEMENTS][2] = {
2.1 frystyk 58: { "", "" }, /* HTML_A */
59: { "", "" }, /* HTML_ABBREV */
60: { "\n\\begin{abstract}\n","\n\\end{abstract}\n"}, /* HTML_ABSTRACT */
61: { "", "" }, /* HTML_ACRONYM */
62: { "", "" }, /* HTML_ADDED */
63: { "{\\it ", "}" }, /* HTML_ADDRESS */
64: { "", "" }, /* HTML_ARG */
65: { "{\\bf ", "}" }, /* HTML_B */
66: { "", "" }, /* HTML_BASE */
67: { "{\\sf ", "}" }, /* HTML_BLOCKQUOTE */
68: { "", "" }, /* HTML_BODY */
69: { "", "" }, /* HTML_BOX */
70: { "", "" }, /* HTML_BR */
71: { "", "" }, /* HTML_BYLINE */
72: { "", "" }, /* HTML_CAPTION */
73: { "", "" }, /* HTML_CHANGED */
74: { "\\cite{", "}" }, /* HTML_CITE */
75: { "", "" }, /* HTML_CMD */
76: { "{\\tt ", "}" }, /* HTML_CODE */
77: { "\n\\typeout{", "}\n" }, /* HTML_COMMENT */
78: { "]", "" }, /* HTML_DD */
79: { "", "" }, /* HTML_DFN */
80: { "", "" }, /* HTML_DIR */
81: { "\n\\begin{description}","\n\\end{description}\n"}, /* HTML_DL */
82: { "\n\\item[", "" }, /* HTML_DT */
83: { "{\\em ", "}" }, /* HTML_EM */
84: { "", "" }, /* HTML_FIG */
85: { "\n\\footnote{", "}\n" }, /* HTML_FOOTNOTE */
86: { "", "" }, /* HTML_FORM */
87: { "\n\\chapter{", "}\n" }, /* HTML_H1 */
88: { "\n\\section{", "}\n" }, /* HTML_H2 */
89: { "\n\\subsection{","}\n" }, /* HTML_H3 */
90: { "\n\\subsubsection{","}\n" }, /* HTML_H4 */
91: { "\n\\paragraph{", "}\n" }, /* HTML_H5 */
92: { "\n\\subparagraph{","}\n" }, /* HTML_H6 */
93: { "", "\n" }, /* HTML_H7 */
94: { "", "" }, /* HTML_HEAD */
95: { "", "" }, /* HTML_HR */
96: { "", "" }, /* HTML_HTML */
2.3 duns 97: { "", "" }, /* HTML_HTMLPLUS */
2.1 frystyk 98: { "{\\it ", "}" }, /* HTML_I */
99: { "", "" }, /* HTML_IMAGE */
2.8 frystyk 100: { "_FIGUR_", "" }, /* HTML_IMG */
2.1 frystyk 101: { "", "" }, /* HTML_INPUT */
102: { "", "" }, /* HTML_ISINDEX */
103: { "{\\tt ", "}" }, /* HTML_KBD */
104: { "", "" }, /* HTML_L */
105: { "\n\\item ", "" }, /* HTML_LI */
106: { "", "" }, /* HTML_LINK */
107: { "", "" }, /* HTML_LISTING */
108: { "", "" }, /* HTML_LIT */
109: { "", "" }, /* HTML_MARGIN */
110: { "", "" }, /* HTML_MATH */
111: { "", "" }, /* HTML_MENU */
112: { "", "" }, /* HTML_NEXTID */
113: { "", "" }, /* HTML_NOTE */
114: { "\n\\begin{enumerate}\n","\n\\end{enumerate}\n"}, /* HTML_OL */
115: { "", "" }, /* HTML_OPTION */
116: { "", "" }, /* HTML_OVER */
117: { "\n\n", "" }, /* HTML_P */
118: { "", "" }, /* HTML_PERSON */
119: { "", "" }, /* HTML_PLAINTEXT */
120: { "\n\\begin{verbatim}"," \\end{verbatim}\n"}, /* HTML_PRE */
121: { "", "" }, /* HTML_Q */
122: { "\\begin{quote}", "\\end{quote}"}, /* HTML_QUOTE */
123: { "", "" }, /* HTML_RENDER */
124: { "", "" }, /* HTML_REMOVED */
125: { "", "" }, /* HTML_S */
126: { "", "" }, /* HTML_SAMP */
127: { "", "" }, /* HTML_SELECT */
128: { "{\\bf ", "}" }, /* HTML_STRONG */
129: { "", "" }, /* HTML_SUB */
130: { "", "" }, /* HTML_SUP */
131: { "", "" }, /* HTML_TAB */
132: { "", "" }, /* HTML_TABLE */
133: { "", "" }, /* HTML_TD */
134: { "", "" }, /* HTML_TEXTAREA */
135: { "", "" }, /* HTML_TH */
136: { "\n\\title{", "}\n\\author{}\n\\maketitle\n"}, /* HTML_TITLE */
137: { "", "" }, /* HTML_TR */
138: { "", "" }, /* HTML_TT */
139: { "", "" }, /* HTML_U */
140: { "\n\\begin{itemize}","\n\\end{itemize}\n"}, /* HTML_UL */
141: { "", "" }, /* HTML_VAR */
142: { "{\\sf ", "}" } /* HTML_XMP */
143: };
144:
2.8 frystyk 145: PRIVATE char *TeX_entities[HTML_ENTITIES] = {
2.1 frystyk 146: "\\AE ", /*"AElig", capital AE diphthong (ligature) */
2.2 frystyk 147: "\\\'{A}", /*"Aacute", capital A, acute accent */
148: "\\^{A}", /*"Acirc", capital A, circumflex accent */
149: "\\`{A}", /*"Agrave", capital A, grave accent */
150: "\\AA", /*"Aring", capital A, ring */
151: "\\~{A}", /*"Atilde", capital A, tilde */
152: "\\\"{A}", /*"Auml", capital A, dieresis or umlaut mark */
153: "\\c{C}", /*"Ccedil", capital C, cedilla */
154: "\\OE ", /*"ETH", capital Eth, Icelandic */
155: "\\\'{E}", /*"Eacute", capital E, acute accent */
156: "\\^{E}", /*"Ecirc", capital E, circumflex accent */
157: "\\`{E}", /*"Egrave", capital E, grave accent */
158: "\\\"{E}", /*"Euml", capital E, dieresis or umlaut mark */
159: "\\\'{I}", /*"Iacute", capital I, acute accent */
160: "\\^{I}", /*"Icirc", capital I, circumflex accent */
161: "\\`{I}", /*"Igrave", capital I, grave accent */
162: "\\\"{I}", /*"Iuml", capital I, dieresis or umlaut mark */
163: "\\~{N}", /*"Ntilde", capital N, tilde */
164: "\\\'{O}", /*"Oacute", capital O, acute accent */
165: "\\^{O}", /*"Ocirc", capital O, circumflex accent */
166: "\\`{O}", /*"Ograve", capital O, grave accent */
2.1 frystyk 167: "\\O ", /*"Oslash", capital O, slash */
2.2 frystyk 168: "\\~{O}", /*"Otilde", capital O, tilde */
169: "\\\"{O}", /*"Ouml", capital O, dieresis or umlaut mark */
170: " ", /*"THORN", capital THORN, Icelandic */
171: "\\\'{U}", /*"Uacute", capital U, acute accent */
172: "\\^{U}", /*"Ucirc", capital U, circumflex accent */
173: "\\`{U}", /*"Ugrave", capital U, grave accent */
174: "\\\"{U}", /*"Uuml", capital U, dieresis or umlaut mark */
175: "\\\'{Y}", /*"Yacute", capital Y, acute accent */
176: "\\\'{a}", /*"aacute", small a, acute accent */
177: "\\^{a}", /*"acirc", small a, circumflex accent */
2.1 frystyk 178: "\\ae ", /*"aelig", small ae diphthong (ligature) */
2.2 frystyk 179: "\\`{a}", /*"agrave", small a, grave accent */
2.1 frystyk 180: "&", /*"amp", ampersand */
181: "\\aa ", /*"aring", small a, ring */
2.2 frystyk 182: "\\~{a}", /*"atilde", small a, tilde */
183: "\\\"{a}", /*"auml", small a, dieresis or umlaut mark */
184: "\\c{c}", /*"ccedil", small c, cedilla */
185: "\\\'{e}", /*"eacute", small e, acute accent */
186: "\\^{c}", /*"ecirc", small e, circumflex accent */
187: "\\`{c}", /*"egrave", small e, grave accent */
188: "\\oe ", /*"eth", small eth, Icelandic */
189: "\\\"{e}", /*"euml", small e, dieresis or umlaut mark */
2.1 frystyk 190: ">", /*"gt", greater than */
2.2 frystyk 191: "\\\'{\\i}", /*"iacute", small i, acute accent */
192: "\\^{\\i}", /*"icirc", small i, circumflex accent */
193: "\\`{\\i}", /*"igrave", small i, grave accent */
194: "\\\"{\\i}", /*"iuml", small i, dieresis or umlaut mark */
2.1 frystyk 195: "<", /*"lt", less than */
2.2 frystyk 196: "\\~{n}", /*"ntilde", small n, tilde */
197: "\\\'{o}", /*"oacute", small o, acute accent */
198: "\\~{o}", /*"ocirc", small o, circumflex accent */
199: "\\`{o}", /*"ograve", small o, grave accent */
2.1 frystyk 200: "\\o ", /*"oslash", small o, slash */
2.2 frystyk 201: "\\~{o}", /*"otilde", small o, tilde */
202: "\\\"{o}", /*"ouml", small o, dieresis or umlaut mark */
2.8 frystyk 203: "\"", /*"quot", double quote sign - June 1994 */
2.1 frystyk 204: "\\ss ", /*"szlig", small sharp s, German (sz ligature)*/
2.2 frystyk 205: " ", /*"thorn", small thorn, Icelandic */
206: "\\\'{u}", /*"uacute", small u, acute accent */
207: "\\^{u}", /*"ucirc", small u, circumflex accent */
208: "\\`{u}", /*"ugrave", small u, grave accent */
209: "\\\"{u}", /*"uuml", small u, dieresis or umlaut mark */
210: "\\\'{y}", /*"yacute", small y, acute accent */
211: "\\\"{y}" /*"yuml", small y, dieresis or umlaut mark */
2.1 frystyk 212: };
213:
214:
215: /* Flush Buffer
216: ** ------------
217: */
218: PRIVATE void HTTeXGen_flush ARGS1(HTStructured *, me)
219: {
220: (*me->targetClass.put_block)(me->target,
221: me->buffer,
222: me->write_pointer - me->buffer);
223: me->write_pointer = me->buffer;
224: me->line_break = me->buffer;
225: }
226:
227:
228: /* Character handling
229: ** ------------------
230: **
231: */
232: PRIVATE void HTTeXGen_put_character ARGS2(HTStructured *, me, char, c)
233: {
234: if (!me->startup) /* To skip MIME header */
235: return;
236: if (c=='\n') {
237: if (me->markup || me->preformatted) { /* Put out as is and flush */
238: *me->write_pointer++ = c;
239: HTTeXGen_flush(me);
240: return;
241: } else if (me->sensitive || *(me->write_pointer-1)==' ') {
242: return;
243: } else
244: *me->write_pointer++ = ' '; /* Try to pretty print */
245: } else if (me->markup || me->preformatted) {
246: *me->write_pointer++ = c;
247: } else if (c==' ' || c=='\t') { /* Skip space and tabs */
248: if (*(me->write_pointer-1) != ' ')
249: *me->write_pointer++ = ' ';
250: else
251: return;
252: } else {
253: if (c=='$' || c=='&' || c=='%' || c=='#' || /* Special chars */
254: c=='{' || c=='}' || c=='_') {
255: *me->write_pointer++ = '\\';
256: *me->write_pointer++ = c;
257: } else if (c=='\\') { /* Special names */
258: char *temp = "$\\backslash$";
259: strcpy(me->write_pointer, temp);
260: me->write_pointer += strlen(temp);
261: } else if (c=='^') {
262: char *temp = "$\\hat{ }$";
263: strcpy(me->write_pointer, temp);
264: me->write_pointer += strlen(temp);
265: } else if (c=='~') {
266: char *temp = "$\\tilde{ }$";
267: strcpy(me->write_pointer, temp);
268: me->write_pointer += strlen(temp);
269: } else if (c=='|' || c=='<' || c=='>') { /* Math mode */
270: *me->write_pointer++ = '$';
271: *me->write_pointer++ = c;
272: *me->write_pointer++ = '$';
273: } else
274: *me->write_pointer++ = c; /* Char seems normal */
275: }
276:
2.6 frystyk 277: if (c==' ') /* Find delimiter */
2.1 frystyk 278: me->line_break = me->write_pointer;
279: else if (strchr(WORD_DELIMITERS, c))
280: me->line_break = me->write_pointer-1;
281:
282: /* Flush buffer out when full */
283: if (me->write_pointer >= me->buffer+BUFFER_SIZE-3) {
2.9 frystyk 284: #ifdef OLD_CODE
2.1 frystyk 285: if (me->markup || me->preformatted) {
2.9 frystyk 286: #endif /* OLD_CODE */
2.8 frystyk 287: if (me->preformatted) {
2.1 frystyk 288: *me->write_pointer = '\n';
289: (*me->targetClass.put_block)(me->target,
290: me->buffer,
291: me->write_pointer-me->buffer+1);
292: me->write_pointer = me->buffer;
293: } else { /* Use break-point */
294: char line_break_char = *me->line_break;
295: char *saved = me->line_break;
296: *me->line_break = '\n';
297: (*me->targetClass.put_block)(me->target,
298: me->buffer,
299: me->line_break-me->buffer+1);
300: *me->line_break = line_break_char;
301: { /* move next line in */
302: char *p = saved;
303: char *q;
304: for(q=me->buffer; p<me->write_pointer; )
305: *q++ = *p++;
306: }
307: me->write_pointer = me->buffer + (me->write_pointer-saved);
308: }
309: me->line_break = me->buffer;
310: }
2.9 frystyk 311: return;
2.1 frystyk 312: }
313:
314:
315:
316: /* String handling
317: ** ---------------
318: */
319: PRIVATE void HTTeXGen_put_string ARGS2(HTStructured *, me, CONST char*, s)
320: {
321: CONST char * p;
322: for (p=s; *p; p++)
323: HTTeXGen_put_character(me, *p);
324: }
325:
326:
327: PRIVATE void HTTeXGen_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
328: {
329: CONST char * p;
330: for(p=s; p<s+l; p++)
331: HTTeXGen_put_character(me, *p);
332: }
333:
334:
335: /* Start Element
336: ** -------------
337: **
338: ** No attributes are put to the output Henrik 07/03-94
339: ** Does no assumptions of WHAT element is started...
340: */
341: PRIVATE void HTTeXGen_start_element ARGS4(
342: HTStructured *, me,
343: int, element_number,
344: CONST BOOL*, present,
345: CONST char **, value)
346: {
347: me->startup = YES; /* Now, let's get down to it */
2.6 frystyk 348: if (me->preformatted == YES) { /* Don't start markup in here */
349: if (TRACE)
350: fprintf(stderr, "LaTeX....... No Markup in verbatim mode\n");
2.1 frystyk 351: return;
2.6 frystyk 352: }
2.1 frystyk 353: if (element_number == HTML_PRE)
354: me->preformatted = YES;
355: if (element_number == HTML_CITE || /* No \n here, please! */
356: element_number == HTML_DT ||
357: element_number == HTML_H1 ||
358: element_number == HTML_H2 ||
359: element_number == HTML_H3 ||
360: element_number == HTML_H4 ||
361: element_number == HTML_H5 ||
362: element_number == HTML_H6 ||
363: element_number == HTML_H7 ||
364: element_number == HTML_TITLE)
365: me->sensitive = YES;
366: else if (element_number == HTML_DD) /* Only way to turn <DT> off */
367: me->sensitive = NO;
368: me->markup = element_number == HTML_A ? NO : YES;
369: HTTeXGen_put_string(me, *TeX_names[element_number]);
370: me->markup = NO;
371: }
372:
373:
374: /* End Element
375: ** -----------
376: **
377: ** Ends an markup element Henrik 07/03-94
378: ** Does no assumptions of WHAT element is ended...
379: */
380: PRIVATE void HTTeXGen_end_element ARGS2(HTStructured *, me,
2.9 frystyk 381: int , element_number)
2.1 frystyk 382: {
2.6 frystyk 383: if (me->preformatted && element_number != HTML_PRE) {
384: if (TRACE)
385: fprintf(stderr, "LaTeX....... No markup in verbatim mode\n");
386: return;
387: }
2.1 frystyk 388: me->preformatted = NO;
389: me->markup = YES;
390: HTTeXGen_put_string(me, *(TeX_names[element_number]+1));
391: me->markup = NO;
392: if (element_number == HTML_CITE ||
393: element_number == HTML_DL ||
394: element_number == HTML_H1 ||
395: element_number == HTML_H2 ||
396: element_number == HTML_H3 ||
397: element_number == HTML_H4 ||
398: element_number == HTML_H5 ||
399: element_number == HTML_H6 ||
400: element_number == HTML_H7 ||
401: element_number == HTML_TITLE)
402: me->sensitive = NO;
403: }
404:
405:
406: /* Expanding entities
407: ** ------------------
408: **
409: */
410: PRIVATE void HTTeXGen_put_entity ARGS2(HTStructured *, me, int, entity_number)
411: {
412: BOOL mark = me->markup;
413: if (*TeX_entities[entity_number] != '&' && /* Theese are converted later */
414: *TeX_entities[entity_number] != '<' &&
415: *TeX_entities[entity_number] != '>')
416: me->markup = YES;
417: HTTeXGen_put_string(me, TeX_entities[entity_number]);
418: me->markup = mark;
419: }
420:
421:
422:
423: /* Free an HTML object
424: ** -------------------
425: **
426: */
2.9 frystyk 427: PRIVATE int HTTeXGen_free ARGS1(HTStructured *, me)
2.1 frystyk 428: {
429: HTTeXGen_flush(me);
430: (*me->targetClass.put_string)(me->target, "\n\\end{document}\n");
431: HTTeXGen_flush(me);
2.7 duns 432: (*me->targetClass._free)(me->target); /* ripple through */
2.1 frystyk 433: free(me);
2.9 frystyk 434: return 0;
2.1 frystyk 435: }
436:
437:
2.9 frystyk 438: PRIVATE int HTTeXGen_abort ARGS2(HTStructured *, me, HTError, e)
2.1 frystyk 439: {
440: HTTeXGen_free(me);
2.9 frystyk 441: return EOF;
2.1 frystyk 442: }
443:
444:
445: /* Structured Object Class
446: ** -----------------------
447: */
448: PRIVATE CONST HTStructuredClass HTTeXGeneration = /* As opposed to print etc */
449: {
450: "HTMLToTeX",
451: HTTeXGen_free,
452: HTTeXGen_abort,
453: HTTeXGen_put_character, HTTeXGen_put_string, HTTeXGen_write,
454: HTTeXGen_start_element, HTTeXGen_end_element,
455: HTTeXGen_put_entity
456: };
457:
458:
459: /* HTConverter from HTML to TeX Stream
460: ** ------------------------------------------
461: **
462: */
463: PUBLIC HTStream* HTMLToTeX ARGS5(
464: HTRequest *, request,
465: void *, param,
466: HTFormat, input_format,
467: HTFormat, output_format,
468: HTStream *, output_stream)
469: {
470: HTStructured* me = (HTStructured*) calloc(1, sizeof(*me));
471: if (me == NULL) outofmem(__FILE__, "HTMLToTeX");
472:
473: me->isa = (HTStructuredClass*) &HTTeXGeneration;
474: me->dtd = &HTMLP_dtd;
475: me->target = output_stream;
476: me->targetClass = *me->target->isa;/* Copy pointers to routines for speed*/
477: me->write_pointer = me->buffer;
478: me->line_break = me->buffer;
479: (*me->targetClass.put_string)(me->target,
480: "\\documentstyle[11pt]{report}\n\\begin{document}\n");
481: return SGML_new(&HTMLP_dtd, me);
482: }
483:
484:
485: /* END OF FILE HTTeXGen.c */
486:
487:
488:
489:
490:
491:
492:
493:
Webmaster