[net.sources] Tex->nroff conversion for GNUEmacs Manuals

ghr@wucs.UUCP (George Robbert) (12/13/85)

Several people on the net have asked for the v16 GNU Emacs manuals
in nroff/troff format.  I wrote a quicky program to convert from the 
TeX it's distributed in to nroff/troff.  I'm posting it here after
a couple of requests.  It doesn't handle the fancy stuff of TeX changing 
fonts and typefaces.  It's just a simple lex program to produce a nroff 
source for a readable manual.  There also is a small C program to handle
the indecies.  Btw: the nroff it produces assumes that you are using 
the -me package.  If you find any bugs or make any improvements on this,  
Please let me know.  I've also included a makefile, a file of nroff macro 
definitions and a trailer file to print out the table of contents for the 
GNU manual.  You will probably have to modify the makefile to use the 
right filenames for the GNU manual(s) and to put the nroff where you 
want it.

George Robbert   @ Washington University in St. Louis
ghr@wucs     or  ...!ihnp4!wucs!ghr

---------------------------- cut here --------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	indexhead
#	grindindex.sh
#	local.tr
#	mkindex.c
#	textonr.l
#	trailer.me
# This archive created: Sat Dec  7 22:40:25 1985
export PATH; PATH=/bin:$PATH
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
DEV= -Tlp
COL= icol
print:	emacs.out index.out
	lpr emacs.out index.out

emacs.out emacs.index: emacs.tr ggloss.tr
	/bin/sh -c 'nroff $(DEV)-me local.tr emacs.tr trailer.me 1>emacs.out 2>emacs.index'

emacs.nr:	textonr
	textonr <emacs.tex | sed 's/%\.tm'/%\
.tm/' >emacs.tr

ggloss.tr:
	textonr  <ggloss.tex >ggloss.tr

index.out:	emacs.index mkindex grindindex.sh
	cp indexhead index.nr
	echo '.+c "Key (Character) Index"' >>index.nr
	echo '.sp 2' >>index.nr
	echo '.2c' >>index.nr
	grindindex.sh kindex >>index.nr
	echo '.1c' >>index.nr
	echo '.+c "Command Index"' >>index.nr
	echo '.sp 2' >>index.nr
	echo '.2c' >>index.nr
	grindindex.sh cfindex >>index.nr
	echo '.1c' >>index.nr
	echo '.+c "Variable Index"' >>index.nr
	echo '.sp 2' >>index.nr
	echo '.2c' >>index.nr
	grindindex.sh vindex >>index.nr
	echo '.1c' >>index.nr
	echo '.+c "Concept Index"' >>index.nr
	echo '.sp 2' >>index.nr
	echo '.2c' >>index.nr
	grindindex.sh cindex >>index.nr
	nroff $(DEV) -me index.nr | $(COL) >index.out

textonr: textonr.o
	cc -o textonr textonr.o -ll

textonr.o: textonr.c
	cc -c textonr.c

textonr.c: textonr.l
	lex textonr.l ; mv lex.yy.c textonr.c

mkindex: mkindex.c
	cc -o mkindex mkindex.c

SHAR_EOF
fi # end of overwriting check
if test -f 'indexhead'
then
	echo shar: will not over-write existing file "'indexhead'"
else
cat << \SHAR_EOF > 'indexhead'
.++ RA "''GNU Emacs Manual'\\\\\\\\n(ch.%'"
SHAR_EOF
fi # end of overwriting check
if test -f 'grindindex.sh'
then
	echo shar: will not over-write existing file "'grindindex.sh'"
else
cat << \SHAR_EOF > 'grindindex.sh'
sed -n "s/@$1{ //p" emacs.index | \
sed -e 's/	\([0-9]\)$/	00\1/' -e \
       's/	\([0-9][0-9]\)$/	0\1/' | sort | mkindex 
SHAR_EOF
chmod +x 'grindindex.sh'
fi # end of overwriting check
if test -f 'local.tr'
then
	echo shar: will not over-write existing file "'local.tr'"
else
cat << \SHAR_EOF > 'local.tr'
.\"  macros for running off the GNU EMACS Manual 
.\"	
.\"		Converted from TeX to nroff by
.\"			George Robbert
.if n \
\{\
.       nr ps 1v		\" set space before paragraph
.	nr LS 1v
\}
.if t \
\{\
.      	nr ps .5v
.	nr ls .5v
\}
.nr pi 2n
.\"
.\" this macro prints the section headings.
.\"
.de $0
.(x
.ie '\\$2'' \
\{\
.ti 5
\\$1
\}
.el \
\{\
.ti 5
\\$2. \\$1
\}
.)x
..
.de $C
.(x
\\$1 \\$2 \\$3
.)x
.sx
..
SHAR_EOF
fi # end of overwriting check
if test -f 'mkindex.c'
then
	echo shar: will not over-write existing file "'mkindex.c'"
