summaryrefslogtreecommitdiff
path: root/support/l2a/l2a.l
blob: 0e6b127191c91009963046e2eebdf916105453b6 (plain)
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
 /* l2a.l -*- C -*-
 +-----------------------------------------------------------------------------
 | Abstract:
 |    Lex filter to detexify texts (handles really LaTeX, and a bit of TeX).
 |
 | Authorship:
 |    Copyright (c) 1988, 1990 Gisle Hannemyr.
 |
 |    Permission is granted to hack, make and distribute copies of this program
 |    as long as this notice and the copyright notices are not removed.
 |    If you intend to distribute changed versions of this program, please make
 |    an entry in the "history" log (below) and mark the hacked lines with your
 |    initials. I maintain the program, and shall appreiciate copies of bug
 |    fixes and new versions.
 |    Flames, bug reports, comments and improvements to:
 |       snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway
 |       EAN:   C=no;PRMD=uninett;O=nr;S=Hannemyr;G=Gisle (X.400 SA format)
 |              gisle.hannemyr@nr.no                      (RFC-822  format)
 |       Inet: gisle@ifi.uio.no
 |       UUCP: ...!mcsun!ifi!gisle
 |
 | History:
 |    1.0  8 oct 90 [gh] Version 1 posted on comp.sources.misc.
 |    0.0 27 dec 88 [gh] Started it.
 |
 | Bugs:
 |    * Works for my style of Latex, but will proably barf on other peoples
 |      files.
 |    * I can't see how \kill can be handled by lex only.
 |    * \cite is hacked.  Should be fixed.
 |
 | Environment:
 |    None.
 |
 | Diagnostics:
 |    Prints plain error messages.  Does not return error codes.
 +---------------------------------------------------------------------------*/


%{

 /*---( start of user written lex prologue )---------------------------------*/


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

#define VERSION "1.0"
#define MAXEROR  100
#define ENGLISH   0
#define NORWEGIAN 1

 void parseerror();
 void bibhead();
 void ptabular();
 void pfigure();
 void pcaption();
 void phorline();
 void pappendix();
 void verbatim();
 void pfotnote();
 void footnote();


 /*---( globals )------------------------------------------------------------*/

char About[] = "\
L2A is a filter to remove markup commands from LaTeX manuscripts.\n\n\
Flames, bug reports, comments and improvements to:\n\
   snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway\n\
   EAN:   C=no;PRMD=uninett;O=nr;S=Hannemyr;G=Gisle (X.400 SA format)\n\
          gisle.hannemyr@nr.no                      (RFC-822  format)\n\
   Inet:  gisle@ifi.uio.no\n\
   UUCP:  ...!mcsun!ifi!gisle\n";

char Usage[] = "   Usage: l2a (options)\n\
   Valid options:\n\
\t-a       -- about l2a\n\
\t-h       -- print this\n\
\t-n       -- norwegian text\n";

int    LineNo;                     /* Line number for debugging              */
int    Enumber;                    /* Number for enumerate                   */
int    Fnumber = 0;                /* Number for footnotes/endnotes          */
FILE   *BFile;                     /* Bibliography file.                     */
FILE   *FNote;                     /* Temp. file for footnotes.              */
int    Language = ENGLISH;         /* language for inserted text             */

 /*---( end of user written lex prologue )-----------------------------------*/

%}

%START EN FN IT QU MM
%n 1000
%p 6000
%a 4000
%e 2000

sp [ \t\n]*
an [0-9a-zA-Z]+

%%

