File: HTTeXGen.c

package info (click to toggle)
cern-httpd 3.0A-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 5,392 kB
  • ctags: 6,554
  • sloc: ansic: 37,902; makefile: 1,746; perl: 535; csh: 167; sh: 143
file content (487 lines) | stat: -rw-r--r-- 16,248 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* Simple LaTeX Generator that converts in a 1:1 manner from HTML to LaTeX
** =======================================================================
**
** 	This version of the HTML object sends LaTeX to the output stream.
** 	No attributes are considered in the translation!
** 	The module uses simple 1:1 table-conversions, but this COULD be
**	expanded to a stack-machine. This would then be in start_element and
**	end_element...
**						Henrik 07/03-94
**
** HISTORY:
**	 8 Jul 94  FM	Insulate free() from _free structure element.
**
*/

#define BUFFER_SIZE	80	/* Line buffer attempts to make neat breaks */
#define WORD_DELIMITERS ",;:[]()"

/* Implements: */
#include "HTTeXGen.h"
#include "HTMLPDTD.h"
#include "HTStream.h"
#include "SGML.h"
#include "HTFormat.h"

/*		HTML Object
**		-----------
*/

struct _HTStream {
	CONST HTStreamClass *		isa;	
	HTStream * 			target;
	HTStreamClass			targetClass;	   /* COPY for speed */
};

struct _HTStructured {
	CONST HTStructuredClass *	isa;
	HTStream * 			target;
	HTStreamClass			targetClass;	   /* COPY for speed */
	CONST SGML_dtd *		dtd;
	
	char				buffer[2*BUFFER_SIZE];   /* See note */
	char *				write_pointer;
	char *				line_break;
	BOOL				sensitive;          /* Can we put \n */
	BOOL				preformatted;     /* Is it verbatim? */
	BOOL				markup;     /* If doing LaTeX markup */
	BOOL				startup;      /* To skip MIME header */
};

/* The buffer has to be bigger than 80 as latex markup might make the line
   longer before we get to flush it. */