else
cat << \SHAR_EOF > 'mkindex.c'
#include <stdio.h>

main()

{
    register char *s,*t;
    char topic[80],prevtopic[80];
    char stg[80],outstg[2048];
    char *osp;
    char *head;
    int references[2048],i,j,f;

    prevtopic[0] = '\0';
    i = 0;
    topic[0] = '\0';
    f = 0;
    printf(".in 8\n");
    while (gets(stg) != NULL) {
	for(s = stg,t = topic;*s != '\t';)
	    *t++ = *s++;
	*t = '\0';
	s++;
	if (strcmp(topic,prevtopic) != 0) {
	  output:
	    if (prevtopic[0] != '\0') {
		if (prevtopic[0] == '.') 
		    printf(".ti 0\n\\&%s,   ",prevtopic);
		else
		    printf(".ti 0\n%s,   ",prevtopic);
		if (i > 0)
		  printf("%d",references[0]);
		for(j=1;j < i;j++) {
		    printf(", %d",references[j]);
		}
		putchar('\n');
	    }
	    strcpy(prevtopic,topic);
	    references[0] = atoi(s);
	    i = 1;
	    if (f) exit(0);
	}
	else {
	    references[i++] = atoi(s);
	}
    }
    f = 1;
    goto output;
}
SHAR_EOF
fi # end of overwriting check
if test -f 'textonr.l'
then
	echo shar: will not over-write existing file "'textonr.l'"
