File: rtf2rtf.l

package info (click to toggle)
linuxdoc-tools 0.9.86-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,312 kB
  • sloc: ansic: 16,247; perl: 3,769; sh: 1,134; makefile: 813; lex: 566; lisp: 309
file content (441 lines) | stat: -rw-r--r-- 10,601 bytes parent folder | download | duplicates (2)
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
/* -*- c -*- */

/* Butchered version of html2html.l to post-process RTF files
 *
 * Steve Tynor (tynor@atlanta.twr.com). I'm not proud of this code. It's a hack
 * foisted upon a hack, nested within another hack or two.  It sort of works
 * well enough for my purposes (generating WinHelp .RTF files for my documents),
 * but it could definitely use a redesign/rewrite. I shamelessly blame the RTF
 * format for most of the hackery here -- RTF is not a _language_ like LaTeX or
 * ROFF -- it's just a file format. So, we can't rely on RTF to do even simple
 * things like "insert a paragraph break here only if the previous token was not
 * also a paragraph break. Since the SGML front end has no conditional processing
 * capabilities, multiple blank likes in the SGML get translated to multiple para
 * breaks in the RTF (actually, that's why I use the <@@par> crap....).
 *
 * XREF: BROKEN_DELIMITERS:  We say "{}K{\footnote .." instead of just
 * "\nK{\footnote .." since the MicroS$ft RTF parser does not (always?) treat
 * white space as a delimeter before a hyperlink markup.
 *
 * ESR reordered the @@indent rules to eliminate a generation-time warning.
 */