PRIVATE char *TeX_names[HTMLP_ELEMENTS][2] = {
    { "",       	""		},	/* HTML_A		*/
    { "",		""		},	/* HTML_ABBREV		*/
    { "\n\\begin{abstract}\n","\n\\end{abstract}\n"},  /* HTML_ABSTRACT	*/
    { "",		""		},	/* HTML_ACRONYM		*/
    { "",		""		},	/* HTML_ADDED		*/
    { "{\\it ",		"}"		},	/* HTML_ADDRESS		*/
    { "",		""		},	/* HTML_ARG		*/
    { "{\\bf ",		"}"		},	/* HTML_B		*/
    { "",		""		},	/* HTML_BASE		*/
    { "{\\sf ",		"}"		},	/* HTML_BLOCKQUOTE 	*/
    { "", 		""		},	/* HTML_BODY     	*/
    { "",		""		},	/* HTML_BOX		*/
    { "",		""		},	/* HTML_BR		*/
    { "",		""		},	/* HTML_BYLINE		*/
    { "",		""		},	/* HTML_CAPTION		*/
    { "",		""		},	/* HTML_CHANGED		*/
    { "\\cite{",      	"}"		},	/* HTML_CITE		*/
    { "",		""		},	/* HTML_CMD		*/
    { "{\\tt ",		"}"		},	/* HTML_CODE		*/
    { "\n\\typeout{",	"}\n"		},	/* HTML_COMMENT		*/
    { "]",		""		},	/* HTML_DD		*/
    { "",		""		},	/* HTML_DFN		*/
    { "",		""		},	/* HTML_DIR		*/
    { "\n\\begin{description}","\n\\end{description}\n"}, /* HTML_DL 	*/
    { "\n\\item[",	""		},	/* HTML_DT		*/
    { "{\\em ",		"}"		},	/* HTML_EM		*/
    { "",	""			},	/* HTML_FIG		*/
    { "\n\\footnote{",	"}\n"	       	},	/* HTML_FOOTNOTE	*/
    { "",	"" 			},	/* HTML_FORM		*/
    { "\n\\chapter{",	"}\n"		},	/* HTML_H1		*/
    { "\n\\section{",	"}\n"		},	/* HTML_H2		*/
    { "\n\\subsection{","}\n"  		},	/* HTML_H3		*/
    { "\n\\subsubsection{","}\n"	},	/* HTML_H4		*/
    { "\n\\paragraph{",	"}\n"		},	/* HTML_H5		*/
    { "\n\\subparagraph{","}\n"		},	/* HTML_H6		*/
    { "",		"\n"		},	/* HTML_H7		*/
    { "",		""		},	/* HTML_HEAD		*/
    { "",		""		},	/* HTML_HR		*/
    { "",		""		}, 	/* HTML_HTML		*/
    { "",		""		},    	/* HTML_HTMLPLUS	*/
    { "{\\it ",		"}"		},	/* HTML_I		*/
    { "",		""		},	/* HTML_IMAGE		*/
    { "_FIGUR_",	""		},	/* HTML_IMG		*/
    { "",		""		},	/* HTML_INPUT		*/
    { "",		""		},	/* HTML_ISINDEX		*/
    { "{\\tt ",		"}"		},	/* HTML_KBD		*/
    { "",		""		},	/* HTML_L		*/
    { "\n\\item ",      "" 	        },	/* HTML_LI		*/
    { "",		""		},	/* HTML_LINK		*/
    { "",		""		},	/* HTML_LISTING		*/
    { "",		""		},	/* HTML_LIT		*/
    { "",		""		},	/* HTML_MARGIN		*/
    { "",		""		},	/* HTML_MATH		*/
    { "",		""		},	/* HTML_MENU		*/
    { "",		""		},	/* HTML_NEXTID		*/
    { "",		""		},	/* HTML_NOTE		*/
    { "\n\\begin{enumerate}\n","\n\\end{enumerate}\n"}, /* HTML_OL     	*/
    { "",		""		},	/* HTML_OPTION		*/
    { "",		""		},	/* HTML_OVER		*/
    { "\n\n",		""		},	/* HTML_P		*/
    { "",		""		},	/* HTML_PERSON		*/
    { "",		""		},	/* HTML_PLAINTEXT	*/
    { "\n\\begin{verbatim}"," \\end{verbatim}\n"},  /* HTML_PRE        */
    { "",		""		},	/* HTML_Q		*/
    { "\\begin{quote}",	"\\end{quote}"}, 	/* HTML_QUOTE		*/
    { "",		""		},	/* HTML_RENDER		*/
    { "",		""		},	/* HTML_REMOVED		*/
    { "",		""		},	/* HTML_S		*/
    { "",		""		},	/* HTML_SAMP		*/
    { "", 		""		},	/* HTML_SELECT		*/
    { "{\\bf ",		"}"		},	/* HTML_STRONG		*/
    { "", 		""		},	/* HTML_SUB		*/
    { "",		""		},	/* HTML_SUP		*/
    { "",		""		},	/* HTML_TAB		*/
    { "",		""		},	/* HTML_TABLE		*/
    { "",		""		},	/* HTML_TD		*/
    { "",		""		},	/* HTML_TEXTAREA	*/
    { "",		""		},	/* HTML_TH		*/
    { "\n\\title{",	"}\n\\author{}\n\\maketitle\n"},  /* HTML_TITLE */
    { "",		""		},	/* HTML_TR		*/
    { "",		""		},	/* HTML_TT		*/
    { "",		""		},	/* HTML_U		*/
    { "\n\\begin{itemize}","\n\\end{itemize}\n"},   /* HTML_UL		*/
    { "",		""		},	/* HTML_VAR		*/
    { "{\\sf ",		"}"		}	/* HTML_XMP		*/
};