^"\\begin{thebibliography".* { bibhead(); }
^"\\end{thebibliography}"    { ; }
^"\\bibitem["{an}"]{"{an}"}" { fputs(yytext,stderr); }
"\\cite{"[^{\n]*"}"   { yytext[0] = '('; yytext[strlen(yytext)] = ')'; printf("@CITE%s@ ",yytext); }
"\\ref{"[^{\n]*"}"    { yytext[0] = '('; yytext[strlen(yytext)] = ')'; printf("@REF%s@ ", yytext); }
^"\\begin{enumerate}" { BEGIN EN; Enumber = 0; }
^"\\end{enumerate}"   { BEGIN 0;  }
^"\\begin{itemize}"   { BEGIN IT; }
^"\\end{itemize}"     { BEGIN 0;  }
^"\\begin{quote}"     { BEGIN QU; }
^"\\end{quote}"       { BEGIN 0;  }
^"\\begin{quotation}" { BEGIN QU; }
^"\\end{quotation}"   { BEGIN 0;  }
<IT>"\\item"          { putchar('*'); }
<EN>"\\item"          { Enumber++; printf("%d)",Enumber); }
"\\item"              { putchar('+'); }
<QU>\n                { ECHO; LineNo++; fputs("   ",stdout); }
<FN>.                 { footnote();  BEGIN 0; }
"$"                   { BEGIN MM; }
<MM>"$"               { BEGIN 0;  }
<MM>"\\langle"        { putchar('<'); }
<MM>"\\rangle"        { putchar('>'); }
<MM>"\\backslash"     { putchar('\\'); }
^"\\begin{tabular".*  { ptabular();   }
^"\\end{tabular}"     { phorline();   }
^"\\begin{figure".*   { pfigure();    }
^"\\end{figure}"      { phorline();   }
^"\\caption{"         { pcaption();   }
"\\footnote"          { Fnumber++; printf("<%d>",Fnumber); BEGIN FN; }
"\\appendix"          { pappendix();  }
"\\verb"              { verbatim();   }
"\\'{e}"{sp}          { putchar('e'); }
\\ae{sp}              { putchar('{'); }
\\o{sp}               { putchar('|'); }
\\aa{sp}              { putchar('}'); }
\\AE{sp}              { putchar('['); }
\\O{sp}               { putchar('\\'); }
\\AA{sp}              { putchar(']'); }
"&"                   { putchar('\t'); }
"\\>"                 { putchar('\t'); }
"~"                   { putchar(' '); }
"\\ "                 { putchar(' '); }
"\\$"                 { putchar('$'); }
"\\&"                 { putchar('&'); }
"\\%"                 { putchar('%'); }
"\\#"                 { putchar('#'); }
"\\_"                 { putchar('_'); }
"\\{"                 { putchar('{'); }
"\\}"                 { putchar('{'); }
"\\\\"{sp}            { putchar('\n'); }
^"\\vspace".*         { putchar('\n'); }
^"\\title"            { putchar('\n'); }
^"\\author"           { putchar('\n'); }
^"\\date".*           { putchar('\n'); }
^"\\maketitle"        { putchar('\n'); }
^"\\part"             { putchar('\n'); }
^"\\chapter"          { putchar('\n'); }
^"\\section"          {;}
^"\\subsection"       {;}
^"\\subsubsection"    {;}
^"\\paragraph"        {;}
^"\\subparagraph"     {;}
^"\\input lcustom"    {;}
^"\\documentstyle".*  {;}
^"\\pagenumbering".*  {;}
^"\\setcounter".*     {;}
^"\\newcommand".*     {;}
^"\\setlength".*      {;}
^"\\hyphenation".*    {;}
^"\\label".*          {;}
^"\\draftfalse"       {;}
^"\\begin{document}"  {;}
^"\\end{document}"    {;}
^"\\begin{sloppypar}" {;}
^"\\end{sloppypar}"   {;}
^"\\begin{tabbing}"   {;}
^"\\end{tabbing}"     {;}
"\\TeX"               { fputs("TeX",   stdout); }
"\\LaTeX"             { fputs("LaTeX", stdout); }
"\\LtoA"              { fputs("L2A",   stdout); }
"\\ldots"             { fputs("...",   stdout); }
"\\tiny"              {;}
"\\scriptsize"        {;}
"\\footnotesize"      {;}
"\\small"             {;}
"\\normalsize"        {;}
"\\large"             {;}
"\\Large"             {;}
"\\LARGE"             {;}
"\\huge"              {;}
"\\Huge"              {;}
"\\rm"{sp}            {;}
"\\em"{sp}            {;}
"\\bf"{sp}            {;}
"\\it"{sp}            {;}
"\\sl"{sp}            {;}
"\\sf"{sp}            {;}
"\\sc"{sp}            {;}
"\\tt"{sp}            {;}
"{"                   {;}
"}"                   {;}
"%".*                 {;}
"\\-"                 {;}
\n                    { ECHO; LineNo++; }
.                     { ECHO; }
"\\"[@A-Za-z]+        { parseerror(12,yytext); yytext[0] = '('; printf("@%s)@ ",yytext); }

%%

 /*---( routines )-----------------------------------------------------------*/

 void parseerror(type,ss)
 int  type;
 char *ss;
 {
   static errcnt = 0;

   fprintf(stderr,"l2a: Error %d -- ",type);
   switch (type) {
      case  1: fputs("Instruction not recognized",stderr); break;
      case  2: fputs("Wrong number of parameters",stderr); break;
      case  3: fputs("Bad parameter received",stderr); break;
      case  5: fputs("Unknown character set",stderr); break;
      case  6: fputs("Position overflow",stderr); break;
      case 10: fputs("Bogus output request",stderr); break;
      case 11: fputs("Unimplemented command",stderr); break; /* No function */
      case 12: fputs("Unrecognized markup command",stderr); break;  /* No grammar  */
      case 13: fputs("Unhandled command",stderr); break;     /* No semantic */
      default: fputs("Unknown error (internal)",stderr); break;
   } /* switch */
   fprintf(stderr,"\n   @ line %3d: \"%s\"\n",LineNo,ss);
   errcnt++;
   if (errcnt > MAXEROR) {
      fputs("   Too many errors -- aborting\n",stderr);
      exit(-1);
   }
 } /* parseerror */


 void bibhead()
 {
    switch (Language) {
       case ENGLISH:   puts("\nReferences:"); break;
       case NORWEGIAN: puts("\nLitteratur:"); break;
    } /* switch */
 } /* bibhead */


 void ptabular()
 {
    switch (Language) {
       case ENGLISH:   puts("\t<<=============== NB! Typeset as table. NB! ================>>"); break;
       case NORWEGIAN: puts("\t<<============== NB! Settes som tabell. NB! ================>>"); break;
    } /* switch */
 }


 void pfigure()
 {
    switch (Language) {
       case ENGLISH:   puts("\t<<=========== NB! Please insert figure here. NB! ===========>>"); break;
       case NORWEGIAN: puts("\t<<=========== NB! Figur skal settes inn her. NB! ===========>>"); break;
    } /* switch */

 }


 void pcaption()
 {
    switch (Language) {
       case ENGLISH:   puts("\t  --------------- Caption for figure or table: -------------"); break;
       case NORWEGIAN: puts("\t  --------------- Undertekst for bilde/tabell: -------------"); break;
    } /* switch */
 }


 void phorline()
 {
    puts("<<==========================================================>>");
 }


 void pappendix()
 {
    switch (Language) {
       case ENGLISH:   puts("\n\nAPPENDIX\n========\n"); break;
       case NORWEGIAN: puts("\n\nAPPENDIX\n========\n"); break;
    } /* switch */
 }

 void verbatim()
 {
   int cc, ts;

   ts = input();
   for (;;) {
      cc = input();
      if (cc == ts) break;
      putchar(cc);
   } /* forever */
 } /* verbatim */


 void pfotnote()
 { 
    switch (Language) {
       case ENGLISH:   puts("\n\nENDNOTES\n========\n"); break;
       case NORWEGIAN: puts("\n\nNOTER\n=====\n"); break;
    } /* switch */
 }


 void footnote()
 {
   int cc, be = 1;

   fprintf(FNote,"<%d>: ", Fnumber);
   while (be) {
      cc = input();
      if      (cc == '{') be++;
      else if (cc == '}') be--;
      else putc(cc,FNote);
   }
   putc('\n',FNote);
   putc('\n',FNote);
 } /* footnote */


 void bibitem()
 {
   int cc, be = 1;

   fprintf(FNote,"<%d>: ", Fnumber);
   while (be) {
      cc = input();
      if      (cc == '{') be++;
      else if (cc == '}') be--;
      else putc(cc,FNote);
   }
   putc('\n',FNote);
   putc('\n',FNote);
 } /* footnote */

 /*---( main )---------------------------------------------------------------*/

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

   fprintf(stderr,"l2a, version %s -- Copyright (c) 1988, 1990 Gisle Hannemyr\n\n",VERSION);

   argc--; argv++;           /* skip program name  */
   while (argc && (**argv == '-')) {
      (*argv)++;             /* skip initial '-'   */
      switch (**argv) {
         case 'a': fputs(About,stderr);  exit(1);
         case 'n': Language = NORWEGIAN; break;
         default : fputs(Usage,stderr);  exit(1);
      } /* switch */
      argc--; argv++;
   } /* while options */
   if (argc) { fputs(Usage,stderr); exit(1); }

   BEGIN 0;
   LineNo = 0;
   FNote = fopen("FN.TMP","w");
   /* BFile = fopen("references.tex","r"); */
   yylex();

   fclose(FNote);
   if (Fnumber) {
      char buff[80];
      FNote = fopen("FN.TMP","r");
      pfotnote();
      while (fgets(buff,80,FNote)) fputs(buff,stdout);
   } /* if */
   unlink("FN.TMP");
   puts("..EOF");
 } /* main */

 /*---( EOF lex input file )-------------------------------------------------*/