%{

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* #define DEBUG */

  /* NOTE: must be kept in sync with the <p></p> defs in mapping */
#define LEFTMARGIN 500
#define NEWPAR "\\par\\pard\\plain\\li500\\sb200\\fi0"
#define INDENT_AMT 500 /* twips for each level of indentation */

const static char* header =
    "{\\rtf1\\ansi\n"
    "{\\fonttbl\n"
    "{\\f0\\fswiss Helvetica;}\n"
    "{\\f1\\fmodern Courier;}\n"
    "{\\f2\\froman Times;}\n"
    "{\\f3\\froman Symbol;}\n"
    "}\n"
    "{\\info{\\comment Translated from SGML to RTF w/ Linuxdoc-SGML}}\n"
    ;

int big_page_mode = 1; /* default is backwards compatible */

/* <report> style uses <chapt> as the top-level section; <article> uses
   <sect>.  We need to know whether a <sect> should be numbered as "n"
   or "n.m".  We assume <article> more unless we see a <chapt> */
int chapter_mode = 0;
int section_id = 0; /* used for TOC hyperlinks */
int in_appendix = 0;

FILE *out;
FILE *idx_fp;

char buf[2048], sectname[256], labeltxt[256];
char firstname[256], outname[262];
char ssectname[256], Tmp1[4096], Tmp2[256], headname[256];
char urlname[256], Headbuf[4096], refname[256];

int indent = 0;
int nofill = 0;
int filenum = 1;
int filecount = 1;
int tmpfn;
int secnr = 0;
int ssecnr;
int current;
int skipnewline = 0;

extern char *yytext;

#if (YY_FLEX_MAJOR_VERSION == 2 && YY_FLEX_MINOR_VERSION == 5 && YY_FLEX_SUBMINOR_VERSION > 35) \
 || (YY_FLEX_MAJOR_VERSION == 2 && YY_FLEX_MINOR_VERSION == 6 && YY_FLEX_SUBMINOR_VERSION == 0)
extern yy_size_t yyleng;
#else
extern int yyleng;
#endif

#define CHAPTER 1
#define SECTION 2
#define SUBSECT 3

void fatal_perror ( char *fmt, char *arg )
  {
    fprintf( stderr, "rtf2rtf: Fatal: " );
    fprintf( stderr, fmt, arg);
    perror( "\nReason");
    exit(1);
  }

int getstr( char *str, int n )
  {
#ifdef DEBUG
    fprintf( stderr, "DEBUG: yytext: <<%s>>\n", yytext );
#endif
    if( strlen( yytext ) == n )
      return 0;
    strncpy( str, yytext+n, yyleng-n);
    str[ yyleng-n ] = 0;
    return 1;
  }

void
put_reference (FILE *fp, char *str)
  {

    char *p;
    char buf[2] = {0, 0};
    /* trim trailing spaces: */
    while (str[strlen(str)-1] == ' ') {
      str[strlen(str)-1] = '\0';
    }
    /* replace non-legal topic-id characters with underscores */
    p = str;
    while (*p) {
      buf[0] = *p;
      if (! strpbrk(buf,
		    "abcdegfhijklmnopqrstuvwxyz"
		    "ABCDEGFHIJKLMNOPQRSTUVWXYZ"
		    "0123456789")) {
	fprintf (fp, "_");
      } else {
	fprintf (fp, "%c", *p);
      }
      p++;
    }
  }

void
put_secnum (FILE *fp, int s1, int s2)
  {
    if (in_appendix) {
      if (s2 == 0) {
	fprintf (fp, "%c", 'A'-1+s1);
      } else {
	fprintf (fp, "%c.%d", 'A'-1+s1, s2);
      }
    } else {
      if (s2 == 0) {
	fprintf (fp, "%d", s1);
      } else {
	fprintf (fp, "%d.%d", s1, s2);
      }
    }
  }

void
heading (char *str, int s1, int s2, int newpage)
   {
     if (newpage && (out == stdout))
       {
	 sprintf (outname, "%s-0.rtf", firstname);
	 out = fopen (outname, "w");
	 if (! out )
	   {
	     fatal_perror ("Could not open file \"%s\"", outname);
	   }
    	 fputs (header, out);
       }

     /*     fprintf (out, "\n\\page{\\pard\\plain\\s2\\f0\\fs%d\\b\\fi0\\li576\\sb160\\sa160\\tx576 { }",
	    (s2==0 ? 36 : 24) ); */

     if (newpage)
       {
	 fprintf (out, "\n\\page{\\pard\\plain\\keepn\\f0\\fs%d\\b\\tx576{",
		  (s2==0 ? 36 : 24) );
       }
     else
       {
	 fprintf (out, "\n{\\pard\\sb200\\plain\\f0\\fs%d\\b\\tx576{",
		  (s2==0 ? 36 : 24) );
       }
     put_secnum (out, s1, s2);
     fprintf (out, " %s}}", str);

     /* Used as a target for the link on the TOC page. Use a SECT_ prefix
	just in case the document has any explicit <label>'s with the same
	`id' as a section header */
     /* XREF: BROKEN_DELIMITERS: See above for why we say "{}#{\footnote .."
      * instead of just "\n#{\footnote ..".
      */
     fprintf (out, "\n{}#{\\footnote TOC_%d}\n", section_id);
     if (newpage)
       {
	 fprintf (out, "\n${\\footnote ");
	 put_reference (out, str);
	 fprintf (out, "}\n");
	 fprintf (out, "+{\\footnote browse}\n");
	 fprintf (out, "%s\n", NEWPAR);
       }
     /* XREF: BROKEN_DELIMITERS: See above for why we say "{}K{\footnote .."
      * instead of just "\nK{\footnote ..".
      */
     fprintf (out, "{}K{\\footnote %s}\n", str);
/*     fprintf (out, "\\par\\pard\n");*/

     fprintf (idx_fp, "\\li%d\\fi-200{\\f3\\\'B7} ",
	      (s2==0 ? 1 : 2) * 500);
     put_secnum (idx_fp, s1, s2);
     fprintf (idx_fp, " {\\uldb ");
     fprintf (idx_fp, "%s}{\\v TOC_%d}\\par\n", str, section_id);
     section_id++;
   }

void
subheading (char *str, int chapter_size)
  {
    /*     fprintf (fp, "\n{\\pard\\plain\\s2\\f0\\fs%d\\b \\fi0\\li576\\sb160\\sa160\\tx576 { }%s}\n",
	   (chapter_size ? 36 : 24), str);*/

    fprintf (out, "\n{\\pard\\plain\\f0\\sb200\\fs%d\\b\\tx576{%s}}\n",
	     (chapter_size ? 36 : 24), str);
     /* XREF: BROKEN_DELIMITERS: See above for why we say "{}K{\footnote .."
      * instead of just "\nK{\footnote ..".
      */
    fprintf (out, "{}K{\\footnote %s}%s\n", str, NEWPAR);
  }

%}

%s SECT

%%

"<@@indent+>\n"		{ indent++; }
"<@@indent->\n"		{ indent--; }
"<@@indent>\n"		{ fprintf( out, "\\li%d{}", LEFTMARGIN+indent*INDENT_AMT); }

(\\par\\pard\\plain\n"<@@indent>"\n\\sb200\\fi0\n)+ {
  /* HACK: Filter out redundant paragraph marks. I'd rather not put them
   * in in the first place, but it's just TOO HARD.
   */
  fprintf (out, "\\par\\pard\\plain\n\\li%d{}\n\\sb200\\fi0\n",
	   LEFTMARGIN+indent*INDENT_AMT);
}

(("<@@par>"\n)|("<@@indent"[\+\-]">"\n))+ {
  /* Unfortunately, <@@par>'s and <@@indent+>'s get mixed together at the start
   * of <itemize>'s and <descrip>'s.  This hack allows us to treat them as a
   * single <@@par>.
   *
   * Adjust the indentation based on the number of <@@indent+/->'s we see
   */
  char *p = yytext;
  while (*p) {
    if (*p == '+') {
      indent++;
    } else if (*p == '-') {
      indent--;
    }
    p++;
  }
  fprintf (out, "\\par\\pard\\plain\n\\li%d{}\n\\sb200\\fi0\n",
	   LEFTMARGIN+indent*INDENT_AMT);
}