else
cat << \SHAR_EOF > 'textonr.l'
%{
char *sstk[20] = {0};
char *cstk[20] = {0};
char *pstk[20] = {0};
char *estk[20] = {0};
int sptr = 0;
int cptr = 0;
int pptr = 0;
int eptr = 0;
pushstk();
popstk();
char *EOLS = "\n";
char *NULSTG = "";
char *FR = "\\fR";
%}
LB	[ \t]*[\133{(]
RB	[\135})\n]
%e 2000
%p 6000
%n 1000
%START ITEM ENUM
%%
\\				{printf("\\e");}
"\\input"			{printf(".CO %s",yytext);}
\n\n"  "			{printf("\n.pp\n");}
\n\n/[A-Za-z]			{printf("\n.lp\n");}
"@@"				{printf("@");}
"@."				{printf(".");}
\006"9"				{printf("\\s+8");}
\006"*"				{printf("\\s-8");}
".el"				{printf("\\&.el");}
".signature"			{printf("\\&%s",yytext);}
"@menu"				{printf(".ig I1");}
"@end menu"			{printf(".I1");}
"@ignore"			{printf(".ig I2");}
"@end ignore"			{printf(".I2");}
"@info"				{printf(".ig I3");}
"@end info"			{printf(".I3");}
"@node".*$			{printf(".CO %s",yytext);}
"@tex"				{printf(".ig I4");}
"@end tex"			{printf(".I4");}
"@ifinfo"			{printf(".ig I5");}
"@end ifinfo"			{printf(".I5");}"
"@example"			{printf(".(l");}
"@end example"			{printf(".)l\n.sp \\n(psu");}
"@quotation"			{printf(".(q");}
"@end quotation"		{printf(".)q");}
"@format"			{printf(".(q");}
"@end format"			{printf(".)q");}
"@dots"				{printf("...");}
"@bullet"			{printf("\\(bu");}
{RB}				{popstk(*yytext);}
"@code"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@kbd"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@ctl"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@dfn"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@key"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@samp"{LB}			{printf("\\f3"); pushstk(yytext,FR);}
"@var"{LB}			{printf("\\fI"); pushstk(yytext,FR);}
"@i"{LB}			{printf("\\fI"); pushstk(yytext,FR);}
"@b"{LB}			{printf("\\fB"); pushstk(yytext,FR);}
"@w"{LB}			{pushstk(yytext,NULSTG);}
"@ttfont"{LB}			{pushstk(yytext,NULSTG);}
"@c ".*$			{printf(".CO %s",yytext+2);}
"@comment".*$			{printf(".CO %s",yytext+8);}
"@iftex"			{printf(".CO");}
"@end iftex"			{printf(".CO");}
"@unnumberedsec "		{printf(".uh \"");pushstk(EOLS,"\"\n.sp \\npsu\n");}
"@unnumberedsec"{LB}		{printf(".uh \"");pushstk(yytext,"\"\n.sp \\npsu\n");}
"@display"			{printf(".(c\n.(l");}
"@end display"			{printf(".)l\n.)c");}
"@begin"{LB}			{printf(".CO ");pushstk(yytext,NULSTG);}
"@end description"		{printf(".CO %s",yytext);}
"@page"				{printf(".bp");}
"@TeX"				{printf("TeX");}
"@section"{LB}			{printf(".sh 1 \"");pushstk(yytext,"\"");}
"@section "			{printf(".sh 1 \"");pushstk(EOLS,"\"");}
"@unnumbered"{LB}		{printf(".uh \"");pushstk(yytext,"\"");}
"@unnumbered "			{printf(".CO %s",yytext);}
"@summarycontents"		{printf(".CO %s",yytext);}
"@contents"			{printf(".CO %s",yytext);}
"@bye"				{printf(".CO %s",yytext);}
"@subsection "			{printf(".sh 2 \"");pushstk(EOLS,"\"\n");}
"@subsection"{LB}		{printf(".sh 2 \"");pushstk(yytext,"\"\n");}
"@table "			{printf(".ba +");}
"@end table"			{printf(".ba\n.lp");}
"@exdent 1 "			{printf(".ti 0\n");}
"@item "			{printf(".ip \"");pushstk(EOLS,"\" 10\n");}
"@itemx "			{printf(".ip \"");pushstk(EOLS,"\" 10\n");}
"@itemize"			{printf(".ds IC"); BEGIN ITEM;}
<ITEM>"@item"$			{printf(".ip \\*(IC");}
"@end itemize"			{printf(".rm IC"); BEGIN 0;}
"@enumerate"			{printf(".nr $p 0"); BEGIN ENUM;}
<ENUM>"@item"$			{printf(".np");}
"@end enumerate"		{printf(".nr $p 0"); BEGIN 0;}
"@vindex"{LB}			{printf(".tm %s ",yytext);pushstk(yytext,"\t\\n%");}
"@cfindex"{LB}			{printf(".tm %s ",yytext);pushstk(yytext,"\t\\n%");}
"@cindex"{LB}			{printf(".tm %s ",yytext);pushstk(yytext,"\t\\n%");}
"@findex"{LB}			{printf(".tm %s ",yytext);pushstk(yytext,"\t\\n%");}
"@kindex"{LB}			{printf(".tm %s ",yytext);pushstk(yytext,"\t\\n%");}
"@printindex"			{printf(".CO %s",yytext);}
"@chapter"{LB}			{printf(".+c \"");pushstk(yytext,"\"");}
"@chapter"			{printf(".+c \"");pushstk(EOLS,"\"");}
"@xref"{LB}			{printf("See ");pushstk(yytext,NULSTG);}
"@nopara"			{printf(".CO nopara");}
"@pxref"{LB}			{printf("See ");pushstk(yytext,NULSTG);}
"@sp"				{printf(".sp");}
"@center"[ \t]*			{printf(".ce 1\n");}
"@setref"			{printf(".SR");}
"@lisp"				{printf(".(q");}
"@end lisp"			{printf(".)q");}
"@settitle"{LB}			{printf(".he ''");pushstk(yytext,"'%'");}
"@setchapternewpage"		{printf(".CO %s",yytext);}
"@headings"			{printf(".CO %s",yytext);}
"@refill"			{/* do nothing */}
"@include"			{printf(".CO %s",yytext);}
"@include ggloss.tex"		{printf(".so ggloss.tr");}
"@lispnarrowing"		{printf(".CO %s",yytext);}
"@tableindent"			{printf(".CO %s",yytext);}
"@itemindent"			{printf(".CO %s",yytext);}
"@baselineskip"			{printf(".CO %s",yytext);}
"@aboveenvskipamount"		{printf(".CO %s",yytext);}
"@parskip"			{printf(".CO %s",yytext);}
"@parindent"			{printf(".CO %s",yytext);}
"@hsize"			{printf(".CO %s",yytext);}
"@defindex"			{printf(".CO %s",yytext);}
"@kern"				{printf(".CO %s",yytext);}
%%
char *
substr(s,i)
char *s;
int i;

{
    s[strlen(s)-1] = '\0';
    return(s+i);
}

pushstk(c,s)
char c[],*s;

{
    switch(c[strlen(c)-1]) {
	case '[': sstk[sptr++] = s;
		  break;
	case '{': cstk[cptr++] = s;
		  break;
	case '(': pstk[pptr++] = s;
		  break;
	case '\n':estk[eptr++] = s;
		  break;
    }
}

popstk(c)
char c;

{
    switch(c) {
	case ']': if (sptr <= 0)
		      printf("]");
		  else
		      printf("%s",sstk[--sptr]);
		  break;
	case '}': if (cptr <= 0)
		      printf("}");
		  else
		      printf("%s",cstk[--cptr]);
		  break;
	case ')': if (pptr <= 0)
		      printf(")");
		  else
		      printf("%s",pstk[--pptr]);
		  break;
	case '\n':if (eptr <=0)
		      printf("\n");
		  else
		      printf("%s",estk[--eptr]);
		  break;
    }
}
/* -*- C -*- */
SHAR_EOF
chmod +x 'textonr.l'
fi # end of overwriting check
if test -f 'trailer.me'
then
	echo shar: will not over-write existing file "'trailer.me'"
else
cat << \SHAR_EOF > 'trailer.me'
.af % i
.bp 1
.ce 2
CONTENTS
.ce 0
.sp 5v
.xp
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0