PRIVATE char *TeX_entities[HTML_ENTITIES] = {
    "\\AE ",		/*"AElig",	 capital AE diphthong (ligature) */ 
    "\\\'{A}",		/*"Aacute",	 capital A, acute accent */ 
    "\\^{A}",		/*"Acirc",	 capital A, circumflex accent */ 
    "\\`{A}",		/*"Agrave",	 capital A, grave accent */ 
    "\\AA",   	 	/*"Aring",	 capital A, ring */ 
    "\\~{A}", 		/*"Atilde",	 capital A, tilde */ 
    "\\\"{A}",		/*"Auml",	 capital A, dieresis or umlaut mark */ 
    "\\c{C}",		/*"Ccedil",	 capital C, cedilla */ 
    "\\OE ",		/*"ETH",	 capital Eth, Icelandic */ 
    "\\\'{E}",		/*"Eacute",	 capital E, acute accent */ 
    "\\^{E}",		/*"Ecirc",	 capital E, circumflex accent */ 
    "\\`{E}",		/*"Egrave",	 capital E, grave accent */ 
    "\\\"{E}",		/*"Euml",	 capital E, dieresis or umlaut mark */ 
    "\\\'{I}",		/*"Iacute",	 capital I, acute accent */ 
    "\\^{I}",		/*"Icirc",	 capital I, circumflex accent */ 
    "\\`{I}",		/*"Igrave",	 capital I, grave accent */ 
    "\\\"{I}",		/*"Iuml",	 capital I, dieresis or umlaut mark */ 
    "\\~{N}",		/*"Ntilde",	 capital N, tilde */ 
    "\\\'{O}",		/*"Oacute",	 capital O, acute accent */ 
    "\\^{O}",		/*"Ocirc",	 capital O, circumflex accent */ 
    "\\`{O}",		/*"Ograve",	 capital O, grave accent */ 
    "\\O ",            	/*"Oslash",	 capital O, slash */ 
    "\\~{O}",		/*"Otilde",	 capital O, tilde */ 
    "\\\"{O}",        	/*"Ouml",	 capital O, dieresis or umlaut mark */ 
    " ",      	/*"THORN",	 capital THORN, Icelandic */ 
    "\\\'{U}",		/*"Uacute",	 capital U, acute accent */ 
    "\\^{U}",		/*"Ucirc",	 capital U, circumflex accent */ 
    "\\`{U}",		/*"Ugrave",	 capital U, grave accent */ 
    "\\\"{U}",		/*"Uuml",	 capital U, dieresis or umlaut mark */ 
    "\\\'{Y}",		/*"Yacute",	 capital Y, acute accent */ 
    "\\\'{a}",		/*"aacute",	 small a, acute accent */ 
    "\\^{a}",		/*"acirc",	 small a, circumflex accent */ 
    "\\ae ",		/*"aelig",	 small ae diphthong (ligature) */ 
    "\\`{a}",		/*"agrave",	 small a, grave accent */ 
    "&",            	/*"amp",	 ampersand */ 
    "\\aa ",		/*"aring",	 small a, ring */ 
    "\\~{a}",		/*"atilde",	 small a, tilde */ 
    "\\\"{a}",		/*"auml",	 small a, dieresis or umlaut mark */ 
    "\\c{c}",		/*"ccedil",	 small c, cedilla */ 
    "\\\'{e}",		/*"eacute",	 small e, acute accent */ 
    "\\^{c}",		/*"ecirc",	 small e, circumflex accent */ 
    "\\`{c}",		/*"egrave",	 small e, grave accent */ 
    "\\oe ",		/*"eth",	 small eth, Icelandic */ 
    "\\\"{e}",		/*"euml",	 small e, dieresis or umlaut mark */ 
    ">",		/*"gt",	 greater than */ 
    "\\\'{\\i}",      	/*"iacute",	 small i, acute accent */ 
    "\\^{\\i}",       	/*"icirc",	 small i, circumflex accent */ 
    "\\`{\\i}",       	/*"igrave",	 small i, grave accent */ 
    "\\\"{\\i}",     	/*"iuml",	 small i, dieresis or umlaut mark */ 
    "<",		/*"lt",	 less than */ 
    "\\~{n}",		/*"ntilde",	 small n, tilde */ 
    "\\\'{o}",		/*"oacute",	 small o, acute accent */ 
    "\\~{o}",		/*"ocirc",	 small o, circumflex accent */ 
    "\\`{o}",		/*"ograve",	 small o, grave accent */ 
    "\\o ",            	/*"oslash",	 small o, slash */ 
    "\\~{o}",         	/*"otilde",	 small o, tilde */ 
    "\\\"{o}",	   	/*"ouml",	 small o, dieresis or umlaut mark */ 
    "\"",	   	/*"quot",	 double quote sign - June 1994 */ 
    "\\ss ",		/*"szlig",	 small sharp s, German (sz ligature)*/ 
    " ",       	/*"thorn",	 small thorn, Icelandic */ 
    "\\\'{u}",        	/*"uacute",	 small u, acute accent */ 
    "\\^{u}",        	/*"ucirc",	 small u, circumflex accent */ 
    "\\`{u}",         	/*"ugrave",	 small u, grave accent */ 
    "\\\"{u}",        	/*"uuml",	 small u, dieresis or umlaut mark */ 
    "\\\'{y}",		/*"yacute",	 small y, acute accent */ 
    "\\\"{y}"	    	/*"yuml",	 small y, dieresis or umlaut mark */ 
};


