*--  cleanindex.sno
*-- Guido Milanese 2005
*-- guido.milanese@unicatt.it
*-- Requires: ioerror.inc
*--           extrepl.inc


	&MAXLNGTH = 2000000
* If no arguments are given 
	(
+	(~(gt((size(host(2,4))),0))) 
+	(terminal = "No file entered. Usage: cleanindex file.tex")
+	(terminal = "Help: cleanindex -help")
+	)	:s(end)

* If help is required
	(
+	((host(2,4)) ? "-h") 
+	(terminal = "Usage: cleanindex file.tex")
+	(terminal = "Further options: please read the README file or the other documentation files")
+	)	:s(end)


*	Input-output
-include "ioerror.inc"
*	Extended Replace
-include "extrepl.inc"

*	Pattern
	PIndex = "\index" bal

* Read LaTeX file
		  LATEXEXT = (".tex" | ".latex" | ".TEX" | ".LATEX")
		  lfile	=	host(2,4)   ;* LaTeX file input
        lfile ? '' (arb . file) LATEXEXT   
        ofile	= file "-noind.tex"    ;* output
*-- Input files. Using ERRORS
     ( (~(input("l_f",1, "B " &MAXLNGTH,lfile))) (ioerror("in",lfile)) )	:s(end)
*-- Output file 
      output("w_f",2, "B " &MAXLNGTH,ofile)		:f(end)

************************************
* MAIN PROGRAM
************************************

*	Text
	((Text = l_f) (Terminal = "Read file " lfile ' [' size(Text) ' chars]')) :f(end)
	Text ? "\usepackage{makeidx}" = "%\usepackage{makeidx}"
	Text ? "\makeindex" = "%\makeindex"
	Text ? "\printindex" = "%\printindex"


	Text = extrepl(Text,'(', "&0028;")
	Text = extrepl(Text,')', "&0029;")
	Text = replace(Text,'{}','()')

L	Text ? PIndex . VIndex = 	:s(L) 

   Text = replace(Text,"()","{}")
	Text = extrepl(Text,"&0028;",'(')
	Text = extrepl(Text,"&0029;",')')
	((w_f	= Text) (Terminal = "Written file " ofile ' [' size(Text) ' chars]')) :f(end)

end

*************************************
COMMENT
The program is run as such:
	snobol4 -b -d10M cleanindex.sno file.tex
and produces "file-noind.tex" as output

For very large files it is advisable to increas the memory (e.g. "-d20M").
However, MAXLNGTH is set to 2 Megs which should be more than enough for LaTeX
files.

The program relies on this pattern:
	PIndex = "\index" bal
which implies that the {} pairs must be changed to () pairs /BAL works only with
standard parenthesis).
The program simply identifies "\index{...}" entries and removes them.