#!/usr1/local/bin/perl5 # This Perl file takes the specified glossary and makes a list of # defined terms. # It takes, as command line arguments, the names of the files to be # searched for the defined terms. The files are searched in the # order entered on the command line. A link to the text version of # the glossary is created the FIRST TIME the word is found. After that # the match is counted, but not linked. # The name of the glossary to be used must be changed in the code each # time a different glossary is used. # After the files have been modified, they should be checked. If a word # is plural, the 's' needs to be added as part of the linked word. If a # glossary word occurs in a HTML command part of the document it will be # linked if it was the first occurrence. Therefore it must be unlinked # there and the 'real' first occurrence must be found by hand. If a # multi-word term is found and a part of the word is also a term, the # part must be unlinked. For example, in Lunar Module, 'Module' must be # unlinked by hand. (When a second link is made on the same word, it # causes both links not to work properly.) # Author: Meredith Bene 6/2/97 # October 17, 1997 - Used to work on Imagine the Universe Glossary. #---------------------------------------------------------------------- @GlossaryList = &CreateGlossaryList; &WordSearch(@GlossaryList); system('rm temp'); print "Finished!\n"; # This function reads in lines from the specified glossary and creates # a list of defined words. NOTE: The glossary name must be changed # if a different glossary is used. sub CreateGlossaryList { my($TheFile) = ') { $TheLine = $_; if($TheLine !~ /^) return (@GlossaryList); close(INFILE); } # end CreateGlossaryList # This function prompts the user as to whether or not to create a link. sub PromptUser { my($Found, $TheLine, $Word, $Term) = @_; my($Continue, $Response); $Continue = 0; while(!$Continue) { print "\n\nLink $Word with $Term? The line is: \n"; print "\n$TheLine\n\n"; print "Please enter Y, N or M(modify word): "; $Response = ; chomp($Response); if (($Response eq 'Y') or ($Response eq 'y')) {$Continue = 1} elsif (($Response eq 'N') or ($Response eq 'n')) { $Continue = 1; $Found = 0; } elsif (($Response eq 'M') or ($Response eq 'm')) { $Continue = 1; print "Enter the word to be replaced: "; $Word = ; chomp($Word); } else { print "\nPlease enter Y, N or M: "; $Response = ; } } # end while(!$Continue) return($Found, $Word); } # end PromptUser # This function creates a link to the glossary the first time a # defined word is found. sub CreateLink { my($TheFile, $Found, $NewTerm, $Replacement, $Term) = @_; my($TheLine, $Word, $Ending, $Beginning); open(INFILE, $TheFile) or die "The file $TheFile could not ". "be found.\n"; open(OUTFILE, '>temp'); while () { $TheLine = $_; if (!$Found) { # $Found = ($TheLine =~ /(\S*)($NewTerm)(\S*)/i); # [checks match # $Beginning = $1; # $Word = $2; # $Ending = $3; $Found = ($TheLine =~ /(\s+)($NewTerm)(\S*)/i); # [checks match $Word = $2; $Ending = $3; if (defined($Beginning)) { $Word = $Beginning.$Word; } if (defined($Ending)) { $Word = $Word.$Ending; } if (($Found) and ($TheLine !~ /$Term\">/)) {($Found, $Word) = &PromptUser($Found, $TheLine, $Word, $Term)} if (($Found) and ($TheLine !~ /$Term\">/)) { $TheLine =~ s/$Word/$Replacement$Word<\/A>/i } } # end if(!$Found) printf OUTFILE $TheLine; } # end while() close(INFILE); close(OUTFILE); return($Found); } # end CreateLink # This function copies the 'temp' file, with the changes, back to the # original file. This is done each time a word has been searched for. # The file changed is the one where the word was first located, or the # last one in the list if the word wasn't located. sub WriteToFile { my($Count) = shift(@_); open (INFILE, '$ARGV[$Count]"); while () {print OUTFILE $_} close (INFILE); close (OUTFILE); } # end WriteToFile # This function cycles through the files for each word to be searched # for. It writes each line, with changes, to a 'temp' file. # It calls the functions &CreateLink and &WriteToFile. sub CycleThroughFiles { my($Found,$Replacement,$Count,$NewTerm, $Term) = @_; while ($Count < @ARGV) { $TheFile = $ARGV[$Count]; $Found = &CreateLink($TheFile,$Found,$NewTerm,$Replacement,$Term); &WriteToFile($Count); if ($Found) { $Count = @ARGV} else { $Count = $Count + 1 } } # end while($Count < @ARGV) } # end CycleThroughFiles # This function takes each word in the glossary and searches the # file(s) entered on the command line, in order, for the word. It # calls the function &CycleThroughFiles. The variable $replacement # must be modified for different glossaries. sub WordSearch { my(@GlossaryList) = @_; my($Term, $NewTerm, $Found, $Replacement, $Count); foreach $Term (@GlossaryList) { $Found = 0; if (!($Term =~ /_/)) { $NewTerm = substr($Term,0,4)} else { $NewTerm = $Term; $NewTerm =~ s/_/ /g; } $Replacement = " "; # [changed for $Count = 0; # [diff. gloss. &CycleThroughFiles($Found, $Replacement, $Count, $NewTerm, $Term); } } # end WordSearch