^"<@@chapt><@@head>".*$		{
  chapter_mode = 1;
  /*    skipnewline=1; */
  getstr( sectname, 17 ) ;
  current=SECTION;
  secnr++;
  ssecnr=0;
  heading( sectname, secnr, 0, 1);

}

^"<@@sect><@@head>".*$		{

  /*    skipnewline=1; */
  if (chapter_mode)
    {
      getstr( ssectname, 16 ) ;
      current=SECTION;
      ssecnr++;
      heading( ssectname, secnr, ssecnr, !big_page_mode);
    }
  else
    {
      getstr( sectname, 16 ) ;
      current=CHAPTER;
      secnr++;
      ssecnr=0;
      heading( sectname, secnr, 0, 1);
    }
}

^"<@@ssect><@@head>".*$		{
  /*    skipnewline=1;*/
  getstr( ssectname, 17 ) ;
  current=SUBSECT;
  if (! chapter_mode)
    {
      ssecnr++;
      heading( ssectname, secnr, ssecnr, !big_page_mode );
    }
  else
    {
      current=SUBSECT;
      subheading( ssectname, 0 );
    }
}


^"<@@appendix>".*$		{
  in_appendix = 1; secnr=0; ssecnr=0;
  fprintf (idx_fp, "\\par\\pard\\qc{\\b Appendix}\\par\\pard\n");
}

^"<@@part><@@head>".*$		{
  fprintf (idx_fp, "\\par\\pard\\qc{\\b %s}\\par\\pard\n", yytext+16);
}

^"<@@head>".*$		{

  /*  skipnewline=1; */
  getstr( ssectname, 8 ) ;
  current=SUBSECT;
  subheading( ssectname, 0 );
}

^"<@@label>".*$		{

     /* XREF: BROKEN_DELIMITERS: See above for why we say "{}K{\footnote .."
      * instead of just "\nK{\footnote ..".
      */
  fprintf (out, "{}#{\\footnote ");
  put_reference (out, yytext+9 );
  fprintf (out, "}\n");
/* Grrr.  WinHelp uses K footnotes for references from without the .HLP file */
/* (e.g. via the WinHelp API) */
  fprintf (out, "{}K{\\footnote ");
  put_reference (out, yytext+9 );
  fprintf (out, "}\n");
}

^"<@@title>".*$		{

  /*    skipnewline=1;*/
  getstr( ssectname, 9 ) ;
  subheading( ssectname, 1 );
}

"<@@nofill>\n"		{ indent++; nofill = 1; fprintf( out, "\\par\\pard\\sb200\\keep\\li%d{}", LEFTMARGIN+indent*INDENT_AMT); }
"<@@endnofill>\n"	{ indent--; nofill = 0; fprintf( out, "\\par\\pard\\sb200\\li%d{}", LEFTMARGIN+indent*INDENT_AMT); }

..*			{

  if (skipnewline) {
    /*    strcat(Headbuf, yytext ); */
  } else if( out != stdout ) {
    fprintf( out, "%s", yytext );
  } else {
    ECHO;
  }
}

\n			{

/*  fprintf( out, " ");*/		/* Add a space at the end of each line since RTF does
				   not treat EOL as whitespace and will concatenate
				   tokents spanning lines */
  if (nofill)
    {
      fprintf( out, "\\line");
    }
  if(!skipnewline)
    {
      if( out != stdout )
	{
	  fprintf( out, "%s", yytext );
	}
      else
	{
	  ECHO;
	}
    }
}


%%

int
main( int argc, char **argv )
{
  int i;

  out = stdout;
  idx_fp = stdout;

  strcpy( firstname, "RTF" );

  for (i = 1; i < argc; i++)
    {
      if (0 == strcmp("-2", argv[i]))
	{
	  big_page_mode = 0;
	}
      else if (argv[i][0] != '-')
	{
	  strncpy( firstname, argv[i], 256 );
	}
      else
	{
	  fprintf (stderr,
		   "%s: Warning: unhandled command line option \"%s\"\n",
		   argv[0], argv[i]);
	}
    }

  fputs (header, out);
  fprintf (out, "+{\\footnote browse}\n");


  secnr=0;
  yylex();

  if( out != stdout )
    {
      fclose( out );
    }

  fputs ("\n}\n", idx_fp);
  fclose( idx_fp );

  exit( 0 );
}

/* Use the existing indentation style:
 Local variables:
 c-indent-level: 2
 c-continued-statement-offset: 2
 c-brace-offset: 0
 c-label-offset: -2
 End:
 */