/*	Flush Buffer
**	------------
*/
PRIVATE void HTTeXGen_flush ARGS1(HTStructured *, me)
{
    (*me->targetClass.put_block)(me->target, 
				 me->buffer,
				 me->write_pointer - me->buffer);
    me->write_pointer = me->buffer;
    me->line_break = me->buffer;
}


/*	Character handling
**	------------------
**
*/
PRIVATE void HTTeXGen_put_character ARGS2(HTStructured *, me, char, c)
{
    if (!me->startup)		                      /* To skip MIME header */
	return;
    if (c=='\n') {
	if (me->markup || me->preformatted) {     /* Put out as is and flush */
	    *me->write_pointer++ = c;
	    HTTeXGen_flush(me);
	    return;
	} else if (me->sensitive || *(me->write_pointer-1)==' ') {
	    return;
        } else
	    *me->write_pointer++ = ' ';		      /* Try to pretty print */
    } else if (me->markup || me->preformatted) {
	*me->write_pointer++ = c;
    } else if (c==' ' || c=='\t') {	              /* Skip space and tabs */
	if (*(me->write_pointer-1) != ' ')
	    *me->write_pointer++ = ' ';
	else
	    return;
    } else {
	if (c=='$' || c=='&' || c=='%' || c=='#' ||         /* Special chars */
	    c=='{' || c=='}' || c=='_') {
	    *me->write_pointer++ = '\\';
	    *me->write_pointer++ = c;
	} else if (c=='\\') {			            /* Special names */
	    char *temp = "$\\backslash$";
	    strcpy(me->write_pointer, temp);
	    me->write_pointer += strlen(temp);
	} else if (c=='^') {
	    char *temp = "$\\hat{ }$";
	    strcpy(me->write_pointer, temp);
	    me->write_pointer += strlen(temp);	    
	} else if (c=='~') {
	    char *temp = "$\\tilde{ }$";
	    strcpy(me->write_pointer, temp);
	    me->write_pointer += strlen(temp);
	} else if (c=='|' || c=='<' || c=='>') {       	        /* Math mode */
	    *me->write_pointer++ = '$';
	    *me->write_pointer++ = c;
	    *me->write_pointer++ = '$';
	} else
	    *me->write_pointer++ = c;	      	        /* Char seems normal */
    }

    if (c==' ')						   /* Find delimiter */
	me->line_break = me->write_pointer;
    else if (strchr(WORD_DELIMITERS, c))
	me->line_break = me->write_pointer-1;

    /* Flush buffer out when full */
    if (me->write_pointer >= me->buffer+BUFFER_SIZE-3) {
#if 0
	if (me->markup || me->preformatted) {
#endif
	if (me->preformatted) {
	    *me->write_pointer = '\n';
	    (*me->targetClass.put_block)(me->target,
					 me->buffer,
					 me->write_pointer-me->buffer+1);
	    me->write_pointer = me->buffer;
	} else {		       		          /* Use break-point */
	    char line_break_char = *me->line_break;
	    char *saved = me->line_break;
	    *me->line_break = '\n';
	    (*me->targetClass.put_block)(me->target,
					 me->buffer,
					 me->line_break-me->buffer+1);
	    *me->line_break = line_break_char;
	    {                                           /* move next line in */
		char *p = saved;
		char *q;
		for(q=me->buffer; p<me->write_pointer; )
		    *q++ = *p++;
	    }
	    me->write_pointer = me->buffer + (me->write_pointer-saved);
	}	    
	me->line_break = me->buffer;
    }
}



/*	String handling
**	---------------
*/
PRIVATE void HTTeXGen_put_string ARGS2(HTStructured *, me, CONST char*, s)
{
    CONST char * p;
    for (p=s; *p; p++)
	HTTeXGen_put_character(me, *p);
}


PRIVATE void HTTeXGen_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
{
    CONST char * p;
    for(p=s; p<s+l; p++)
	HTTeXGen_put_character(me, *p);
}


/*	Start Element
**	-------------
**
**     	No attributes are put to the output		Henrik 07/03-94
**	Does no assumptions of WHAT element is started...
*/
PRIVATE void HTTeXGen_start_element ARGS4(
	HTStructured *, 	me,
	int,			element_number,
	CONST BOOL*,	 	present,
	CONST char **,		value)
{
    me->startup = YES;			        /* Now, let's get down to it */
    if (me->preformatted == YES) {	       /* Don't start markup in here */
	if (TRACE)
	    fprintf(stderr, "LaTeX....... No Markup in verbatim mode\n");
	return;
    }
    if (element_number == HTML_PRE)
	me->preformatted = YES;
    if (element_number == HTML_CITE ||	              /* No \n here, please! */
	element_number == HTML_DT ||
	element_number == HTML_H1 ||
	element_number == HTML_H2 ||
	element_number == HTML_H3 ||
	element_number == HTML_H4 ||
	element_number == HTML_H5 ||
	element_number == HTML_H6 ||
	element_number == HTML_H7 ||
	element_number == HTML_TITLE)
	me->sensitive = YES;
    else if (element_number == HTML_DD)         /* Only way to turn <DT> off */
	me->sensitive = NO;
    me->markup = element_number == HTML_A ? NO : YES;
    HTTeXGen_put_string(me, *TeX_names[element_number]);
    me->markup = NO;
}


/*		End Element
**		-----------
**
**	Ends an markup element			Henrik 07/03-94
**	Does no assumptions of WHAT element is ended...
*/
PRIVATE void HTTeXGen_end_element ARGS2(HTStructured *, me,
					int , element_number)
{
    if (me->preformatted && element_number != HTML_PRE) {
	if (TRACE)
	    fprintf(stderr, "LaTeX....... No markup in verbatim mode\n");
	return;
    }
    me->preformatted = NO;
    me->markup = YES;
    HTTeXGen_put_string(me, *(TeX_names[element_number]+1));
    me->markup = NO;
    if (element_number == HTML_CITE ||	
	element_number == HTML_DL ||
	element_number == HTML_H1 ||
	element_number == HTML_H2 ||
	element_number == HTML_H3 ||
	element_number == HTML_H4 ||
	element_number == HTML_H5 ||
	element_number == HTML_H6 ||
	element_number == HTML_H7 ||
	element_number == HTML_TITLE)
	me->sensitive = NO;
}


/*		Expanding entities
**		------------------
**
*/
PRIVATE void HTTeXGen_put_entity ARGS2(HTStructured *, me, int, entity_number)
{
    BOOL mark = me->markup;
    if (*TeX_entities[entity_number] != '&' && /* Theese are converted later */
	*TeX_entities[entity_number] != '<' &&
	*TeX_entities[entity_number] != '>')
	me->markup = YES;
    HTTeXGen_put_string(me, TeX_entities[entity_number]);
    me->markup = mark;
}



/*	Free an HTML object
**	-------------------
**
*/
PRIVATE void HTTeXGen_free ARGS1(HTStructured *, me)
{
    HTTeXGen_flush(me);
    (*me->targetClass.put_string)(me->target, "\n\\end{document}\n");
    HTTeXGen_flush(me);
    (*me->targetClass._free)(me->target);	/* ripple through */
    free(me);
}


PRIVATE void HTTeXGen_abort ARGS2(HTStructured *, me, HTError, e)
{
    HTTeXGen_free(me);
}


/*	Structured Object Class
**	-----------------------
*/
PRIVATE CONST HTStructuredClass HTTeXGeneration = /* As opposed to print etc */
{		
	"HTMLToTeX",
	HTTeXGen_free,
	HTTeXGen_abort,
	HTTeXGen_put_character,        	HTTeXGen_put_string,	HTTeXGen_write,
	HTTeXGen_start_element, 	HTTeXGen_end_element,
	HTTeXGen_put_entity
}; 


/*	HTConverter from HTML to TeX Stream
**	------------------------------------------
**
*/
PUBLIC HTStream* HTMLToTeX ARGS5(
	HTRequest *,		request,
	void *,			param,
	HTFormat,		input_format,
	HTFormat,		output_format,
	HTStream *,		output_stream)
{
    HTStructured* me = (HTStructured*) calloc(1, sizeof(*me));
    if (me == NULL) outofmem(__FILE__, "HTMLToTeX");    

    me->isa = (HTStructuredClass*) &HTTeXGeneration;
    me->dtd = &HTMLP_dtd;
    me->target = output_stream;
    me->targetClass = *me->target->isa;/* Copy pointers to routines for speed*/
    me->write_pointer = me->buffer;
    me->line_break = 	me->buffer;
    (*me->targetClass.put_string)(me->target,
        "\\documentstyle[11pt]{report}\n\\begin{document}\n");
    return SGML_new(&HTMLP_dtd, me);
}


/* END OF FILE HTTeXGen.c */